-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrolling-restart.go
347 lines (281 loc) · 8.73 KB
/
rolling-restart.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
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"sort"
"strings"
"time"
"regexp"
"strconv"
"github.com/cloudfoundry/cli/plugin"
"github.com/fatih/color"
)
// Basic variables for cf-rolling-restart.
var (
Version = "1.1.1"
GitCommit = "HEAD"
BuildStamp = "UNKNOWN"
maxRestartWaitCycles = 120
printLine = fmt.Println
printFormatted = fmt.Printf
spinner = NewSpinner(os.Stdout)
exit = os.Exit
printRedBold = color.New(color.FgRed, color.Bold).Println
successfulExit = 0
failureExit = 1
)
// Instance provides basicinformation for a CF application which includes
// the current state as well as uptime and last updated time.
type Instance struct {
State string `json:"state"`
Uptime int `json:"uptime"`
Since int `json:"since"`
}
// Instances is grouping of CF Instance for an application.
type Instances map[string]Instance
// RollingRestart provides basic structure required by CF CLI Plugins.
type RollingRestart struct {
Version plugin.VersionType
}
// GetMetadata returns the pertinent metadata for the CF CLI Plugin architecture.
func (c *RollingRestart) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "cf-rolling-restart",
Version: c.Version,
MinCliVersion: plugin.VersionType{
Major: 6,
Minor: 7,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "rolling-restart",
HelpText: "Restart instances of your application one at a time for zero downtime.",
Alias: "rrs",
UsageDetails: plugin.Usage{
Usage: "cf rolling-restart [--max-cycles #] APP_NAME",
},
},
},
}
}
// Run executes the main code for Rolling Restart, exposes all required actions for a plugin.
func (c *RollingRestart) Run(conn plugin.CliConnection, args []string) {
if args[0] != "rolling-restart" && args[0] != "rrs" {
return
}
var exitCode int
if exitCode = execute(conn, args); exitCode != 0 {
exit(exitCode)
}
}
func execute(conn plugin.CliConnection, args []string) (exitCode int) {
var appName string
var appGUID string
var instances Instances
var instanceIDs []string
var restarted bool
var err error
if appName, err = setFlagsAndReturnAppName(args); err != nil {
printError(err.Error())
return failureExit
}
if err = validateCLISession(conn); err != nil {
printError(err.Error())
return failureExit
}
if appGUID, err = getappGUID(conn, appName); err != nil {
printError(err.Error())
return failureExit
}
if instances, err = getInstances(conn, appGUID); err != nil {
printFormatted("Failed to get the instance information for %s.\n", appName)
printError(err.Error())
return failureExit
}
if instanceIDs = getKeysFor(instances); len(instanceIDs) < 2 {
printFormatted("Only found a single instance of %s, scaling up to two instances.\n", appName)
if err = scaleApplication(conn, appName, 2); err != nil {
printFormatted("Failed to scale %s to two instances.\n", appName)
printError(err.Error())
return failureExit
}
if _, err = checkInstanceStatus(conn, appGUID, "1"); err != nil {
printFormatted("Failed to get the instance information for %s.\n", appName)
printError(err.Error())
return failureExit
}
printFormatted("Finished scaling %s to two instances.\n", appName)
}
printFormatted("Beginning restart of app instances for %s.\n", appName)
for _, instanceID := range instanceIDs {
if err = restartInstance(conn, appName, instanceID); err != nil {
printFormatted("Failed to restart instance %s.\n", instanceID)
printError(err.Error())
return failureExit
}
if restarted, err = checkInstanceStatus(conn, appGUID, instanceID); err != nil {
printFormatted("Failed to get the instance information for %s.\n", appName)
printError(err.Error())
return failureExit
}
if restarted == false {
printError(fmt.Sprintf("Application did not restart within %d Second(s), failing out. Check your current application state.\n", maxRestartWaitCycles))
return failureExit
}
}
if len(instanceIDs) == 1 {
printFormatted("Scaling %s back down to one instance.\n", appName)
scaleApplication(conn, appName, 1)
}
printFormatted("Finished restart of app instances for %s.\n", appName)
return successfulExit
}
func scaleApplication(conn plugin.CliConnection, appName string, numberOfInstances int) error {
_, err := conn.CliCommand("scale", appName, "-i", strconv.Itoa(numberOfInstances))
return err
}
func checkInstanceStatus(conn plugin.CliConnection, appGUID string, instanceID string) (bool, error) {
printFormatted("Checking status of instance %s.\n", instanceID)
var isRunning bool
var restarted bool
var err error
for i := 0; i < maxRestartWaitCycles; i++ {
spinner.Next()
if isRunning, err = isInstanceRunning(conn, appGUID, instanceID); err != nil {
return false, err
}
if isRunning {
spinner.Done()
restarted = true
break
}
time.Sleep(time.Second)
}
return restarted, nil
}
func printError(message string) {
printRedBold("FAILED")
printLine(message)
}
func setFlagsAndReturnAppName(args []string) (string, error) {
rrsFlags := flag.NewFlagSet("rolling-restart", flag.ExitOnError)
maxCycles := rrsFlags.Int("max-cycles", maxRestartWaitCycles, "Maximum number of cycles to wait when checking for restart status. (Optional)")
rrsFlags.Parse(args[1:])
if !rrsFlags.Parsed() {
return "", errors.New("Failed parsing command line arguments.")
}
maxRestartWaitCycles = *maxCycles
remainingArgs := rrsFlags.Args()
if len(remainingArgs) == 0 {
return "", errors.New("An application name was not provided. Usage: cf rolling-restart APP_NAME")
}
if len(remainingArgs) > 1 {
return "", errors.New("Only a single app name is currently supported, please try again.")
}
return remainingArgs[0], nil
}
func validateCLISession(conn plugin.CliConnection) error {
var loggedIn bool
var hasOrg bool
var hasSpace bool
var err error
if loggedIn, err = conn.IsLoggedIn(); err != nil {
return err
}
if !loggedIn {
return errors.New("You are not logged in, please log in and try again.")
}
if hasOrg, err = conn.HasOrganization(); err != nil {
return err
}
if !hasOrg {
return errors.New("The logged in user does not have an Org set, please select an Org and Space and try again.")
}
if hasSpace, err = conn.HasSpace(); err != nil {
return err
}
if !hasSpace {
return errors.New("The logged in user does not have a Space set, please select a Space and try again.")
}
return nil
}
func isInstanceRunning(conn plugin.CliConnection, appGUID string, instanceID string) (bool, error) {
var instanceStatuses Instances
var err error
if instanceStatuses, err = getInstances(conn, appGUID); err != nil {
return false, err
}
instance := instanceStatuses[instanceID]
running := instance.State == "RUNNING" && instance.Uptime < 10
return running, nil
}
func restartInstance(conn plugin.CliConnection, appName string, instanceID string) error {
_, err := conn.CliCommand("restart-app-instance", appName, instanceID)
return err
}
func getappGUID(conn plugin.CliConnection, appName string) (string, error) {
var appGUID []string
var err error
if appGUID, err = conn.CliCommandWithoutTerminalOutput("app", appName, "--guid"); err != nil {
return "", err
}
return appGUID[0], nil
}
func getInstances(conn plugin.CliConnection, appGUID string) (Instances, error) {
var instances Instances
instancesCurlURL := fmt.Sprintf("/v2/apps/%s/instances", appGUID)
instanceJSON, curlErr := conn.CliCommandWithoutTerminalOutput("curl", "-X", "GET", instancesCurlURL)
if curlErr != nil {
return nil, curlErr
}
unmarshallErr := json.Unmarshal([]byte(strings.Join(instanceJSON, "")), &instances)
if unmarshallErr != nil {
return nil, unmarshallErr
}
return instances, nil
}
func getKeysFor(m map[string]Instance) []string {
keys := make([]string, len(m))
i := 0
for k := range m {
keys[i] = k
i++
}
sort.Strings(keys)
return keys
}
var versionRegexp = regexp.MustCompile(`^v?([0-9]+).([0-9]+).([0-9]+)$`)
func main() {
submatches := versionRegexp.FindAllStringSubmatch(Version, -1)
if len(submatches) == 0 || len(submatches[0]) != 4 {
printError("unable to parse version `" + Version + "`")
exit(failureExit)
}
major, err := strconv.Atoi(submatches[0][1])
if err != nil {
printError("unable to parse major version `" + Version + "`")
exit(failureExit)
}
minor, err := strconv.Atoi(submatches[0][2])
if err != nil {
printError("unable to parse minor version `" + Version + "`")
exit(failureExit)
}
build, err := strconv.Atoi(submatches[0][3])
if err != nil {
printError("unable to parse build version `" + Version + "`")
exit(failureExit)
}
rollingRestart := &RollingRestart{
plugin.VersionType{
Major: major,
Minor: minor,
Build: build,
},
}
plugin.Start(rollingRestart)
}