github.com/daniellockard/packer@v0.7.6-0.20141210173435-5a9390934716/builder/openstack/server.go (about)

     1  package openstack
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/mitchellh/multistep"
     7  	"github.com/racker/perigee"
     8  	"log"
     9  	"time"
    10  
    11  	"github.com/mitchellh/gophercloud-fork-40444fb"
    12  )
    13  
    14  // StateRefreshFunc is a function type used for StateChangeConf that is
    15  // responsible for refreshing the item being watched for a state change.
    16  //
    17  // It returns three results. `result` is any object that will be returned
    18  // as the final object after waiting for state change. This allows you to
    19  // return the final updated object, for example an openstack instance after
    20  // refreshing it.
    21  //
    22  // `state` is the latest state of that object. And `err` is any error that
    23  // may have happened while refreshing the state.
    24  type StateRefreshFunc func() (result interface{}, state string, progress int, err error)
    25  
    26  // StateChangeConf is the configuration struct used for `WaitForState`.
    27  type StateChangeConf struct {
    28  	Pending   []string
    29  	Refresh   StateRefreshFunc
    30  	StepState multistep.StateBag
    31  	Target    string
    32  }
    33  
    34  // ServerStateRefreshFunc returns a StateRefreshFunc that is used to watch
    35  // an openstack server.
    36  func ServerStateRefreshFunc(csp gophercloud.CloudServersProvider, s *gophercloud.Server) StateRefreshFunc {
    37  	return func() (interface{}, string, int, error) {
    38  		resp, err := csp.ServerById(s.Id)
    39  		if err != nil {
    40  			urce, ok := err.(*perigee.UnexpectedResponseCodeError)
    41  			if ok && (urce.Actual == 404) {
    42  				log.Printf("404 on ServerStateRefresh, returning DELETED")
    43  
    44  				return nil, "DELETED", 0, nil
    45  			} else {
    46  				log.Printf("Error on ServerStateRefresh: %s", err)
    47  				return nil, "", 0, err
    48  			}
    49  		}
    50  		return resp, resp.Status, resp.Progress, nil
    51  	}
    52  }
    53  
    54  // WaitForState watches an object and waits for it to achieve a certain
    55  // state.
    56  func WaitForState(conf *StateChangeConf) (i interface{}, err error) {
    57  	log.Printf("Waiting for state to become: %s", conf.Target)
    58  
    59  	for {
    60  		var currentProgress int
    61  		var currentState string
    62  		i, currentState, currentProgress, err = conf.Refresh()
    63  		if err != nil {
    64  			return
    65  		}
    66  
    67  		if currentState == conf.Target {
    68  			return
    69  		}
    70  
    71  		if conf.StepState != nil {
    72  			if _, ok := conf.StepState.GetOk(multistep.StateCancelled); ok {
    73  				return nil, errors.New("interrupted")
    74  			}
    75  		}
    76  
    77  		found := false
    78  		for _, allowed := range conf.Pending {
    79  			if currentState == allowed {
    80  				found = true
    81  				break
    82  			}
    83  		}
    84  
    85  		if !found {
    86  			return nil, fmt.Errorf("unexpected state '%s', wanted target '%s'", currentState, conf.Target)
    87  		}
    88  
    89  		log.Printf("Waiting for state to become: %s currently %s (%d%%)", conf.Target, currentState, currentProgress)
    90  		time.Sleep(2 * time.Second)
    91  	}
    92  
    93  	return
    94  }