forked from openshift/installer
/
destroy.go
88 lines (78 loc) · 2.15 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
package main
import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
assetstore "github.com/openshift/installer/pkg/asset/store"
"github.com/openshift/installer/pkg/destroy"
"github.com/openshift/installer/pkg/destroy/bootstrap"
_ "github.com/openshift/installer/pkg/destroy/libvirt"
_ "github.com/openshift/installer/pkg/destroy/openstack"
)
func newDestroyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "destroy",
Short: "Destroy part of an OpenShift cluster",
Long: "",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
cmd.AddCommand(newDestroyBootstrapCmd())
cmd.AddCommand(newDestroyClusterCmd())
return cmd
}
func newDestroyClusterCmd() *cobra.Command {
return &cobra.Command{
Use: "cluster",
Short: "Destroy an OpenShift cluster",
Args: cobra.ExactArgs(0),
Run: func(_ *cobra.Command, _ []string) {
cleanup := setupFileHook(rootOpts.dir)
defer cleanup()
err := runDestroyCmd(rootOpts.dir)
if err != nil {
logrus.Fatal(err)
}
},
}
}
func runDestroyCmd(directory string) error {
destroyer, err := destroy.New(logrus.StandardLogger(), directory)
if err != nil {
return errors.Wrap(err, "Failed while preparing to destroy cluster")
}
if err := destroyer.Run(); err != nil {
return errors.Wrap(err, "Failed to destroy cluster")
}
store, err := assetstore.NewStore(directory)
if err != nil {
return errors.Wrap(err, "failed to create asset store")
}
for _, asset := range clusterTarget.assets {
if err := store.Destroy(asset); err != nil {
return errors.Wrapf(err, "failed to destroy asset %q", asset.Name())
}
}
// delete the state file as well
err = store.DestroyState()
if err != nil {
return errors.Wrap(err, "failed to remove state file")
}
return nil
}
func newDestroyBootstrapCmd() *cobra.Command {
return &cobra.Command{
Use: "bootstrap",
Short: "Destroy the bootstrap resources",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
cleanup := setupFileHook(rootOpts.dir)
defer cleanup()
err := bootstrap.Destroy(rootOpts.dir)
if err != nil {
logrus.Fatal(err)
}
},
}
}