-
Notifications
You must be signed in to change notification settings - Fork 5
/
strategy-aws.go
97 lines (78 loc) · 2.44 KB
/
strategy-aws.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
package main
import (
"context"
"errors"
"fmt"
"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"
)
func strategyAWS(ctx context.Context, region string, asgName string, strategy string, warmup int64, minHealthy int64, wait bool) error {
ses := session.Must(session.NewSession(aws.NewConfig().WithRegion(region)))
svc := autoscaling.New(ses)
prefs := &autoscaling.RefreshPreferences{
MinHealthyPercentage: aws.Int64(minHealthy),
}
if warmup > -1 {
prefs.InstanceWarmup = aws.Int64(warmup)
}
var awsStrategy *string
strategies := autoscaling.RefreshStrategy_Values()
for _, s := range strategies {
if strings.ToLower(s) == strings.ToLower(strategy) {
awsStrategy = aws.String(s)
}
}
if awsStrategy == nil {
return errors.New(fmt.Sprintf("strategy %s not an AWS strategy in %v", strategy, strategies))
}
input := &autoscaling.StartInstanceRefreshInput{
AutoScalingGroupName: aws.String(asgName),
Strategy: awsStrategy,
Preferences: prefs,
}
result, err := svc.StartInstanceRefreshWithContext(ctx, input)
if err != nil {
return err
}
refreshId := *result.InstanceRefreshId
fmt.Printf("Refresh (strategy: AWS native %s) started on ASG %s with request ID %s\n", strategy, asgName, refreshId)
if !wait {
return nil
}
start := time.Now()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(5 * time.Second):
}
result, err := svc.DescribeInstanceRefreshesWithContext(ctx, &autoscaling.DescribeInstanceRefreshesInput{
AutoScalingGroupName: aws.String(asgName),
InstanceRefreshIds: []*string{result.InstanceRefreshId},
})
if err != nil {
return err
}
if len(result.InstanceRefreshes) != 1 {
fmt.Printf("Found %v refresh with ID %v\n", len(result.InstanceRefreshes), refreshId)
return ErrStrategyFailed
}
refresh := result.InstanceRefreshes[0]
switch *refresh.Status {
case "Successful":
fmt.Printf("Refresh completed in %v\n", refresh.EndTime.Sub(*refresh.StartTime).Truncate(time.Second))
return nil
case "Pending", "InProgress":
fmt.Printf("%v\tRefresh is %v\n", time.Since(start).Truncate(time.Second), *refresh.Status)
case "Failed", "Cancelling", "Cancelled":
fmt.Printf("Refresh is %v\n", *refresh.Status)
return ErrStrategyFailed
default:
fmt.Printf("Unknown refresh status %v\n", *refresh.Status)
return ErrStrategyFailed
}
}
}