2013-12-22 12:37:27 -05:00
|
|
|
package common
|
2013-06-12 21:02:42 -04:00
|
|
|
|
|
|
|
import (
|
2013-06-20 00:07:53 -04:00
|
|
|
"errors"
|
2013-06-12 21:02:42 -04:00
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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>
|
2013-12-22 12:37:27 -05:00
|
|
|
type StepShutdown struct {
|
|
|
|
Command string
|
|
|
|
Timeout time.Duration
|
|
|
|
}
|
2013-06-12 21:02:42 -04:00
|
|
|
|
2013-12-22 12:37:27 -05:00
|
|
|
func (s *StepShutdown) Run(state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:44:58 -04:00
|
|
|
comm := state.Get("communicator").(packer.Communicator)
|
2013-12-22 12:37:27 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-08-31 15:44:58 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
2013-06-12 21:02:42 -04:00
|
|
|
|
2013-12-22 12:37:27 -05:00
|
|
|
if s.Command != "" {
|
2013-06-12 21:02:42 -04:00
|
|
|
ui.Say("Gracefully halting virtual machine...")
|
2013-12-22 12:37:27 -05:00
|
|
|
log.Printf("Executing shutdown command: %s", s.Command)
|
|
|
|
cmd := &packer.RemoteCmd{Command: s.Command}
|
2013-07-23 23:42:39 -04:00
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
2013-06-20 00:07:53 -04:00
|
|
|
err := fmt.Errorf("Failed to send shutdown command: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:07:53 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-12 21:02:42 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the machine to actually shut down
|
2013-12-22 12:37:27 -05:00
|
|
|
log.Printf("Waiting max %s for shutdown to complete", s.Timeout)
|
|
|
|
shutdownTimer := time.After(s.Timeout)
|
2013-06-12 21:02:42 -04:00
|
|
|
for {
|
|
|
|
running, _ := driver.IsRunning(vmName)
|
|
|
|
if !running {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-shutdownTimer:
|
2013-06-20 00:07:53 -04:00
|
|
|
err := errors.New("Timeout while waiting for machine to shut down.")
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:07:53 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-12 21:02:42 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
default:
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-06-12 21:19:29 -04:00
|
|
|
ui.Say("Halting the virtual machine...")
|
2013-06-12 21:02:42 -04:00
|
|
|
if err := driver.Stop(vmName); err != nil {
|
2013-06-20 00:07:53 -04:00
|
|
|
err := fmt.Errorf("Error stopping VM: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:07:53 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-12 21:02:42 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("VM shut down.")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-22 12:37:27 -05:00
|
|
|
func (s *StepShutdown) Cleanup(state multistep.StateBag) {}
|