2013-06-13 13:56:34 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2017-04-08 15:52:57 -04:00
|
|
|
"context"
|
2013-09-05 00:20:41 -04:00
|
|
|
"fmt"
|
2013-06-13 13:56:34 -04:00
|
|
|
"log"
|
|
|
|
"time"
|
2015-06-10 17:02:06 -04:00
|
|
|
|
|
|
|
"github.com/digitalocean/godo"
|
2013-06-13 13:56:34 -04:00
|
|
|
)
|
|
|
|
|
2015-06-10 22:53:07 -04:00
|
|
|
// waitForDropletUnlocked waits for the Droplet to be unlocked to
|
|
|
|
// avoid "pending" errors when making state changes.
|
|
|
|
func waitForDropletUnlocked(
|
|
|
|
client *godo.Client, dropletId int, 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("[DEBUG] Checking droplet lock state... (attempt: %d)", attempts)
|
2017-04-09 14:33:05 -04:00
|
|
|
droplet, _, err := client.Droplets.Get(context.TODO(), dropletId)
|
2015-06-10 22:53:07 -04:00
|
|
|
if err != nil {
|
|
|
|
result <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !droplet.Locked {
|
|
|
|
result <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait 3 seconds in between
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
|
|
|
// Verify we shouldn't exit
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
// We finished, so just exit the goroutine
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// Keep going
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Waiting for up to %d seconds for droplet to unlock", timeout/time.Second)
|
|
|
|
select {
|
|
|
|
case err := <-result:
|
|
|
|
return err
|
|
|
|
case <-time.After(timeout):
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Timeout while waiting to for droplet to unlock")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 04:32:43 -04:00
|
|
|
// waitForDropletState simply blocks until the droplet is in
|
2013-06-13 13:56:34 -04:00
|
|
|
// a state we expect, while eventually timing out.
|
2015-06-10 17:02:06 -04:00
|
|
|
func waitForDropletState(
|
|
|
|
desiredState string, dropletId int,
|
|
|
|
client *godo.Client, timeout time.Duration) error {
|
2013-09-05 01:21:59 -04:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
|
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)
|
2017-04-09 14:33:05 -04:00
|
|
|
droplet, _, err := client.Droplets.Get(context.TODO(), dropletId)
|
2013-06-13 13:56:34 -04:00
|
|
|
if err != nil {
|
2013-09-05 00:20:41 -04:00
|
|
|
result <- err
|
|
|
|
return
|
2013-06-13 13:56:34 -04:00
|
|
|
}
|
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
if droplet.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-09-05 01:21:59 -04:00
|
|
|
|
|
|
|
// Verify we shouldn't exit
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
// We finished, so just exit the goroutine
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// Keep going
|
|
|
|
}
|
2013-06-13 13:56:34 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-10-05 22:29:02 -04:00
|
|
|
log.Printf("Waiting for up to %d seconds for droplet to become %s", timeout/time.Second, 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
|
|
|
}
|
|
|
|
}
|
2016-09-08 04:32:43 -04:00
|
|
|
|
|
|
|
// waitForActionState simply blocks until the droplet action is in
|
|
|
|
// a state we expect, while eventually timing out.
|
|
|
|
func waitForActionState(
|
|
|
|
desiredState string, dropletId, actionId int,
|
|
|
|
client *godo.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 action status... (attempt: %d)", attempts)
|
2017-04-09 14:33:05 -04:00
|
|
|
action, _, err := client.DropletActions.Get(context.TODO(), dropletId, actionId)
|
2016-09-08 04:32:43 -04:00
|
|
|
if err != nil {
|
|
|
|
result <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if action.Status == desiredState {
|
|
|
|
result <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait 3 seconds in between
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
|
|
|
// Verify we shouldn't exit
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
// We finished, so just exit the goroutine
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// Keep going
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Printf("Waiting for up to %d seconds for action to become %s", timeout/time.Second, desiredState)
|
|
|
|
select {
|
|
|
|
case err := <-result:
|
|
|
|
return err
|
|
|
|
case <-time.After(timeout):
|
|
|
|
err := fmt.Errorf("Timeout while waiting to for action to become '%s'", desiredState)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|