-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
513 lines (435 loc) · 11.5 KB
/
main.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package main
import (
"crypto/tls"
"errors"
"io/ioutil"
"net/http"
neturl "net/url"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
type contentAuth struct {
user string
password string
insecure bool
}
type pollASGActivities func(
*autoscaling.DescribeScalingActivitiesInput,
autoscalingiface.AutoScalingAPI,
string,
) (bool, error)
func main() {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
viper.AutomaticEnv()
viper.SetDefault("poll", 10)
viper.SetDefault("timeout", 600)
viper.SetDefault("auth.insecure", false)
viper.SetConfigName("config") // name of config file (without extension)
viper.AddConfigPath(".") // look for config in the working directory
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.WithError(err).Panic("Fatal error trying to read the config file")
}
sess := session.Must(session.NewSession())
svc := autoscaling.New(sess)
os.Exit(do(
svc,
viper.GetString("primary"),
viper.GetString("secondary"),
viper.GetString("url"),
contentAuth{
user: viper.GetString("auth.user"),
password: viper.GetString("auth.password"),
insecure: viper.GetBool("auth.insecure"),
},
(time.Duration(viper.GetInt("poll")))*time.Second,
(time.Duration(viper.GetInt("timeout")))*time.Second))
}
func do(
svc autoscalingiface.AutoScalingAPI,
primary string,
secondary string,
u string,
auth contentAuth,
poll time.Duration,
timeout time.Duration,
) int {
log.WithFields(log.Fields{
"primary": primary,
"secondary": secondary,
"poll": poll,
"timeout": timeout,
"auth.user": auth.user,
"auth.password": auth.password,
"auth.insecure": auth.insecure,
}).Info("Parameters")
exitCode := 0
asgName := os.Getenv("ASG_NAME")
err := validateAwsCredentials()
if err != nil {
log.WithError(err).Fatal("AWS environment variables needed")
}
instanceIDs := getInstanceIDs(getInstancesInAutoScalingGroup(&asgName, svc))
result := enterStandby(asgName, svc, instanceIDs, poll, timeout)
exitCode += result
if result == 0 {
exitCode += pollForContent(secondary, u, auth, poll, timeout, checkForContentAtURL)
}
// This tries forever to get all the instances back into service
for !areAllInstancesInService(
getInstancesInAutoScalingGroup(&asgName, svc)) {
exitCode += exitStandby(
asgName,
svc,
instanceIDs,
poll,
timeout,
func(in bool) bool { return in },
)
}
// Now check that the content of the url is the original primary content
exitCode += pollForContent(primary, u, auth, poll, timeout, checkForContentAtURL)
log.WithFields(log.Fields{
"extCode": exitCode,
}).Info("Finished")
return exitCode
}
func areAllInstancesInService(instances []*autoscaling.Instance) bool {
log.WithField("instances", instances).Debug("areAllInstancesInService")
for _, i := range instances {
if *(i.LifecycleState) != "InService" {
log.WithField("instances", instances).Info("Some instances not in service")
return false
}
}
log.Info("All instances now in service")
return true
}
func pollForContent(
content string,
u string,
auth contentAuth,
poll time.Duration,
timeout time.Duration,
check func(string, string, contentAuth) int,
) int {
done := make(chan bool)
ticker := time.NewTicker(poll)
go func() {
for t := range ticker.C {
log.WithField("t", t).Debug("Poll for content check")
if check(content, u, auth) == 0 {
done <- true
}
}
}()
select {
case _ = <-done:
ticker.Stop()
log.Info("Content check polling finished")
return 0
case <-time.After(timeout):
ticker.Stop()
log.Warn("Content check polling timed out")
return 1
}
}
func checkForContentAtURL(content string, u string, auth contentAuth) int {
log.WithFields(log.Fields{
"url": u,
"content": content,
}).Debug("checkForContentAtURL")
_, err := neturl.ParseRequestURI(u)
if err != nil {
log.
WithError(err).
WithField("url", u).
Error("Could not parse the URL")
return 1
}
res, err := getURL(u, auth.user, auth.password, auth.insecure)
if err != nil {
log.
WithError(err).
WithField("url", u).
Error("Could not get the URL")
return 1
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.
WithError(err).
WithField("res", res).
Error("Could not read the response body")
return 1
}
exists := strings.Contains(string(body), content)
if !exists {
log.WithFields(log.Fields{
"res": res,
"body": string(body),
}).Debug("Did not find the expected content at the failover url")
log.WithFields(log.Fields{
"content": content,
}).Warn("Did not find the expected content at the failover url")
return 1
}
log.WithFields(log.Fields{
"content": content,
"url": u,
}).Info("Found the expected content")
return 0
}
func getURL(
url string,
user string,
password string,
insecure bool) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.WithError(err).Error("Creating request")
return nil, err
}
if user != "" {
req.SetBasicAuth(user, password)
}
client := &http.Client{}
if insecure {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client.Transport = tr
}
response, err := client.Do(req)
if err != nil {
log.WithError(err).Error("Request")
}
return response, err
}
func enterStandby(
asgName string,
svc autoscalingiface.AutoScalingAPI,
instanceIDs []*string,
poll time.Duration,
timeout time.Duration,
) int {
log.Info("Attempting to enter standby")
ret := 0
enterStandbyInput := getEnterStandbyInput(instanceIDs, &asgName)
enterStandbyOutput, err := svc.EnterStandby(enterStandbyInput)
if err != nil {
log.WithFields(log.Fields{
"err": err,
"enterStandbyOutput": enterStandbyOutput,
}).Error("Error entering instances into standby")
// We'll let the logic carry on, which may mean that the wait for
// standby will timeout but will continue to attempt to put everything
// back into service.
ret++
}
activityIDs := []*string{enterStandbyOutput.Activities[0].ActivityId}
result := waitForInstancesToReachSuccessfulStatus(
&asgName,
activityIDs,
svc,
poll,
timeout)
if result == false {
log.
Error("Some (or all) of the instances in the autoscaling group did not enter standby")
ret++
} else {
log.
Info("Instances now in standby")
}
return ret
}
func exitStandby(
asgName string,
svc autoscalingiface.AutoScalingAPI,
instanceIDs []*string,
poll time.Duration,
timeout time.Duration,
isSuccess func(bool) bool,
) int {
log.Info("Attempting to exit standby")
exitStandbyArgs := autoscaling.ExitStandbyInput{
AutoScalingGroupName: &asgName,
InstanceIds: instanceIDs,
}
exitStandbyOutput, err := svc.ExitStandby(&exitStandbyArgs)
if err != nil {
log.WithFields(log.Fields{
"exitStandbyOutput": exitStandbyOutput,
"err": err,
}).Error("Error calling ExitStandby")
return 1
}
activityIDs := []*string{exitStandbyOutput.Activities[0].ActivityId}
ret := 0
retryAttempts := 3
for i := 0; i < retryAttempts; i++ {
result := waitForInstancesToReachSuccessfulStatus(
&asgName,
activityIDs,
svc,
poll,
timeout)
if isSuccess(result) {
log.Info("Instances exited standby")
ret = 0
break
}
log.Error("Instances failed to reach successful status")
ret++
}
return ret
}
func waitForInstancesToReachSuccessfulStatus(
asgName *string,
activityIDs []*string,
svc autoscalingiface.AutoScalingAPI,
poll time.Duration,
timeout time.Duration,
) bool {
describeScalingActivitiesQueryParams := &autoscaling.DescribeScalingActivitiesInput{
ActivityIds: activityIDs,
AutoScalingGroupName: asgName,
MaxRecords: aws.Int64(1),
}
return handleASGActivityPolling(
describeScalingActivitiesQueryParams,
checkActivitiesForStatus,
svc,
poll,
timeout,
"Successful")
}
func getInstancesInAutoScalingGroup(
asgName *string,
svc autoscalingiface.AutoScalingAPI) []*autoscaling.Instance {
instanceIDQueryParams := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{
asgName,
},
MaxRecords: aws.Int64(1),
}
resp, err := svc.DescribeAutoScalingGroups(instanceIDQueryParams)
if err != nil {
log.WithError(err).Fatal("Coud not get instances in asg, DescribeAutoScalingGroups failed")
}
return resp.AutoScalingGroups[0].Instances
}
func handleASGActivityPolling(
describeActivityConfig *autoscaling.DescribeScalingActivitiesInput,
pollFunc pollASGActivities,
svc autoscalingiface.AutoScalingAPI,
poll time.Duration,
timeout time.Duration,
statusCode string,
) bool {
log.WithFields(log.Fields{
"describeActivityConfig": describeActivityConfig,
}).Debug("handleASGActivityPolling: ASG describe input")
var pollIteration int64
for {
if pollIteration >= (int64(timeout) / int64(poll)) {
break
}
success, err := pollFunc(describeActivityConfig, svc, statusCode)
if err != nil {
log.WithError(err).Error("Error waiting for ASG update")
break
}
if success {
return true
}
time.Sleep(poll)
pollIteration++
log.WithField("poll", pollIteration).Info("Polling ASG status")
}
return false
}
func checkActivitiesForStatus(
describeActivityConfig *autoscaling.DescribeScalingActivitiesInput,
svc autoscalingiface.AutoScalingAPI,
statusCode string,
) (bool, error) {
resp, err := svc.DescribeScalingActivities(describeActivityConfig)
if err != nil {
log.WithFields(log.Fields{
"response": resp,
"err": err,
}).Error("DescribeScalingActivities failed")
return false, err
}
finished := true
for _, activity := range resp.Activities {
if *activity.StatusCode != statusCode {
finished = false
}
}
return finished, err
}
func getDescribeScalingActivitiesInput(
activityIDs []*string,
resourceName *string) *autoscaling.DescribeScalingActivitiesInput {
return &autoscaling.DescribeScalingActivitiesInput{
ActivityIds: activityIDs,
AutoScalingGroupName: resourceName,
MaxRecords: aws.Int64(1),
}
}
func getEnterStandbyInput(
instanceIDs []*string,
resourceName *string) *autoscaling.EnterStandbyInput {
ret := &autoscaling.EnterStandbyInput{
AutoScalingGroupName: resourceName,
ShouldDecrementDesiredCapacity: aws.Bool(true),
InstanceIds: instanceIDs,
}
log.WithFields(log.Fields{
"EnterStandbyInput": ret,
}).Debug("Query parameters for stand by")
return ret
}
func getInstanceIDs(
instances []*autoscaling.Instance) []*string {
instanceIDs := []*string{}
for _, instance := range instances {
instanceIDs = append(instanceIDs, instance.InstanceId)
}
log.WithFields(log.Fields{
"instanceIDs": *instanceIDs[0],
}).Debug("Instances in auto scaling group")
return instanceIDs
}
func validateAwsCredentials() error {
log.Info("Checking Credentials...")
if isEnvVarSetWithValue("AWS_ACCESS_KEY_ID") &&
isEnvVarSetWithValue("AWS_SECRET_ACCESS_KEY") &&
isEnvVarSetWithValue("AWS_REGION") &&
isEnvVarSetWithValue("ASG_NAME") {
log.Info("Credentials OK")
return nil
}
return errors.New("AWS credentials not set")
}
func isEnvVarSetWithValue(key string) bool {
val, ok := os.LookupEnv(key)
log.WithFields(log.Fields{
"key": key,
"val": val,
"ok": ok,
}).Debug("isEnvVarSetWithValue")
return ok && val != ""
}