2013-06-13 13:56:34 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2013-09-05 00:20:41 -04:00
|
|
|
"fmt"
|
2013-06-13 13:56:34 -04:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// waitForState simply blocks until the droplet is in
|
|
|
|
// a state we expect, while eventually timing out.
|
2013-09-05 00:59:58 -04:00
|
|
|
func waitForDropletState(desiredState string, dropletId uint, client *DigitalOceanClient, timeout time.Duration) error {
|
2013-09-05 00:20:41 -04:00
|
|
|
result := make(chan error, 1)
|
2013-06-13 13:56:34 -04:00
|
|
|
go func() {
|
|
|
|
attempts := 0
|
|
|
|
for {
|
|
|
|
attempts += 1
|
|
|
|
|
|
|
|
log.Printf("Checking droplet status... (attempt: %d)", attempts)
|
|
|
|
_, status, err := client.DropletStatus(dropletId)
|
|
|
|
if err != nil {
|
2013-09-05 00:20:41 -04:00
|
|
|
result <- err
|
|
|
|
return
|
2013-06-13 13:56:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if status == desiredState {
|
2013-09-05 00:20:41 -04:00
|
|
|
result <- nil
|
|
|
|
return
|
2013-06-13 13:56:34 -04:00
|
|
|
}
|
|
|
|
|
2013-06-14 09:26:03 -04:00
|
|
|
// Wait 3 seconds in between
|
|
|
|
time.Sleep(3 * time.Second)
|
2013-06-13 13:56:34 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-09-05 00:59:58 -04:00
|
|
|
log.Printf("Waiting for up to %d seconds for droplet to become %s", timeout, desiredState)
|
2013-09-05 00:20:41 -04:00
|
|
|
select {
|
|
|
|
case err := <-result:
|
|
|
|
return err
|
2013-09-05 00:59:58 -04:00
|
|
|
case <-time.After(timeout):
|
2013-09-05 00:20:41 -04:00
|
|
|
err := fmt.Errorf("Timeout while waiting to for droplet to become '%s'", desiredState)
|
|
|
|
return err
|
2013-06-13 13:56:34 -04:00
|
|
|
}
|
|
|
|
}
|