forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scale_command.go
225 lines (188 loc) · 6.79 KB
/
scale_command.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
package v7
import (
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/command/v7/shared"
"code.cloudfoundry.org/clock"
)
//go:generate counterfeiter . ScaleActor
type ScaleActor interface {
AppActor
ScaleProcessByApplication(appGUID string, process v7action.Process) (v7action.Warnings, error)
StopApplication(appGUID string) (v7action.Warnings, error)
StartApplication(appGUID string) (v7action.Warnings, error)
PollStart(appGUID string, noWait bool) (v7action.Warnings, error)
}
type ScaleCommand struct {
RequiredArgs flag.AppName `positional-args:"yes"`
Force bool `short:"f" description:"Force restart of app without prompt"`
Instances flag.Instances `short:"i" required:"false" description:"Number of instances"`
DiskLimit flag.Megabytes `short:"k" required:"false" description:"Disk limit (e.g. 256M, 1024M, 1G)"`
MemoryLimit flag.Megabytes `short:"m" required:"false" description:"Memory limit (e.g. 256M, 1024M, 1G)"`
ProcessType string `long:"process" default:"web" description:"App process to scale"`
usage interface{} `usage:"CF_NAME scale APP_NAME [--process PROCESS] [-i INSTANCES] [-k DISK] [-m MEMORY] [-f]"`
relatedCommands interface{} `related_commands:"push"`
envCFStartupTimeout interface{} `environmentName:"CF_STARTUP_TIMEOUT" environmentDescription:"Max wait time for app instance startup, in minutes" environmentDefault:"5"`
UI command.UI
Config command.Config
Actor ScaleActor
SharedActor command.SharedActor
}
func (cmd *ScaleCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
sharedActor := sharedaction.NewActor(config)
cmd.SharedActor = sharedActor
ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui, "")
if err != nil {
return err
}
cmd.Actor = v7action.NewActor(ccClient, config, sharedActor, uaaClient, clock.NewClock())
return nil
}
func (cmd ScaleCommand) Execute(args []string) error {
err := cmd.SharedActor.CheckTarget(true, true)
if err != nil {
return err
}
user, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
app, warnings, err := cmd.Actor.GetApplicationByNameAndSpace(cmd.RequiredArgs.AppName, cmd.Config.TargetedSpace().GUID)
cmd.UI.DisplayWarningsV7(warnings)
if err != nil {
return err
}
if !cmd.Instances.IsSet && !cmd.DiskLimit.IsSet && !cmd.MemoryLimit.IsSet {
return cmd.showCurrentScale(user.Name, err)
}
scaled, err := cmd.scaleProcess(app.GUID, user.Name)
if err != nil {
return err
}
if !scaled {
return nil
}
warnings, err = cmd.Actor.PollStart(app.GUID, false)
cmd.UI.DisplayWarningsV7(warnings)
showErr := cmd.showCurrentScale(user.Name, err)
if showErr != nil {
return showErr
}
return cmd.translateErrors(err)
}
func (cmd ScaleCommand) translateErrors(err error) error {
if _, ok := err.(actionerror.StartupTimeoutError); ok {
return translatableerror.StartupTimeoutError{
AppName: cmd.RequiredArgs.AppName,
BinaryName: cmd.Config.BinaryName(),
}
} else if _, ok := err.(actionerror.AllInstancesCrashedError); ok {
return translatableerror.ApplicationUnableToStartError{
AppName: cmd.RequiredArgs.AppName,
BinaryName: cmd.Config.BinaryName(),
}
}
return err
}
func (cmd ScaleCommand) scaleProcess(appGUID string, username string) (bool, error) {
cmd.UI.DisplayTextWithFlavor("Scaling app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
"AppName": cmd.RequiredArgs.AppName,
"OrgName": cmd.Config.TargetedOrganization().Name,
"SpaceName": cmd.Config.TargetedSpace().Name,
"Username": username,
})
cmd.UI.DisplayNewline()
shouldRestart := cmd.DiskLimit.IsSet || cmd.MemoryLimit.IsSet
if shouldRestart && !cmd.Force {
shouldScale, err := cmd.UI.DisplayBoolPrompt(
false,
"This will cause the app to restart. Are you sure you want to scale {{.AppName}}?",
map[string]interface{}{"AppName": cmd.RequiredArgs.AppName})
if err != nil {
return false, err
}
if !shouldScale {
cmd.UI.DisplayText("Scaling cancelled")
return false, nil
}
cmd.UI.DisplayNewline()
}
warnings, err := cmd.Actor.ScaleProcessByApplication(appGUID, v7action.Process{
Type: cmd.ProcessType,
Instances: cmd.Instances.NullInt,
MemoryInMB: cmd.MemoryLimit.NullUint64,
DiskInMB: cmd.DiskLimit.NullUint64,
})
cmd.UI.DisplayWarningsV7(warnings)
if err != nil {
return false, err
}
if shouldRestart {
err := cmd.restartApplication(appGUID, username)
if err != nil {
return false, err
}
}
return true, nil
}
func (cmd ScaleCommand) restartApplication(appGUID string, username string) error {
cmd.UI.DisplayTextWithFlavor("Stopping app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
"AppName": cmd.RequiredArgs.AppName,
"OrgName": cmd.Config.TargetedOrganization().Name,
"SpaceName": cmd.Config.TargetedSpace().Name,
"Username": username,
})
cmd.UI.DisplayNewline()
warnings, err := cmd.Actor.StopApplication(appGUID)
cmd.UI.DisplayWarningsV7(warnings)
if err != nil {
return err
}
cmd.UI.DisplayTextWithFlavor("Starting app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
"AppName": cmd.RequiredArgs.AppName,
"OrgName": cmd.Config.TargetedOrganization().Name,
"SpaceName": cmd.Config.TargetedSpace().Name,
"Username": username,
})
cmd.UI.DisplayNewline()
warnings, err = cmd.Actor.StartApplication(appGUID)
cmd.UI.DisplayWarningsV7(warnings)
if err != nil {
return err
}
return nil
}
func (cmd ScaleCommand) showCurrentScale(userName string, runningErr error) error {
if !shouldShowCurrentScale(runningErr) {
return nil
}
cmd.UI.DisplayTextWithFlavor("Showing current scale of app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
"AppName": cmd.RequiredArgs.AppName,
"OrgName": cmd.Config.TargetedOrganization().Name,
"SpaceName": cmd.Config.TargetedSpace().Name,
"Username": userName,
})
summary, warnings, err := cmd.Actor.GetDetailedAppSummary(cmd.RequiredArgs.AppName, cmd.Config.TargetedSpace().GUID, false)
cmd.UI.DisplayWarningsV7(warnings)
if err != nil {
return err
}
appSummaryDisplayer := shared.NewAppSummaryDisplayer(cmd.UI)
appSummaryDisplayer.AppDisplay(summary, false)
return nil
}
func shouldShowCurrentScale(err error) bool {
if err == nil {
return true
}
if _, ok := err.(actionerror.AllInstancesCrashedError); ok {
return true
}
return false
}