/
switch_partition.go
226 lines (199 loc) · 5.52 KB
/
switch_partition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package appliance
import (
"bytes"
"context"
"fmt"
"io"
"text/template"
"github.com/appgate/sdp-api-client-go/api/v20/openapi"
appliancepkg "github.com/appgate/sdpctl/pkg/appliance"
"github.com/appgate/sdpctl/pkg/configuration"
"github.com/appgate/sdpctl/pkg/docs"
"github.com/appgate/sdpctl/pkg/factory"
"github.com/appgate/sdpctl/pkg/prompt"
"github.com/appgate/sdpctl/pkg/tui"
"github.com/appgate/sdpctl/pkg/util"
"github.com/cenkalti/backoff/v4"
"github.com/google/uuid"
"github.com/hashicorp/go-version"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type options struct {
cfg *configuration.Config
api *appliancepkg.Appliance
id *uuid.UUID
out io.Writer
spinnerOut func() io.Writer
canPrompt bool
ciMode bool
applianceStats *openapi.StatsAppliancesListAllOfData
}
func NewSwitchPartitionCmd(f *factory.Factory) *cobra.Command {
opts := &options{
cfg: f.Config,
out: f.IOOutWriter,
spinnerOut: f.GetSpinnerOutput(),
canPrompt: f.CanPrompt(),
}
cmd := &cobra.Command{
Use: "switch-partition",
Short: docs.ApplianceSwitchPartitionDocs.Short,
Long: docs.ApplianceSwitchPartitionDocs.Long,
Example: docs.ApplianceSwitchPartitionDocs.ExampleString(),
Annotations: map[string]string{
"MinAPIVersion": "19",
},
Args: cobra.MatchAll(cobra.MaximumNArgs(1), func(cmd *cobra.Command, args []string) error {
if len(args) <= 0 {
if !opts.canPrompt {
return fmt.Errorf("no TTY present and no appliance ID provided")
}
return nil
}
id := args[0]
var err error
if !util.IsUUID(id) {
return fmt.Errorf("'%s' is not a valid appliance ID", id)
}
uid, err := uuid.Parse(id)
if err != nil {
return err
}
opts.id = &uid
return nil
}),
PreRunE: func(cmd *cobra.Command, args []string) error {
api, err := f.Appliance(opts.cfg)
if err != nil {
return err
}
opts.api = api
ctx := context.Background()
if opts.id == nil {
id, err := appliancepkg.PromptSelect(ctx, api, nil, nil, false)
if err != nil {
return err
}
uid, err := uuid.Parse(id)
if err != nil {
return err
}
opts.id = &uid
}
if opts.id == nil {
return fmt.Errorf("failed to switch partition: no appliance identifier provided")
}
stats, _, err := api.Stats(ctx, nil, nil, false)
if err != nil {
return err
}
for _, s := range stats.GetData() {
if opts.id.String() != s.GetId() {
continue
}
opts.applianceStats = &s
}
minVersion, _ := version.NewVersion("6.2.6-0")
currentVersion, err := version.NewVersion(opts.applianceStats.GetVersion())
if err != nil {
return err
}
i, err := appliancepkg.CompareVersionsAndBuildNumber(minVersion, currentVersion)
if err != nil {
return err
}
if i < 0 {
return fmt.Errorf("minimum supported version for the 'switch-partition' command is 6.2.6. current version is %s", currentVersion.String())
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return switchPartitionRunE(opts)
},
}
return cmd
}
func switchPartitionRunE(opts *options) error {
ctx := context.Background()
api := opts.api
if api.ApplianceStats == nil {
api.ApplianceStats = &appliancepkg.ApplianceStatus{
Appliance: api,
}
}
appliance, err := api.Get(ctx, opts.id.String())
if err != nil {
return fmt.Errorf("failed to get appliance: %w", err)
}
volume := opts.applianceStats.GetVolumeNumber()
log := logrus.WithFields(logrus.Fields{
"id": opts.id.String(),
"volume": volume,
})
log.Info("initial appliance stats")
summary, err := switchPartitionSummary(appliance.GetName())
if err != nil {
return err
}
fmt.Fprintln(opts.out, summary)
if opts.canPrompt {
if err := prompt.AskConfirmation(); err != nil {
return err
}
}
var p *tui.Progress
var t *tui.Tracker
if !opts.ciMode {
p = tui.New(ctx, opts.spinnerOut())
t = p.AddTracker(appliance.GetName(), opts.applianceStats.GetState(), "finished")
go t.Watch(appliancepkg.StatusNotBusy, []string{"error"})
}
err = backoff.Retry(func() error {
return api.ApplianceSwitchPartition(ctx, opts.id.String())
}, backoff.NewExponentialBackOff())
if err != nil {
return fmt.Errorf("partition switch failed on appliance %s: %v", opts.id, err)
}
if err := api.ApplianceStats.WaitForApplianceState(ctx, *appliance, appliancepkg.StatReady, t); err != nil {
if t != nil {
t.Fail(err.Error())
}
return fmt.Errorf("failed to get appliance stats: %w", err)
}
if p != nil {
p.Wait()
}
// verify partition switch
stats, _, err := api.Stats(ctx, nil, nil, false)
if err != nil {
return fmt.Errorf("partition switch failed: %w", err)
}
var newVolume float32
for _, a := range stats.GetData() {
if a.GetId() == appliance.GetId() {
newVolume = a.GetVolumeNumber()
}
}
log.WithField("new_volume", newVolume).Info("new stats after partition switch")
if newVolume == volume {
return fmt.Errorf("partition switch failed: volume number is the same as before executing the command")
}
fmt.Fprintf(opts.out, "switched partition on %s", appliance.GetName())
return nil
}
func switchPartitionSummary(applianceName string) (string, error) {
type tplStub struct {
Name string
}
tplString := `Confirm partition switch on appliance {{ .Name }}`
tpl, err := template.New("").Parse(tplString)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := tpl.Execute(&buf, tplStub{Name: applianceName}); err != nil {
return "", err
}
return buf.String(), nil
}