github.phpd.cn/hashicorp/packer@v1.3.2/builder/openstack/server.go (about)

     1  package openstack
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"log"
     7  	"time"
     8  
     9  	"github.com/gophercloud/gophercloud"
    10  	"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
    11  	"github.com/hashicorp/packer/helper/multistep"
    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(
    37  	client *gophercloud.ServiceClient, s *servers.Server) StateRefreshFunc {
    38  	return func() (interface{}, string, int, error) {
    39  		serverNew, err := servers.Get(client, s.ID).Extract()
    40  		if err != nil {
    41  			if _, ok := err.(gophercloud.ErrDefault404); ok {
    42  				log.Printf("[INFO] 404 on ServerStateRefresh, returning DELETED")
    43  				return nil, "DELETED", 0, nil
    44  			}
    45  			log.Printf("[ERROR] Error on ServerStateRefresh: %s", err)
    46  			return nil, "", 0, err
    47  		}
    48  
    49  		return serverNew, serverNew.Status, serverNew.Progress, nil
    50  	}
    51  }
    52  
    53  // WaitForState watches an object and waits for it to achieve a certain
    54  // state.
    55  func WaitForState(conf *StateChangeConf) (i interface{}, err error) {
    56  	log.Printf("Waiting for state to become: %s", conf.Target)
    57  
    58  	for {
    59  		var currentProgress int
    60  		var currentState string
    61  		i, currentState, currentProgress, err = conf.Refresh()
    62  		if err != nil {
    63  			return
    64  		}
    65  
    66  		for _, t := range conf.Target {
    67  			if currentState == t {
    68  				return
    69  			}
    70  		}
    71  
    72  		if conf.StepState != nil {
    73  			if _, ok := conf.StepState.GetOk(multistep.StateCancelled); ok {
    74  				return nil, errors.New("interrupted")
    75  			}
    76  		}
    77  
    78  		found := false
    79  		for _, allowed := range conf.Pending {
    80  			if currentState == allowed {
    81  				found = true
    82  				break
    83  			}
    84  		}
    85  
    86  		if !found {
    87  			return nil, fmt.Errorf("unexpected state '%s', wanted target '%s'", currentState, conf.Target)
    88  		}
    89  
    90  		log.Printf("Waiting for state to become: %s currently %s (%d%%)", conf.Target, currentState, currentProgress)
    91  		time.Sleep(2 * time.Second)
    92  	}
    93  }