-
Notifications
You must be signed in to change notification settings - Fork 1
/
rm.go
115 lines (89 loc) · 2.26 KB
/
rm.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
package cmd
import (
"sort"
"github.com/spf13/cobra"
"github.com/fromanirh/pack8s/cmd/cmdutil"
"github.com/fromanirh/pack8s/iopodman"
"github.com/fromanirh/pack8s/internal/pkg/podman"
)
type rmOptions struct {
prune bool
}
// NewRemoveCommand returns command to remove the cluster
func NewRemoveCommand() *cobra.Command {
flags := &rmOptions{}
rm := &cobra.Command{
Use: "rm",
Short: "rm deletes all traces of a cluster",
RunE: func(cmd *cobra.Command, args []string) error {
return remove(cmd, flags)
},
Args: cobra.NoArgs,
}
rm.Flags().BoolVarP(&flags.prune, "prune", "P", false, "prune removes unused volumes on the host")
return rm
}
type containerList []iopodman.Container
func (cl containerList) Len() int {
return len(cl)
}
func (cl containerList) Less(i, j int) bool {
contA := cl[i]
contB := cl[j]
genA := contA.Labels[podman.LabelGeneration]
genB := contB.Labels[podman.LabelGeneration]
if genA != "" && genB != "" {
// CAVEAT! we want the latest generation first, so we swap the condition
return genA > genB
}
return false // do not change the ordering
}
func (cl containerList) Swap(i, j int) {
cl[i], cl[j] = cl[j], cl[i]
}
func remove(cmd *cobra.Command, rmOpts *rmOptions) error {
cOpts, err := cmdutil.GetCommonOpts(cmd)
if err != nil {
return err
}
hnd, log, err := cOpts.GetHandle()
if err != nil {
return err
}
containers, err := hnd.GetPrefixedContainers(cOpts.Prefix)
if err != nil {
return err
}
sort.Sort(containerList(containers))
force := true
removeVolumes := true
log.Infof("bringing cluster down (containers=%d)", len(containers))
for _, cont := range containers {
log.Noticef("stopping container: %s", cont.Names)
_, err = hnd.StopContainer(cont.Id, 5) // TODO
if err != nil {
return err
}
log.Noticef("removing container: %s", cont.Names)
_, err = hnd.RemoveContainer(cont, force, removeVolumes)
if err != nil {
return err
}
}
volumes, err := hnd.GetPrefixedVolumes(cOpts.Prefix)
if err != nil {
return err
}
if len(volumes) > 0 {
log.Infof("cleaning cluster (volumes=%d)", len(volumes))
err = hnd.RemoveVolumes(volumes)
if err != nil {
return err
}
}
if rmOpts.prune {
err = hnd.PruneVolumes()
}
log.Infof("cluster removed err=%s", err)
return err
}