2013-06-13 13:56:34 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// waitForState simply blocks until the droplet is in
|
|
|
|
// a state we expect, while eventually timing out.
|
2013-06-23 06:51:51 -04:00
|
|
|
func waitForDropletState(desiredState string, dropletId uint, client *DigitalOceanClient, c config) error {
|
2013-06-13 13:56:34 -04:00
|
|
|
active := make(chan bool, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
attempts := 0
|
|
|
|
for {
|
|
|
|
attempts += 1
|
|
|
|
|
|
|
|
log.Printf("Checking droplet status... (attempt: %d)", attempts)
|
|
|
|
_, status, err := client.DropletStatus(dropletId)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if status == desiredState {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
active <- true
|
|
|
|
}()
|
|
|
|
|
2013-06-23 06:51:51 -04:00
|
|
|
log.Printf("Waiting for up to %s for droplet to become %s", c.RawStateTimeout, desiredState)
|
2013-07-14 08:20:29 -04:00
|
|
|
timeout := time.After(c.stateTimeout)
|
2013-06-13 13:56:34 -04:00
|
|
|
|
|
|
|
ActiveWaitLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-active:
|
|
|
|
// We connected. Just break the loop.
|
|
|
|
break ActiveWaitLoop
|
|
|
|
case <-timeout:
|
|
|
|
err := errors.New("Timeout while waiting to for droplet to become active")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far, there were no errors
|
|
|
|
return nil
|
|
|
|
}
|