-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.go
317 lines (265 loc) · 9.32 KB
/
vm.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package upgradecmd
import (
"errors"
"fmt"
"github.com/lasthyphen/dijets-cli/pkg/binutils"
"github.com/lasthyphen/dijets-cli/pkg/constants"
"github.com/lasthyphen/dijets-cli/pkg/models"
"github.com/lasthyphen/dijets-cli/pkg/plugins"
"github.com/lasthyphen/dijets-cli/pkg/subnet"
"github.com/lasthyphen/dijets-cli/pkg/ux"
"github.com/lasthyphen/dijets-cli/pkg/vm"
"github.com/spf13/cobra"
)
const (
futureDeployment = "Update config for future deployments"
localDeployment = "Existing local deployment"
tahoeDeployment = "Tahoe"
mainnetDeployment = "Mainnet (coming soon)"
)
var (
pluginDir string
useTahoe bool
useMainnet bool
useLocal bool
useConfig bool
useManual bool
useLatest bool
targetVersion string
binaryPathArg string
)
// avalanche subnet update vm
func newUpgradeVMCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "vm [subnetName]",
Short: "Upgrade a subnet's binary",
Long: "",
RunE: upgradeVM,
Args: cobra.ExactArgs(1),
SilenceUsage: true,
}
cmd.Flags().BoolVar(&useConfig, "config", false, "upgrade config for future subnet deployments")
cmd.Flags().BoolVar(&useLocal, "local", false, "upgrade existing `local` deployment")
cmd.Flags().BoolVar(&useTahoe, "tahoe", false, "upgrade existing `tahoe` deployment (alias for `testnet`)")
cmd.Flags().BoolVar(&useTahoe, "testnet", false, "upgrade existing `testnet` deployment (alias for `tahoe`)")
cmd.Flags().BoolVar(&useMainnet, "mainnet", false, "upgrade existing `mainnet` deployment")
cmd.Flags().BoolVar(&useManual, "print", false, "print instructions for upgrading")
cmd.Flags().StringVar(&pluginDir, "plugin-dir", "", "plugin directory to automatically upgrade VM")
cmd.Flags().BoolVar(&useLatest, "latest", false, "upgrade to latest version")
cmd.Flags().StringVar(&targetVersion, "version", "", "Upgrade to custom version")
cmd.Flags().StringVar(&binaryPathArg, "binary", "", "Upgrade to custom binary")
return cmd
}
func atMostOneNetworkSelected() bool {
return !(useConfig && useLocal || useConfig && useTahoe || useConfig && useMainnet || useLocal && useTahoe ||
useLocal && useMainnet || useTahoe && useMainnet)
}
func atMostOneVersionSelected() bool {
return !(useLatest && targetVersion != "" || useLatest && binaryPathArg != "" || targetVersion != "" && binaryPathArg != "")
}
func atMostOneAutomationSelected() bool {
return !(useManual && pluginDir != "")
}
func upgradeVM(_ *cobra.Command, args []string) error {
// Check flag preconditions
if !atMostOneNetworkSelected() {
return errors.New("too many networks selected")
}
if !atMostOneVersionSelected() {
return errors.New("too many versions selected")
}
if !atMostOneAutomationSelected() {
return errors.New("--print and --plugin-dir are mutually exclusive")
}
subnetName := args[0]
if !app.SubnetConfigExists(subnetName) {
return errors.New("subnet does not exist")
}
sc, err := app.LoadSidecar(subnetName)
if err != nil {
return fmt.Errorf("unable to load sidecar: %w", err)
}
upgradeOptions := []string{futureDeployment}
networkToUpgrade, err := selectNetworkToUpgrade(sc, upgradeOptions)
if err != nil {
return err
}
vmType := sc.VM
if vmType == models.SubnetEvm || vmType == models.SpacesVM {
return selectUpdateOption(subnetName, vmType, sc, networkToUpgrade)
}
// Must be a custom update
return updateToCustomBin(subnetName, sc, networkToUpgrade, binaryPathArg)
}
// select which network to upgrade
// optionally provide a list of options to preload
func selectNetworkToUpgrade(sc models.Sidecar, upgradeOptions []string) (string, error) {
switch {
case useConfig:
return futureDeployment, nil
case useLocal:
return localDeployment, nil
case useTahoe:
return tahoeDeployment, nil
case useMainnet:
return mainnetDeployment, nil
}
updatePrompt := "What deployment would you like to upgrade"
if upgradeOptions == nil {
upgradeOptions = []string{}
}
// check if subnet already deployed locally
locallyDeployedSubnets, err := subnet.GetLocallyDeployedSubnets()
if err != nil {
// ignore error if we can't reach the server, assume subnet isn't deployed
app.Log.Warn("Unable to reach server to get deployed subnets")
}
if _, ok := locallyDeployedSubnets[sc.Subnet]; ok {
upgradeOptions = append(upgradeOptions, localDeployment)
}
// check if subnet deployed on tahoe
if _, ok := sc.Networks[models.Tahoe.String()]; ok {
upgradeOptions = append(upgradeOptions, tahoeDeployment)
}
// check if subnet deployed on mainnet
if _, ok := sc.Networks[models.Mainnet.String()]; ok {
upgradeOptions = append(upgradeOptions, mainnetDeployment)
}
if len(upgradeOptions) == 0 {
return "", errors.New("no deployment target available")
}
selectedDeployment, err := app.Prompt.CaptureList(updatePrompt, upgradeOptions)
if err != nil {
return "", err
}
return selectedDeployment, nil
}
func selectUpdateOption(subnetName string, vmType models.VMType, sc models.Sidecar, networkToUpgrade string) error {
switch {
case useLatest:
return updateToLatestVersion(vmType, sc, networkToUpgrade)
case targetVersion != "":
return updateToSpecificVersion(sc, networkToUpgrade)
case binaryPathArg != "":
return updateToCustomBin(subnetName, sc, networkToUpgrade, binaryPathArg)
}
latestVersionUpdate := "Update to latest version"
specificVersionUpdate := "Update to a specific version"
customBinaryUpdate := "Update to a custom binary"
updateOptions := []string{latestVersionUpdate, specificVersionUpdate, customBinaryUpdate}
updatePrompt := "How would you like to update your subnet's virtual machine"
updateDecision, err := app.Prompt.CaptureList(updatePrompt, updateOptions)
if err != nil {
return err
}
switch updateDecision {
case latestVersionUpdate:
return updateToLatestVersion(vmType, sc, networkToUpgrade)
case specificVersionUpdate:
return updateToSpecificVersion(sc, networkToUpgrade)
case customBinaryUpdate:
return updateToCustomBin(subnetName, sc, networkToUpgrade, binaryPathArg)
default:
return errors.New("invalid option")
}
}
func updateToLatestVersion(vmType models.VMType, sc models.Sidecar, networkToUpgrade string) error {
// pull in current version
currentVersion := sc.VMVersion
// check latest version
latestVersion, err := app.Downloader.GetLatestReleaseVersion(binutils.GetGithubLatestReleaseURL(
constants.AvaLabsOrg,
vmType.RepoName(),
))
if err != nil {
return err
}
// check if current version equals latest
if currentVersion == "latest" || currentVersion == latestVersion {
ux.Logger.PrintToUser("VM already up-to-date")
return nil
}
return updateVMByNetwork(sc, latestVersion, networkToUpgrade)
}
func updateToSpecificVersion(sc models.Sidecar, networkToUpgrade string) error {
// pull in current version
currentVersion := sc.VMVersion
// Get version to update to
var err error
if targetVersion == "" {
targetVersion, err = app.Prompt.CaptureVersion("Enter version")
if err != nil {
return err
}
}
// check if current version equals chosen version
if currentVersion == targetVersion {
ux.Logger.PrintToUser("VM already up-to-date")
return nil
}
return updateVMByNetwork(sc, targetVersion, networkToUpgrade)
}
func updateVMByNetwork(sc models.Sidecar, targetVersion string, networkToUpgrade string) error {
switch networkToUpgrade {
case futureDeployment:
return updateFutureVM(sc, targetVersion)
case localDeployment:
return updateExistingLocalVM(sc, targetVersion)
case tahoeDeployment:
return chooseManualOrAutomatic(sc, targetVersion, networkToUpgrade)
case mainnetDeployment:
return updateMainnetVM()
default:
return errors.New("unknown deployment")
}
}
func updateToCustomBin(subnetName string, sc models.Sidecar, networkToUpgrade, binaryPath string) error {
if err := vm.CopyCustomVM(app, subnetName, binaryPath); err != nil {
return err
}
sc.VM = models.CustomVM
targetVersion = ""
return updateVMByNetwork(sc, targetVersion, networkToUpgrade)
}
func updateFutureVM(sc models.Sidecar, targetVersion string) error {
// to switch to new version, just need to update sidecar
sc.VMVersion = targetVersion
if err := app.UpdateSidecar(&sc); err != nil {
return err
}
ux.Logger.PrintToUser("VM updated for future deployments. Update will apply next time subnet is deployed.")
return nil
}
func updateExistingLocalVM(_ models.Sidecar, _ string) error {
ux.Logger.PrintToUser("Coming soon. For now, please upgrade your existing deployments and redeploy the subnet.")
return nil
}
func chooseManualOrAutomatic(sc models.Sidecar, targetVersion string, _ string) error {
switch {
case useManual:
return plugins.ManualUpgrade(app, sc, targetVersion)
case pluginDir != "":
return plugins.AutomatedUpgrade(app, sc, targetVersion, pluginDir)
}
const (
choiceManual = "Manual"
choiceAutomatic = "Automatic (Make sure your node isn't running)"
)
choice, err := app.Prompt.CaptureList(
"How would you like to update the dijetsnode config?",
[]string{choiceAutomatic, choiceManual},
)
if err != nil {
return err
}
if choice == choiceManual {
return plugins.ManualUpgrade(app, sc, targetVersion)
}
return plugins.AutomatedUpgrade(app, sc, targetVersion, pluginDir)
}
func updateMainnetVM() error {
ux.Logger.PrintToUser("Coming soon. For now, please upgrade your mainnet deployments manually.")
return nil
}