-
Notifications
You must be signed in to change notification settings - Fork 15
/
scaleover_plugin.go
353 lines (305 loc) · 9.23 KB
/
scaleover_plugin.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
// Copyright 2016 Joshua Kruck
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/andrew-d/go-termutil"
"github.com/cloudfoundry/cli/plugin"
)
//AppStatus represents the sattus of a app in CF
type AppStatus struct {
name string
countRunning int
countRequested int
state string
routes []string
}
//ScaleoverCmd is this plugin
type ScaleoverCmd struct {
app1 *AppStatus
app2 *AppStatus
maxcount int
}
//GetMetadata returns metatada
func (cmd *ScaleoverCmd) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "scaleover",
Version: plugin.VersionType{
Major: 1,
Minor: 1,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "scaleover",
HelpText: "Roll traffic from one application to another",
UsageDetails: plugin.Usage{
Usage: "cf scaleover APP1 APP2 ROLLOVER_DURATION",
Options: map[string]string{
"-no-route-check": "Since both apps are live at the same time, there is assumed use of a shared route (default true)",
"-wait-for-start": "Should scaleover wait for confirmation that the scaled up instace(s) are 'started' before scaling down? (default false)",
"-post-start-sleep": "How long should scaleover wait after the new instances are considered 'started' for the app itself to initialize/bootstrap. Supports standard duration strings (eg '10s', '1m', etc). Used ONLY in conjunction with `--wait-for-start`. (default 0)",
"-leave": "How many 'blue' instances should remain running when using scaleover to 'green'? (default 1/stopped)",
"-batch-size": "How many instances should be scaled (both up/down) at a time? (default 1)",
},
},
},
},
}
}
func main() {
plugin.Start(new(ScaleoverCmd))
}
func (cmd *ScaleoverCmd) usage(args []string) error {
badArgs := 4 != len(args)
for i := 4; i < len(args); i++ {
switch args[i] {
case "--no-route-check":
badArgs = false
case "--leave":
if i < len(args) -1 {
_, err := strconv.Atoi(args[i + 1])
badArgs = err != nil
}
case "--batch-size":
if i < len(args) -1 {
_, err := strconv.Atoi(args[i + 1])
badArgs = err != nil
}
case "--post-start-sleep":
if i < len(args) -1 {
badArgs = args[i + 1] == ""
}
}
}
if badArgs {
return errors.New("Usage: cf scaleover\n\tcf scaleover APP1 APP2 ROLLOVER_DURATION [--no-route-check] [--leave N]")
}
return nil
}
func (cmd *ScaleoverCmd) parseArgs(args []string) (bool, int, bool, time.Duration, int) {
enforceRoutes := true
waitForStarted := false
batchSize :=1
leave := 0
var postStartSleep time.Duration
var err error
for i := 4; i < len(args); i++ {
switch(args[i]) {
case "--no-route-check":
enforceRoutes = false
case "--wait-for-start":
waitForStarted = true
case "--leave":
if i < len(args) -1 {
leave, err = strconv.Atoi(args[i + 1])
}
case "--batch-size":
if i < len(args) -1 {
batchSize, err = strconv.Atoi(args[i + 1])
}
case "--post-start-sleep":
if i < len(args) -1 {
postStartSleep, err = cmd.parseTime(args[i + 1])
}
}
}
if err == nil {
return enforceRoutes, leave, waitForStarted, postStartSleep, batchSize
}
//defaults on error
return enforceRoutes, 0, waitForStarted, postStartSleep, 1
}
func (cmd *ScaleoverCmd) parseTime(duration string) (time.Duration, error) {
rolloverTime := time.Duration(0)
var err error
rolloverTime, err = time.ParseDuration(duration)
if err != nil {
return rolloverTime, err
}
if 0 > rolloverTime {
return rolloverTime, errors.New("Duration must be a positive number in the format of 1m")
}
return rolloverTime, nil
}
//Run runs the plugin
func (cmd *ScaleoverCmd) Run(cliConnection plugin.CliConnection, args []string) {
if args[0] == "scaleover" {
cmd.ScaleoverCommand(cliConnection, args)
}
}
//ScaleoverCommand creates a new instance of this plugin
func (cmd *ScaleoverCmd) ScaleoverCommand(cliConnection plugin.CliConnection, args []string) {
if err := cmd.usage(args); nil != err {
fmt.Println(err)
os.Exit(1)
}
rolloverTime, err := cmd.parseTime(args[3])
if nil != err {
fmt.Println(err)
os.Exit(1)
}
enforceRoutes, leave, waitForStarted, postStartSleep, batchSize := cmd.parseArgs(args)
// The getAppStatus calls will exit with an error if the named apps don't exist
if cmd.app1, err = cmd.getAppStatus(cliConnection, args[1]); nil != err {
fmt.Println(err)
os.Exit(1)
}
if cmd.app2, err = cmd.getAppStatus(cliConnection, args[2]); nil != err {
fmt.Println(err)
os.Exit(1)
}
if enforceRoutes {
if err = cmd.errorIfNoSharedRoute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
cmd.showStatus()
count := cmd.app1.countRequested
if count == 0 {
fmt.Println("\nThere are no instances of the source app to scale over\n")
os.Exit(0)
}
sleepInterval := time.Duration(rolloverTime.Nanoseconds() / int64(count))
cmd.doScaleover(cliConnection, count, leave, sleepInterval, waitForStarted, postStartSleep, batchSize)
fmt.Println()
}
func (cmd *ScaleoverCmd) doScaleover(cliConnection plugin.CliConnection,
count int, leave int, sleepInterval time.Duration, waitForStarted bool, postStartSleep time.Duration, batchSize int) {
origInstances := count
count -= cmd.app2.countRunning
leave -= cmd.app2.countRunning
for count > 0 {
cmd.app2.scaleUp(cliConnection, origInstances, batchSize)
cmd.app1.scaleDown(cliConnection, leave, batchSize, cmd.app2, waitForStarted, postStartSleep)
count-=batchSize
cmd.showStatus()
if count > 0 {
time.Sleep(sleepInterval)
}
}
}
func (cmd *ScaleoverCmd) getAppStatus(cliConnection plugin.CliConnection, name string) (*AppStatus, error) {
app, err := cliConnection.GetApp(name)
if nil != err {
return nil, err
}
status := &AppStatus{
name: name,
countRunning: 0,
countRequested: 0,
state: "unknown",
routes: make([]string, len(app.Routes)),
}
status.state = app.State
if app.State != "stopped" {
status.countRequested = app.InstanceCount
}
status.countRunning = app.RunningInstances
for idx, route := range app.Routes {
status.routes[idx] = route.Host + "." + route.Domain.Name
}
return status, nil
}
func (app *AppStatus) scaleUp(cliConnection plugin.CliConnection, origInstances int, batchSize int) {
app.countRequested+=batchSize
if(app.countRequested > origInstances){
app.countRequested = origInstances;
}
cliConnection.CliCommandWithoutTerminalOutput("scale", "-i", strconv.Itoa(app.countRequested), app.name)
// If not already started, start it
if app.state == "stopped" {
cliConnection.CliCommandWithoutTerminalOutput("start", app.name)
app.state = "started"
}
}
func (app *AppStatus) scaleDown(cliConnection plugin.CliConnection, leave int,
batchSize int, app2 *AppStatus, waitForStarted bool, postStartSleep time.Duration) {
if waitForStarted {
for {
refreshedApp2Status, err := cliConnection.GetApp(app2.name)
if nil != err {
fmt.Println("Can't scale down. Error")
break
}
// iterate through instances
hasStarting := false
for _,instance := range refreshedApp2Status.Instances {
if(instance.State != "running") {
hasStarting = true
time.Sleep(1 * time.Second)
break //instance loop
}
}
if !hasStarting {
time.Sleep(postStartSleep)
break
} // while loop
}
}
app.countRequested-=batchSize
if(app.countRequested < leave) {
app.countRequested = leave
}
// If going to zero, stop the app, and force to one instance
if app.countRequested <= 0 {
app.countRequested = 1
cliConnection.CliCommandWithoutTerminalOutput("stop", app.name)
app.state = "stopped"
}
cliConnection.CliCommandWithoutTerminalOutput("scale", "-i", strconv.Itoa(app.countRequested), app.name)
}
func (cmd *ScaleoverCmd) showStatus() {
if termutil.Isatty(os.Stdout.Fd()) {
fmt.Printf("%s (%s) %s %s %s (%s) \r",
cmd.app1.name,
cmd.app1.state,
strings.Repeat("<", cmd.app1.countRequested),
strings.Repeat(">", cmd.app2.countRequested),
cmd.app2.name,
cmd.app2.state,
)
} else {
fmt.Printf("%s (%s) %d instances, %s (%s) %d instances\n",
cmd.app1.name,
cmd.app1.state,
cmd.app1.countRequested,
cmd.app2.name,
cmd.app2.state,
cmd.app2.countRequested,
)
}
}
func (cmd *ScaleoverCmd) appsShareARoute() bool {
for _, r1 := range cmd.app1.routes {
for _, r2 := range cmd.app2.routes {
if r1 == r2 {
return true
}
}
}
return false
}
func (cmd *ScaleoverCmd) errorIfNoSharedRoute() error {
if cmd.appsShareARoute() {
return nil
}
return errors.New("Apps do not share a route!")
}