From 0fdf9b09c91e1ae8df7e15afa4c94bd37af5f957 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 9 Jul 2013 14:38:34 -0700 Subject: [PATCH] builder/vmware: error if shutdown command failed --- CHANGELOG.md | 4 ++++ builder/vmware/step_shutdown.go | 20 +++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9850c56e1..9570c03ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ FEATURES: * VirtualBox and VMware can now have `floppy_files` specified to attach floppy disks when booting. This allows for unattended Windows installs. +IMPROVEMENTS: + +* vmware: error if shutdown command has non-zero exit status. + BUG FIXES: * core: UI messages are now properly prefixed with spaces again. diff --git a/builder/vmware/step_shutdown.go b/builder/vmware/step_shutdown.go index 38574238f..09a6c6205 100644 --- a/builder/vmware/step_shutdown.go +++ b/builder/vmware/step_shutdown.go @@ -1,6 +1,7 @@ package vmware import ( + "bytes" "errors" "fmt" "github.com/mitchellh/multistep" @@ -35,7 +36,13 @@ func (s *stepShutdown) Run(state map[string]interface{}) multistep.StepAction { if config.ShutdownCommand != "" { ui.Say("Gracefully halting virtual machine...") log.Printf("Executing shutdown command: %s", config.ShutdownCommand) - cmd := &packer.RemoteCmd{Command: config.ShutdownCommand} + + var stdout, stderr bytes.Buffer + cmd := &packer.RemoteCmd{ + Command: config.ShutdownCommand, + Stdout: &stdout, + Stderr: &stderr, + } if err := comm.Start(cmd); err != nil { err := fmt.Errorf("Failed to send shutdown command: %s", err) state["error"] = err @@ -46,6 +53,17 @@ func (s *stepShutdown) Run(state map[string]interface{}) multistep.StepAction { // 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()) + // Wait for the machine to actually shut down log.Printf("Waiting max %s for shutdown to complete", config.ShutdownTimeout) shutdownTimer := time.After(config.ShutdownTimeout)