-
Notifications
You must be signed in to change notification settings - Fork 55
/
upgrade.go
260 lines (243 loc) · 9.07 KB
/
upgrade.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
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package nodecmd
import (
"encoding/json"
"fmt"
"strings"
"sync"
"github.com/ava-labs/avalanche-cli/pkg/ansible"
"github.com/ava-labs/avalanche-cli/pkg/binutils"
"github.com/ava-labs/avalanche-cli/pkg/cobrautils"
"github.com/ava-labs/avalanche-cli/pkg/constants"
"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/ssh"
"github.com/ava-labs/avalanche-cli/pkg/utils"
"github.com/ava-labs/avalanche-cli/pkg/ux"
"github.com/ava-labs/avalanche-cli/pkg/vm"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
type nodeUpgradeInfo struct {
AvalancheGoVersion string // avalanche go version to update to on cloud server
SubnetEVMVersion string // subnet EVM version to update to on cloud server
SubnetEVMIDsToUpgrade []string // list of ID of Subnet EVM to be upgraded to subnet EVM version to update to
}
func newUpgradeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "upgrade",
Short: "(ALPHA Warning) Update avalanchego or VM version for all node in a cluster",
Long: `(ALPHA Warning) This command is currently in experimental mode.
The node update command suite provides a collection of commands for nodes to update
their avalanchego or VM version.
You can check the status after upgrade by calling avalanche node status`,
Args: cobrautils.ExactArgs(1),
RunE: upgrade,
}
return cmd
}
func upgrade(_ *cobra.Command, args []string) error {
clusterName := args[0]
if err := checkCluster(clusterName); err != nil {
return err
}
clusterConfig, err := app.GetClusterConfig(clusterName)
if err != nil {
return err
}
network := clusterConfig.Network
hosts, err := ansible.GetInventoryFromAnsibleInventoryFile(app.GetAnsibleInventoryDirPath(clusterName))
if err != nil {
return err
}
defer disconnectHosts(hosts)
toUpgradeNodesMap, err := getNodesUpgradeInfo(hosts)
if err != nil {
return err
}
spinSession := ux.NewUserSpinner()
for host, upgradeInfo := range toUpgradeNodesMap {
if upgradeInfo.AvalancheGoVersion != "" {
spinner := spinSession.SpinToUser(utils.ScriptLog(host.NodeID, fmt.Sprintf("Upgrading avalanchego to version %s...", upgradeInfo.AvalancheGoVersion)))
if err := upgradeAvalancheGo(host, network, upgradeInfo.AvalancheGoVersion); err != nil {
ux.SpinFailWithError(spinner, "", err)
return err
}
ux.SpinComplete(spinner)
}
if upgradeInfo.SubnetEVMVersion != "" {
subnetEVMVersionToUpgradeToWoPrefix := strings.TrimPrefix(upgradeInfo.SubnetEVMVersion, "v")
subnetEVMArchive := fmt.Sprintf(constants.SubnetEVMArchive, subnetEVMVersionToUpgradeToWoPrefix)
subnetEVMReleaseURL := fmt.Sprintf(constants.SubnetEVMReleaseURL, upgradeInfo.SubnetEVMVersion, subnetEVMArchive)
spinner := spinSession.SpinToUser(utils.ScriptLog(host.NodeID, fmt.Sprintf("Upgrading SubnetEVM to version %s...", upgradeInfo.SubnetEVMVersion)))
if err := getNewSubnetEVMRelease(host, subnetEVMReleaseURL, subnetEVMArchive); err != nil {
ux.SpinFailWithError(spinner, "", err)
return err
}
if err := ssh.RunSSHStopNode(host); err != nil {
ux.SpinFailWithError(spinner, "", err)
return err
}
for _, vmID := range upgradeInfo.SubnetEVMIDsToUpgrade {
subnetEVMBinaryPath := fmt.Sprintf(constants.CloudNodeSubnetEvmBinaryPath, vmID)
if err := upgradeSubnetEVM(host, subnetEVMBinaryPath); err != nil {
ux.SpinFailWithError(spinner, "", err)
return err
}
}
if err := ssh.RunSSHStartNode(host); err != nil {
ux.SpinFailWithError(spinner, "", err)
return err
}
ux.SpinComplete(spinner)
}
}
spinSession.Stop()
return nil
}
// getNodesUpgradeInfo gets the node versions of all given nodes and checks which
// nodes needs to have Avalanche Go & SubnetEVM upgraded. It first checks the subnet EVM version -
// it will install the newest subnet EVM version and install the latest avalanche Go that is still compatible with the Subnet EVM version
// if the node is not tracking any subnet, it will just install latestAvagoVersion
func getNodesUpgradeInfo(hosts []*models.Host) (map[*models.Host]nodeUpgradeInfo, error) {
latestAvagoVersion, err := app.Downloader.GetLatestReleaseVersion(binutils.GetGithubLatestReleaseURL(
constants.AvaLabsOrg,
constants.AvalancheGoRepoName,
))
if err != nil {
return nil, err
}
latestSubnetEVMVersion, err := app.Downloader.GetLatestReleaseVersion(binutils.GetGithubLatestReleaseURL(
constants.AvaLabsOrg,
constants.SubnetEVMRepoName,
))
if err != nil {
return nil, err
}
rpcVersion, err := vm.GetRPCProtocolVersion(app, models.SubnetEvm, latestSubnetEVMVersion)
if err != nil {
return nil, err
}
nodeErrors := map[string]error{}
nodesToUpgrade := make(map[*models.Host]nodeUpgradeInfo)
wg := sync.WaitGroup{}
wgResults := models.NodeResults{}
for _, host := range hosts {
wg.Add(1)
go func(nodeResults *models.NodeResults, host *models.Host) {
defer wg.Done()
if resp, err := ssh.RunSSHCheckAvalancheGoVersion(host); err != nil {
nodeResults.AddResult(host.NodeID, nil, err)
return
} else {
if vmVersions, err := parseNodeVersionOutput(resp); err != nil {
nodeResults.AddResult(host.NodeID, nil, err)
} else {
nodeResults.AddResult(host.NodeID, vmVersions, err)
}
}
}(&wgResults, host)
}
wg.Wait()
if wgResults.HasErrors() {
return nil, fmt.Errorf("failed to get avalanchego version for node(s) %s", wgResults.GetErrorHostMap())
}
nodeIDToHost := map[string]*models.Host{}
for _, host := range hosts {
nodeIDToHost[host.NodeID] = host
}
for hostID, vmVersionsInterface := range wgResults.GetResultMap() {
vmVersions, err := utils.ConvertInterfaceToMap(vmVersionsInterface)
if err != nil {
return nil, err
}
currentAvalancheGoVersion := vmVersions[constants.PlatformKeyName]
avalancheGoVersionToUpdateTo := latestAvagoVersion
nodeUpgradeInfo := nodeUpgradeInfo{}
nodeUpgradeInfo.SubnetEVMIDsToUpgrade = []string{}
for vmName, vmVersion := range vmVersions {
// when calling info.getNodeVersion, this is what we get
// "vmVersions":{"avm":"v1.10.12","evm":"v0.12.5","n8Anw9kErmgk7KHviddYtecCmziLZTphDwfL1V2DfnFjWZXbE":"v0.5.6","platform":"v1.10.12"}},
// we need to get the VM ID of the subnets that the node is currently validating, in the example above it is n8Anw9kErmgk7KHviddYtecCmziLZTphDwfL1V2DfnFjWZXbE
if !checkIfKeyIsStandardVMName(vmName) {
if vmVersion != latestSubnetEVMVersion {
// update subnet EVM version
ux.Logger.PrintToUser("Upgrading Subnet EVM version for node %s from version %s to version %s", hostID, vmVersion, latestSubnetEVMVersion)
nodeUpgradeInfo.SubnetEVMVersion = latestSubnetEVMVersion
nodeUpgradeInfo.SubnetEVMIDsToUpgrade = append(nodeUpgradeInfo.SubnetEVMIDsToUpgrade, vmName)
}
// find the highest version of avalanche go that is still compatible with current highest rpc
avalancheGoVersionToUpdateTo, err = GetLatestAvagoVersionForRPC(rpcVersion, latestAvagoVersion)
if err != nil {
nodeErrors[hostID] = err
continue
}
}
}
if _, hasFailed := nodeErrors[hostID]; hasFailed {
continue
}
if currentAvalancheGoVersion != avalancheGoVersionToUpdateTo {
ux.Logger.PrintToUser("Upgrading Avalanche Go version for node %s from version %s to version %s", hostID, currentAvalancheGoVersion, avalancheGoVersionToUpdateTo)
nodeUpgradeInfo.AvalancheGoVersion = avalancheGoVersionToUpdateTo
}
nodesToUpgrade[nodeIDToHost[hostID]] = nodeUpgradeInfo
}
if len(nodeErrors) > 0 {
ux.Logger.PrintToUser("Failed to upgrade nodes: ")
for node, nodeErr := range nodeErrors {
ux.Logger.PrintToUser("node %s failed to upgrade due to %s", node, nodeErr)
}
return nil, fmt.Errorf("failed to upgrade node(s) %s", maps.Keys(nodeErrors))
}
return nodesToUpgrade, nil
}
// checks if vmName is "avm", "evm" or "platform"
func checkIfKeyIsStandardVMName(vmName string) bool {
standardVMNames := []string{constants.PlatformKeyName, constants.EVMKeyName, constants.AVMKeyName}
return slices.Contains(standardVMNames, vmName)
}
func upgradeAvalancheGo(
host *models.Host,
network models.Network,
avaGoVersionToUpdateTo string,
) error {
if err := ssh.RunSSHUpgradeAvalanchego(host, network, avaGoVersionToUpdateTo); err != nil {
return err
}
return nil
}
func upgradeSubnetEVM(
host *models.Host,
subnetEVMBinaryPath string,
) error {
if err := ssh.RunSSHUpgradeSubnetEVM(host, subnetEVMBinaryPath); err != nil {
return err
}
return nil
}
func getNewSubnetEVMRelease(
host *models.Host,
subnetEVMReleaseURL string,
subnetEVMArchive string,
) error {
if err := ssh.RunSSHGetNewSubnetEVMRelease(host, subnetEVMReleaseURL, subnetEVMArchive); err != nil {
return err
}
return nil
}
func parseNodeVersionOutput(byteValue []byte) (map[string]interface{}, error) {
var result map[string]interface{}
if err := json.Unmarshal(byteValue, &result); err != nil {
return nil, err
}
nodeIDInterface, ok := result["result"].(map[string]interface{})
if ok {
vmVersions, ok := nodeIDInterface["vmVersions"].(map[string]interface{})
if ok {
return vmVersions, nil
}
}
return nil, nil
}