/
teardown.go
99 lines (83 loc) · 2.18 KB
/
teardown.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
package cmd
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/eezhee/eezhee/pkg/config"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(teardownCmd)
}
var teardownCmd = &cobra.Command{
Use: "teardown",
Short: "Will teardown a running app",
Long: `All software has versions. This is Eezhee's`,
Run: func(cmd *cobra.Command, args []string) {
err := teardownVM()
if err != nil {
log.Error(err)
os.Exit(1)
}
},
}
// teardownVM will tear down the cluster & app
func teardownVM() error {
// see if there is a state file (so we know what we're supposed to teardown)
deployStateFile := config.NewDeployState()
if !deployStateFile.FileExists() {
return errors.New("app is not deployed. nothing to teardown")
}
// load state file
err := deployStateFile.Load()
if err != nil {
return errors.New("error reading deploy state file")
}
// see which cloud cluster created on
cloud := deployStateFile.Cloud
// create a manager for desired cloud
vmManager, err := GetManager(cloud)
if err != nil {
log.Error(err)
return err
}
// get details of VM
ID := deployStateFile.ID
if len(ID) == 0 {
msg := fmt.Sprintf("invalid VM ID: %s - Can not teardown VM\n", ID)
return errors.New(msg)
}
// prompt to make sure user really wants to do this
// note: if trying to debug with vscode, look at:
// https://github.com/golang/vscode-go/issues/124 or
// https://github.com/go-delve/delve/issues/1274
response := ""
fmt.Printf("are you sure you want to delete %s cluster (Y/n)? ", deployStateFile.Name)
fmt.Scanln(&response)
response = strings.ToLower(response)
if (response != "") && (response != "y") {
return errors.New("deletion aborted")
}
// ready to delete the cluster
err = vmManager.DeleteVM(ID)
if err != nil {
return err
}
log.Info("k3s cluster (and VM) deleted")
// remove the kubeconfig file
kubeConfigFile, _ := filepath.Abs("kubeconfig")
_ = os.Remove(kubeConfigFile)
if err == nil {
log.Debug("removed kubeconfig for cluster")
}
// remove the deploy state file
err = deployStateFile.Delete()
if err != nil {
return err
}
log.Debug("deploy-state file deleted")
return nil
}