This repository has been archived by the owner on Apr 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
deploy_args.go
274 lines (242 loc) · 7.03 KB
/
deploy_args.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
package deploy
import (
"errors"
"fmt"
"regexp"
"gopkg.in/urfave/cli.v1"
)
// Args are arguments passed to the deploy command
type Args struct {
IAAS string
IAASIsSet bool
Region string
RegionIsSet bool
Domain string
DomainIsSet bool
TLSCert string
TLSCertIsSet bool
TLSKey string
TLSKeyIsSet bool
WorkerCount int
WorkerCountIsSet bool
WorkerSize string
WorkerSizeIsSet bool
WebSize string
WebSizeIsSet bool
SelfUpdate bool
SelfUpdateIsSet bool
DBSize string
// DBSizeIsSet is true if the user has manually specified the db-size (ie, it's not the default)
DBSizeIsSet bool
Namespace string
NamespaceIsSet bool
AllowIPs string
AllowIPsIsSet bool
GithubAuthClientID string
GithubAuthClientIDIsSet bool
GithubAuthClientSecret string
GithubAuthClientSecretIsSet bool
// GithubAuthIsSet is true if the user has specified both the --github-auth-client-secret and --github-auth-client-id flags
GithubAuthIsSet bool
Tags cli.StringSlice
// TagsIsSet is true if the user has specified tags using --tags
TagsIsSet bool
Spot bool
SpotIsSet bool
Preemptible bool
Zone string
ZoneIsSet bool
WorkerType string
WorkerTypeIsSet bool
NetworkCIDR string
NetworkCIDRIsSet bool
PublicCIDR string
PublicCIDRIsSet bool
PrivateCIDR string
PrivateCIDRIsSet bool
RDS1CIDR string
RDS1CIDRIsSet bool
RDS2CIDR string
RDS2CIDRIsSet bool
}
// MarkSetFlags is marking the IsSet DeployArgs
func (a *Args) MarkSetFlags(c FlagSetChecker) error {
for _, f := range c.FlagNames() {
if c.IsSet(f) {
switch f {
case "region":
a.RegionIsSet = true
case "domain":
a.DomainIsSet = true
case "tls-cert":
a.TLSCertIsSet = true
case "tls-key":
a.TLSKeyIsSet = true
case "workers":
a.WorkerCountIsSet = true
case "worker-size":
a.WorkerSizeIsSet = true
case "web-size":
a.WebSizeIsSet = true
case "iaas":
a.IAASIsSet = true
case "self-update":
a.SelfUpdateIsSet = true
case "db-size":
a.DBSizeIsSet = true
case "spot", "preemptible":
a.SpotIsSet = true
case "allow-ips":
a.AllowIPsIsSet = true
case "github-auth-client-id":
a.GithubAuthClientIDIsSet = true
case "github-auth-client-secret":
a.GithubAuthClientSecretIsSet = true
case "add-tag":
a.TagsIsSet = true
case "namespace":
a.NamespaceIsSet = true
case "zone":
a.ZoneIsSet = true
case "worker-type":
a.WorkerTypeIsSet = true
case "vpc-network-range":
a.NetworkCIDRIsSet = true
case "public-subnet-range":
a.PublicCIDRIsSet = true
case "private-subnet-range":
a.PrivateCIDRIsSet = true
case "rds-subnet-range1":
a.RDS1CIDRIsSet = true
case "rds-subnet-range2":
a.RDS2CIDRIsSet = true
default:
return fmt.Errorf("flag %q is not supported by deployment flags", f)
}
}
}
a.GithubAuthIsSet = c.IsSet("github-auth-client-id") && c.IsSet("github-auth-client-secret")
return nil
}
// WorkerSizes are the permitted concourse worker sizes
var WorkerSizes = []string{"medium", "large", "xlarge", "2xlarge", "4xlarge", "12xlarge", "24xlarge"}
// WebSizes are the permitted concourse web sizes
var WebSizes = []string{"small", "medium", "large", "xlarge", "2xlarge"}
// AllowedDBSizes contains the valid values for --db-size flag
var AllowedDBSizes = []string{"small", "medium", "large", "xlarge", "2xlarge", "4xlarge"}
// ModifyGithub allows mutation of github related fields
func (a *Args) ModifyGithub(GithubAuthClientID, GithubAuthClientSecret string, GithubAuthIsSet bool) {
a.GithubAuthClientID = GithubAuthClientID
a.GithubAuthClientSecret = GithubAuthClientSecret
a.GithubAuthIsSet = GithubAuthIsSet
}
// Validate validates that flag interdependencies
func (a Args) Validate() error {
if err := a.validateCertFields(); err != nil {
return err
}
if err := a.validateWorkerFields(); err != nil {
return err
}
if err := a.validateWebFields(); err != nil {
return err
}
if err := a.validateDBFields(); err != nil {
return err
}
if err := a.validateGithubFields(); err != nil {
return err
}
if err := a.validateNetworkRanges(); err != nil {
return err
}
if err := a.validateTags(); err != nil {
return err
}
return nil
}
func (a Args) validateCertFields() error {
if a.TLSKey != "" && a.TLSCert == "" {
return errors.New("--tls-key requires --tls-cert to also be provided")
}
if a.TLSCert != "" && a.TLSKey == "" {
return errors.New("--tls-cert requires --tls-key to also be provided")
}
if (a.TLSKey != "" || a.TLSCert != "") && a.Domain == "" {
return errors.New("custom certificates require --domain to be provided")
}
return nil
}
func (a Args) validateWorkerFields() error {
if a.WorkerCount < 1 {
return errors.New("minimum number of workers is 1")
}
for _, size := range WorkerSizes {
if size == a.WorkerSize {
return nil
}
}
return fmt.Errorf("unknown worker size: `%s`. Valid sizes are: %v", a.WorkerSize, WorkerSizes)
}
func (a Args) validateWebFields() error {
for _, size := range WebSizes {
if size == a.WebSize {
return nil
}
}
return fmt.Errorf("unknown web node size: `%s`. Valid sizes are: %v", a.WebSize, WebSizes)
}
func (a Args) validateDBFields() error {
for _, size := range AllowedDBSizes {
if size == a.DBSize {
return nil
}
}
return fmt.Errorf("unknown DB size: `%s`. Valid sizes are: %v", a.DBSize, AllowedDBSizes)
}
func (a Args) validateGithubFields() error {
if a.GithubAuthClientID != "" && a.GithubAuthClientSecret == "" {
return errors.New("--github-auth-client-id requires --github-auth-client-secret to also be provided")
}
if a.GithubAuthClientID == "" && a.GithubAuthClientSecret != "" {
return errors.New("--github-auth-client-secret requires --github-auth-client-id to also be provided")
}
return nil
}
func (a Args) validateNetworkRanges() error {
if a.PublicCIDR != "" || a.PrivateCIDR != "" {
if a.PublicCIDR == "" || a.PrivateCIDR == "" {
return errors.New("both --public-subnet-range and --private-subnet-range are required when either is provided")
}
}
return nil
}
func (a Args) validateTags() error {
for _, tag := range a.Tags {
m, err := regexp.MatchString(`\w+=\w+`, tag)
if err != nil {
return err
}
if !m {
return fmt.Errorf("`%v` is not in the format `key=value`", tag)
}
}
return nil
}
// FlagSetChecker allows us to find out if flags were set, adn what the names of all flags are
type FlagSetChecker interface {
IsSet(name string) bool
FlagNames() (names []string)
}
// ContextWrapper wraps a CLI context for testing
type ContextWrapper struct {
c *cli.Context
}
// IsSet tells you if a user provided a flag
func (t *ContextWrapper) IsSet(name string) bool {
return t.c.IsSet(name)
}
// FlagNames lists all flags it's possible for a user to provide
func (t *ContextWrapper) FlagNames() (names []string) {
return t.c.FlagNames()
}