-
Notifications
You must be signed in to change notification settings - Fork 55
/
destroy.go
287 lines (268 loc) · 9.26 KB
/
destroy.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
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package nodecmd
import (
"errors"
"fmt"
"os"
"strings"
awsAPI "github.com/ava-labs/avalanche-cli/pkg/cloud/aws"
gcpAPI "github.com/ava-labs/avalanche-cli/pkg/cloud/gcp"
"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/utils"
"github.com/ava-labs/avalanche-cli/pkg/ux"
"golang.org/x/exp/maps"
"golang.org/x/net/context"
"github.com/spf13/cobra"
)
var (
authorizeRemove bool
authorizeAll bool
)
func newDestroyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "destroy [clusterName]",
Short: "(ALPHA Warning) Destroys all nodes in a cluster",
Long: `(ALPHA Warning) This command is currently in experimental mode.
The node destroy command terminates all running nodes in cloud server and deletes all storage disks.
If there is a static IP address attached, it will be released.`,
Args: cobrautils.ExactArgs(1),
RunE: destroyNodes,
}
cmd.Flags().BoolVar(&authorizeAccess, "authorize-access", false, "authorize CLI to release cloud resources")
cmd.Flags().BoolVar(&authorizeRemove, "authorize-remove", false, "authorize CLI to remove all local files related to cloud nodes")
cmd.Flags().BoolVarP(&authorizeAll, "authorize-all", "y", false, "authorize all CLI requests")
cmd.Flags().StringVar(&awsProfile, "aws-profile", constants.AWSDefaultCredential, "aws profile to use")
return cmd
}
func removeNodeFromClustersConfig(clusterName string) error {
clustersConfig := models.ClustersConfig{}
var err error
if app.ClustersConfigExists() {
clustersConfig, err = app.LoadClustersConfig()
if err != nil {
return err
}
}
if clustersConfig.Clusters != nil {
delete(clustersConfig.Clusters, clusterName)
}
return app.WriteClustersConfigFile(&clustersConfig)
}
func removeDeletedNodeDirectory(clusterName string) error {
return os.RemoveAll(app.GetNodeInstanceDirPath(clusterName))
}
func removeClusterInventoryDir(clusterName string) error {
return os.RemoveAll(app.GetAnsibleInventoryDirPath(clusterName))
}
func getDeleteConfigConfirmation() error {
if authorizeRemove {
return nil
}
ux.Logger.PrintToUser("Please note that if your node(s) are validating a Subnet, destroying them could cause Subnet instability and it is irreversible")
confirm := "Running this command will delete all stored files associated with your cloud server. Do you want to proceed? " +
fmt.Sprintf("Stored files can be found at %s", app.GetNodesDir())
yes, err := app.Prompt.CaptureYesNo(confirm)
if err != nil {
return err
}
if !yes {
return errors.New("abort avalanche node destroy command")
}
return nil
}
func removeClustersConfigFiles(clusterName string) error {
if err := removeClusterInventoryDir(clusterName); err != nil {
return err
}
return removeNodeFromClustersConfig(clusterName)
}
func destroyNodes(_ *cobra.Command, args []string) error {
clusterName := args[0]
if err := checkCluster(clusterName); err != nil {
return err
}
isExternalCluster, err := checkClusterExternal(clusterName)
if err != nil {
return err
}
if authorizeAll {
authorizeAccess = true
authorizeRemove = true
}
if err := getDeleteConfigConfirmation(); err != nil {
return err
}
nodesToStop, err := getClusterNodes(clusterName)
if err != nil {
return err
}
monitoringNode, err := getClusterMonitoringNode(clusterName)
if err != nil {
return err
}
if monitoringNode != "" {
nodesToStop = append(nodesToStop, monitoringNode)
}
// stop all load test nodes if specified
ltHosts, err := getLoadTestInstancesInCluster(clusterName)
if err != nil {
return err
}
for _, loadTestName := range ltHosts {
ltInstance, err := getExistingLoadTestInstance(clusterName, loadTestName)
if err != nil {
return err
}
nodesToStop = append(nodesToStop, ltInstance)
}
nodeErrors := map[string]error{}
cloudSecurityGroupList, err := getCloudSecurityGroupList(nodesToStop)
if err != nil {
return err
}
nodeToStopConfig, err := app.LoadClusterNodeConfig(nodesToStop[0])
if err != nil {
return err
}
// TODO: will need to change this logic if we decide to mix AWS and GCP instances in a cluster
filteredSGList := utils.Filter(cloudSecurityGroupList, func(sg regionSecurityGroup) bool { return sg.cloud == nodeToStopConfig.CloudService })
if len(filteredSGList) == 0 {
return fmt.Errorf("no endpoint found in the %s", nodeToStopConfig.CloudService)
}
var gcpCloud *gcpAPI.GcpCloud
ec2SvcMap := make(map[string]*awsAPI.AwsCloud)
// TODO: need implementation for GCP
if nodeToStopConfig.CloudService == constants.AWSCloudService {
for _, sg := range filteredSGList {
sgEc2Svc, err := awsAPI.NewAwsCloud(awsProfile, sg.region)
if err != nil {
return err
}
ec2SvcMap[sg.region] = sgEc2Svc
}
}
for _, node := range nodesToStop {
if !isExternalCluster {
nodeConfig, err := app.LoadClusterNodeConfig(node)
if err != nil {
nodeErrors[node] = err
ux.Logger.RedXToUser("Failed to destroy node %s due to %s", node, err.Error())
continue
}
if nodeConfig.CloudService == "" || nodeConfig.CloudService == constants.AWSCloudService {
if !(authorizeAccess || authorizedAccessFromSettings()) && (requestCloudAuth(constants.AWSCloudService) != nil) {
return fmt.Errorf("cloud access is required")
}
if err = ec2SvcMap[nodeConfig.Region].DestroyAWSNode(nodeConfig, clusterName); err != nil {
if isExpiredCredentialError(err) {
ux.Logger.PrintToUser("")
printExpiredCredentialsOutput(awsProfile)
return nil
}
if !errors.Is(err, awsAPI.ErrNodeNotFoundToBeRunning) {
nodeErrors[node] = err
continue
}
ux.Logger.PrintToUser("node %s is already destroyed", nodeConfig.NodeID)
}
for _, sg := range filteredSGList {
if err = deleteHostSecurityGroupRule(ec2SvcMap[sg.region], nodeConfig.ElasticIP, sg.securityGroup); err != nil {
ux.Logger.RedXToUser("unable to delete IP address %s from security group %s in region %s due to %s, please delete it manually",
nodeConfig.ElasticIP, sg.securityGroup, sg.region, err.Error())
}
}
} else {
if !(authorizeAccess || authorizedAccessFromSettings()) && (requestCloudAuth(constants.GCPCloudService) != nil) {
return fmt.Errorf("cloud access is required")
}
if gcpCloud == nil {
gcpClient, projectName, _, err := getGCPCloudCredentials()
if err != nil {
return err
}
gcpCloud, err = gcpAPI.NewGcpCloud(gcpClient, projectName, context.Background())
if err != nil {
return err
}
}
if err = gcpCloud.DestroyGCPNode(nodeConfig, clusterName); err != nil {
if !errors.Is(err, gcpAPI.ErrNodeNotFoundToBeRunning) {
nodeErrors[node] = err
continue
}
ux.Logger.GreenCheckmarkToUser("node %s is already destroyed", nodeConfig.NodeID)
}
}
ux.Logger.GreenCheckmarkToUser("Node instance %s in cluster %s successfully destroyed!", nodeConfig.NodeID, clusterName)
}
if err := removeDeletedNodeDirectory(node); err != nil {
ux.Logger.RedXToUser("Failed to delete node config for node %s due to %s", node, err.Error())
return err
}
}
if len(nodeErrors) > 0 {
ux.Logger.PrintToUser("Failed nodes: ")
for node, nodeErr := range nodeErrors {
if strings.Contains(nodeErr.Error(), constants.ErrReleasingGCPStaticIP) {
ux.Logger.RedXToUser("Node is destroyed, but failed to release static ip address for node %s due to %s", node, nodeErr)
} else {
ux.Logger.RedXToUser("Failed to destroy node %s due to %s", node, nodeErr)
}
}
return fmt.Errorf("failed to destroy node(s) %s", maps.Keys(nodeErrors))
} else {
if isExternalCluster {
ux.Logger.GreenCheckmarkToUser("All nodes in EXTERNAL cluster %s are successfully removed!", clusterName)
} else {
ux.Logger.GreenCheckmarkToUser("All nodes in cluster %s are successfully destroyed!", clusterName)
}
}
return removeClustersConfigFiles(clusterName)
}
func getClusterMonitoringNode(clusterName string) (string, error) {
clustersConfig := models.ClustersConfig{}
if app.ClustersConfigExists() {
var err error
clustersConfig, err = app.LoadClustersConfig()
if err != nil {
return "", err
}
}
if _, ok := clustersConfig.Clusters[clusterName]; !ok {
return "", fmt.Errorf("cluster %q does not exist", clusterName)
}
return clustersConfig.Clusters[clusterName].MonitoringInstance, nil
}
func checkCluster(clusterName string) error {
_, err := getClusterNodes(clusterName)
return err
}
func checkClusterExists(clusterName string) (bool, error) {
clustersConfig := models.ClustersConfig{}
if app.ClustersConfigExists() {
var err error
clustersConfig, err = app.LoadClustersConfig()
if err != nil {
return false, err
}
}
_, ok := clustersConfig.Clusters[clusterName]
return ok, nil
}
func getClusterNodes(clusterName string) ([]string, error) {
if exists, err := checkClusterExists(clusterName); err != nil || !exists {
return nil, fmt.Errorf("cluster %q not found", clusterName)
}
clustersConfig, err := app.LoadClustersConfig()
if err != nil {
return nil, err
}
clusterNodes := clustersConfig.Clusters[clusterName].Nodes
if len(clusterNodes) == 0 {
return nil, fmt.Errorf("no nodes found in cluster %s", clusterName)
}
return clusterNodes, nil
}