2020-01-07 16:59:31 -08:00
|
|
|
//go:generate struct-markdown
|
|
|
|
//go:generate mapstructure-to-hcl2 -type ShutdownConfig
|
|
|
|
|
2018-01-24 14:04:39 +03:00
|
|
|
package common
|
2017-05-09 17:23:57 +03:00
|
|
|
|
|
|
|
import (
|
2018-11-01 00:42:24 +03:00
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-02-14 11:42:29 -05:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2020-01-07 16:59:31 -08:00
|
|
|
"github.com/hashicorp/packer/builder/vsphere/driver"
|
2018-04-25 14:22:38 +03:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-05-09 17:23:57 +03:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
2017-07-02 03:06:02 +03:00
|
|
|
type ShutdownConfig struct {
|
2020-01-07 16:59:31 -08:00
|
|
|
// Specify a VM guest shutdown command. VMware guest tools are used by
|
|
|
|
// default.
|
|
|
|
Command string `mapstructure:"shutdown_command"`
|
|
|
|
// Amount of time to wait for graceful VM shutdown. Examples 45s and 10m.
|
|
|
|
// Defaults to 5m(5 minutes).
|
2019-07-12 11:29:41 +03:00
|
|
|
Timeout time.Duration `mapstructure:"shutdown_timeout"`
|
2017-07-02 03:06:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ShutdownConfig) Prepare() []error {
|
|
|
|
var errs []error
|
|
|
|
|
2019-07-12 11:29:41 +03:00
|
|
|
if c.Timeout == 0 {
|
2017-07-02 03:06:02 +03:00
|
|
|
c.Timeout = 5 * time.Minute
|
|
|
|
}
|
|
|
|
|
2019-07-12 11:29:41 +03:00
|
|
|
return errs
|
2017-07-02 03:06:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type StepShutdown struct {
|
2018-01-24 14:04:39 +03:00
|
|
|
Config *ShutdownConfig
|
2017-05-09 17:23:57 +03:00
|
|
|
}
|
|
|
|
|
2018-05-16 16:05:08 +03:00
|
|
|
func (s *StepShutdown) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-05-09 17:23:57 +03:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-07-02 15:27:52 +03:00
|
|
|
comm := state.Get("communicator").(packer.Communicator)
|
2017-08-24 03:06:50 +03:00
|
|
|
vm := state.Get("vm").(*driver.VirtualMachine)
|
2017-05-09 17:23:57 +03:00
|
|
|
|
2018-01-24 14:04:39 +03:00
|
|
|
if s.Config.Command != "" {
|
2017-07-02 15:27:52 +03:00
|
|
|
ui.Say("Executing shutdown command...")
|
2018-01-24 14:04:39 +03:00
|
|
|
log.Printf("Shutdown command: %s", s.Config.Command)
|
2017-05-09 17:23:57 +03:00
|
|
|
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmd := &packer.RemoteCmd{
|
2018-01-24 14:04:39 +03:00
|
|
|
Command: s.Config.Command,
|
2017-05-09 17:23:57 +03:00
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
|
|
|
}
|
2019-05-25 00:45:53 -05:00
|
|
|
err := comm.Start(ctx, cmd)
|
2017-07-02 23:29:50 +03:00
|
|
|
if err != nil {
|
2017-07-02 03:06:02 +03:00
|
|
|
state.Put("error", fmt.Errorf("Failed to send shutdown command: %s", err))
|
2017-05-09 17:23:57 +03:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
} else {
|
2017-07-02 15:27:52 +03:00
|
|
|
ui.Say("Shut down VM...")
|
2017-05-09 17:23:57 +03:00
|
|
|
|
2017-08-24 03:06:50 +03:00
|
|
|
err := vm.StartShutdown()
|
2017-05-09 17:23:57 +03:00
|
|
|
if err != nil {
|
2017-07-02 03:06:02 +03:00
|
|
|
state.Put("error", fmt.Errorf("Cannot shut down VM: %v", err))
|
2017-05-09 17:23:57 +03:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-06-13 14:11:41 +03:00
|
|
|
}
|
2017-05-09 17:23:57 +03:00
|
|
|
|
2018-01-24 14:04:39 +03:00
|
|
|
log.Printf("Waiting max %s for shutdown to complete", s.Config.Timeout)
|
2018-05-16 16:05:08 +03:00
|
|
|
err := vm.WaitForShutdown(ctx, s.Config.Timeout)
|
2017-07-02 15:27:52 +03:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
2017-05-09 17:23:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepShutdown) Cleanup(state multistep.StateBag) {}
|