forked from apporbit/libretto
/
wait.go
288 lines (241 loc) · 7.47 KB
/
wait.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
package aws
import (
"errors"
"fmt"
"math"
"time"
"github.com/aws/aws-sdk-go/service/ec2"
)
const (
// Timeout for VM operations viz. Halt, Start & Terminate in seconds
VmOpsTimeout = 900 // 15 mins
// Retry interval for VM operations in seconds
VmOpsInterval = 15
// Timeout for Volume operations viz. Create, Detach in seconds
VolTimeout = 600 // 10 mins
// Retry interval for Volume operations in seconds
VolInterval = 5
)
// ReadyError is an information error that tells you why an instance wasn't
// ready.
type ReadyError struct {
Err error
ImageID string
InstanceID string
InstanceType string
LaunchTime time.Time
PublicIPAddress string
State string
StateReason string
StateTransitionReason string
SubnetID string
VPCID string
}
// Error returns a summarized string version of ReadyError. More details about
// the failed instance can be accessed through the struct.
func (e ReadyError) Error() string {
return fmt.Sprintf(
"failed waiting for instance (%s) to be ready, reason was: %s",
e.InstanceID,
e.StateReason,
)
}
func newReadyError(out *ec2.DescribeInstancesOutput) ReadyError {
if len(out.Reservations) < 1 {
return ReadyError{Err: ErrNoInstance}
}
if len(out.Reservations[0].Instances) < 1 {
return ReadyError{Err: ErrNoInstance}
}
var rerr ReadyError
if v := out.Reservations[0].Instances[0].ImageId; v != nil {
rerr.ImageID = *v
}
if v := out.Reservations[0].Instances[0].InstanceId; v != nil {
rerr.InstanceID = *v
}
if v := out.Reservations[0].Instances[0].InstanceType; v != nil {
rerr.InstanceType = *v
}
if v := out.Reservations[0].Instances[0].LaunchTime; v != nil {
rerr.LaunchTime = *v
}
if v := out.Reservations[0].Instances[0].PublicIpAddress; v != nil {
rerr.PublicIPAddress = *v
}
if v := out.Reservations[0].Instances[0].State; v != nil {
if v.Name != nil {
rerr.State = *v.Name
}
}
if v := out.Reservations[0].Instances[0].StateReason; v != nil {
if v.Message != nil {
rerr.StateReason = *v.Message
}
}
if v := out.Reservations[0].Instances[0].StateTransitionReason; v != nil {
rerr.StateTransitionReason = *v
}
if v := out.Reservations[0].Instances[0].SubnetId; v != nil {
rerr.SubnetID = *v
}
if v := out.Reservations[0].Instances[0].VpcId; v != nil {
rerr.VPCID = *v
}
return rerr
}
func waitUntilReady(svc *ec2.EC2, instanceID string) error {
// With 10 retries, total timeout is about 17 minutes.
const maxRetries = 10
var resp *ec2.DescribeInstancesOutput
var err error
for ii := 0; ii < maxRetries; ii++ {
// Sleep will be 1, 2, 4, 8, 16...
// time.Sleep(2ⁱ * time.Second)
time.Sleep(time.Duration(math.Exp2(float64(ii))) * time.Second)
resp, err = svc.DescribeInstances(&ec2.DescribeInstancesInput{
InstanceIds: []*string{&instanceID},
})
if err != nil {
continue
}
if len(resp.Reservations) < 1 {
continue
}
if len(resp.Reservations[0].Instances) < 1 {
continue
}
if resp.Reservations[0].Instances[0].State == nil {
continue
}
if resp.Reservations[0].Instances[0].State.Name == nil {
continue
}
state := *resp.Reservations[0].Instances[0].State.Name
switch state {
case ec2.InstanceStateNameRunning:
// We're ready!
return nil
case ec2.InstanceStateNameTerminated, ec2.InstanceStateNameShuttingDown:
// Polling is useless. This instance isn't coming up. Return error.
return fmt.Errorf("Instance unexpectedly terminating " +
"or has terminated")
case ec2.InstanceStateNameStopping, ec2.InstanceStateNameStopped:
// Polling is useless. This instance isn't coming up. Return error.
return fmt.Errorf("Instance unexpectedly stopping " +
"or has stopped")
}
}
rerr := newReadyError(resp)
if err != nil {
rerr.Err = err
} else {
rerr.Err = errors.New("wait until instance ready timeout")
}
return rerr
}
// waitUntilStopped: waits until vm is stopped
func waitUntilStopped(svc *ec2.EC2, instanceID string) error {
matchBreakState := make(map[string]bool)
matchBreakState[ec2.InstanceStateNameStopped] = true
matchBreakState[ec2.InstanceStateNameTerminated] = false
matchBreakState[ec2.InstanceStateNameShuttingDown] = false
return waitUntilState(svc, instanceID, matchBreakState)
}
// waitUntilRunning: waits until vm is running
func waitUntilRunning(svc *ec2.EC2, instanceID string) error {
matchBreakState := make(map[string]bool)
matchBreakState[ec2.InstanceStateNameRunning] = true
matchBreakState[ec2.InstanceStateNameTerminated] = false
matchBreakState[ec2.InstanceStateNameShuttingDown] = false
return waitUntilState(svc, instanceID, matchBreakState)
}
// waitUntilTerminated: waits until vm is terminated
func waitUntilTerminated(svc *ec2.EC2, instanceID string) error {
matchBreakState := make(map[string]bool)
matchBreakState[ec2.InstanceStateNameTerminated] = true
return waitUntilState(svc, instanceID, matchBreakState)
}
// waitUntilState: waits until vm is in given state
func waitUntilState(svc *ec2.EC2, instanceID string,
matchBreakState map[string]bool) error {
var instanceStatus *InstanceStatus
var err error
matchState := ""
for state, val := range matchBreakState {
if val {
matchState = state
break
}
}
if matchState == "" {
err = fmt.Errorf("No valid match state given for wait")
panic(err)
}
const maxRetries = VmOpsTimeout / VmOpsInterval
for ii := 0; ii < maxRetries; ii++ {
// Sleep will be VmOpsInterval seconds
time.Sleep(time.Duration(VmOpsInterval) * time.Second)
instanceStatus, err = GetInstanceStatus(svc, instanceID)
if err != nil {
continue
}
state := instanceStatus.State
if val, ok := matchBreakState[state]; ok {
if val {
// true val means its state to be matched
return nil
}
// false val means an unexpected state has reached
// Polling is useless. So return appropriate error
return fmt.Errorf("Instance unexpectedly %s", state)
}
}
return fmt.Errorf("Timeout while waiting for VM to be %s", matchState)
}
// waitForCreate: waits for volume to get created
func waitForCreate(svc *ec2.EC2, volumeID string) error {
return waitUntilVolumeState(svc, volumeID, ec2.VolumeStateAvailable)
}
// waitForAttach: waits for volume to attach to a instance
func waitForAttach(svc *ec2.EC2, volumeID string) error {
return waitUntilVolumeState(svc, volumeID, ec2.VolumeStateInUse)
}
// waitForDetach: waits for volume to detach from a instance
func waitForDetach(svc *ec2.EC2, volumeID string) error {
return waitUntilVolumeState(svc, volumeID, ec2.VolumeStateAvailable)
}
// waitUntiVolumeState: waits for volume to get to state given as argument
func waitUntilVolumeState(svc *ec2.EC2, volumeID string, stateEnum string) error {
var resp *ec2.DescribeVolumesOutput
var err error
const maxRetries = VolTimeout / VolInterval
for ii := 0; ii < maxRetries; ii++ {
// Sleep will be VolInterval seconds
time.Sleep(time.Duration(VolInterval) * time.Second)
resp, err = svc.DescribeVolumes(&ec2.DescribeVolumesInput{
VolumeIds: []*string{&volumeID},
})
if err != nil {
continue
}
if len(resp.Volumes) < 1 {
continue
}
if resp.Volumes[0].State == nil {
continue
}
state := *resp.Volumes[0].State
if state == ec2.VolumeStateError {
return fmt.Errorf("Volume in error state")
}
if state == stateEnum {
// volume reached required state
return nil
}
}
if err != nil {
return fmt.Errorf("Error in getting volume state: %v", err)
}
return fmt.Errorf("Timeout while waiting for Volume to be %s", stateEnum)
}