packer-cn/builder/vmware/step_shutdown.go

136 lines
3.4 KiB
Go
Raw Normal View History

2013-06-06 19:30:37 -04:00
package vmware
import (
"bytes"
2013-06-20 00:20:48 -04:00
"errors"
2013-06-06 19:30:37 -04:00
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
"path/filepath"
"runtime"
"strings"
2013-06-06 19:30:37 -04:00
"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
// config *config
// driver Driver
// ui packer.Ui
// vmx_path string
//
// Produces:
// <nothing>
type stepShutdown struct{}
func (s *stepShutdown) Run(state map[string]interface{}) multistep.StepAction {
comm := state["communicator"].(packer.Communicator)
config := state["config"].(*config)
driver := state["driver"].(Driver)
ui := state["ui"].(packer.Ui)
vmxPath := state["vmx_path"].(string)
if config.ShutdownCommand != "" {
ui.Say("Gracefully halting virtual machine...")
log.Printf("Executing shutdown command: %s", config.ShutdownCommand)
var stdout, stderr bytes.Buffer
cmd := &packer.RemoteCmd{
Command: config.ShutdownCommand,
2013-07-11 18:01:30 -04:00
Stdout: &stdout,
Stderr: &stderr,
}
2013-06-06 19:30:37 -04:00
if err := comm.Start(cmd); err != nil {
2013-06-20 00:20:48 -04:00
err := fmt.Errorf("Failed to send shutdown command: %s", err)
state["error"] = err
ui.Error(err.Error())
2013-06-06 19:30:37 -04:00
return multistep.ActionHalt
}
// Wait for the command to run
cmd.Wait()
// If the command failed to run, notify the user in some way.
if cmd.ExitStatus != 0 {
state["error"] = fmt.Errorf(
"Shutdown command has non-zero exit status.\n\nStdout: %s\n\nStderr: %s",
stdout.String(), stderr.String())
return multistep.ActionHalt
}
log.Printf("Shutdown stdout: %s", stdout.String())
log.Printf("Shutdown stderr: %s", stderr.String())
2013-06-06 19:30:37 -04:00
// Wait for the machine to actually shut down
log.Printf("Waiting max %s for shutdown to complete", config.shutdownTimeout)
shutdownTimer := time.After(config.shutdownTimeout)
2013-06-06 19:30:37 -04:00
for {
running, _ := driver.IsRunning(vmxPath)
if !running {
break
}
select {
case <-shutdownTimer:
2013-06-20 00:20:48 -04:00
err := errors.New("Timeout while waiting for machine to shut down.")
state["error"] = err
ui.Error(err.Error())
2013-06-06 19:30:37 -04:00
return multistep.ActionHalt
default:
time.Sleep(1 * time.Second)
}
}
} else {
if err := driver.Stop(vmxPath); err != nil {
2013-06-20 00:20:48 -04:00
err := fmt.Errorf("Error stopping VM: %s", err)
state["error"] = err
ui.Error(err.Error())
2013-06-06 19:30:37 -04:00
return multistep.ActionHalt
}
}
ui.Message("Waiting for VMware to clean up after itself...")
lockPattern := filepath.Join(config.OutputDir, "*.lck")
timer := time.After(15 * time.Second)
LockWaitLoop:
for {
locks, err := filepath.Glob(lockPattern)
if err == nil {
if len(locks) == 0 {
log.Println("No more lock files found. VMware is clean.")
break
}
if len(locks) == 1 && strings.HasSuffix(locks[0], ".vmx.lck") {
log.Println("Only waiting on VMX lock. VMware is clean.")
break
}
log.Printf("Waiting on lock files: %#v", locks)
}
select {
case <-timer:
log.Println("Reached timeout on waiting for clean VMware. Assuming clean.")
break LockWaitLoop
case <-time.After(1 * time.Second):
}
}
if runtime.GOOS == "windows" {
// Windows takes a while to yield control of the files when the
// process is exiting. We just sleep here. In the future, it'd be
// nice to find a better solution to this.
time.Sleep(5 * time.Second)
}
2013-06-06 19:30:37 -04:00
log.Println("VM shut down.")
return multistep.ActionContinue
}
func (s *stepShutdown) Cleanup(state map[string]interface{}) {}