forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscale.go
183 lines (159 loc) · 4.56 KB
/
scale.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
package main
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/go-docopt"
"github.com/flynn/flynn/controller/client"
ct "github.com/flynn/flynn/controller/types"
)
func init() {
register("scale", runScale, `
usage: flynn scale [options] [<type>=<qty>...]
Scale changes the number of jobs for each process type in a release.
Ommitting the arguments will show the current scale.
Options:
-n, --no-wait don't wait for the scaling events to happen
-r, --release <release> id of release to scale (defaults to current app release)
Example:
$ flynn scale
web=4 worker=2
$ flynn scale web=2 worker=5
scaling web: 4=>2, worker: 2=>5
02:28:34.333 ==> web flynn-3f656af6f1e44092aa7037046236b203 down
02:28:34.466 ==> web flynn-ee83def0b8e4455793a43c8c70f5b34e down
02:28:35.479 ==> worker flynn-84f70ca18c9641ef83a178a19db867a3 up
02:28:36.508 ==> worker flynn-a3de8c326cc542aa89235e53ba304260 up
02:28:37.601 ==> worker flynn-e24760c511af4733b01ed5b98aa54647 up
scale completed in 3.944629056s
`)
}
const scaleTimeout = 20 * time.Second
// takes args of the form "web=1", "worker=3", etc
func runScale(args *docopt.Args, client *controller.Client) error {
app := mustApp()
release, err := determineRelease(client, args.String["--release"], app)
if err != nil {
return err
}
formation, err := client.GetFormation(app, release.ID)
if err == controller.ErrNotFound {
formation = &ct.Formation{
AppID: app,
ReleaseID: release.ID,
Processes: make(map[string]int),
}
} else if err != nil {
return err
}
if formation.Processes == nil {
formation.Processes = make(map[string]int)
}
typeCounts := args.All["<type>=<qty>"].([]string)
if len(typeCounts) == 0 {
scale := make([]string, 0, len(release.Processes))
for typ := range release.Processes {
scale = append(scale, fmt.Sprintf("%s=%d", typ, formation.Processes[typ]))
}
fmt.Println(strings.Join(scale, " "))
return nil
}
current := formation.Processes
processes := make(map[string]int, len(current)+len(typeCounts))
for k, v := range current {
processes[k] = v
}
for _, arg := range typeCounts {
i := strings.IndexRune(arg, '=')
if i < 0 {
fmt.Println(commands["scale"].usage)
}
val, err := strconv.Atoi(arg[i+1:])
if err != nil {
fmt.Println(commands["scale"].usage)
}
processes[arg[:i]] = val
}
formation.Processes = processes
if scalingComplete(current, processes) {
fmt.Println("requested scale equals current scale, nothing to do!")
return nil
}
scale := make([]string, 0, len(release.Processes))
for typ := range release.Processes {
if current[typ] != processes[typ] {
scale = append(scale, fmt.Sprintf("%s: %d=>%d", typ, current[typ], processes[typ]))
}
}
fmt.Printf("scaling %s\n\n", strings.Join(scale, ", "))
events := make(chan *ct.JobEvent)
stream, err := client.StreamJobEvents(app, 0, events)
if err != nil {
return err
}
defer stream.Close()
err = client.PutFormation(formation)
if err != nil || args.Bool["--no-wait"] {
return err
}
start := time.Now()
loop:
for {
select {
case e, ok := <-events:
if !ok {
if err := stream.Err(); err != nil {
return err
}
return fmt.Errorf("event stream unexpectedly ended")
}
// ignore one-off jobs or starting events
if e.Job.State == "starting" || e.Job.Type == "" {
continue loop
}
fmt.Printf("%s ==> %s %s %s\n", time.Now().Format("15:04:05.000"), e.Job.Type, e.JobID, e.Job.State)
switch e.Job.State {
case "up":
current[e.Job.Type]++
case "down", "crashed":
current[e.Job.Type]--
}
if scalingComplete(current, processes) {
fmt.Printf("\nscale completed in %s\n", time.Since(start))
return nil
}
case <-time.After(scaleTimeout):
return fmt.Errorf("timed out waiting for scale events")
}
}
}
func determineRelease(client *controller.Client, releaseID, app string) (*ct.Release, error) {
if releaseID == "" {
release, err := client.GetAppRelease(app)
if err == controller.ErrNotFound {
return nil, errors.New("No app release, specify a release with --release")
}
if err != nil {
return nil, err
}
return release, nil
}
return client.GetRelease(releaseID)
}
func scalingComplete(actual, expected map[string]int) bool {
// check all the expected counts are the same in actual
for typ, count := range expected {
if actual[typ] != count {
return false
}
}
// check any counts in actual which aren't in expected are zero
for typ, count := range actual {
if _, ok := expected[typ]; !ok && count != 0 {
return false
}
}
return true
}