2015-06-12 18:04:46 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-06-12 18:04:46 -04:00
|
|
|
"fmt"
|
2019-10-01 09:04:49 -04:00
|
|
|
"log"
|
2015-06-12 18:04:46 -04:00
|
|
|
|
2019-10-01 09:04:49 -04:00
|
|
|
"github.com/gophercloud/gophercloud"
|
2016-11-27 19:59:26 -05:00
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/startstop"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-12 18:04:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepStopServer struct{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepStopServer) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-06-12 18:04:46 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-09-18 10:05:49 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2015-06-12 18:04:46 -04:00
|
|
|
server := state.Get("server").(*servers.Server)
|
|
|
|
|
|
|
|
// We need the v2 compute client
|
|
|
|
client, err := config.computeV2Client()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error initializing compute client: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2015-12-17 14:12:52 -05:00
|
|
|
ui.Say(fmt.Sprintf("Stopping server: %s ...", server.ID))
|
2015-06-12 18:04:46 -04:00
|
|
|
if err := startstop.Stop(client, server.ID).ExtractErr(); err != nil {
|
2019-10-01 09:04:49 -04:00
|
|
|
if _, ok := err.(gophercloud.ErrDefault409); ok {
|
|
|
|
// The server might have already been shut down by Windows Sysprep
|
|
|
|
log.Printf("[WARN] 409 on stopping an already stopped server, continuing")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("Error stopping server: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-06-12 18:04:46 -04:00
|
|
|
}
|
|
|
|
|
2015-12-17 14:12:52 -05:00
|
|
|
ui.Message(fmt.Sprintf("Waiting for server to stop: %s ...", server.ID))
|
2015-06-12 18:04:46 -04:00
|
|
|
stateChange := StateChangeConf{
|
|
|
|
Pending: []string{"ACTIVE"},
|
2015-06-12 22:50:59 -04:00
|
|
|
Target: []string{"SHUTOFF", "STOPPED"},
|
2015-06-12 18:04:46 -04:00
|
|
|
Refresh: ServerStateRefreshFunc(client, server),
|
|
|
|
StepState: state,
|
|
|
|
}
|
|
|
|
if _, err := WaitForState(&stateChange); err != nil {
|
|
|
|
err := fmt.Errorf("Error waiting for server (%s) to stop: %s", server.ID, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepStopServer) Cleanup(state multistep.StateBag) {}
|