-
Notifications
You must be signed in to change notification settings - Fork 111
/
cleanup.go
211 lines (187 loc) · 7.09 KB
/
cleanup.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
package main
import (
"fmt"
"io/ioutil"
"strings"
"github.com/spf13/cobra"
)
var (
AWS string = "aws"
DEPLOYMENT string = "deployment"
TERRAFORM_CMD = "terraform"
AWS_DESTROY_DIR = "/hab/a2_deploy_workspace/terraform/destroy/aws/"
TERRAFORM_INIT = "init"
AWS_PROVISION = `
for i in 1;do i=$PWD;cd /hab/a2_deploy_workspace/terraform/destroy/aws/;terraform init;cd $i;done
%s
for i in 1;do i=$PWD;cd /hab/a2_deploy_workspace/terraform/destroy/aws/;terraform destroy -state=/hab/a2_deploy_workspace/terraform/destroy/aws/terraform.tfstate -auto-approve;cd $i;done
`
DEPLOYMENT_CLEANUP = `hab pkg uninstall chef/automate-ha-deployment`
DESTROY_S3_BUCKET = `HAB_LICENSE=accept-no-persist hab pkg exec core/aws-cli aws s3 rm s3://%s --recursive; hab pkg exec core/aws-cli aws s3 rb s3://%s`
)
var cleanupFlags = struct {
onprem bool
aws bool
force bool
}{}
func init() {
RootCmd.AddCommand(cleanupCmd)
cleanupCmd.PersistentFlags().BoolVar(&cleanupFlags.onprem, "onprem-deployment", false, "Cleaning up all the instances related to onprem ")
cleanupCmd.PersistentFlags().BoolVar(&cleanupFlags.aws, "aws-deployment", false, "Remove AWS resources created by provisioning and clean-up hab workspace")
cleanupCmd.PersistentFlags().BoolVar(&cleanupFlags.force, "force", false, "Remove backup storage")
}
var cleanupCmd = &cobra.Command{
Use: "cleanup",
Short: "cleanup the Automate HA instances",
Long: "cleaning up the instance of all the Automate HA related Applications.",
Annotations: map[string]string{
NoCheckVersionAnnotation: NoCheckVersionAnnotation,
},
RunE: runCleanupCmd,
Hidden: true,
}
const (
FRONTENDCLEANUP_COMMANDS = `
sudo systemctl stop chef-automate;
[ ! -L /hab ] && [ -d /hab ] && echo "not a symbolic, but it is directory...so deleting /hab" && sudo rm -rf /hab;
[ -L /hab ] && echo "/hab is the symbolic link, so deleting the content of symbolic link" && sudo rm -rf /hab/*
sudo rm -rf /var/automate-ha;
sudo rm -rf /var/tmp/*.aib*;
`
BACKENDCLEANUP_COMMANDS = `
sudo systemctl stop hab-sup;
[ ! -L /hab ] && [ -d /hab ] && echo "not a symbolic, but it is directory...so deleting /hab" && sudo rm -rf /hab;
[ -L /hab ] && echo "/hab is the symbolic link, so deleting the content of symbolic link" && sudo rm -rf /hab/*
[ -f /bin/hab ] && sudo rm -rf /bin/hab;
sudo rm -rf /var/automate-ha;
sudo rm -rf /var/tmp/*.aib*;
`
)
func runCleanupCmd(cmd *cobra.Command, args []string) error {
infra, err := getAutomateHAInfraDetails()
if err != nil {
return err
}
if infra != nil {
if isA2HARBFileExist() {
sshconfig := &SSHConfig{}
sshconfig.sshUser = infra.Outputs.SSHUser.Value
sshconfig.sshKeyFile = infra.Outputs.SSHKeyFile.Value
sshconfig.sshPort = infra.Outputs.SSHPort.Value
sshUtil := NewSSHUtil(sshconfig)
if cleanupFlags.onprem {
res, err := writer.Prompt("Cleanup will remove all the Automate HA resources created by deploy command. Do you want to continue?\nPress y to agree, n to disagree? [y/n]")
if err != nil {
return err
}
if strings.ToLower(res) == "n" {
writer.Println("Cleanup execution cancelled.")
return nil
}
automateIps := infra.Outputs.AutomatePrivateIps.Value
err = executeCommandForArrayofIPs(sshUtil, automateIps, FRONTENDCLEANUP_COMMANDS, "automate")
if err != nil {
return err
}
chefserverIps := infra.Outputs.ChefServerPrivateIps.Value
err = executeCommandForArrayofIPs(sshUtil, chefserverIps, FRONTENDCLEANUP_COMMANDS, "chef-server")
if err != nil {
return err
}
postgresqlIps := infra.Outputs.PostgresqlPrivateIps.Value
err = executeCommandForArrayofIPs(sshUtil, postgresqlIps, BACKENDCLEANUP_COMMANDS, "postgresql")
if err != nil {
return err
}
opensearchIps := infra.Outputs.OpensearchPrivateIps.Value
err = executeCommandForArrayofIPs(sshUtil, opensearchIps, BACKENDCLEANUP_COMMANDS, "opensearch")
if err != nil {
return err
}
// Cleanup HA Deployment workspace
args := []string{
"-c",
DEPLOYMENT_CLEANUP,
}
err = executeCommand("/bin/sh", args, "")
if err != nil {
return err
}
}
if cleanupFlags.aws {
res, err := writer.Prompt("Cleanup will remove all the AWS resources created by provison-infra. Do you want to continue?\nPress y to agree, n to disagree? [y/n]")
if err != nil {
return err
}
if strings.ToLower(res) == "n" {
writer.Println("Cleanup execution cancelled.")
return nil
}
archBytes, err := ioutil.ReadFile("/hab/a2_deploy_workspace/terraform/.tf_arch") // nosemgrep
if err != nil {
writer.Errorf("%s", err.Error())
return err
}
var arch = strings.Trim(string(archBytes), "\n")
writer.Println("Reference architecture type :" + arch)
appendString := ""
backup_config, err := getTheValueFromA2HARB("backup_config")
if err != nil {
writer.Error("Error in getting backup_config")
return err
}
if backup_config == "s3" && cleanupFlags.force {
bucket_name, err := getTheValueFromA2HARB("s3_bucketName")
if err != nil {
writer.Error("Error in getting bucket_name")
return err
}
writer.Body("BucketName :" + bucket_name)
appendString = appendString + fmt.Sprintf(DESTROY_S3_BUCKET, bucket_name, bucket_name)
} else if infra.Outputs.BackupConfigEFS.Value == "true" && !cleanupFlags.force {
appendString = appendString + `for i in 1;do i=$PWD;cd /hab/a2_deploy_workspace/terraform/destroy/aws/;terraform state rm "module.efs[0].aws_efs_file_system.backups";cd $i;done`
}
writer.Println("Cleaning up all AWS provisioned resources.")
// Init terraform folder
err = executeCommand(TERRAFORM_CMD, []string{TERRAFORM_INIT}, AWS_DESTROY_DIR)
if err != nil {
writer.Error("Terraform init failed")
return err
}
// Modify tf state or remove bucket based in "--force" arg
writer.Printf("%v", len(appendString) > 0)
if len(appendString) > 0 {
err = executeCommand("/bin/sh", []string{"-c", appendString}, "")
if err != nil {
writer.Errorf(err.Error())
return err
}
}
// Destroy AWS resources provisioned by terraform
err = executeCommand(TERRAFORM_CMD, []string{"destroy", "-auto-approve"}, AWS_DESTROY_DIR)
if err != nil {
writer.Errorf("error while destroying infra, %v", err)
return err
}
writer.Success("cleanup completed successfully. Run the following command to remove/uninstall deployment workspace\n hab pkg uninstall chef/automate-ha-deployment\n")
}
}
} else {
writer.Println("\nCleanup not executed.")
}
return nil
}
func executeCommandForArrayofIPs(sshUtil SSHUtil, remoteIps []string, script string, servername string) error {
for i := 0; i < len(remoteIps); i++ {
sshUtil.getSSHConfig().hostIP = remoteIps[i]
writer.Println("Cleanup has started on " + servername + " node : " + remoteIps[i] + "\n")
_, err := sshUtil.connectAndExecuteCommandOnRemote(script, true)
if err != nil {
writer.Error(err.Error())
return err
} else {
writer.Success("Cleanup is completed on " + servername + " node : " + remoteIps[i] + "\n")
}
}
return nil
}