/
wait.go
133 lines (114 loc) · 3.23 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
package aliyun
import (
"time"
"log"
"errors"
"fmt"
"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/ecs"
)
// Simply blocks until the snapshot progress is 100%,
// while eventually timing out.
func waitForSnapshotAccomplished(snapshotId string, regionId common.Region,
client *ecs.Client, timeout time.Duration) error {
done := make(chan struct{})
defer close(done)
result := make(chan error, 1)
go func() {
attempts := 0
for {
attempts += 1
log.Printf("Checking snapshot progress... (attempt: %d)", attempts)
snapshotIds := []string{snapshotId}
snapshots, _, err := client.DescribeSnapshots(&ecs.DescribeSnapshotsArgs{
RegionId: regionId,
SnapshotIds: snapshotIds,
})
if (err != nil) {
result <- err
return
}
if snapshots == nil || len(snapshots) != 1 {
result <- errors.New("fail to get snapshot progress")
return
}
// TODO check snapshot status?
progress := snapshots[0].Progress
log.Printf("Current snapshot progress: '%s'", progress)
if progress == "100%" {
result <- nil
return
}
// Wait 10 seconds in between
time.Sleep(10 * time.Second)
// Verify we shouldn't exist
select {
case <- done:
// We finished, so just exit the goroutine
return
default:
// Keep going
}
}
}()
log.Printf("Waiting for up to %d seconds for snapshot to become accomplished", timeout/time.Second)
select {
case err := <- result:
return err
case <-time.After(timeout):
err := fmt.Errorf("Timeout while waiting for snapshot to become accomplished")
return err
}
}
// waitForStatus simply blocks until the instance is in
// a state we expect, while eventually timing out.
func waitForInstanceStatus(desiredStatus ecs.InstanceStatus, instanceId string, regionId common.Region,
client *ecs.Client, timeout time.Duration) error {
done := make(chan struct{})
defer close(done)
result := make(chan error, 1)
go func() {
attempts := 0
for {
attempts += 1
log.Printf("Checking instance status... (attempt: %d)", attempts)
instanceIds := fmt.Sprintf("[\"%s\"]", instanceId)
instances, _, err := client.DescribeInstances(&ecs.DescribeInstancesArgs{
RegionId: regionId,
InstanceIds: instanceIds,
})
if (err != nil) {
result <- err
return
}
if instances == nil || len(instances) != 1 {
result <- errors.New("fail to get instance status")
return
}
status := instances[0].Status
log.Printf("Current instance status: '%s'", status)
if status == desiredStatus {
result <- nil
return
}
// Wait 3 seconds in between
time.Sleep(3 * time.Second)
// Verify we shouldn't exist
select {
case <- done:
// We finished, so just exit the goroutine
return
default:
// Keep going
}
}
}()
log.Printf("Waiting for up to %d seconds for instance to become %s", timeout/time.Second, desiredStatus)
select {
case err := <- result:
return err
case <-time.After(timeout):
err := fmt.Errorf("Timeout while waiting for instance to become '%s'", desiredStatus)
return err
}
}