2014-12-29 06:46:54 -06:00
|
|
|
package common
|
2014-11-10 12:16:02 -06:00
|
|
|
|
|
|
|
import (
|
2014-12-09 08:02:48 -06:00
|
|
|
"fmt"
|
2014-12-08 09:34:48 -06:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2015-03-13 08:23:20 -05:00
|
|
|
xsclient "github.com/xenserver/go-xenserver-client"
|
2014-12-17 10:11:22 -06:00
|
|
|
"log"
|
2014-11-10 12:16:02 -06:00
|
|
|
)
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
type StepStartVmPaused struct{}
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
func (self *StepStartVmPaused) Run(state multistep.StateBag) multistep.StepAction {
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2015-03-13 08:23:20 -05:00
|
|
|
client := state.Get("client").(xsclient.XenAPIClient)
|
2014-12-08 09:34:48 -06:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
ui.Say("Step: Start VM Paused")
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2014-12-09 08:02:48 -06:00
|
|
|
uuid := state.Get("instance_uuid").(string)
|
|
|
|
instance, err := client.GetVMByUuid(uuid)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2015-01-06 05:34:44 -06:00
|
|
|
// note that here "cd" means boot from hard drive ('c') first, then CDROM ('d')
|
|
|
|
err = instance.SetHVMBoot("BIOS order", "cd")
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to set HVM boot params: %s", err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2014-12-09 08:02:48 -06:00
|
|
|
err = instance.Start(true, false)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to start VM with UUID '%s': %s", uuid, err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
domid, err := instance.GetDomainId()
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to get domid of VM with UUID '%s': %s", uuid, err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2014-12-08 09:34:48 -06:00
|
|
|
state.Put("domid", domid)
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
return multistep.ActionContinue
|
2014-11-10 12:16:02 -06:00
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
func (self *StepStartVmPaused) Cleanup(state multistep.StateBag) {
|
|
|
|
config := state.Get("commonconfig").(CommonConfig)
|
2015-03-13 08:23:20 -05:00
|
|
|
client := state.Get("client").(xsclient.XenAPIClient)
|
2014-12-17 10:11:22 -06:00
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
if config.ShouldKeepVM(state) {
|
2014-12-17 10:11:22 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid := state.Get("instance_uuid").(string)
|
|
|
|
instance, err := client.GetVMByUuid(uuid)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = instance.HardShutdown()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(fmt.Sprintf("Unable to force shutdown VM '%s': %s", uuid, err.Error()))
|
|
|
|
}
|
2014-11-10 12:16:02 -06:00
|
|
|
}
|