* I had to contextualise Communicator.Start and RemoteCmd.StartWithUi NOTE: Communicator.Start starts a RemoteCmd but RemoteCmd.StartWithUi will run the cmd and wait for a return, so I renamed StartWithUi to RunWithUi so that the intent is clearer. Ideally in the future RunWithUi will be named back to StartWithUi and the exit status or wait funcs of the command will allow to wait for a return. If you do so please read carrefully https://golang.org/pkg/os/exec/#Cmd.Stdout to avoid a deadlock * cmd.ExitStatus to cmd.ExitStatus() is now blocking to avoid race conditions * also had to simplify StartWithUi
88 lines
2.1 KiB
Go
88 lines
2.1 KiB
Go
package common
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
"github.com/hashicorp/packer/packer"
|
|
)
|
|
|
|
// 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
|
|
// driver Driver
|
|
// ui packer.Ui
|
|
// vmName string
|
|
//
|
|
// Produces:
|
|
// <nothing>
|
|
type StepShutdown struct {
|
|
Command string
|
|
Timeout time.Duration
|
|
Delay time.Duration
|
|
}
|
|
|
|
func (s *StepShutdown) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
comm := state.Get("communicator").(packer.Communicator)
|
|
driver := state.Get("driver").(Driver)
|
|
ui := state.Get("ui").(packer.Ui)
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
if s.Command != "" {
|
|
ui.Say("Gracefully halting virtual machine...")
|
|
log.Printf("Executing shutdown command: %s", s.Command)
|
|
cmd := &packer.RemoteCmd{Command: s.Command}
|
|
if err := cmd.RunWithUi(ctx, 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
|
|
}
|
|
|
|
// Wait for the machine to actually shut down
|
|
log.Printf("Waiting max %s for shutdown to complete", s.Timeout)
|
|
shutdownTimer := time.After(s.Timeout)
|
|
for {
|
|
running, _ := driver.IsRunning(vmName)
|
|
if !running {
|
|
|
|
if s.Delay.Nanoseconds() > 0 {
|
|
log.Printf("Delay for %s after shutdown to allow locks to clear...", s.Delay)
|
|
time.Sleep(s.Delay)
|
|
}
|
|
|
|
break
|
|
}
|
|
|
|
select {
|
|
case <-shutdownTimer:
|
|
err := errors.New("Timeout while waiting for machine to shut down.")
|
|
state.Put("error", err)
|
|
ui.Error(err.Error())
|
|
return multistep.ActionHalt
|
|
default:
|
|
time.Sleep(500 * time.Millisecond)
|
|
}
|
|
}
|
|
} else {
|
|
ui.Say("Halting the virtual machine...")
|
|
if err := driver.Stop(vmName); err != nil {
|
|
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) {}
|