-
Notifications
You must be signed in to change notification settings - Fork 55
/
status.go
312 lines (296 loc) · 9.26 KB
/
status.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
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package nodecmd
import (
"fmt"
"os"
"strings"
"sync"
"github.com/ava-labs/avalanche-cli/cmd/subnetcmd"
"github.com/ava-labs/avalanche-cli/pkg/ansible"
"github.com/ava-labs/avalanche-cli/pkg/cobrautils"
"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/ids"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/olekukonko/tablewriter"
"github.com/pborman/ansi"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
)
var subnetName string
func newStatusCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "status [clusterName]",
Short: "(ALPHA Warning) Get node bootstrap status",
Long: `(ALPHA Warning) This command is currently in experimental mode.
The node status command gets the bootstrap status of all nodes in a cluster with the Primary Network.
If no cluster is given, defaults to node list behaviour.
To get the bootstrap status of a node with a Subnet, use --subnet flag`,
Args: cobrautils.MinimumNArgs(0),
RunE: statusNode,
}
cmd.Flags().StringVar(&subnetName, "subnet", "", "specify the subnet the node is syncing with")
return cmd
}
func statusNode(_ *cobra.Command, args []string) error {
if len(args) == 0 {
return list(nil, nil)
}
clusterName := args[0]
if err := checkCluster(clusterName); err != nil {
return err
}
clusterConf, err := app.GetClusterConfig(clusterName)
if err != nil {
return err
}
var blockchainID ids.ID
if subnetName != "" {
sc, err := app.LoadSidecar(subnetName)
if err != nil {
return err
}
blockchainID = sc.Networks[clusterConf.Network.Name()].BlockchainID
if blockchainID == ids.Empty {
return ErrNoBlockchainID
}
}
hostIDs := utils.Filter(clusterConf.GetCloudIDs(), clusterConf.IsAvalancheGoHost)
nodeIDs, err := utils.MapWithError(hostIDs, func(s string) (string, error) {
n, err := getNodeID(app.GetNodeInstanceDirPath(s))
return n.String(), err
})
if err != nil {
return err
}
if subnetName != "" {
// check subnet first
if _, err := subnetcmd.ValidateSubnetNameAndGetChains([]string{subnetName}); err != nil {
return err
}
}
hosts, err := ansible.GetInventoryFromAnsibleInventoryFile(app.GetAnsibleInventoryDirPath(clusterName))
if err != nil {
return err
}
defer disconnectHosts(hosts)
spinSession := ux.NewUserSpinner()
spinner := spinSession.SpinToUser("Checking node status...")
notBootstrappedNodes, err := getNotBootstrappedNodes(hosts)
if err != nil {
ux.SpinFailWithError(spinner, "", err)
return err
}
ux.SpinComplete(spinner)
spinner = spinSession.SpinToUser("Checking if node(s) are healthy...")
unhealthyNodes, err := getUnhealthyNodes(hosts)
if err != nil {
ux.SpinFailWithError(spinner, "", err)
return err
}
ux.SpinComplete(spinner)
spinner = spinSession.SpinToUser("Getting avalanchego version of node(s)...")
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 avalancheGoVersion, _, err := parseAvalancheGoOutput(resp); err != nil {
nodeResults.AddResult(host.GetCloudID(), nil, err)
} else {
nodeResults.AddResult(host.GetCloudID(), avalancheGoVersion, err)
}
}
}(&wgResults, host)
}
wg.Wait()
if wgResults.HasErrors() {
e := fmt.Errorf("failed to get avalanchego version for node(s) %s", wgResults.GetErrorHostMap())
ux.SpinFailWithError(spinner, "", e)
return e
}
ux.SpinComplete(spinner)
spinSession.Stop()
avagoVersions := map[string]string{}
for nodeID, avalanchegoVersion := range wgResults.GetResultMap() {
avagoVersions[nodeID] = fmt.Sprintf("%v", avalanchegoVersion)
}
notSyncedNodes := []string{}
subnetSyncedNodes := []string{}
subnetValidatingNodes := []string{}
if subnetName != "" {
hostsToCheckSyncStatus := []string{}
for _, hostID := range hostIDs {
if slices.Contains(notBootstrappedNodes, hostID) {
notSyncedNodes = append(notSyncedNodes, hostID)
} else {
hostsToCheckSyncStatus = append(hostsToCheckSyncStatus, hostID)
}
}
if len(hostsToCheckSyncStatus) != 0 {
ux.Logger.PrintToUser("Getting subnet sync status of node(s)")
hostsToCheck := utils.Filter(hosts, func(h *models.Host) bool { return slices.Contains(hostsToCheckSyncStatus, h.GetCloudID()) })
wg := sync.WaitGroup{}
wgResults := models.NodeResults{}
for _, host := range hostsToCheck {
wg.Add(1)
go func(nodeResults *models.NodeResults, host *models.Host) {
defer wg.Done()
if syncstatus, err := ssh.RunSSHSubnetSyncStatus(host, blockchainID.String()); err != nil {
nodeResults.AddResult(host.GetCloudID(), nil, err)
return
} else {
if subnetSyncStatus, err := parseSubnetSyncOutput(syncstatus); err != nil {
nodeResults.AddResult(host.GetCloudID(), nil, err)
return
} else {
nodeResults.AddResult(host.GetCloudID(), subnetSyncStatus, err)
}
}
}(&wgResults, host)
}
wg.Wait()
if wgResults.HasErrors() {
return fmt.Errorf("failed to check sync status for node(s) %s", wgResults.GetErrorHostMap())
}
for nodeID, subnetSyncStatus := range wgResults.GetResultMap() {
switch subnetSyncStatus {
case status.Syncing.String():
subnetSyncedNodes = append(subnetSyncedNodes, nodeID)
case status.Validating.String():
subnetValidatingNodes = append(subnetValidatingNodes, nodeID)
default:
notSyncedNodes = append(notSyncedNodes, nodeID)
}
}
}
}
if clusterConf.MonitoringInstance != "" {
hostIDs = append(hostIDs, clusterConf.MonitoringInstance)
nodeIDs = append(nodeIDs, "")
}
nodeConfigs := []models.NodeConfig{}
for _, hostID := range hostIDs {
nodeConfig, err := app.LoadClusterNodeConfig(hostID)
if err != nil {
return err
}
nodeConfigs = append(nodeConfigs, nodeConfig)
}
printOutput(
clusterConf,
hostIDs,
nodeIDs,
avagoVersions,
unhealthyNodes,
notBootstrappedNodes,
notSyncedNodes,
subnetSyncedNodes,
subnetValidatingNodes,
clusterName,
subnetName,
nodeConfigs,
)
return nil
}
func printOutput(
clusterConf models.ClusterConfig,
cloudIDs []string,
nodeIDs []string,
avagoVersions map[string]string,
unhealthyHosts []string,
notBootstrappedHosts []string,
notSyncedHosts []string,
subnetSyncedHosts []string,
subnetValidatingHosts []string,
clusterName string,
subnetName string,
nodeConfigs []models.NodeConfig,
) {
if clusterConf.External {
ux.Logger.PrintToUser("Cluster %s (%s) is EXTERNAL", logging.LightBlue.Wrap(clusterName), clusterConf.Network.Kind.String())
}
if subnetName == "" && len(notBootstrappedHosts) == 0 {
ux.Logger.PrintToUser("All nodes in cluster %s are bootstrapped to Primary Network!", clusterName)
}
if subnetName != "" && len(notSyncedHosts) == 0 {
// all nodes are either synced to or validating subnet
status := "synced to"
if len(subnetSyncedHosts) == 0 {
status = "validators of"
}
ux.Logger.PrintToUser("All nodes in cluster %s are %s Subnet %s", logging.LightBlue.Wrap(clusterName), status, subnetName)
}
ux.Logger.PrintToUser("")
tit := fmt.Sprintf("STATUS FOR CLUSTER: %s", logging.LightBlue.Wrap(clusterName))
ux.Logger.PrintToUser(tit)
ux.Logger.PrintToUser(strings.Repeat("=", len(removeColors(tit))))
ux.Logger.PrintToUser("")
header := []string{"Cloud ID", "Node ID", "IP", "Network", "Role", "Avago Version", "Primary Network", "Healthy"}
if subnetName != "" {
header = append(header, "Subnet "+subnetName)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(header)
table.SetRowLine(true)
for i, cloudID := range cloudIDs {
boostrappedStatus := ""
healthyStatus := ""
nodeIDStr := ""
avagoVersion := ""
roles := clusterConf.GetHostRoles(nodeConfigs[i])
if clusterConf.IsAvalancheGoHost(cloudID) {
boostrappedStatus = logging.Green.Wrap("BOOTSTRAPPED")
if slices.Contains(notBootstrappedHosts, cloudID) {
boostrappedStatus = logging.Red.Wrap("NOT_BOOTSTRAPPED")
}
healthyStatus = logging.Green.Wrap("OK")
if slices.Contains(unhealthyHosts, cloudID) {
healthyStatus = logging.Red.Wrap("UNHEALTHY")
}
nodeIDStr = nodeIDs[i]
avagoVersion = avagoVersions[cloudID]
}
row := []string{
cloudID,
logging.Green.Wrap(nodeIDStr),
nodeConfigs[i].ElasticIP,
clusterConf.Network.Kind.String(),
strings.Join(roles, ","),
avagoVersion,
boostrappedStatus,
healthyStatus,
}
if subnetName != "" {
syncedStatus := ""
if clusterConf.MonitoringInstance != cloudID {
syncedStatus = logging.Red.Wrap("NOT_BOOTSTRAPPED")
if slices.Contains(subnetSyncedHosts, cloudID) {
syncedStatus = logging.Green.Wrap("SYNCED")
}
if slices.Contains(subnetValidatingHosts, cloudID) {
syncedStatus = logging.Green.Wrap("VALIDATING")
}
}
row = append(row, syncedStatus)
}
table.Append(row)
}
table.Render()
}
func removeColors(s string) string {
bs, err := ansi.Strip([]byte(s))
if err != nil {
return s
}
return string(bs)
}