github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/builder/digitalocean/wait.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"errors"
     5  	"log"
     6  	"time"
     7  )
     8  
     9  // waitForState simply blocks until the droplet is in
    10  // a state we expect, while eventually timing out.
    11  func waitForDropletState(desiredState string, dropletId uint, client *DigitalOceanClient, c config) error {
    12  	active := make(chan bool, 1)
    13  
    14  	go func() {
    15  		attempts := 0
    16  		for {
    17  			attempts += 1
    18  
    19  			log.Printf("Checking droplet status... (attempt: %d)", attempts)
    20  			_, status, err := client.DropletStatus(dropletId)
    21  			if err != nil {
    22  				log.Println(err)
    23  				break
    24  			}
    25  
    26  			if status == desiredState {
    27  				break
    28  			}
    29  
    30  			// Wait 3 seconds in between
    31  			time.Sleep(3 * time.Second)
    32  		}
    33  
    34  		active <- true
    35  	}()
    36  
    37  	log.Printf("Waiting for up to %s for droplet to become %s", c.RawStateTimeout, desiredState)
    38  	timeout := time.After(c.stateTimeout)
    39  
    40  ActiveWaitLoop:
    41  	for {
    42  		select {
    43  		case <-active:
    44  			// We connected. Just break the loop.
    45  			break ActiveWaitLoop
    46  		case <-timeout:
    47  			err := errors.New("Timeout while waiting to for droplet to become active")
    48  			return err
    49  		}
    50  	}
    51  
    52  	// If we got this far, there were no errors
    53  	return nil
    54  }