2014-11-10 12:16:02 -06:00
|
|
|
package xenserver
|
|
|
|
|
|
|
|
/* Taken from https://raw.githubusercontent.com/mitchellh/packer/master/builder/qemu/step_prepare_output_dir.go */
|
|
|
|
|
|
|
|
import (
|
2014-12-08 09:34:48 -06:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
2014-11-10 12:16:02 -06:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2014-12-08 09:34:48 -06:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2014-11-10 12:16:02 -06:00
|
|
|
"os"
|
2014-12-12 04:32:46 -06:00
|
|
|
"time"
|
2014-11-10 12:16:02 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepShutdownAndExport struct{}
|
|
|
|
|
|
|
|
func downloadFile(url, filename string) (err error) {
|
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
// Create the file
|
|
|
|
fh, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
// Define a new transport which allows self-signed certs
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
// Create a client
|
|
|
|
client := &http.Client{Transport: tr}
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
// Create request and download file
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
resp, err := client.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
defer resp.Body.Close()
|
|
|
|
io.Copy(fh, resp.Body)
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
return nil
|
2014-11-10 12:16:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (stepShutdownAndExport) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(config)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2014-12-08 09:34:48 -06:00
|
|
|
client := state.Get("client").(XenAPIClient)
|
|
|
|
instance_uuid := state.Get("instance_uuid").(string)
|
|
|
|
|
2014-12-09 08:02:48 -06:00
|
|
|
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
|
|
|
|
}
|
2014-12-08 09:34:48 -06:00
|
|
|
|
|
|
|
ui.Say("Step: Shutdown and export VPX")
|
|
|
|
|
|
|
|
// Shutdown the VM
|
2014-12-12 04:32:46 -06:00
|
|
|
success := func() bool {
|
|
|
|
if config.ShutdownCommand != "" {
|
|
|
|
ui.Say("Executing shutdown command...")
|
|
|
|
|
|
|
|
_, err := execute_ssh_cmd(config.ShutdownCommand, config.HostIp, "22", config.Username, config.Password)
|
|
|
|
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
|
|
|
|
}
|
2014-12-09 08:02:48 -06:00
|
|
|
}
|
2014-12-08 09:34:48 -06:00
|
|
|
|
2014-12-12 04:32:46 -06:00
|
|
|
ui.Say("Successfully shut down VM")
|
|
|
|
|
2014-12-11 08:30:39 -06:00
|
|
|
switch config.ExportFormat {
|
|
|
|
case "xva":
|
|
|
|
// export the VM
|
2014-12-08 09:34:48 -06:00
|
|
|
|
2014-12-12 04:40:12 -06:00
|
|
|
export_url := fmt.Sprintf("https://%s/export?uuid=%s&session_id=%s",
|
2014-12-11 08:30:39 -06:00
|
|
|
client.Host,
|
|
|
|
instance_uuid,
|
|
|
|
client.Session.(string),
|
|
|
|
)
|
2014-12-08 09:34:48 -06:00
|
|
|
|
2014-12-18 11:20:51 -06:00
|
|
|
export_filename := fmt.Sprintf("%s/%s.xva", config.OutputDir, config.VMName)
|
2014-12-15 06:15:27 -06:00
|
|
|
|
2014-12-11 08:30:39 -06:00
|
|
|
ui.Say("Getting XVA " + export_url)
|
2014-12-15 06:15:27 -06:00
|
|
|
err = downloadFile(export_url, export_filename)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Could not download XVA: %s", err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2014-12-08 09:34:48 -06:00
|
|
|
|
2014-12-11 08:30:39 -06:00
|
|
|
case "vdi_raw":
|
|
|
|
// export the disks
|
|
|
|
|
|
|
|
disks, err := instance.GetDisks()
|
2014-12-09 08:02:48 -06:00
|
|
|
if err != nil {
|
2014-12-11 08:30:39 -06:00
|
|
|
ui.Error(fmt.Sprintf("Could not get VM disks: %s", err.Error()))
|
2014-12-09 08:02:48 -06:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2014-12-11 08:30:39 -06:00
|
|
|
for _, disk := range disks {
|
|
|
|
disk_uuid, err := disk.GetUuid()
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Could not get disk with UUID '%s': %s", disk_uuid, err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Basic auth in URL request is required as session token is not
|
|
|
|
// accepted for some reason.
|
|
|
|
// @todo: raise with XAPI team.
|
|
|
|
disk_export_url := fmt.Sprintf("https://%s:%s@%s/export_raw_vdi?vdi=%s",
|
|
|
|
client.Username,
|
|
|
|
client.Password,
|
|
|
|
client.Host,
|
|
|
|
disk_uuid,
|
|
|
|
)
|
|
|
|
|
|
|
|
disk_export_filename := fmt.Sprintf("%s/%s.raw", config.OutputDir, disk_uuid)
|
2014-12-15 06:15:27 -06:00
|
|
|
|
2014-12-11 08:30:39 -06:00
|
|
|
ui.Say("Getting VDI " + disk_export_url)
|
2014-12-15 06:15:27 -06:00
|
|
|
err = downloadFile(disk_export_url, disk_export_filename)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Could not download VDI: %s", err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2014-12-11 08:30:39 -06:00
|
|
|
}
|
2014-12-08 09:34:48 -06:00
|
|
|
|
2014-12-11 08:30:39 -06:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown export_format '%s'", config.ExportFormat))
|
2014-12-08 09:34:48 -06:00
|
|
|
}
|
|
|
|
|
2014-12-08 09:37:16 -06:00
|
|
|
ui.Say("Download completed: " + config.OutputDir)
|
2014-11-10 12:16:02 -06:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stepShutdownAndExport) Cleanup(state multistep.StateBag) {
|
|
|
|
}
|