forked from convox/rack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscale.go
137 lines (112 loc) · 2.69 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
package main
import (
"fmt"
"github.com/convox/rack/client"
"github.com/convox/rack/cmd/convox/stdcli"
"gopkg.in/urfave/cli.v1"
)
func init() {
stdcli.RegisterCommand(cli.Command{
Name: "scale",
Description: "scale an app's processes",
Usage: "<process> [--count=2] [--memory=256] [--cpu=256]",
Action: cmdScale,
Flags: []cli.Flag{
appFlag,
rackFlag,
cli.IntFlag{
Name: "count",
Usage: "Number of processes to keep running for specified process type.",
},
cli.IntFlag{
Name: "memory",
Usage: "Amount of memory, in MB, available to specified process type.",
},
cli.IntFlag{
Name: "cpu",
Usage: "CPU units available to specified process type.",
},
cli.BoolFlag{
Name: "wait",
Usage: "wait for app to finish scaling before returning",
},
},
})
}
func cmdScale(c *cli.Context) error {
_, app, err := stdcli.DirApp(c, ".")
if err != nil {
return stdcli.Error(err)
}
opts := client.FormationOptions{}
if c.IsSet("count") {
opts.Count = c.String("count")
}
if c.IsSet("cpu") {
opts.CPU = c.String("cpu")
}
if c.IsSet("memory") {
opts.Memory = c.String("memory")
}
// validate single process type argument
switch len(c.Args()) {
case 0:
if opts.Memory != "" || opts.CPU != "" || opts.Count != "" {
return stdcli.Error(fmt.Errorf("missing process name"))
}
displayFormation(c, app)
return nil
case 1:
if opts.Count == "" && opts.CPU == "" && opts.Memory == "" {
displayFormation(c, app)
return nil
}
// fall through to scale API call
default:
stdcli.Usage(c, "scale")
return nil
}
process := c.Args()[0]
err = rackClient(c).SetFormation(app, process, opts)
if err != nil {
return stdcli.Error(err)
}
err = displayFormation(c, app)
if err != nil {
return stdcli.Error(err)
}
if c.Bool("wait") {
fmt.Printf("Waiting for stabilization... ")
a, err := rackClient(c).GetApp(app)
if err != nil {
return stdcli.Error(err)
}
if err := waitForReleasePromotion(c, app, a.Release); err != nil {
return stdcli.Error(err)
}
fmt.Println("OK")
}
return nil
}
func displayFormation(c *cli.Context, app string) error {
formation, err := rackClient(c).ListFormation(app)
if err != nil {
return err
}
pss, err := rackClient(c).GetProcesses(app, false)
if err != nil {
return err
}
running := map[string]int{}
for _, ps := range pss {
if ps.Id != "pending" {
running[ps.Name] += 1
}
}
t := stdcli.NewTable("NAME", "DESIRED", "RUNNING", "CPU", "MEMORY")
for _, f := range formation {
t.AddRow(f.Name, fmt.Sprintf("%d", f.Count), fmt.Sprintf("%d", running[f.Name]), fmt.Sprintf("%d", f.CPU), fmt.Sprintf("%d", f.Memory))
}
t.Print()
return nil
}