2020-01-07 19:59:31 -05:00
|
|
|
//go:generate struct-markdown
|
|
|
|
//go:generate mapstructure-to-hcl2 -type ShutdownConfig
|
|
|
|
|
2018-01-24 06:04:39 -05:00
|
|
|
package common
|
2017-05-09 10:23:57 -04:00
|
|
|
|
|
|
|
import (
|
2018-10-31 17:42:24 -04:00
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-02-14 11:42:29 -05:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2020-01-07 19:59:31 -05:00
|
|
|
"github.com/hashicorp/packer/builder/vsphere/driver"
|
2020-10-13 15:12:28 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2017-05-09 10:23:57 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
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"
|
2017-05-09 10:23:57 -04:00
|
|
|
)
|
|
|
|
|
2017-07-01 20:06:02 -04:00
|
|
|
type ShutdownConfig struct {
|
2020-09-18 16:01:16 -04:00
|
|
|
// Specify a VM guest shutdown command. This command will be executed using
|
|
|
|
// the `communicator`. Otherwise the VMware guest tools are used to gracefully
|
|
|
|
// shutdown the VM guest.
|
2020-01-07 19:59:31 -05:00
|
|
|
Command string `mapstructure:"shutdown_command"`
|
2020-04-20 13:56:53 -04:00
|
|
|
// Amount of time to wait for graceful VM shutdown.
|
|
|
|
// Defaults to 5m or five minutes.
|
2020-09-18 16:01:16 -04:00
|
|
|
// This will likely need to be modified if the `communicator` is 'none'.
|
2019-07-12 04:29:41 -04:00
|
|
|
Timeout time.Duration `mapstructure:"shutdown_timeout"`
|
2020-04-20 13:56:53 -04:00
|
|
|
// Packer normally halts the virtual machine after all provisioners have
|
|
|
|
// run when no `shutdown_command` is defined. If this is set to `true`, Packer
|
|
|
|
// *will not* halt the virtual machine but will assume that you will send the stop
|
2020-10-13 16:29:33 -04:00
|
|
|
// signal yourself through a preseed.cfg, a script or the final provisioner.
|
2020-04-20 13:56:53 -04:00
|
|
|
// Packer will wait for a default of five minutes until the virtual machine is shutdown.
|
|
|
|
// The timeout can be changed using `shutdown_timeout` option.
|
|
|
|
DisableShutdown bool `mapstructure:"disable_shutdown"`
|
2017-07-01 20:06:02 -04:00
|
|
|
}
|
|
|
|
|
2020-10-13 15:12:28 -04:00
|
|
|
func (c *ShutdownConfig) Prepare(comm communicator.Config) (warnings []string, errs []error) {
|
2017-07-01 20:06:02 -04:00
|
|
|
|
2019-07-12 04:29:41 -04:00
|
|
|
if c.Timeout == 0 {
|
2017-07-01 20:06:02 -04:00
|
|
|
c.Timeout = 5 * time.Minute
|
|
|
|
}
|
|
|
|
|
2020-10-13 15:12:28 -04:00
|
|
|
if comm.Type == "none" && c.Command != "" {
|
|
|
|
warnings = append(warnings, "The parameter `shutdown_command` is ignored as it requires a `communicator`.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2017-07-01 20:06:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type StepShutdown struct {
|
2018-01-24 06:04:39 -05:00
|
|
|
Config *ShutdownConfig
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
|
2018-05-16 09:05:08 -04:00
|
|
|
func (s *StepShutdown) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2020-08-31 04:34:41 -04:00
|
|
|
vm := state.Get("vm").(*driver.VirtualMachineDriver)
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2020-04-20 13:56:53 -04:00
|
|
|
if off, _ := vm.IsPoweredOff(); off {
|
|
|
|
// Probably power off initiated by last provisioner, though disable_shutdown is not set
|
|
|
|
ui.Say("VM is already powered off")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-10-13 15:53:27 -04:00
|
|
|
comm, _ := state.Get("communicator").(packer.Communicator)
|
|
|
|
if comm == nil {
|
2020-09-18 16:00:30 -04:00
|
|
|
|
|
|
|
msg := fmt.Sprintf("Please shutdown virtual machine within %s.", s.Config.Timeout)
|
|
|
|
ui.Message(msg)
|
|
|
|
|
|
|
|
} else if s.Config.DisableShutdown {
|
2020-04-20 13:56:53 -04:00
|
|
|
ui.Say("Automatic shutdown disabled. Please shutdown virtual machine.")
|
|
|
|
} else if s.Config.Command != "" {
|
2020-09-15 09:56:36 -04:00
|
|
|
// Communicator is not needed unless shutdown_command is populated
|
|
|
|
|
2017-07-02 08:27:52 -04:00
|
|
|
ui.Say("Executing shutdown command...")
|
2018-01-24 06:04:39 -05:00
|
|
|
log.Printf("Shutdown command: %s", s.Config.Command)
|
2017-05-09 10:23:57 -04:00
|
|
|
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmd := &packer.RemoteCmd{
|
2018-01-24 06:04:39 -05:00
|
|
|
Command: s.Config.Command,
|
2017-05-09 10:23:57 -04:00
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
|
|
|
}
|
2019-05-25 01:45:53 -04:00
|
|
|
err := comm.Start(ctx, cmd)
|
2017-07-02 16:29:50 -04:00
|
|
|
if err != nil {
|
2017-07-01 20:06:02 -04:00
|
|
|
state.Put("error", fmt.Errorf("Failed to send shutdown command: %s", err))
|
2017-05-09 10:23:57 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-20 13:56:53 -04:00
|
|
|
ui.Say("Shutting down VM...")
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
err := vm.StartShutdown()
|
2017-05-09 10:23:57 -04:00
|
|
|
if err != nil {
|
2017-07-01 20:06:02 -04:00
|
|
|
state.Put("error", fmt.Errorf("Cannot shut down VM: %v", err))
|
2017-05-09 10:23:57 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-06-13 07:11:41 -04:00
|
|
|
}
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2018-01-24 06:04:39 -05:00
|
|
|
log.Printf("Waiting max %s for shutdown to complete", s.Config.Timeout)
|
2018-05-16 09:05:08 -04:00
|
|
|
err := vm.WaitForShutdown(ctx, s.Config.Timeout)
|
2017-07-02 08:27:52 -04:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepShutdown) Cleanup(state multistep.StateBag) {}
|