forked from krujos/scaleover-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaleover_plugin.go
249 lines (218 loc) · 5.79 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
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: 0,
Build: 4,
},
Commands: []plugin.Command{
{
Name: "scaleover",
HelpText: "Roll http traffic from one application to another",
UsageDetails: plugin.Usage{
Usage: "cf scaleover APP1 APP2 ROLLOVER_DURATION [--no-route-check]",
},
},
},
}
}
func main() {
plugin.Start(new(ScaleoverCmd))
}
func (cmd *ScaleoverCmd) usage(args []string) error {
badArgs := 4 != len(args)
if 5 == len(args) {
if "--no-route-check" == args[4] {
badArgs = false
}
}
if badArgs {
return errors.New("Usage: cf scaleover\n\tcf scaleover APP1 APP2 ROLLOVER_DURATION [--no-route-check]")
}
return nil
}
func (cmd *ScaleoverCmd) shouldEnforceRoutes(args []string) bool {
return "--no-route-check" != args[len(args)-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) {
enforceRoutes := cmd.shouldEnforceRoutes(args)
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)
}
// 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("There are no instances of the source app to scale over")
os.Exit(0)
}
sleepInterval := time.Duration(rolloverTime.Nanoseconds() / int64(count))
for count > 0 {
count--
cmd.app2.scaleUp(cliConnection)
cmd.app1.scaleDown(cliConnection)
cmd.showStatus()
if count > 0 {
time.Sleep(sleepInterval)
}
}
fmt.Println()
}
func (cmd *ScaleoverCmd) getAppStatus(cliConnection plugin.CliConnection, name string) (AppStatus, error) {
status := AppStatus{
name: name,
countRunning: 0,
countRequested: 0,
state: "unknown",
routes: []string{},
}
output, _ := cliConnection.CliCommandWithoutTerminalOutput("app", name)
for idx, v := range output {
v = strings.TrimSpace(v)
if strings.HasPrefix(v, "FAILED") {
e := output[idx+1]
return status, errors.New(e)
}
if strings.HasPrefix(v, "requested state: ") {
status.state = strings.TrimPrefix(v, "requested state: ")
}
if strings.HasPrefix(v, "instances: ") {
instances := strings.TrimPrefix(v, "instances: ")
split := strings.Split(instances, "/")
status.countRunning, _ = strconv.Atoi(split[0])
status.countRequested, _ = strconv.Atoi(split[1])
}
if strings.HasPrefix(v, "urls: ") {
urls := strings.TrimPrefix(v, "urls: ")
status.routes = strings.Split(urls, ", ")
}
}
// Compensate for some CF weirdness that leaves the requested instances non-zero
// even though the app is stopped
if "stopped" == status.state {
status.countRequested = 0
}
return status, nil
}
func (app *AppStatus) scaleUp(cliConnection plugin.CliConnection) {
// If not already started, start it
if app.state != "started" {
cliConnection.CliCommandWithoutTerminalOutput("start", app.name)
app.state = "started"
}
app.countRequested++
cliConnection.CliCommandWithoutTerminalOutput("scale", "-i", strconv.Itoa(app.countRequested), app.name)
}
func (app *AppStatus) scaleDown(cliConnection plugin.CliConnection) {
app.countRequested--
// If going to zero, stop the app
if app.countRequested == 0 {
cliConnection.CliCommandWithoutTerminalOutput("stop", app.name)
app.state = "stopped"
} else {
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!")
}