Skip to content

Commit

Permalink
Stop vm on cleanup without communicator (#153)
Browse files Browse the repository at this point in the history
* Report start of the VM independently of typing boot commands

The typing of the boot commands happens after the VM has been started.

* Restructure boot command logging

Log the fact that we're about to type boot commands over VNC
before we initiate the VNC connection flow, which has its own
logging.

Then log again once we actually start typing, and when we are
done typing.

* Stop VM on cleanup if not stopped through SSH communicator

We don't want to leave the VM hanging around indefinitely.

* .golangci.yml: disable "mnd" linter

---------

Co-authored-by: Nikolay Edigaryev <[email protected]>
  • Loading branch information
torarnv and edigaryev authored Jun 13, 2024
1 parent 25732aa commit 9237819
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ linters:
# We can control this ourselves
- varnamelen
- contextcheck
- mnd

# Packer-plugin specific disables
- forcetypeassert
Expand Down
36 changes: 25 additions & 11 deletions builder/tart/step_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
"github.com/mitchellh/go-vnc"
"net"
"os"
"os/exec"
"regexp"
"strings"
Expand Down Expand Up @@ -76,14 +77,14 @@ func (s *stepRun) Run(ctx context.Context, state multistep.StateBag) multistep.S

state.Put("tart-cmd", cmd)

ui.Say("Successfully started the virtual machine...")

if len(config.BootCommand) > 0 && !config.DisableVNC {
if !typeBootCommandOverVNC(ctx, state, config, ui, stdout) {
return multistep.ActionHalt
}
}

ui.Say("Successfully started the virtual machine...")

return multistep.ActionContinue
}

Expand All @@ -100,11 +101,14 @@ func (u uiWriter) Write(p []byte) (n int, err error) {
func (s *stepRun) Cleanup(state multistep.StateBag) {
config := state.Get("config").(*Config)
ui := state.Get("ui").(packersdk.Ui)
cmd := state.Get("tart-cmd").(*exec.Cmd)
if cmd == nil {
return // Nothing to shut down
}

communicator := state.Get("communicator")
if communicator != nil {
ui.Say("Gracefully shutting down the VM...")

shutdownCmd := packersdk.RemoteCmd{
Command: fmt.Sprintf("echo %s | sudo -S -p '' shutdown -h now", config.CommunicatorConfig.Password()),
}
Expand All @@ -114,14 +118,19 @@ func (s *stepRun) Cleanup(state multistep.StateBag) {
ui.Say("Failed to gracefully shutdown VM...")
ui.Error(err.Error())
}
} else {
ui.Say("Shutting down the VM...")
err := cmd.Process.Signal(os.Interrupt)
if err != nil {
ui.Say("Failed to shutdown VM...")
ui.Error(err.Error())
}
}

cmd := state.Get("tart-cmd").(*exec.Cmd)

if cmd != nil {
ui.Say("Waiting for the tart process to exit...")
_, _ = cmd.Process.Wait()
}
// Always wait, even if we didn't initiate shutdown,
// so that we properly read and close stdout/stderr.
ui.Say("Waiting for the tart process to exit...")
_, _ = cmd.Process.Wait()
}

func typeBootCommandOverVNC(
Expand All @@ -131,6 +140,8 @@ func typeBootCommandOverVNC(
ui packersdk.Ui,
tartRunStdout *bytes.Buffer,
) bool {
ui.Say("Typing boot commands over VNC...")

if config.HTTPDir != "" || len(config.HTTPContent) != 0 {
ui.Say("Detecting host IP...")

Expand Down Expand Up @@ -220,9 +231,10 @@ func typeBootCommandOverVNC(
time.Sleep(config.VNCConfig.BootWait)
}

vncDriver := bootcommand.NewVNCDriver(vncClient, config.BootKeyInterval)
message := fmt.Sprintf("Typing commands with key interval %v...", config.BootKeyInterval)
ui.Say(message)

ui.Say("Typing the commands over VNC...")
vncDriver := bootcommand.NewVNCDriver(vncClient, config.BootKeyInterval)

command, err := interpolate.Render(config.VNCConfig.FlatBootCommand(), &config.ctx)
if err != nil {
Expand Down Expand Up @@ -250,6 +262,8 @@ func typeBootCommandOverVNC(
return false
}

ui.Say("Done typing commands!")

return true
}

Expand Down

0 comments on commit 9237819

Please sign in to comment.