-
Notifications
You must be signed in to change notification settings - Fork 927
/
restart_command.go
147 lines (126 loc) · 4.78 KB
/
restart_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
package v7
import (
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/v7/shared"
"code.cloudfoundry.org/clock"
)
//go:generate counterfeiter . RestartActor
type RestartActor interface {
GetApplicationByNameAndSpace(appName string, spaceGUID string) (v7action.Application, v7action.Warnings, error)
GetDetailedAppSummary(appName string, spaceGUID string, withObfuscatedValues bool) (v7action.DetailedApplicationSummary, v7action.Warnings, error)
PollStart(appGUID string, noWait bool) (v7action.Warnings, error)
StartApplication(appGUID string) (v7action.Warnings, error)
StopApplication(appGUID string) (v7action.Warnings, error)
CreateDeployment(appGUID string, dropletGUID string) (string, v7action.Warnings, error)
PollStartForRolling(appGUID string, deploymentGUID string, noWait bool) (v7action.Warnings, error)
}
type RestartCommand struct {
RequiredArgs flag.AppName `positional-args:"yes"`
Strategy flag.DeploymentStrategy `long:"strategy" description:"Deployment strategy, either rolling or null."`
NoWait bool `long:"no-wait" description:"Do not wait for the long-running operation to complete; push exits when one instance of the web process is healthy"`
usage interface{} `usage:"CF_NAME restart APP_NAME"`
relatedCommands interface{} `related_commands:"restage, restart-app-instance"`
envCFStagingTimeout interface{} `environmentName:"CF_STAGING_TIMEOUT" environmentDescription:"Max wait time for staging, in minutes" environmentDefault:"15"`
envCFStartupTimeout interface{} `environmentName:"CF_STARTUP_TIMEOUT" environmentDescription:"Max wait time for app instance startup, in minutes" environmentDefault:"5"`
UI command.UI
Config command.Config
SharedActor command.SharedActor
Actor RestartActor
}
func (cmd *RestartCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
cmd.SharedActor = sharedaction.NewActor(config)
ccClient, _, err := shared.GetNewClientsAndConnectToCF(config, ui, "")
if err != nil {
return err
}
cmd.Actor = v7action.NewActor(ccClient, config, nil, nil, clock.NewClock())
return nil
}
func (cmd RestartCommand) 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.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayTextWithFlavor("Restarting app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...\n", map[string]interface{}{
"AppName": cmd.RequiredArgs.AppName,
"OrgName": cmd.Config.TargetedOrganization().Name,
"SpaceName": cmd.Config.TargetedSpace().Name,
"Username": user.Name,
})
switch cmd.Strategy.Name {
case constant.DeploymentStrategyRolling:
err = cmd.zeroDowntimeRestart(app)
default:
err = cmd.downtimeRestart(app)
}
if err != nil {
return err
}
appSummaryDisplayer := shared.NewAppSummaryDisplayer(cmd.UI)
summary, warnings, err := cmd.Actor.GetDetailedAppSummary(
cmd.RequiredArgs.AppName,
cmd.Config.TargetedSpace().GUID,
false,
)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
appSummaryDisplayer.AppDisplay(summary, false)
return nil
}
func (cmd RestartCommand) downtimeRestart(app v7action.Application) error {
var warnings v7action.Warnings
var err error
if app.Started() {
cmd.UI.DisplayText("Stopping app...\n")
warnings, err = cmd.Actor.StopApplication(app.GUID)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
}
warnings, err = cmd.Actor.StartApplication(app.GUID)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayText("Waiting for app to start...\n")
warnings, err = cmd.Actor.PollStart(app.GUID, cmd.NoWait)
cmd.UI.DisplayWarnings(warnings)
return err
}
func (cmd RestartCommand) zeroDowntimeRestart(app v7action.Application) error {
cmd.UI.DisplayText("Creating deployment for app {{.AppName}}...\n",
map[string]interface{}{
"AppName": cmd.RequiredArgs.AppName,
},
)
deploymentGUID, warnings, err := cmd.Actor.CreateDeployment(app.GUID, "")
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayText("Waiting for app to deploy...\n")
warnings, err = cmd.Actor.PollStartForRolling(app.GUID, deploymentGUID, cmd.NoWait)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
return err
}