github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/vmware/common/step_shutdown.go (about)

     1  package common
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  	"github.com/hashicorp/packer/packer"
     8  	"github.com/mitchellh/multistep"
     9  	"log"
    10  	"regexp"
    11  	"strings"
    12  	"time"
    13  )
    14  
    15  // This step shuts down the machine. It first attempts to do so gracefully,
    16  // but ultimately forcefully shuts it down if that fails.
    17  //
    18  // Uses:
    19  //   communicator packer.Communicator
    20  //   dir OutputDir
    21  //   driver Driver
    22  //   ui     packer.Ui
    23  //   vmx_path string
    24  //
    25  // Produces:
    26  //   <nothing>
    27  type StepShutdown struct {
    28  	Command string
    29  	Timeout time.Duration
    30  
    31  	// Set this to true if we're testing
    32  	Testing bool
    33  }
    34  
    35  func (s *StepShutdown) Run(state multistep.StateBag) multistep.StepAction {
    36  	comm := state.Get("communicator").(packer.Communicator)
    37  	dir := state.Get("dir").(OutputDir)
    38  	driver := state.Get("driver").(Driver)
    39  	ui := state.Get("ui").(packer.Ui)
    40  	vmxPath := state.Get("vmx_path").(string)
    41  
    42  	if s.Command != "" {
    43  		ui.Say("Gracefully halting virtual machine...")
    44  		log.Printf("Executing shutdown command: %s", s.Command)
    45  
    46  		var stdout, stderr bytes.Buffer
    47  		cmd := &packer.RemoteCmd{
    48  			Command: s.Command,
    49  			Stdout:  &stdout,
    50  			Stderr:  &stderr,
    51  		}
    52  		if err := comm.Start(cmd); err != nil {
    53  			err := fmt.Errorf("Failed to send shutdown command: %s", err)
    54  			state.Put("error", err)
    55  			ui.Error(err.Error())
    56  			return multistep.ActionHalt
    57  		}
    58  
    59  		// Wait for the machine to actually shut down
    60  		log.Printf("Waiting max %s for shutdown to complete", s.Timeout)
    61  		shutdownTimer := time.After(s.Timeout)
    62  		for {
    63  			running, _ := driver.IsRunning(vmxPath)
    64  			if !running {
    65  				break
    66  			}
    67  
    68  			select {
    69  			case <-shutdownTimer:
    70  				log.Printf("Shutdown stdout: %s", stdout.String())
    71  				log.Printf("Shutdown stderr: %s", stderr.String())
    72  				err := errors.New("Timeout while waiting for machine to shut down.")
    73  				state.Put("error", err)
    74  				ui.Error(err.Error())
    75  				return multistep.ActionHalt
    76  			default:
    77  				time.Sleep(150 * time.Millisecond)
    78  			}
    79  		}
    80  	} else {
    81  		ui.Say("Forcibly halting virtual machine...")
    82  		if err := driver.Stop(vmxPath); err != nil {
    83  			err := fmt.Errorf("Error stopping VM: %s", err)
    84  			state.Put("error", err)
    85  			ui.Error(err.Error())
    86  			return multistep.ActionHalt
    87  		}
    88  	}
    89  
    90  	ui.Message("Waiting for VMware to clean up after itself...")
    91  	lockRegex := regexp.MustCompile(`(?i)\.lck$`)
    92  	timer := time.After(120 * time.Second)
    93  LockWaitLoop:
    94  	for {
    95  		files, err := dir.ListFiles()
    96  		if err != nil {
    97  			log.Printf("Error listing files in outputdir: %s", err)
    98  		} else {
    99  			var locks []string
   100  			for _, file := range files {
   101  				if lockRegex.MatchString(file) {
   102  					locks = append(locks, file)
   103  				}
   104  			}
   105  
   106  			if len(locks) == 0 {
   107  				log.Println("No more lock files found. VMware is clean.")
   108  				break
   109  			}
   110  
   111  			if len(locks) == 1 && strings.HasSuffix(locks[0], ".vmx.lck") {
   112  				log.Println("Only waiting on VMX lock. VMware is clean.")
   113  				break
   114  			}
   115  
   116  			log.Printf("Waiting on lock files: %#v", locks)
   117  		}
   118  
   119  		select {
   120  		case <-timer:
   121  			log.Println("Reached timeout on waiting for clean VMware. Assuming clean.")
   122  			break LockWaitLoop
   123  		case <-time.After(150 * time.Millisecond):
   124  		}
   125  	}
   126  
   127  	if !s.Testing {
   128  		// Windows takes a while to yield control of the files when the
   129  		// process is exiting. Ubuntu and OS X will yield control of the files
   130  		// but VMWare may overwrite the VMX cleanup steps that run after this,
   131  		// so we wait to ensure VMWare has exited and flushed the VMX.
   132  
   133  		// We just sleep here. In the future, it'd be nice to find a better
   134  		// solution to this.
   135  		time.Sleep(5 * time.Second)
   136  	}
   137  
   138  	log.Println("VM shut down.")
   139  	return multistep.ActionContinue
   140  }
   141  
   142  func (s *StepShutdown) Cleanup(state multistep.StateBag) {}