-
Notifications
You must be signed in to change notification settings - Fork 21
/
package_uninstall.go
208 lines (177 loc) · 5.31 KB
/
package_uninstall.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
package pkg
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/dcos/dcos-cli/api"
"github.com/dcos/dcos-core-cli/pkg/cosmos"
"github.com/dcos/dcos-core-cli/pkg/pluginutil"
"github.com/spf13/cobra"
)
const (
removeAllWarningTemplate = `WARNING: This action cannot be undone. This will uninstall ` +
`all instances of the [%s] package, and delete all data for all %s instances.`
removeAppWarningTemplate = `WARNING: This action cannot be undone. This will uninstall ` +
`[%s] and delete all of its persistent data (logs, configurations, database artifacts, everything).`
)
func newCmdPackageUninstall(ctx api.Context) *cobra.Command {
var all, appOnly, cliOnly, yes bool
var appID string
cmd := &cobra.Command{
Use: "uninstall <package-name>",
Short: "Uninstall a package",
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
if cliOnly && (appOnly || appID != "" || all) {
return errors.New("--cli cannot be used with --app, --app-id, or --all")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
packageName := args[0]
if cliOnly {
return removeCLI(ctx, packageName)
}
c, err := cosmos.NewClient(ctx, pluginutil.HTTPClient(""))
if err != nil {
return err
}
installed, err := packageInstallCount(c, packageName)
if err != nil {
return err
}
cliInstalled, err := cliPackageInstalled(ctx, packageName)
if err != nil {
return err
}
// handle an edge case where we call uninstall with no flags but only
// a cli command installed for that package
if !appOnly && installed == 0 && cliInstalled {
return removeCLI(ctx, packageName)
}
if installed == 0 {
return fmt.Errorf("Package [%s] is not installed", packageName)
}
name := packageName
removingApp := appID != ""
if removingApp {
name = appID
}
if !yes {
fmt.Fprintln(ctx.ErrOut(), warningMessage(name, all))
}
promptMsg := promptMessage(name, removingApp, all)
expected := name
if all {
expected = fmt.Sprintf("uninstall all %s", name)
}
if !uninstallConfirmed(ctx, expected, promptMsg, yes) {
return errors.New("cancelling uninstall")
}
resp, err := c.PackageUninstall(packageName, all, appID)
if err != nil {
return err
}
for _, r := range resp.Results {
fmt.Fprintf(ctx.ErrOut(), "Uninstalled package [%s] version [%s]\n", r.PackageName, r.PackageVersion)
if r.PostUninstallNotes != "" {
fmt.Fprintln(ctx.ErrOut(), r.PostUninstallNotes)
}
}
// only remove the CLI if there aren't any other of the same package remaining
// so only 1 was installed at the start
// calling this with --app when there are multiple apps will not result in
// all of those being removed so we should only have to check if there was 1
// instance installed already
if !appOnly && (all || installed == 1) {
removeCLI(ctx, packageName)
}
return err
},
}
cmd.Flags().BoolVar(&all, "all", false, "All applications")
cmd.Flags().BoolVar(&appOnly, "app", false, "Application only")
cmd.Flags().StringVar(&appID, "app-id", "", "The application ID")
cmd.Flags().BoolVar(&cliOnly, "cli", false, "Command line only")
cmd.Flags().BoolVar(&yes, "yes", false, "Disable interactive mode and assume “yes” is the answer to all prompts")
return cmd
}
func removeCLI(ctx api.Context, packageName string) error {
cluster, err := ctx.Cluster()
if err != nil {
return err
}
manager := ctx.PluginManager(cluster)
return manager.Remove(packageName)
}
func packageInstallCount(c cosmos.Client, packageName string) (int, error) {
packages, err := c.PackageList()
if err != nil {
return 0, err
}
for _, p := range packages {
if packageName == p.Name {
return len(p.Apps), nil
}
}
return 0, nil
}
func cliPackageInstalled(ctx api.Context, packageName string) (bool, error) {
cluster, err := ctx.Cluster()
if err != nil {
return false, err
}
plugins := ctx.PluginManager(cluster).Plugins()
for _, plugin := range plugins {
packagePath := path.Join(filepath.Dir(plugin.Dir()), "package.json")
if _, err := os.Stat(packagePath); err == nil {
file, err := ioutil.ReadFile(packagePath)
if err != nil {
return false, err
}
pkg := cosmos.Package{}
err = json.Unmarshal(file, &pkg)
if err != nil {
return false, err
}
if pkg.Name == packageName {
return true, nil
}
}
}
return false, nil
}
func warningMessage(name string, all bool) string {
if all {
return fmt.Sprintf(removeAllWarningTemplate, name, name)
}
return fmt.Sprintf(removeAppWarningTemplate, name)
}
func promptMessage(name string, appID bool, all bool) string {
if all {
return fmt.Sprintf("Please type 'uninstall all %s': ", name)
}
if appID {
return "Please type the full name of the app ID to confirm: "
}
// if not all or appID, default to removing the package (i.e. --app but not only the app)
return "Please type the name of the service to confirm: "
}
func uninstallConfirmed(ctx api.Context, expected string, promptMsg string, skip bool) bool {
if skip {
return true
}
p := ctx.Prompt()
for i := 0; i < 3; i++ {
response := p.Input(promptMsg)
if response == expected {
return true
}
fmt.Fprintf(ctx.ErrOut(), "Expected '%s'. You supplied '%s'.\n", expected, response)
}
return false
}