Split shutdown and export, and detach VBDs in between
This commit is contained in:
parent
d1e6bb66a9
commit
6ffa99d624
@ -1,7 +1,5 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
/* Taken from https://raw.githubusercontent.com/mitchellh/packer/master/builder/qemu/step_prepare_output_dir.go */
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -10,10 +8,9 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type StepShutdownAndExport struct{}
|
type StepExport struct{}
|
||||||
|
|
||||||
func downloadFile(url, filename string) (err error) {
|
func downloadFile(url, filename string) (err error) {
|
||||||
|
|
||||||
@ -44,7 +41,7 @@ func downloadFile(url, filename string) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (StepShutdownAndExport) Run(state multistep.StateBag) multistep.StepAction {
|
func (StepExport) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
config := state.Get("commonconfig").(CommonConfig)
|
config := state.Get("commonconfig").(CommonConfig)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
client := state.Get("client").(XenAPIClient)
|
client := state.Get("client").(XenAPIClient)
|
||||||
@ -56,58 +53,7 @@ func (StepShutdownAndExport) Run(state multistep.StateBag) multistep.StepAction
|
|||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.Say("Step: Shutdown and export")
|
ui.Say("Step: export artifact")
|
||||||
|
|
||||||
// Shutdown the VM
|
|
||||||
success := func() bool {
|
|
||||||
if config.ShutdownCommand != "" {
|
|
||||||
ui.Say("Executing shutdown command...")
|
|
||||||
|
|
||||||
_, err := ExecuteGuestSSHCmd(state, config.ShutdownCommand)
|
|
||||||
if err != nil {
|
|
||||||
ui.Error(fmt.Sprintf("Shutdown command failed: %s", err.Error()))
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
ui.Say("Waiting for VM to enter Halted state...")
|
|
||||||
|
|
||||||
err = InterruptibleWait{
|
|
||||||
Predicate: func() (bool, error) {
|
|
||||||
power_state, err := instance.GetPowerState()
|
|
||||||
return power_state == "Halted", err
|
|
||||||
},
|
|
||||||
PredicateInterval: 5 * time.Second,
|
|
||||||
Timeout: 300 * time.Second,
|
|
||||||
}.Wait(state)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
ui.Error(fmt.Sprintf("Error waiting for VM to halt: %s", err.Error()))
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
ui.Say("Attempting to cleanly shutdown the VM...")
|
|
||||||
|
|
||||||
err = instance.CleanShutdown()
|
|
||||||
if err != nil {
|
|
||||||
ui.Error(fmt.Sprintf("Could not shut down VM: %s", err.Error()))
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}()
|
|
||||||
|
|
||||||
if !success {
|
|
||||||
ui.Say("Forcing hard shutdown of the VM...")
|
|
||||||
err = instance.HardShutdown()
|
|
||||||
if err != nil {
|
|
||||||
ui.Error(fmt.Sprintf("Could not hard shut down VM -- giving up: %s", err.Error()))
|
|
||||||
return multistep.ActionHalt
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ui.Say("Successfully shut down VM")
|
|
||||||
|
|
||||||
switch config.Format {
|
switch config.Format {
|
||||||
case "xva":
|
case "xva":
|
||||||
@ -172,4 +118,4 @@ func (StepShutdownAndExport) Run(state multistep.StateBag) multistep.StepAction
|
|||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (StepShutdownAndExport) Cleanup(state multistep.StateBag) {}
|
func (StepExport) Cleanup(state multistep.StateBag) {}
|
79
builder/xenserver/common/step_shutdown.go
Normal file
79
builder/xenserver/common/step_shutdown.go
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/mitchellh/multistep"
|
||||||
|
"github.com/mitchellh/packer/packer"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type StepShutdown struct{}
|
||||||
|
|
||||||
|
func (StepShutdown) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
|
config := state.Get("commonconfig").(CommonConfig)
|
||||||
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
client := state.Get("client").(XenAPIClient)
|
||||||
|
instance_uuid := state.Get("instance_uuid").(string)
|
||||||
|
|
||||||
|
instance, err := client.GetVMByUuid(instance_uuid)
|
||||||
|
if err != nil {
|
||||||
|
ui.Error(fmt.Sprintf("Could not get VM with UUID '%s': %s", instance_uuid, err.Error()))
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.Say("Step: Shutdown and export")
|
||||||
|
|
||||||
|
// Shutdown the VM
|
||||||
|
success := func() bool {
|
||||||
|
if config.ShutdownCommand != "" {
|
||||||
|
ui.Message("Executing shutdown command...")
|
||||||
|
|
||||||
|
_, err := ExecuteGuestSSHCmd(state, config.ShutdownCommand)
|
||||||
|
if err != nil {
|
||||||
|
ui.Error(fmt.Sprintf("Shutdown command failed: %s", err.Error()))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.Message("Waiting for VM to enter Halted state...")
|
||||||
|
|
||||||
|
err = InterruptibleWait{
|
||||||
|
Predicate: func() (bool, error) {
|
||||||
|
power_state, err := instance.GetPowerState()
|
||||||
|
return power_state == "Halted", err
|
||||||
|
},
|
||||||
|
PredicateInterval: 5 * time.Second,
|
||||||
|
Timeout: 300 * time.Second,
|
||||||
|
}.Wait(state)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
ui.Error(fmt.Sprintf("Error waiting for VM to halt: %s", err.Error()))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ui.Message("Attempting to cleanly shutdown the VM...")
|
||||||
|
|
||||||
|
err = instance.CleanShutdown()
|
||||||
|
if err != nil {
|
||||||
|
ui.Error(fmt.Sprintf("Could not shut down VM: %s", err.Error()))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}()
|
||||||
|
|
||||||
|
if !success {
|
||||||
|
ui.Say("WARNING: Forcing hard shutdown of the VM...")
|
||||||
|
err = instance.HardShutdown()
|
||||||
|
if err != nil {
|
||||||
|
ui.Error(fmt.Sprintf("Could not hard shut down VM -- giving up: %s", err.Error()))
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.Message("Successfully shut down VM")
|
||||||
|
return multistep.ActionContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
func (StepShutdown) Cleanup(state multistep.StateBag) {}
|
@ -272,7 +272,17 @@ func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (pa
|
|||||||
SSHWaitTimeout: self.config.SSHWaitTimeout,
|
SSHWaitTimeout: self.config.SSHWaitTimeout,
|
||||||
},
|
},
|
||||||
new(common.StepProvision),
|
new(common.StepProvision),
|
||||||
new(xscommon.StepShutdownAndExport),
|
new(xscommon.StepShutdown),
|
||||||
|
&xscommon.StepDetachVdi{
|
||||||
|
VdiUuidKey: "floppy_vdi_uuid",
|
||||||
|
},
|
||||||
|
&xscommon.StepDetachVdi{
|
||||||
|
VdiUuidKey: "iso_vdi_uuid",
|
||||||
|
},
|
||||||
|
&xscommon.StepDetachVdi{
|
||||||
|
VdiUuidKey: "tools_vdi_uuid",
|
||||||
|
},
|
||||||
|
new(xscommon.StepExport),
|
||||||
}
|
}
|
||||||
|
|
||||||
self.runner = &multistep.BasicRunner{Steps: steps}
|
self.runner = &multistep.BasicRunner{Steps: steps}
|
||||||
|
@ -160,13 +160,14 @@ func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (pa
|
|||||||
SSHWaitTimeout: self.config.SSHWaitTimeout,
|
SSHWaitTimeout: self.config.SSHWaitTimeout,
|
||||||
},
|
},
|
||||||
new(common.StepProvision),
|
new(common.StepProvision),
|
||||||
|
new(xscommon.StepShutdown),
|
||||||
&xscommon.StepDetachVdi{
|
&xscommon.StepDetachVdi{
|
||||||
VdiUuidKey: "floppy_vdi_uuid",
|
VdiUuidKey: "floppy_vdi_uuid",
|
||||||
},
|
},
|
||||||
&xscommon.StepDetachVdi{
|
&xscommon.StepDetachVdi{
|
||||||
VdiUuidKey: "iso_vdi_uuid",
|
VdiUuidKey: "tools_vdi_uuid",
|
||||||
},
|
},
|
||||||
new(xscommon.StepShutdownAndExport),
|
new(xscommon.StepExport),
|
||||||
}
|
}
|
||||||
|
|
||||||
self.runner = &multistep.BasicRunner{Steps: steps}
|
self.runner = &multistep.BasicRunner{Steps: steps}
|
||||||
|
Loading…
Reference in New Issue
Block a user