2013-08-27 00:57:23 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
2014-10-27 19:40:49 -04:00
|
|
|
|
2016-11-27 19:59:26 -05:00
|
|
|
"github.com/gophercloud/gophercloud"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2013-08-27 00:57:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// StateRefreshFunc is a function type used for StateChangeConf that is
|
|
|
|
// responsible for refreshing the item being watched for a state change.
|
|
|
|
//
|
|
|
|
// It returns three results. `result` is any object that will be returned
|
|
|
|
// as the final object after waiting for state change. This allows you to
|
|
|
|
// return the final updated object, for example an openstack instance after
|
|
|
|
// refreshing it.
|
|
|
|
//
|
|
|
|
// `state` is the latest state of that object. And `err` is any error that
|
|
|
|
// may have happened while refreshing the state.
|
|
|
|
type StateRefreshFunc func() (result interface{}, state string, progress int, err error)
|
|
|
|
|
|
|
|
// StateChangeConf is the configuration struct used for `WaitForState`.
|
|
|
|
type StateChangeConf struct {
|
|
|
|
Pending []string
|
|
|
|
Refresh StateRefreshFunc
|
2013-08-31 15:37:07 -04:00
|
|
|
StepState multistep.StateBag
|
2015-06-12 22:50:59 -04:00
|
|
|
Target []string
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServerStateRefreshFunc returns a StateRefreshFunc that is used to watch
|
2014-06-30 00:50:01 -04:00
|
|
|
// an openstack server.
|
2015-06-12 00:16:43 -04:00
|
|
|
func ServerStateRefreshFunc(
|
|
|
|
client *gophercloud.ServiceClient, s *servers.Server) StateRefreshFunc {
|
2013-08-27 00:57:23 -04:00
|
|
|
return func() (interface{}, string, int, error) {
|
2015-06-12 00:16:43 -04:00
|
|
|
serverNew, err := servers.Get(client, s.ID).Extract()
|
2013-08-27 00:57:23 -04:00
|
|
|
if err != nil {
|
2016-12-13 17:20:39 -05:00
|
|
|
if _, ok := err.(gophercloud.ErrDefault404); ok {
|
2015-06-12 00:16:43 -04:00
|
|
|
log.Printf("[INFO] 404 on ServerStateRefresh, returning DELETED")
|
2014-06-30 20:51:48 -04:00
|
|
|
return nil, "DELETED", 0, nil
|
2014-06-30 00:45:50 -04:00
|
|
|
}
|
2016-12-13 17:20:39 -05:00
|
|
|
log.Printf("[ERROR] Error on ServerStateRefresh: %s", err)
|
|
|
|
return nil, "", 0, err
|
2014-06-30 00:45:50 -04:00
|
|
|
}
|
2015-06-12 00:16:43 -04:00
|
|
|
|
|
|
|
return serverNew, serverNew.Status, serverNew.Progress, nil
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForState watches an object and waits for it to achieve a certain
|
|
|
|
// state.
|
|
|
|
func WaitForState(conf *StateChangeConf) (i interface{}, err error) {
|
|
|
|
log.Printf("Waiting for state to become: %s", conf.Target)
|
|
|
|
|
|
|
|
for {
|
|
|
|
var currentProgress int
|
|
|
|
var currentState string
|
|
|
|
i, currentState, currentProgress, err = conf.Refresh()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-12 22:50:59 -04:00
|
|
|
for _, t := range conf.Target {
|
|
|
|
if currentState == t {
|
|
|
|
return
|
|
|
|
}
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if conf.StepState != nil {
|
2013-08-31 15:37:07 -04:00
|
|
|
if _, ok := conf.StepState.GetOk(multistep.StateCancelled); ok {
|
2013-08-27 00:57:23 -04:00
|
|
|
return nil, errors.New("interrupted")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
|
|
|
for _, allowed := range conf.Pending {
|
|
|
|
if currentState == allowed {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2014-04-08 14:26:09 -04:00
|
|
|
return nil, fmt.Errorf("unexpected state '%s', wanted target '%s'", currentState, conf.Target)
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Waiting for state to become: %s currently %s (%d%%)", conf.Target, currentState, currentProgress)
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
|
|
|
}
|