2013-09-02 23:23:52 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
2016-07-06 11:52:40 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-07-06 11:52:40 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-09-02 23:23:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step shuts down the machine. It first attempts to do so gracefully,
|
|
|
|
// but ultimately forcefully shuts it down if that fails.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// communicator packer.Communicator
|
|
|
|
// config *config
|
|
|
|
// driver Driver
|
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
|
|
|
type stepShutdown struct{}
|
|
|
|
|
|
|
|
func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction {
|
2015-05-27 16:39:43 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2013-09-02 23:23:52 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2016-07-06 11:52:40 -04:00
|
|
|
if state.Get("communicator") == nil {
|
|
|
|
cancelCh := make(chan struct{}, 1)
|
|
|
|
go func() {
|
|
|
|
defer close(cancelCh)
|
|
|
|
<-time.After(config.shutdownTimeout)
|
|
|
|
}()
|
|
|
|
ui.Say("Waiting for shutdown...")
|
|
|
|
if ok := driver.WaitForShutdown(cancelCh); ok {
|
|
|
|
log.Println("VM shut down.")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
} else {
|
|
|
|
err := fmt.Errorf("Failed to shutdown")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
comm := state.Get("communicator").(packer.Communicator)
|
2013-09-02 23:23:52 -04:00
|
|
|
if config.ShutdownCommand != "" {
|
|
|
|
ui.Say("Gracefully halting virtual machine...")
|
|
|
|
log.Printf("Executing shutdown command: %s", config.ShutdownCommand)
|
|
|
|
cmd := &packer.RemoteCmd{Command: config.ShutdownCommand}
|
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
err := fmt.Errorf("Failed to send shutdown command: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
// Start the goroutine that will time out our graceful attempt
|
|
|
|
cancelCh := make(chan struct{}, 1)
|
|
|
|
go func() {
|
|
|
|
defer close(cancelCh)
|
|
|
|
<-time.After(config.shutdownTimeout)
|
|
|
|
}()
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
log.Printf("Waiting max %s for shutdown to complete", config.shutdownTimeout)
|
|
|
|
if ok := driver.WaitForShutdown(cancelCh); !ok {
|
|
|
|
err := errors.New("Timeout while waiting for machine to shut down.")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ui.Say("Halting the virtual machine...")
|
2013-11-06 00:40:49 -05:00
|
|
|
if err := driver.Stop(); err != nil {
|
2013-09-02 23:23:52 -04:00
|
|
|
err := fmt.Errorf("Error stopping VM: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("VM shut down.")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepShutdown) Cleanup(state multistep.StateBag) {}
|