-
Notifications
You must be signed in to change notification settings - Fork 55
/
helpers.go
229 lines (213 loc) · 7.23 KB
/
helpers.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
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package nodecmd
import (
"encoding/json"
"errors"
"fmt"
"sync"
"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/avalanchego/api/info"
)
// NumNodes is a struct to hold number of nodes with and without stake
type NumNodes struct {
numValidators int // with stake
numAPI int // without stake
}
func (n NumNodes) All() int {
return n.numValidators + n.numAPI
}
func getUnhealthyNodes(hosts []*models.Host) ([]string, error) {
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.RunSSHCheckHealthy(host); err != nil {
nodeResults.AddResult(host.GetCloudID(), nil, err)
return
} else {
if isHealthy, err := parseHealthyOutput(resp); err != nil {
nodeResults.AddResult(host.GetCloudID(), nil, err)
} else {
nodeResults.AddResult(host.GetCloudID(), isHealthy, err)
}
}
}(&wgResults, host)
}
wg.Wait()
if wgResults.HasErrors() {
return nil, fmt.Errorf("failed to get health status for node(s) %s", wgResults.GetErrorHostMap())
}
return utils.Filter(wgResults.GetNodeList(), func(nodeID string) bool {
return !wgResults.GetResultMap()[nodeID].(bool)
}), nil
}
func parseHealthyOutput(byteValue []byte) (bool, error) {
var result map[string]interface{}
if err := json.Unmarshal(byteValue, &result); err != nil {
return false, err
}
isHealthyInterface, ok := result["result"].(map[string]interface{})
if ok {
isHealthy, ok := isHealthyInterface["healthy"].(bool)
if ok {
return isHealthy, nil
}
}
return false, fmt.Errorf("unable to parse node healthy status")
}
func getNotBootstrappedNodes(hosts []*models.Host) ([]string, error) {
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.RunSSHCheckBootstrapped(host); err != nil {
nodeResults.AddResult(host.GetCloudID(), nil, err)
return
} else {
if isBootstrapped, err := parseBootstrappedOutput(resp); err != nil {
nodeResults.AddResult(host.GetCloudID(), nil, err)
} else {
nodeResults.AddResult(host.GetCloudID(), isBootstrapped, err)
}
}
}(&wgResults, host)
}
wg.Wait()
if wgResults.HasErrors() {
return nil, fmt.Errorf("failed to get avalanchego bootstrap status for node(s) %s", wgResults.GetErrorHostMap())
}
return utils.Filter(wgResults.GetNodeList(), func(nodeID string) bool {
return !wgResults.GetResultMap()[nodeID].(bool)
}), nil
}
func parseBootstrappedOutput(byteValue []byte) (bool, error) {
var result map[string]interface{}
if err := json.Unmarshal(byteValue, &result); err != nil {
return false, err
}
isBootstrappedInterface, ok := result["result"].(map[string]interface{})
if ok {
isBootstrapped, ok := isBootstrappedInterface["isBootstrapped"].(bool)
if ok {
return isBootstrapped, nil
}
}
return false, errors.New("unable to parse node bootstrap status")
}
func getRPCIncompatibleNodes(hosts []*models.Host, subnetName string) ([]string, error) {
ux.Logger.PrintToUser("Checking compatibility of node(s) avalanche go RPC protocol version with Subnet EVM RPC of subnet %s ...", subnetName)
sc, err := app.LoadSidecar(subnetName)
if err != nil {
return nil, err
}
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.GetCloudID(), nil, err)
return
} else {
if _, rpcVersion, err := parseAvalancheGoOutput(resp); err != nil {
nodeResults.AddResult(host.GetCloudID(), nil, err)
} else {
nodeResults.AddResult(host.GetCloudID(), rpcVersion, err)
}
}
}(&wgResults, host)
}
wg.Wait()
if wgResults.HasErrors() {
return nil, fmt.Errorf("failed to get rpc protocol version for node(s) %s", wgResults.GetErrorHostMap())
}
incompatibleNodes := []string{}
for nodeID, rpcVersionI := range wgResults.GetResultMap() {
rpcVersion := rpcVersionI.(uint32)
if rpcVersion != uint32(sc.RPCVersion) {
incompatibleNodes = append(incompatibleNodes, nodeID)
}
}
if len(incompatibleNodes) > 0 {
ux.Logger.PrintToUser(fmt.Sprintf("Compatible Avalanche Go RPC version is %d", sc.RPCVersion))
}
return incompatibleNodes, nil
}
func parseAvalancheGoOutput(byteValue []byte) (string, uint32, error) {
reply := map[string]interface{}{}
if err := json.Unmarshal(byteValue, &reply); err != nil {
return "", 0, err
}
resultMap := reply["result"]
resultJSON, err := json.Marshal(resultMap)
if err != nil {
return "", 0, err
}
nodeVersionReply := info.GetNodeVersionReply{}
if err := json.Unmarshal(resultJSON, &nodeVersionReply); err != nil {
return "", 0, err
}
return nodeVersionReply.VMVersions["platform"], uint32(nodeVersionReply.RPCProtocolVersion), nil
}
func disconnectHosts(hosts []*models.Host) {
for _, host := range hosts {
_ = host.Disconnect()
}
}
func authorizedAccessFromSettings() bool {
return app.Conf.GetConfigBoolValue(constants.ConfigAuthorizeCloudAccessKey)
}
func checkHostsAreRPCCompatible(hosts []*models.Host, subnetName string) error {
incompatibleNodes, err := getRPCIncompatibleNodes(hosts, subnetName)
if err != nil {
return err
}
if len(incompatibleNodes) > 0 {
sc, err := app.LoadSidecar(subnetName)
if err != nil {
return err
}
ux.Logger.PrintToUser("Either modify your Avalanche Go version or modify your VM version")
ux.Logger.PrintToUser("To modify your Avalanche Go version: https://docs.avax.network/nodes/maintain/upgrade-your-avalanchego-node")
switch sc.VM {
case models.SubnetEvm:
ux.Logger.PrintToUser("To modify your Subnet-EVM version: https://docs.avax.network/build/subnet/upgrade/upgrade-subnet-vm")
case models.CustomVM:
ux.Logger.PrintToUser("To modify your Custom VM binary: avalanche subnet upgrade vm %s --config", subnetName)
}
ux.Logger.PrintToUser("Yoy can use \"avalanche node upgrade\" to upgrade Avalanche Go and/or Subnet-EVM to their latest versions")
return fmt.Errorf("the Avalanche Go version of node(s) %s is incompatible with VM RPC version of %s", incompatibleNodes, subnetName)
}
return nil
}
func checkHostsAreHealthy(hosts []*models.Host) error {
ux.Logger.PrintToUser("Checking if node(s) are healthy...")
unhealthyNodes, err := getUnhealthyNodes(hosts)
if err != nil {
return err
}
if len(unhealthyNodes) > 0 {
return fmt.Errorf("node(s) %s are not healthy, please check the issue and try again later", unhealthyNodes)
}
return nil
}
func checkHostsAreBootstrapped(hosts []*models.Host) error {
notBootstrappedNodes, err := getNotBootstrappedNodes(hosts)
if err != nil {
return err
}
if len(notBootstrappedNodes) > 0 {
return fmt.Errorf("node(s) %s are not bootstrapped yet, please try again later", notBootstrappedNodes)
}
return nil
}