2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-06-21 07:36:07 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
2017-03-20 18:32:42 -04:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2015-06-21 07:36:07 -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
|
2018-07-17 11:33:48 -04:00
|
|
|
// driver Driver
|
2020-11-19 14:54:31 -05:00
|
|
|
// ui packersdk.Ui
|
2018-07-17 11:33:48 -04:00
|
|
|
// vmName string
|
2015-06-21 07:36:07 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
|
|
|
type StepShutdown struct {
|
|
|
|
Command string
|
|
|
|
Timeout time.Duration
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepShutdown) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
comm := state.Get("communicator").(packersdk.Communicator)
|
2015-06-21 07:36:07 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2015-06-21 07:36:07 -04:00
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
|
|
|
if s.Command != "" {
|
|
|
|
ui.Say("Gracefully halting virtual machine...")
|
|
|
|
log.Printf("Executing shutdown command: %s", s.Command)
|
|
|
|
|
|
|
|
var stdout, stderr bytes.Buffer
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd := &packersdk.RemoteCmd{
|
2015-06-21 07:36:07 -04:00
|
|
|
Command: s.Command,
|
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := comm.Start(ctx, cmd); err != nil {
|
2015-06-21 07:36:07 -04:00
|
|
|
err := fmt.Errorf("Failed to send shutdown command: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-shutdownTimer:
|
2017-03-20 18:32:42 -04:00
|
|
|
log.Printf("Shutdown stdout: %s", stdout.String())
|
|
|
|
log.Printf("Shutdown stderr: %s", stderr.String())
|
2015-06-21 07:36:07 -04:00
|
|
|
err := errors.New("Timeout while waiting for machine to shut down.")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
default:
|
2017-03-20 18:32:42 -04:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ui.Say("Forcibly halting 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("VM shut down.")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepShutdown) Cleanup(state multistep.StateBag) {}
|