-
Notifications
You must be signed in to change notification settings - Fork 491
/
removeunit.go
397 lines (349 loc) · 11.1 KB
/
removeunit.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package application
import (
"fmt"
"strings"
"time"
"github.com/juju/cmd/v3"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/juju/names/v5"
"github.com/juju/juju/api/client/application"
"github.com/juju/juju/api/client/modelconfig"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/juju/block"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/core/model"
"github.com/juju/juju/rpc/params"
)
// NewRemoveUnitCommand returns a command which removes an application's units.
func NewRemoveUnitCommand() modelcmd.ModelCommand {
return modelcmd.Wrap(&removeUnitCommand{})
}
// removeUnitCommand is responsible for destroying application units.
type removeUnitCommand struct {
modelcmd.RemoveConfirmationCommandBase
modelcmd.ModelCommandBase
DestroyStorage bool
NumUnits int
EntityNames []string
api RemoveApplicationAPI
modelConfigApi ModelConfigClient
unknownModel bool
Force bool
NoWait bool
NoPrompt bool
DryRun bool
fs *gnuflag.FlagSet
}
const removeUnitDoc = `
Remove application units from the model.
The usage of this command differs depending on whether it is being used on a
k8s or cloud model.
Removing all units of a application is not equivalent to removing the
application itself; for that, the ` + "`juju remove-application`" + ` command
is used.
For k8s models only a single application can be supplied and only the
--num-units argument supported.
Specific units cannot be targeted for removal as that is handled by k8s,
instead the total number of units to be removed is specified.
Examples:
juju remove-unit wordpress --num-units 2
For cloud models specific units can be targeted for removal.
Units of a application are numbered in sequence upon creation. For example, the
fourth unit of wordpress will be designated "wordpress/3". These identifiers
can be supplied in a space delimited list to remove unwanted units from the
model.
Juju will also remove the machine if the removed unit was the only unit left
on that machine (including units in containers).
Sometimes, the removal of the unit may fail as Juju encounters errors
and failures that need to be dealt with before a unit can be removed.
For example, Juju will not remove a unit if there are hook failures.
However, at times, there is a need to remove a unit ignoring
all operational errors. In these rare cases, use --force option but note
that --force will remove a unit and, potentially, its machine without
given them the opportunity to shutdown cleanly.
Unit removal is a multi-step process. Under normal circumstances, Juju will not
proceed to the next step until the current step has finished.
However, when using --force, users can also specify --no-wait to progress through steps
without delay waiting for each step to complete.
`
const removeUnitExamples = `
juju remove-unit wordpress/2 wordpress/3 wordpress/4
juju remove-unit wordpress/2 --destroy-storage
juju remove-unit wordpress/2 --force
juju remove-unit wordpress/2 --force --no-wait
`
var removeUnitMsgNoDryRun = `
This command will remove unit(s) %q
Your controller does not support dry runs`[1:]
var removeUnitMsgPrefix = "This command will perform the following actions:"
func (c *removeUnitCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "remove-unit",
Args: "<unit> [...] | <application>",
Purpose: "Remove application units from the model.",
Doc: removeUnitDoc,
Examples: removeUnitExamples,
SeeAlso: []string{
"remove-application",
"scale-application",
},
})
}
func (c *removeUnitCommand) SetFlags(f *gnuflag.FlagSet) {
c.ModelCommandBase.SetFlags(f)
c.RemoveConfirmationCommandBase.SetFlags(f)
f.BoolVar(&c.DryRun, "dry-run", false, "Print what this command would remove without removing")
f.IntVar(&c.NumUnits, "num-units", 0, "Number of units to remove (k8s models only)")
f.BoolVar(&c.DestroyStorage, "destroy-storage", false, "Destroy storage attached to the unit")
f.BoolVar(&c.Force, "force", false, "Completely remove an unit and all its dependencies")
f.BoolVar(&c.NoWait, "no-wait", false, "Rush through unit removal without waiting for each individual step to complete")
c.fs = f
}
func (c *removeUnitCommand) Init(args []string) error {
c.EntityNames = args
if err := c.validateArgsByModelType(); err != nil {
if !errors.IsNotFound(err) {
return errors.Trace(err)
}
c.unknownModel = true
}
if !c.Force && c.NoWait {
return errors.NotValidf("--no-wait without --force")
}
return nil
}
func (c *removeUnitCommand) validateArgsByModelType() error {
modelType, err := c.ModelType()
if err != nil {
return err
}
if modelType == model.CAAS {
return c.validateCAASRemoval()
}
return c.validateIAASRemoval()
}
func (c *removeUnitCommand) validateCAASRemoval() error {
if c.DryRun {
// TODO(caas): enable --dry-run for caas model.
return errors.New("`--dry-run` is not supported for kubernetes units")
}
if c.DestroyStorage {
// TODO(caas): enable --destroy-storage for caas model.
return errors.New("k8s models only support --num-units")
}
if len(c.EntityNames) == 0 {
return errors.Errorf("no application specified")
}
if len(c.EntityNames) != 1 {
return errors.Errorf("only single application supported")
}
if names.IsValidUnit(c.EntityNames[0]) {
msg := `
k8s models do not support removing named units.
Instead specify an application with --num-units.
`[1:]
return errors.Errorf(msg)
}
if !names.IsValidApplication(c.EntityNames[0]) {
return errors.NotValidf("application name %q", c.EntityNames[0])
}
if c.NumUnits <= 0 {
return errors.New("specify the number of units (> 0) to remove using --num-units")
}
return nil
}
func (c *removeUnitCommand) validateIAASRemoval() error {
if c.NumUnits != 0 {
return errors.NotValidf("--num-units for non k8s models")
}
if len(c.EntityNames) == 0 {
return errors.Errorf("no units specified")
}
for _, name := range c.EntityNames {
if !names.IsValidUnit(name) {
return errors.Errorf("invalid unit name %q", name)
}
}
return nil
}
func (c *removeUnitCommand) getAPI() (RemoveApplicationAPI, error) {
if c.api != nil {
return c.api, nil
}
root, err := c.NewAPIRoot()
if err != nil {
return nil, errors.Trace(err)
}
api := application.NewClient(root)
return api, nil
}
func (c *removeUnitCommand) getModelConfigAPI() (ModelConfigClient, error) {
if c.modelConfigApi != nil {
return c.modelConfigApi, nil
}
root, err := c.NewAPIRoot()
if err != nil {
return nil, errors.Trace(err)
}
c.modelConfigApi = modelconfig.NewClient(root)
return c.modelConfigApi, nil
}
// Run connects to the environment specified on the command line and destroys
// units therein.
func (c *removeUnitCommand) Run(ctx *cmd.Context) error {
client, err := c.getAPI()
if err != nil {
return err
}
defer client.Close()
if err := c.validateArgsByModelType(); err != nil {
return errors.Trace(err)
}
modelType, err := c.ModelType()
if err != nil {
return err
}
if modelType == model.CAAS {
return c.removeCaasUnits(ctx, client)
}
return c.removeUnits(ctx, client)
}
func (c *removeUnitCommand) removeUnits(ctx *cmd.Context, client RemoveApplicationAPI) error {
var maxWait *time.Duration
if c.NoWait {
zeroSec := 0 * time.Second
maxWait = &zeroSec
}
if c.DryRun {
return c.performDryRun(ctx, client)
}
modelConfigClient, err := c.getModelConfigAPI()
if err != nil {
return err
}
defer modelConfigClient.Close()
needsConfirmation := c.NeedsConfirmation(modelConfigClient)
if needsConfirmation {
err := c.performDryRun(ctx, client)
if err == errDryRunNotSupportedByController {
ctx.Warningf(removeUnitMsgNoDryRun, strings.Join(c.EntityNames, ", "))
} else if err != nil {
return err
}
if err := jujucmd.UserConfirmYes(ctx); err != nil {
return errors.Annotate(err, "unit removal")
}
}
results, err := client.DestroyUnits(application.DestroyUnitsParams{
Units: c.EntityNames,
DestroyStorage: c.DestroyStorage,
Force: c.Force,
MaxWait: maxWait,
DryRun: false,
})
if err != nil {
return block.ProcessBlockedError(err, block.BlockRemove)
}
logAll := !needsConfirmation || client.BestAPIVersion() < 16
if logAll {
return c.logResults(ctx, results)
} else {
return c.logErrors(ctx, results)
}
}
func (c *removeUnitCommand) performDryRun(ctx *cmd.Context, client RemoveApplicationAPI) error {
// TODO(jack-w-shaw) Drop this once application 15 support is dropped
if client.BestAPIVersion() < 16 {
return errDryRunNotSupportedByController
}
results, err := client.DestroyUnits(application.DestroyUnitsParams{
Units: c.EntityNames,
DestroyStorage: c.DestroyStorage,
DryRun: true,
})
if err != nil {
return block.ProcessBlockedError(err, block.BlockRemove)
}
if err := c.logErrors(ctx, results); err != nil {
return err
}
ctx.Warningf(removeUnitMsgPrefix)
_ = c.logResults(ctx, results)
return nil
}
func (c *removeUnitCommand) logErrors(ctx *cmd.Context, results []params.DestroyUnitResult) error {
return c.log(ctx, results, true)
}
func (c *removeUnitCommand) logResults(ctx *cmd.Context, results []params.DestroyUnitResult) error {
return c.log(ctx, results, false)
}
func (c *removeUnitCommand) log(
ctx *cmd.Context,
results []params.DestroyUnitResult,
errorOnly bool,
) error {
anyFailed := false
for i, name := range c.EntityNames {
result := results[i]
if err := c.logResult(ctx, name, result, errorOnly); err != nil {
anyFailed = true
}
}
if anyFailed {
return cmd.ErrSilent
}
return nil
}
func (c *removeUnitCommand) logResult(
ctx *cmd.Context,
name string,
result params.DestroyUnitResult,
errorOnly bool,
) error {
if result.Error != nil {
err := errors.Annotatef(result.Error, "removing unit %s failed", name)
cmd.WriteError(ctx.Stderr, err)
return errors.Trace(err)
}
if !errorOnly {
c.logRemovedUnit(ctx, name, result.Info)
}
return nil
}
func (c *removeUnitCommand) logRemovedUnit(ctx *cmd.Context, name string, info *params.DestroyUnitInfo) {
_, _ = fmt.Fprintf(ctx.Stdout, "will remove unit %s\n", name)
for _, entity := range info.DestroyedStorage {
storageTag, err := names.ParseStorageTag(entity.Tag)
if err != nil {
ctx.Warningf("%s", err)
continue
}
_, _ = fmt.Fprintf(ctx.Stdout, "- will remove %s\n", names.ReadableString(storageTag))
}
for _, entity := range info.DetachedStorage {
storageTag, err := names.ParseStorageTag(entity.Tag)
if err != nil {
ctx.Warningf("%s", err)
continue
}
_, _ = fmt.Fprintf(ctx.Stdout, "- will detach %s\n", names.ReadableString(storageTag))
}
}
func (c *removeUnitCommand) removeCaasUnits(ctx *cmd.Context, client RemoveApplicationAPI) error {
result, err := client.ScaleApplication(application.ScaleApplicationParams{
ApplicationName: c.EntityNames[0],
ScaleChange: -c.NumUnits,
Force: c.Force,
})
if params.IsCodeNotSupported(err) {
return errors.Annotate(err, "can not remove unit")
}
if err != nil {
return block.ProcessBlockedError(err, block.BlockRemove)
}
ctx.Infof("scaling down to %d units", result.Info.Scale)
return nil
}