forked from deitch/aws-asg-roller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.go
156 lines (147 loc) · 5.33 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
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"
)
func setAsgDesired(svc autoscalingiface.AutoScalingAPI, asg *autoscaling.Group, count int64) error {
// increase the desired capacity by 1
desiredInput := &autoscaling.SetDesiredCapacityInput{
AutoScalingGroupName: asg.AutoScalingGroupName,
DesiredCapacity: aws.Int64(count),
HonorCooldown: aws.Bool(true),
}
_, err := svc.SetDesiredCapacity(desiredInput)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case autoscaling.ErrCodeScalingActivityInProgressFault:
return fmt.Errorf("%s %v", autoscaling.ErrCodeScalingActivityInProgressFault, aerr.Error())
case autoscaling.ErrCodeResourceContentionFault:
return fmt.Errorf("%s %v", autoscaling.ErrCodeResourceContentionFault, aerr.Error())
default:
return fmt.Errorf("Unexpected and unknown AWS error: %v", aerr.Error())
}
} else {
return fmt.Errorf("Unexpected and unknown non-AWS error: %v", err.Error())
}
}
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) {
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
}