/
scale.go
159 lines (138 loc) · 3.85 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
package apps
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/Scalingo/cli/config"
"github.com/Scalingo/go-scalingo/debug"
"github.com/Scalingo/cli/io"
"github.com/Scalingo/cli/utils"
"github.com/Scalingo/go-scalingo"
"gopkg.in/errgo.v1"
)
type ScaleRes struct {
Containers []scalingo.ContainerType `json:"containers"`
}
func Scale(app string, sync bool, types []string) error {
var (
size string
containers []scalingo.ContainerType
modificator byte
err error
)
c, err := config.ScalingoClient()
if err != nil {
return errgo.Notef(err, "fail to get Scalingo client")
}
scaleParams := &scalingo.AppsScaleParams{}
typesWithAutoscaler := []string{}
autoscalers, err := c.AutoscalersList(app)
if err != nil {
return errgo.NoteMask(err, "fail to list the autoscalers")
}
for _, t := range types {
splitT := strings.Split(t, ":")
if len(splitT) != 2 && len(splitT) != 3 {
return errgo.Newf("%s is invalid, format is <type>:<amount>[:<size>]", t)
}
typeName, typeAmount := splitT[0], splitT[1]
if len(splitT) == 3 {
size = splitT[2]
}
if typeAmount[0] == '-' || typeAmount[0] == '+' {
modificator = typeAmount[0]
typeAmount = typeAmount[1:]
if size != "" {
return errgo.Newf("%s is invalid, can't use relative modificator with size, change the size first", t)
}
if containers == nil {
containers, err = c.AppsPs(app)
if err != nil {
return errgo.Notef(err, "fail to get list of running containers")
}
debug.Println("get container list", containers)
}
}
amount, err := strconv.ParseInt(typeAmount, 10, 32)
if err != nil {
return errgo.Newf("%s in %s should be an integer", typeAmount, t)
}
for _, a := range autoscalers {
if a.ContainerType == typeName {
typesWithAutoscaler = append(typesWithAutoscaler, typeName)
break
}
}
newContainerConfig := scalingo.ContainerType{Name: typeName, Size: size}
if modificator != 0 {
for _, container := range containers {
if container.Name == typeName {
if modificator == '-' {
newContainerConfig.Amount = container.Amount - int(amount)
} else if modificator == '+' {
newContainerConfig.Amount = container.Amount + int(amount)
}
break
}
}
} else {
newContainerConfig.Amount = int(amount)
}
scaleParams.Containers = append(scaleParams.Containers, newContainerConfig)
}
if len(typesWithAutoscaler) > 0 {
io.Warning(autoscaleDisableMessage(typesWithAutoscaler))
var confirm string
fmt.Scanln(&confirm)
if confirm != "y" && confirm != "Y" {
return errgo.New("You didn't confirm, aborting…")
}
}
res, err := c.AppsScale(app, scaleParams)
if err != nil {
if !utils.IsPaymentRequiredAndFreeTrialExceededError(err) {
return errgo.Mask(err)
}
// If error is Payment Required and user tries to exceed its free trial
return utils.AskAndStopFreeTrial(c, func() error {
return Scale(app, sync, types)
})
}
defer res.Body.Close()
var scaleRes ScaleRes
err = json.NewDecoder(res.Body).Decode(&scaleRes)
if err != nil {
return errgo.Mask(err)
}
fmt.Printf("Your application is being scaled to:\n")
for _, ct := range scaleRes.Containers {
fmt.Println(io.Indent(fmt.Sprintf("%s: %d - %s", ct.Name, ct.Amount, ct.Size), 2))
}
if !sync {
return nil
}
err = handleOperation(app, res)
if err != nil {
return errgo.Mask(err)
}
fmt.Println("Your application has been scaled.")
return nil
}
func autoscaleDisableMessage(typesWithAutoscaler []string) string {
if len(typesWithAutoscaler) <= 0 {
return ""
}
msg := "An autoscaler is configured for " + strings.Join(typesWithAutoscaler, ", ") + " container"
if len(typesWithAutoscaler) > 1 {
msg += "s"
}
msg += ". Manually scaling "
if len(typesWithAutoscaler) > 1 {
msg += "them"
} else {
msg += "it"
}
msg += " will disable the autoscaler. Do you confirm? (y/N)"
return msg
}