github.com/sneal/packer@v0.5.2/builder/vmware/common/step_shutdown.go (about)

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