github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/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 so we can print std{err,out} 61 // We don't care if the command errored, since we'll notice 62 // if the vm didn't shut down. 63 cmd.Wait() 64 65 log.Printf("Shutdown stdout: %s", stdout.String()) 66 log.Printf("Shutdown stderr: %s", stderr.String()) 67 68 // Wait for the machine to actually shut down 69 log.Printf("Waiting max %s for shutdown to complete", s.Timeout) 70 shutdownTimer := time.After(s.Timeout) 71 for { 72 running, _ := driver.IsRunning(vmxPath) 73 if !running { 74 break 75 } 76 77 select { 78 case <-shutdownTimer: 79 err := errors.New("Timeout while waiting for machine to shut down.") 80 state.Put("error", err) 81 ui.Error(err.Error()) 82 return multistep.ActionHalt 83 default: 84 time.Sleep(150 * time.Millisecond) 85 } 86 } 87 } else { 88 ui.Say("Forcibly halting virtual machine...") 89 if err := driver.Stop(vmxPath); err != nil { 90 err := fmt.Errorf("Error stopping VM: %s", err) 91 state.Put("error", err) 92 ui.Error(err.Error()) 93 return multistep.ActionHalt 94 } 95 } 96 97 ui.Message("Waiting for VMware to clean up after itself...") 98 lockRegex := regexp.MustCompile(`(?i)\.lck$`) 99 timer := time.After(120 * time.Second) 100 LockWaitLoop: 101 for { 102 files, err := dir.ListFiles() 103 if err != nil { 104 log.Printf("Error listing files in outputdir: %s", err) 105 } else { 106 var locks []string 107 for _, file := range files { 108 if lockRegex.MatchString(file) { 109 locks = append(locks, file) 110 } 111 } 112 113 if len(locks) == 0 { 114 log.Println("No more lock files found. VMware is clean.") 115 break 116 } 117 118 if len(locks) == 1 && strings.HasSuffix(locks[0], ".vmx.lck") { 119 log.Println("Only waiting on VMX lock. VMware is clean.") 120 break 121 } 122 123 log.Printf("Waiting on lock files: %#v", locks) 124 } 125 126 select { 127 case <-timer: 128 log.Println("Reached timeout on waiting for clean VMware. Assuming clean.") 129 break LockWaitLoop 130 case <-time.After(150 * time.Millisecond): 131 } 132 } 133 134 if runtime.GOOS != "darwin" && !s.Testing { 135 // Windows takes a while to yield control of the files when the 136 // process is exiting. Ubuntu will yield control of the files but 137 // VMWare may overwrite the VMX cleanup steps that run after this, 138 // so we wait to ensure VMWare has exited and flushed the VMX. 139 140 // We just sleep here. In the future, it'd be nice to find a better 141 // solution to this. 142 time.Sleep(5 * time.Second) 143 } 144 145 log.Println("VM shut down.") 146 return multistep.ActionContinue 147 } 148 149 func (s *StepShutdown) Cleanup(state multistep.StateBag) {}