github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/builder/amazon/common/instance.go (about)

     1  package common
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/mitchellh/goamz/ec2"
     7  	"github.com/mitchellh/multistep"
     8  	"log"
     9  	"time"
    10  )
    11  
    12  // StateRefreshFunc is a function type used for StateChangeConf that is
    13  // responsible for refreshing the item being watched for a state change.
    14  //
    15  // It returns three results. `result` is any object that will be returned
    16  // as the final object after waiting for state change. This allows you to
    17  // return the final updated object, for example an EC2 instance after refreshing
    18  // it.
    19  //
    20  // `state` is the latest state of that object. And `err` is any error that
    21  // may have happened while refreshing the state.
    22  type StateRefreshFunc func() (result interface{}, state string, err error)
    23  
    24  // StateChangeConf is the configuration struct used for `WaitForState`.
    25  type StateChangeConf struct {
    26  	Conn      *ec2.EC2
    27  	Pending   []string
    28  	Refresh   StateRefreshFunc
    29  	StepState map[string]interface{}
    30  	Target    string
    31  }
    32  
    33  // InstanceStateRefreshFunc returns a StateRefreshFunc that is used to watch
    34  // an EC2 instance.
    35  func InstanceStateRefreshFunc(conn *ec2.EC2, i *ec2.Instance) StateRefreshFunc {
    36  	return func() (interface{}, string, error) {
    37  		resp, err := conn.Instances([]string{i.InstanceId}, ec2.NewFilter())
    38  		if err != nil {
    39  			log.Printf("Error on InstanceStateRefresh: %s", err)
    40  			return nil, "", err
    41  		}
    42  
    43  		i = &resp.Reservations[0].Instances[0]
    44  		return i, i.State.Name, nil
    45  	}
    46  }
    47  
    48  // WaitForState watches an object and waits for it to achieve a certain
    49  // state.
    50  func WaitForState(conf *StateChangeConf) (i interface{}, err error) {
    51  	log.Printf("Waiting for state to become: %s", conf.Target)
    52  
    53  	for {
    54  		var currentState string
    55  		i, currentState, err = conf.Refresh()
    56  		if err != nil {
    57  			return
    58  		}
    59  
    60  		if currentState == conf.Target {
    61  			return
    62  		}
    63  
    64  		if conf.StepState != nil {
    65  			if _, ok := conf.StepState[multistep.StateCancelled]; ok {
    66  				return nil, errors.New("interrupted")
    67  			}
    68  		}
    69  
    70  		found := false
    71  		for _, allowed := range conf.Pending {
    72  			if currentState == allowed {
    73  				found = true
    74  				break
    75  			}
    76  		}
    77  
    78  		if !found {
    79  			fmt.Errorf("unexpected state '%s', wanted target '%s'", currentState, conf.Target)
    80  			return
    81  		}
    82  
    83  		time.Sleep(2 * time.Second)
    84  	}
    85  
    86  	return
    87  }