github.com/rothwerx/packer@v0.9.0/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 // Set this to true if we're testing 33 Testing bool 34 } 35 36 func (s *StepShutdown) Run(state multistep.StateBag) multistep.StepAction { 37 comm := state.Get("communicator").(packer.Communicator) 38 dir := state.Get("dir").(OutputDir) 39 driver := state.Get("driver").(Driver) 40 ui := state.Get("ui").(packer.Ui) 41 vmxPath := state.Get("vmx_path").(string) 42 43 if s.Command != "" { 44 ui.Say("Gracefully halting virtual machine...") 45 log.Printf("Executing shutdown command: %s", s.Command) 46 47 var stdout, stderr bytes.Buffer 48 cmd := &packer.RemoteCmd{ 49 Command: s.Command, 50 Stdout: &stdout, 51 Stderr: &stderr, 52 } 53 if err := comm.Start(cmd); err != nil { 54 err := fmt.Errorf("Failed to send shutdown command: %s", err) 55 state.Put("error", err) 56 ui.Error(err.Error()) 57 return multistep.ActionHalt 58 } 59 60 // Wait for the command to run 61 cmd.Wait() 62 63 // If the command failed to run, notify the user in some way. 64 if cmd.ExitStatus != 0 { 65 state.Put("error", fmt.Errorf( 66 "Shutdown command has non-zero exit status.\n\nStdout: %s\n\nStderr: %s", 67 stdout.String(), stderr.String())) 68 return multistep.ActionHalt 69 } 70 71 log.Printf("Shutdown stdout: %s", stdout.String()) 72 log.Printf("Shutdown stderr: %s", stderr.String()) 73 74 // Wait for the machine to actually shut down 75 log.Printf("Waiting max %s for shutdown to complete", s.Timeout) 76 shutdownTimer := time.After(s.Timeout) 77 for { 78 running, _ := driver.IsRunning(vmxPath) 79 if !running { 80 break 81 } 82 83 select { 84 case <-shutdownTimer: 85 err := errors.New("Timeout while waiting for machine to shut down.") 86 state.Put("error", err) 87 ui.Error(err.Error()) 88 return multistep.ActionHalt 89 default: 90 time.Sleep(150 * time.Millisecond) 91 } 92 } 93 } else { 94 ui.Say("Forcibly halting virtual machine...") 95 if err := driver.Stop(vmxPath); err != nil { 96 err := fmt.Errorf("Error stopping VM: %s", err) 97 state.Put("error", err) 98 ui.Error(err.Error()) 99 return multistep.ActionHalt 100 } 101 } 102 103 ui.Message("Waiting for VMware to clean up after itself...") 104 lockRegex := regexp.MustCompile(`(?i)\.lck$`) 105 timer := time.After(120 * time.Second) 106 LockWaitLoop: 107 for { 108 files, err := dir.ListFiles() 109 if err != nil { 110 log.Printf("Error listing files in outputdir: %s", err) 111 } else { 112 var locks []string 113 for _, file := range files { 114 if lockRegex.MatchString(file) { 115 locks = append(locks, file) 116 } 117 } 118 119 if len(locks) == 0 { 120 log.Println("No more lock files found. VMware is clean.") 121 break 122 } 123 124 if len(locks) == 1 && strings.HasSuffix(locks[0], ".vmx.lck") { 125 log.Println("Only waiting on VMX lock. VMware is clean.") 126 break 127 } 128 129 log.Printf("Waiting on lock files: %#v", locks) 130 } 131 132 select { 133 case <-timer: 134 log.Println("Reached timeout on waiting for clean VMware. Assuming clean.") 135 break LockWaitLoop 136 case <-time.After(150 * time.Millisecond): 137 } 138 } 139 140 if runtime.GOOS != "darwin" && !s.Testing { 141 // Windows takes a while to yield control of the files when the 142 // process is exiting. Ubuntu will yield control of the files but 143 // VMWare may overwrite the VMX cleanup steps that run after this, 144 // so we wait to ensure VMWare has exited and flushed the VMX. 145 146 // We just sleep here. In the future, it'd be nice to find a better 147 // solution to this. 148 time.Sleep(5 * time.Second) 149 } 150 151 log.Println("VM shut down.") 152 return multistep.ActionContinue 153 } 154 155 func (s *StepShutdown) Cleanup(state multistep.StateBag) {}