forked from fairfaxmedia/flywheel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
210 lines (181 loc) · 4.55 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
204
205
206
207
208
209
210
package flywheel
import (
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
)
// Start all the resources managed by the flywheel.
func (fw *Flywheel) Start() error {
fw.lastStarted = time.Now()
log.Print("Startup beginning")
var err error
err = fw.startInstances()
if err == nil {
err = fw.unterminateAutoScaling()
}
if err == nil {
err = fw.startAutoScaling()
}
if err != nil {
log.Printf("Error starting: %v", err)
return err
}
fw.ready = false
fw.stopAt = time.Now().Add(fw.idleTimeout)
fw.status = STARTING
return nil
}
// Start EC2 instances
func (fw *Flywheel) startInstances() error {
if len(fw.config.Instances) == 0 {
return nil
}
log.Printf("Starting instances %v", fw.config.Instances)
_, err := fw.ec2.StartInstances(
&ec2.StartInstancesInput{
InstanceIds: fw.config.AwsInstances(),
},
)
return err
}
// UnterminateAutoScaling - Restore autoscaling group instances
func (fw *Flywheel) unterminateAutoScaling() error {
var err error
for groupName, size := range fw.config.AutoScaling.Terminate {
log.Printf("Restoring autoscaling group %s to max/min size of %d", groupName, size)
_, err = fw.autoscaling.UpdateAutoScalingGroup(
&autoscaling.UpdateAutoScalingGroupInput{
AutoScalingGroupName: &groupName,
MaxSize: &size,
MinSize: &size,
},
)
if err != nil {
return err
}
}
return nil
}
// Start EC2 instances in a suspended autoscale group
// @note The autoscale group isn't unsuspended here. It's done by the
// healthcheck once all the instances are healthy.
func (fw *Flywheel) startAutoScaling() error {
for _, groupName := range fw.config.AutoScaling.Stop {
log.Printf("Starting autoscaling group %s", groupName)
resp, err := fw.autoscaling.DescribeAutoScalingGroups(
&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{&groupName},
},
)
if err != nil {
return err
}
group := resp.AutoScalingGroups[0]
instanceIds := []*string{}
for _, instance := range group.Instances {
instanceIds = append(instanceIds, instance.InstanceId)
}
_, err = fw.ec2.StartInstances(
&ec2.StartInstancesInput{
InstanceIds: instanceIds,
},
)
if err != nil {
return err
}
}
return nil
}
// Stop all resources managed by the flywheel
func (fw *Flywheel) Stop() error {
fw.lastStopped = time.Now()
var err error
err = fw.stopInstances()
if err == nil {
err = fw.terminateAutoScaling()
}
if err == nil {
err = fw.stopAutoScaling()
}
if err != nil {
log.Printf("Error stopping: %v", err)
return err
}
fw.ready = false
fw.status = STOPPING
fw.stopAt = fw.lastStopped
return nil
}
// Stop EC2 instances
func (fw *Flywheel) stopInstances() error {
if len(fw.config.Instances) == 0 {
return nil
}
log.Printf("Stopping instances %v", fw.config.Instances)
_, err := fw.ec2.StopInstances(
&ec2.StopInstancesInput{
InstanceIds: fw.config.AwsInstances(),
},
)
return err
}
// Suspend ReplaceUnhealthy in an autoscale group and stop the instances.
func (fw *Flywheel) stopAutoScaling() error {
for _, groupName := range fw.config.AutoScaling.Stop {
log.Printf("Stopping autoscaling group %s", groupName)
resp, err := fw.autoscaling.DescribeAutoScalingGroups(
&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{&groupName},
},
)
if err != nil {
return err
}
group := resp.AutoScalingGroups[0]
_, err = fw.autoscaling.SuspendProcesses(
&autoscaling.ScalingProcessQuery{
AutoScalingGroupName: group.AutoScalingGroupName,
ScalingProcesses: []*string{
aws.String("ReplaceUnhealthy"),
},
},
)
if err != nil {
return err
}
instanceIds := []*string{}
for _, instance := range group.Instances {
instanceIds = append(instanceIds, instance.InstanceId)
}
_, err = fw.ec2.StopInstances(
&ec2.StopInstancesInput{
InstanceIds: instanceIds,
},
)
if err != nil {
return err
}
}
return nil
}
// Reduce autoscaling min/max instances to 0, causing the instances to be terminated.
func (fw *Flywheel) terminateAutoScaling() error {
var err error
var zero int64
for groupName := range fw.config.AutoScaling.Terminate {
log.Printf("Terminating autoscaling group %s", groupName)
_, err = fw.autoscaling.UpdateAutoScalingGroup(
&autoscaling.UpdateAutoScalingGroupInput{
AutoScalingGroupName: &groupName,
MaxSize: &zero,
MinSize: &zero,
},
)
if err != nil {
return err
}
}
return nil
}