Skip to content

Commit 2e6ce17

Browse files
Revert "changes for shutting down the VM for none communicator" (#203)
1 parent 6203408 commit 2e6ce17

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

builder/qemu/step_shutdown.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,25 @@ func (s *stepShutdown) Run(ctx context.Context, state multistep.StateBag) multis
3838
driver := state.Get("driver").(Driver)
3939
ui := state.Get("ui").(packersdk.Ui)
4040

41-
if s.ShutdownCommand != "" && s.Comm.Type != "none" {
41+
if s.Comm.Type == "none" {
42+
cancelCh := make(chan struct{}, 1)
43+
go func() {
44+
defer close(cancelCh)
45+
<-time.After(s.ShutdownTimeout)
46+
}()
47+
ui.Say("Waiting for shutdown...")
48+
if ok := driver.WaitForShutdown(cancelCh); ok {
49+
log.Println("VM shut down.")
50+
return multistep.ActionContinue
51+
} else {
52+
err := fmt.Errorf("Failed to shutdown")
53+
state.Put("error", err)
54+
ui.Error(err.Error())
55+
return multistep.ActionHalt
56+
}
57+
}
58+
59+
if s.ShutdownCommand != "" {
4260
comm := state.Get("communicator").(packersdk.Communicator)
4361
ui.Say("Gracefully halting virtual machine...")
4462
log.Printf("Executing shutdown command: %s", s.ShutdownCommand)

builder/qemu/step_shutdown_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func Test_Shutdown_Null_failure(t *testing.T) {
4242
state := new(multistep.BasicStateBag)
4343
state.Put("ui", packersdk.TestUi(t))
4444
driverMock := new(DriverMock)
45+
driverMock.WaitForShutdownState = false
4546
state.Put("driver", driverMock)
4647

4748
step := &stepShutdown{
@@ -52,14 +53,12 @@ func Test_Shutdown_Null_failure(t *testing.T) {
5253
},
5354
}
5455
action := step.Run(context.TODO(), state)
55-
if action != multistep.ActionContinue {
56-
t.Fatalf("Should have successfully shut down.")
56+
if action != multistep.ActionHalt {
57+
t.Fatalf("Shouldn't have successfully shut down.")
5758
}
58-
5959
err := state.Get("error")
60-
if err != nil {
61-
err = err.(error)
62-
t.Fatalf("Shutdown shouldn't have errored; err: %v", err)
60+
if err == nil {
61+
t.Fatalf("Shutdown should have errored")
6362
}
6463
}
6564

0 commit comments

Comments
 (0)