2013-12-25 01:33:49 -05:00
|
|
|
package common
|
2013-06-06 19:30:37 -04:00
|
|
|
|
|
|
|
import (
|
2013-07-09 17:38:34 -04:00
|
|
|
"bytes"
|
2013-06-20 00:20:48 -04:00
|
|
|
"errors"
|
2013-06-06 19:30:37 -04:00
|
|
|
"fmt"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-06-06 19:30:37 -04:00
|
|
|
"log"
|
2013-12-25 01:16:13 -05:00
|
|
|
"regexp"
|
2013-07-02 12:15:52 -04:00
|
|
|
"strings"
|
2013-06-06 19:30:37 -04:00
|
|
|
"time"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/mitchellh/multistep"
|
2013-06-06 19:30:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2013-12-25 01:33:49 -05:00
|
|
|
// dir OutputDir
|
2013-06-06 19:30:37 -04:00
|
|
|
// driver Driver
|
|
|
|
// ui packer.Ui
|
|
|
|
// vmx_path string
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
2013-12-25 01:33:49 -05:00
|
|
|
type StepShutdown struct {
|
|
|
|
Command string
|
|
|
|
Timeout time.Duration
|
2014-03-07 13:29:04 -05:00
|
|
|
|
|
|
|
// Set this to true if we're testing
|
|
|
|
Testing bool
|
2013-12-25 01:33:49 -05:00
|
|
|
}
|
2013-06-06 19:30:37 -04:00
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepShutdown) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:50:25 -04:00
|
|
|
comm := state.Get("communicator").(packer.Communicator)
|
2013-12-25 01:33:49 -05:00
|
|
|
dir := state.Get("dir").(OutputDir)
|
|
|
|
driver := state.Get("driver").(Driver)
|
2013-08-31 15:50:25 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmxPath := state.Get("vmx_path").(string)
|
2013-06-06 19:30:37 -04:00
|
|
|
|
2013-12-25 01:33:49 -05:00
|
|
|
if s.Command != "" {
|
2013-06-06 19:30:37 -04:00
|
|
|
ui.Say("Gracefully halting virtual machine...")
|
2013-12-25 01:33:49 -05:00
|
|
|
log.Printf("Executing shutdown command: %s", s.Command)
|
2013-07-09 17:38:34 -04:00
|
|
|
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmd := &packer.RemoteCmd{
|
2013-12-25 01:33:49 -05:00
|
|
|
Command: s.Command,
|
2013-07-11 18:01:30 -04:00
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
2013-07-09 17:38:34 -04:00
|
|
|
}
|
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)
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:20:48 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-06 19:30:37 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the machine to actually shut down
|
2013-12-25 01:33:49 -05:00
|
|
|
log.Printf("Waiting max %s for shutdown to complete", s.Timeout)
|
|
|
|
shutdownTimer := time.After(s.Timeout)
|
2013-06-06 19:30:37 -04:00
|
|
|
for {
|
|
|
|
running, _ := driver.IsRunning(vmxPath)
|
|
|
|
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())
|
2013-06-20 00:20:48 -04:00
|
|
|
err := errors.New("Timeout while waiting for machine to shut down.")
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:20:48 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-06 19:30:37 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
default:
|
2013-12-25 01:33:49 -05:00
|
|
|
time.Sleep(150 * time.Millisecond)
|
2013-06-06 19:30:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-12-26 17:34:53 -05:00
|
|
|
ui.Say("Forcibly halting virtual machine...")
|
2013-06-06 19:30:37 -04:00
|
|
|
if err := driver.Stop(vmxPath); err != nil {
|
2013-06-20 00:20:48 -04:00
|
|
|
err := fmt.Errorf("Error stopping VM: %s", err)
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:20:48 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-06 19:30:37 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-02 12:15:52 -04:00
|
|
|
ui.Message("Waiting for VMware to clean up after itself...")
|
2013-12-25 01:16:13 -05:00
|
|
|
lockRegex := regexp.MustCompile(`(?i)\.lck$`)
|
2014-06-19 19:07:40 -04:00
|
|
|
timer := time.After(120 * time.Second)
|
2013-07-02 12:15:52 -04:00
|
|
|
LockWaitLoop:
|
|
|
|
for {
|
2013-12-25 01:16:13 -05:00
|
|
|
files, err := dir.ListFiles()
|
2013-12-25 01:33:49 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error listing files in outputdir: %s", err)
|
|
|
|
} else {
|
2013-12-25 01:16:13 -05:00
|
|
|
var locks []string
|
|
|
|
for _, file := range files {
|
|
|
|
if lockRegex.MatchString(file) {
|
|
|
|
locks = append(locks, file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-02 12:15:52 -04:00
|
|
|
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
|
2013-12-25 01:33:49 -05:00
|
|
|
case <-time.After(150 * time.Millisecond):
|
2013-07-02 12:15:52 -04:00
|
|
|
}
|
|
|
|
}
|
2013-07-02 12:05:17 -04:00
|
|
|
|
2017-04-19 19:02:40 -04:00
|
|
|
if !s.Testing {
|
2013-07-31 18:35:47 -04:00
|
|
|
// Windows takes a while to yield control of the files when the
|
2017-04-19 19:02:40 -04:00
|
|
|
// process is exiting. Ubuntu and OS X will yield control of the files
|
|
|
|
// but VMWare may overwrite the VMX cleanup steps that run after this,
|
2014-09-17 14:06:56 -04:00
|
|
|
// so we wait to ensure VMWare has exited and flushed the VMX.
|
|
|
|
|
|
|
|
// We just sleep here. In the future, it'd be nice to find a better
|
|
|
|
// solution to this.
|
2013-07-31 18:35:47 -04:00
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
}
|
|
|
|
|
2013-06-06 19:30:37 -04:00
|
|
|
log.Println("VM shut down.")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-25 01:33:49 -05:00
|
|
|
func (s *StepShutdown) Cleanup(state multistep.StateBag) {}
|