-
Notifications
You must be signed in to change notification settings - Fork 17
/
aws.go
203 lines (193 loc) · 7.26 KB
/
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
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
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"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"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"log"
)
func setAsgDesired(svc autoscalingiface.AutoScalingAPI, asg *autoscaling.Group, count int64, canIncreaseMax, verbose bool) error {
if count > *asg.MaxSize {
if canIncreaseMax {
err := setAsgMax(svc, asg, count, verbose)
if err != nil {
return err
}
} else {
return fmt.Errorf("unable to increase ASG %s desired size to %d as greater than max size %d", *asg.AutoScalingGroupName, count, *asg.MaxSize)
}
}
if verbose {
log.Printf("increasing ASG %s desired count to %d", *asg.AutoScalingGroupName, count)
}
desiredInput := &autoscaling.SetDesiredCapacityInput{
AutoScalingGroupName: asg.AutoScalingGroupName,
DesiredCapacity: aws.Int64(count),
HonorCooldown: aws.Bool(true),
}
_, err := svc.SetDesiredCapacity(desiredInput)
if err != nil {
errMsg := fmt.Sprintf("unable to increase ASG %s desired count to %d", *asg.AutoScalingGroupName, count)
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case autoscaling.ErrCodeScalingActivityInProgressFault:
return fmt.Errorf("%s - %s %v", errMsg, autoscaling.ErrCodeScalingActivityInProgressFault, aerr.Error())
case autoscaling.ErrCodeResourceContentionFault:
return fmt.Errorf("%s - %s %v", errMsg, autoscaling.ErrCodeResourceContentionFault, aerr.Error())
default:
return fmt.Errorf("%s - unexpected and unknown AWS error: %v", errMsg, aerr.Error())
}
}
return fmt.Errorf("%s - unexpected and unknown non-AWS error: %v", errMsg, err.Error())
}
if verbose {
log.Printf("increased ASG %s desired count to %d", *asg.AutoScalingGroupName, count)
}
return nil
}
func setAsgMax(svc autoscalingiface.AutoScalingAPI, asg *autoscaling.Group, count int64, verbose bool) error {
if verbose {
log.Printf("increasing ASG %s max size to %d to accommodate desired count", *asg.AutoScalingGroupName, count)
}
_, err := svc.UpdateAutoScalingGroup(&autoscaling.UpdateAutoScalingGroupInput{
AutoScalingGroupName: asg.AutoScalingGroupName,
MaxSize: aws.Int64(count),
})
if err != nil {
errMsg := fmt.Sprintf("unable to increase ASG %s max size to %d", *asg.AutoScalingGroupName, count)
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case autoscaling.ErrCodeScalingActivityInProgressFault:
return fmt.Errorf("%s - %s %v", errMsg, autoscaling.ErrCodeScalingActivityInProgressFault, aerr.Error())
case autoscaling.ErrCodeResourceContentionFault:
return fmt.Errorf("%s - %s %v", errMsg, autoscaling.ErrCodeResourceContentionFault, aerr.Error())
default:
return fmt.Errorf("%s - unexpected and unknown AWS error: %v", errMsg, aerr.Error())
}
}
return fmt.Errorf("%s - unexpected and unknown non-AWS error: %v", errMsg, err.Error())
}
if verbose {
log.Printf("increased ASG %s max size to %d to accommodate desired count", *asg.AutoScalingGroupName, count)
}
return nil
}
func awsGetHostname(svc ec2iface.EC2API, id string) (string, error) {
hostnames, err := awsGetHostnames(svc, []string{id})
if err != nil {
return "", err
}
if len(hostnames) < 1 {
return "", err
}
return hostnames[0], nil
}
func awsGetLaunchTemplateByID(svc ec2iface.EC2API, id string) (*ec2.LaunchTemplate, error) {
input := &ec2.DescribeLaunchTemplatesInput{
LaunchTemplateIds: []*string{
aws.String(id),
},
}
return awsGetLaunchTemplate(svc, input)
}
func awsGetLaunchTemplateByName(svc ec2iface.EC2API, name string) (*ec2.LaunchTemplate, error) {
input := &ec2.DescribeLaunchTemplatesInput{
LaunchTemplateNames: []*string{
aws.String(name),
},
}
return awsGetLaunchTemplate(svc, input)
}
func awsGetLaunchTemplate(svc ec2iface.EC2API, input *ec2.DescribeLaunchTemplatesInput) (*ec2.LaunchTemplate, error) {
templatesOutput, err := svc.DescribeLaunchTemplates(input)
descriptiveMsg := fmt.Sprintf("%v / %v", input.LaunchTemplateIds, input.LaunchTemplateNames)
if err != nil {
return nil, fmt.Errorf("Unable to get description for Launch Template %s: %v", descriptiveMsg, err)
}
if len(templatesOutput.LaunchTemplates) < 1 {
return nil, nil
}
return templatesOutput.LaunchTemplates[0], nil
}
func awsGetHostnames(svc ec2iface.EC2API, ids []string) ([]string, error) {
if len(ids) == 0 {
return []string{}, nil
}
ec2input := &ec2.DescribeInstancesInput{
InstanceIds: aws.StringSlice(ids),
}
nodesResult, err := svc.DescribeInstances(ec2input)
if err != nil {
return nil, fmt.Errorf("Unable to get description for node %v: %v", ids, err)
}
if len(nodesResult.Reservations) < 1 {
return nil, fmt.Errorf("Did not get any reservations for node %v", ids)
}
hostnames := make([]string, 0)
for _, i := range nodesResult.Reservations {
for _, j := range i.Instances {
hostnames = append(hostnames, *j.PrivateDnsName)
}
}
return hostnames, nil
}
func awsDescribeGroups(svc autoscalingiface.AutoScalingAPI, names []string) ([]*autoscaling.Group, error) {
input := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: aws.StringSlice(names),
}
result, err := svc.DescribeAutoScalingGroups(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case autoscaling.ErrCodeInvalidNextToken:
return nil, fmt.Errorf("Unexpected AWS NextToken error when doing non-pagination describe")
case autoscaling.ErrCodeResourceContentionFault:
return nil, fmt.Errorf("Unexpected AWS ResourceContentionFault when doing describe")
default:
return nil, fmt.Errorf("Unexpected and unknown AWS error when doing describe: %v", aerr)
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
return nil, fmt.Errorf("Unexpected and unknown non-AWS error when doing describe: %v", err.Error())
}
}
return result.AutoScalingGroups, nil
}
func awsTerminateNode(svc autoscalingiface.AutoScalingAPI, id string) error {
input := &autoscaling.TerminateInstanceInAutoScalingGroupInput{
InstanceId: aws.String(id),
ShouldDecrementDesiredCapacity: aws.Bool(false),
}
_, err := svc.TerminateInstanceInAutoScalingGroup(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case autoscaling.ErrCodeScalingActivityInProgressFault:
return fmt.Errorf("Could not terminate instance, autoscaling already in progress, will try next loop")
case autoscaling.ErrCodeResourceContentionFault:
return fmt.Errorf("Could not terminate instance, instance in contention, will try next loop")
default:
return fmt.Errorf("Unknown aws error when terminating old instance: %v", aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
return fmt.Errorf("Unknown non-aws error when terminating old instance: %v", err.Error())
}
}
return nil
}
func awsGetServices() (ec2iface.EC2API, autoscalingiface.AutoScalingAPI, error) {
sess, err := session.NewSession()
if err != nil {
return nil, nil, err
}
asgSvc := autoscaling.New(sess)
ec2svc := ec2.New(sess)
return ec2svc, asgSvc, nil
}