2013-09-02 23:23:52 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-09-02 23:23:52 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
2016-07-06 11:52:40 -04:00
|
|
|
|
2020-12-01 18:30:31 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/communicator"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
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:
|
2020-11-19 18:10:00 -05:00
|
|
|
// communicator packersdk.Communicator
|
2013-09-02 23:23:52 -04:00
|
|
|
// config *config
|
|
|
|
// driver Driver
|
2020-11-19 14:54:31 -05:00
|
|
|
// ui packersdk.Ui
|
2013-09-02 23:23:52 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
2020-10-28 06:14:03 -04:00
|
|
|
type stepShutdown struct {
|
|
|
|
ShutdownCommand string
|
|
|
|
ShutdownTimeout time.Duration
|
|
|
|
Comm *communicator.Config
|
|
|
|
}
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepShutdown) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-09-02 23:23:52 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2020-10-28 06:14:03 -04:00
|
|
|
if s.Comm.Type == "none" {
|
2016-07-06 11:52:40 -04:00
|
|
|
cancelCh := make(chan struct{}, 1)
|
|
|
|
go func() {
|
|
|
|
defer close(cancelCh)
|
2020-10-28 06:14:03 -04:00
|
|
|
<-time.After(s.ShutdownTimeout)
|
2016-07-06 11:52:40 -04:00
|
|
|
}()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 06:14:03 -04:00
|
|
|
if s.ShutdownCommand != "" {
|
2020-11-19 18:10:00 -05:00
|
|
|
comm := state.Get("communicator").(packersdk.Communicator)
|
2013-09-02 23:23:52 -04:00
|
|
|
ui.Say("Gracefully halting virtual machine...")
|
2020-10-28 06:14:03 -04:00
|
|
|
log.Printf("Executing shutdown command: %s", s.ShutdownCommand)
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd := &packersdk.RemoteCmd{Command: s.ShutdownCommand}
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
|
2013-09-02 23:23:52 -04:00
|
|
|
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)
|
2020-10-28 06:14:03 -04:00
|
|
|
<-time.After(s.ShutdownTimeout)
|
2013-11-06 00:40:49 -05:00
|
|
|
}()
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2020-10-28 06:14:03 -04:00
|
|
|
log.Printf("Waiting max %s for shutdown to complete", s.ShutdownTimeout)
|
2013-11-06 00:40:49 -05:00
|
|
|
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) {}
|