2013-12-22 12:37:27 -05:00
|
|
|
package common
|
2013-06-12 21:02:42 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-06-20 00:07:53 -04:00
|
|
|
"errors"
|
2013-06-12 21:02:42 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-06-12 21:02:42 -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:
|
2020-11-19 18:10:00 -05:00
|
|
|
// communicator packersdk.Communicator
|
2013-06-12 21:02:42 -04:00
|
|
|
// driver Driver
|
2020-11-19 14:54:31 -05:00
|
|
|
// ui packersdk.Ui
|
2013-06-12 21:02:42 -04:00
|
|
|
// vmName string
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
2013-12-22 12:37:27 -05:00
|
|
|
type StepShutdown struct {
|
2019-12-05 08:34:56 -05:00
|
|
|
Command string
|
|
|
|
Timeout time.Duration
|
|
|
|
Delay time.Duration
|
|
|
|
DisableShutdown bool
|
2020-01-09 11:36:19 -05:00
|
|
|
ACPIShutdown bool
|
2013-12-22 12:37:27 -05:00
|
|
|
}
|
2013-06-12 21:02:42 -04:00
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepShutdown) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-19 18:10:00 -05:00
|
|
|
comm := state.Get("communicator").(packersdk.Communicator)
|
2013-12-22 12:37:27 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-08-31 15:44:58 -04:00
|
|
|
vmName := state.Get("vmName").(string)
|
2013-06-12 21:02:42 -04:00
|
|
|
|
2020-01-09 11:36:19 -05:00
|
|
|
if s.ACPIShutdown {
|
|
|
|
ui.Say("Shuting down the virtual machine via ACPI power button...")
|
|
|
|
if err := driver.StopViaACPI(vmName); err != nil {
|
|
|
|
err := fmt.Errorf("Error stopping VM: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
} else if !s.DisableShutdown {
|
2019-12-05 08:34:56 -05:00
|
|
|
if s.Command != "" {
|
|
|
|
ui.Say("Gracefully halting virtual machine...")
|
|
|
|
log.Printf("Executing shutdown command: %s", s.Command)
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd := &packersdk.RemoteCmd{Command: s.Command}
|
2019-12-05 08:34:56 -05:00
|
|
|
if err := cmd.RunWithUi(ctx, comm, ui); err != nil {
|
|
|
|
err := fmt.Errorf("Failed to send shutdown command: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-06-12 21:02:42 -04:00
|
|
|
|
2019-12-05 08:34:56 -05:00
|
|
|
} else {
|
|
|
|
ui.Say("Halting the virtual machine...")
|
|
|
|
if err := driver.Stop(vmName); err != nil {
|
|
|
|
err := fmt.Errorf("Error stopping VM: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-07-26 05:33:39 -04:00
|
|
|
}
|
2019-12-05 08:34:56 -05:00
|
|
|
} else {
|
|
|
|
ui.Say("Automatic shutdown disabled. Please shutdown virtual machine.")
|
2019-07-26 05:33:39 -04:00
|
|
|
}
|
2016-10-02 13:08:32 -04:00
|
|
|
|
2019-07-26 05:33:39 -04:00
|
|
|
// Wait for the machine to actually shut down
|
|
|
|
log.Printf("Waiting max %s for shutdown to complete", s.Timeout)
|
|
|
|
shutdownTimer := time.After(s.Timeout)
|
|
|
|
for {
|
|
|
|
running, _ := driver.IsRunning(vmName)
|
|
|
|
if !running {
|
2016-10-02 13:08:32 -04:00
|
|
|
|
2019-07-26 05:33:39 -04:00
|
|
|
if s.Delay.Nanoseconds() > 0 {
|
|
|
|
log.Printf("Delay for %s after shutdown to allow locks to clear...", s.Delay)
|
|
|
|
time.Sleep(s.Delay)
|
2013-06-12 21:02:42 -04:00
|
|
|
}
|
|
|
|
|
2019-07-26 05:33:39 -04:00
|
|
|
break
|
2013-06-12 21:02:42 -04:00
|
|
|
}
|
2019-07-26 05:33:39 -04:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-shutdownTimer:
|
2019-12-05 08:34:56 -05:00
|
|
|
err := errors.New("Timeout while waiting for machine to shutdown.")
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:07:53 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-12 21:02:42 -04:00
|
|
|
return multistep.ActionHalt
|
2019-07-26 05:33:39 -04:00
|
|
|
default:
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2013-06-12 21:02:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("VM shut down.")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-22 12:37:27 -05:00
|
|
|
func (s *StepShutdown) Cleanup(state multistep.StateBag) {}
|