2014-04-06 13:21:22 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2017-01-20 04:44:18 -05:00
|
|
|
"bytes"
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2014-04-06 13:21:22 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
2016-12-11 14:13:37 -05:00
|
|
|
|
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"
|
2014-04-06 13:21:22 -04:00
|
|
|
)
|
|
|
|
|
2016-12-11 17:37:41 -05:00
|
|
|
// StepShutdown is a step that shuts down the machine. It first attempts to do
|
|
|
|
// so gracefully, but ultimately forcefully shuts it down if that fails.
|
2014-04-06 13:21:22 -04:00
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// communicator packer.Communicator
|
|
|
|
// driver Driver
|
|
|
|
// ui packer.Ui
|
|
|
|
// vmName string
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
|
|
|
type StepShutdown struct {
|
|
|
|
Command string
|
|
|
|
Timeout time.Duration
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// Run shuts down the VM.
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepShutdown) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2014-04-06 13:21:22 -04:00
|
|
|
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)
|
2017-01-20 04:44:18 -05:00
|
|
|
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Command: s.Command,
|
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
|
|
|
}
|
|
|
|
if err := comm.Start(cmd); err != nil {
|
|
|
|
err := fmt.Errorf("Failed to send shutdown command: %s", err)
|
2014-04-06 13:21:22 -04:00
|
|
|
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 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-shutdownTimer:
|
2017-01-20 04:44:18 -05:00
|
|
|
log.Printf("Shutdown stdout: %s", stdout.String())
|
|
|
|
log.Printf("Shutdown stderr: %s", stderr.String())
|
2014-04-06 13:21:22 -04:00
|
|
|
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 {
|
2016-12-11 16:12:55 -05:00
|
|
|
err = fmt.Errorf("Error stopping VM: %s", err)
|
2014-04-06 13:21:22 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("VM shut down.")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// Cleanup does nothing.
|
2014-04-06 13:21:22 -04:00
|
|
|
func (s *StepShutdown) Cleanup(state multistep.StateBag) {}
|