2014-12-29 06:46:54 -06:00
|
|
|
package common
|
2014-11-10 12:16:02 -06:00
|
|
|
|
|
|
|
import (
|
2021-01-03 16:08:24 -06:00
|
|
|
"context"
|
2014-12-08 09:34:48 -06:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2014-11-10 12:16:02 -06:00
|
|
|
"os"
|
2016-07-15 11:27:11 -05:00
|
|
|
"os/exec"
|
2020-09-13 03:21:07 -05:00
|
|
|
|
2021-02-23 16:43:20 -06:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/packer"
|
2014-11-10 12:16:02 -06:00
|
|
|
)
|
|
|
|
|
2015-01-05 10:58:26 -06:00
|
|
|
type StepExport struct{}
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2015-02-25 13:03:13 -06:00
|
|
|
func downloadFile(url, filename string, ui packer.Ui) (err error) {
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2014-12-08 09:34:48 -06:00
|
|
|
// Create the file
|
|
|
|
fh, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-15 11:27:11 -05:00
|
|
|
defer fh.Close()
|
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()
|
2015-02-25 13:03:13 -06:00
|
|
|
|
|
|
|
var progress uint
|
|
|
|
var total uint
|
|
|
|
var percentage uint
|
|
|
|
var marker_len uint
|
|
|
|
|
|
|
|
progress = uint(0)
|
|
|
|
total = uint(resp.ContentLength)
|
|
|
|
percentage = uint(0)
|
|
|
|
marker_len = uint(5)
|
|
|
|
|
|
|
|
var buffer [4096]byte
|
|
|
|
for {
|
|
|
|
n, err := resp.Body.Read(buffer[:])
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
progress += uint(n)
|
|
|
|
|
|
|
|
if _, write_err := fh.Write(buffer[:n]); write_err != nil {
|
|
|
|
return write_err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment percentage in multiples of marker_len
|
|
|
|
cur_percentage := ((progress * 100 / total) / marker_len) * marker_len
|
|
|
|
if cur_percentage > percentage {
|
|
|
|
percentage = cur_percentage
|
|
|
|
ui.Message(fmt.Sprintf("Downloading... %d%%", percentage))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-01-03 16:08:24 -06:00
|
|
|
func (StepExport) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2014-12-29 06:46:54 -06:00
|
|
|
config := state.Get("commonconfig").(CommonConfig)
|
2014-11-10 12:16:02 -06:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2020-09-13 03:21:07 -05:00
|
|
|
c := state.Get("client").(*Connection)
|
2014-12-08 09:34:48 -06:00
|
|
|
instance_uuid := state.Get("instance_uuid").(string)
|
2015-02-11 11:33:36 -06:00
|
|
|
suffix := ".vhd"
|
|
|
|
extrauri := "&format=vhd"
|
2014-12-08 09:34:48 -06:00
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
instance, err := c.client.VM.GetByUUID(c.session, instance_uuid)
|
2014-12-09 08:02:48 -06:00
|
|
|
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
|
|
|
|
2016-11-17 01:51:13 -06:00
|
|
|
if len(config.ExportNetworkNames) > 0 {
|
2020-09-13 03:21:07 -05:00
|
|
|
vifs, err := c.client.VM.GetVIFs(c.session, instance)
|
2016-11-17 01:51:13 -06:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error occured getting VIFs: %s", err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, vif := range vifs {
|
2020-09-13 03:21:07 -05:00
|
|
|
err := c.client.VIF.Destroy(c.session, vif)
|
2016-11-17 01:51:13 -06:00
|
|
|
if err != nil {
|
2020-09-13 03:21:07 -05:00
|
|
|
ui.Error(fmt.Sprintf("Destroy vif fail: '%s': %s", vif, err.Error()))
|
2016-11-17 01:51:13 -06:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, networkNameLabel := range config.ExportNetworkNames {
|
2020-09-13 03:21:07 -05:00
|
|
|
networks, err := c.client.Network.GetByNameLabel(c.session, networkNameLabel)
|
2016-11-17 01:51:13 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error occured getting Network by name-label: %s", err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case len(networks) == 0:
|
|
|
|
ui.Error(fmt.Sprintf("Couldn't find a network with the specified name-label '%s'. Aborting.", networkNameLabel))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
case len(networks) > 1:
|
|
|
|
ui.Error(fmt.Sprintf("Found more than one network with the name '%s'. The name must be unique. Aborting.", networkNameLabel))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
//we need the VIF index string
|
|
|
|
vifIndexString := fmt.Sprintf("%d", i)
|
2020-09-13 03:21:07 -05:00
|
|
|
_, err = ConnectNetwork(c, networks[0], instance, vifIndexString)
|
2016-11-17 01:51:13 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Say(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:58:26 -06:00
|
|
|
ui.Say("Step: export artifact")
|
2014-12-12 04:32:46 -06:00
|
|
|
|
2016-10-20 06:41:29 -05:00
|
|
|
compress_option_xe := "compress=false"
|
|
|
|
compress_option_url := ""
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
switch config.Format {
|
2015-01-07 04:40:01 -06:00
|
|
|
case "none":
|
|
|
|
ui.Say("Skipping export")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
|
2016-10-20 06:41:29 -05:00
|
|
|
case "xva_compressed":
|
|
|
|
compress_option_xe = "compress=true"
|
|
|
|
compress_option_url = "use_compression=true&"
|
|
|
|
fallthrough
|
2014-12-11 08:30:39 -06:00
|
|
|
case "xva":
|
|
|
|
// export the VM
|
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
|
|
|
|
2016-07-15 11:27:11 -05:00
|
|
|
use_xe := os.Getenv("USE_XE") == "1"
|
|
|
|
if xe, e := exec.LookPath("xe"); e == nil && use_xe {
|
|
|
|
cmd := exec.Command(
|
|
|
|
xe,
|
2020-09-13 03:21:07 -05:00
|
|
|
"-s", c.Host,
|
2016-07-15 11:27:11 -05:00
|
|
|
"-p", "443",
|
2020-09-13 03:21:07 -05:00
|
|
|
"-u", c.Username,
|
|
|
|
"-pw", c.Password,
|
2016-07-15 11:27:11 -05:00
|
|
|
"vm-export",
|
|
|
|
"vm="+instance_uuid,
|
2016-10-20 06:41:29 -05:00
|
|
|
compress_option_xe,
|
2016-07-15 11:27:11 -05:00
|
|
|
"filename="+export_filename,
|
|
|
|
)
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Getting XVA %+v %+v", cmd.Path, cmd.Args))
|
|
|
|
|
|
|
|
err = cmd.Run()
|
|
|
|
} else {
|
2016-10-20 06:41:29 -05:00
|
|
|
export_url := fmt.Sprintf("https://%s/export?%suuid=%s&session_id=%s",
|
2020-09-13 03:21:07 -05:00
|
|
|
c.Host,
|
2016-10-20 06:41:29 -05:00
|
|
|
compress_option_url,
|
2016-07-15 11:27:11 -05:00
|
|
|
instance_uuid,
|
2020-09-13 03:21:07 -05:00
|
|
|
c.GetSession(),
|
2016-07-15 11:27:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
ui.Say("Getting XVA " + export_url)
|
|
|
|
err = downloadFile(export_url, export_filename, ui)
|
|
|
|
}
|
|
|
|
|
2014-12-15 06:15:27 -06:00
|
|
|
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":
|
2015-03-13 08:23:20 -05:00
|
|
|
suffix = ".raw"
|
|
|
|
extrauri = ""
|
|
|
|
fallthrough
|
2015-02-11 11:33:36 -06:00
|
|
|
case "vdi_vhd":
|
2014-12-11 08:30:39 -06:00
|
|
|
// export the disks
|
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
disks, err := GetDisks(c, instance)
|
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 {
|
2020-09-13 03:21:07 -05:00
|
|
|
disk_uuid, err := c.client.VDI.GetUUID(c.session, disk)
|
2014-12-11 08:30:39 -06:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Could not get disk with UUID '%s': %s", disk_uuid, err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2015-02-25 13:03:13 -06:00
|
|
|
// Work out XenServer version
|
2020-09-13 03:21:07 -05:00
|
|
|
hosts, err := c.client.Host.GetAll(c.session)
|
2015-02-25 13:03:13 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Could not retrieve hosts in the pool: %s", err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
host := hosts[0]
|
2020-09-13 03:21:07 -05:00
|
|
|
host_software_versions, err := c.client.Host.GetSoftwareVersion(c.session, host)
|
|
|
|
xs_version := host_software_versions["product_version"]
|
2015-02-25 13:03:13 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Could not get the software version: %s", err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
var disk_export_url string
|
|
|
|
|
|
|
|
// @todo: check for 6.5 SP1
|
|
|
|
if xs_version <= "6.5.0" && config.Format == "vdi_vhd" {
|
|
|
|
// Export the VHD using a Transfer VM
|
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
disk_export_url, err = Expose(c, disk, "vhd")
|
2015-02-25 13:03:13 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Failed to expose disk %s: %s", disk_uuid, err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Use the preferred direct export from XAPI
|
|
|
|
// 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%s",
|
2020-09-13 03:21:07 -05:00
|
|
|
c.Username,
|
|
|
|
c.Password,
|
|
|
|
c.Host,
|
2015-02-25 13:03:13 -06:00
|
|
|
disk_uuid,
|
|
|
|
extrauri)
|
|
|
|
|
|
|
|
}
|
2014-12-11 08:30:39 -06:00
|
|
|
|
2015-02-11 11:33:36 -06:00
|
|
|
disk_export_filename := fmt.Sprintf("%s/%s%s", config.OutputDir, disk_uuid, suffix)
|
2014-12-15 06:15:27 -06:00
|
|
|
|
2014-12-11 08:30:39 -06:00
|
|
|
ui.Say("Getting VDI " + disk_export_url)
|
2015-02-25 13:03:13 -06:00
|
|
|
err = downloadFile(disk_export_url, disk_export_filename, ui)
|
2014-12-15 06:15:27 -06:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Could not download VDI: %s", err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-02-25 13:03:13 -06:00
|
|
|
|
|
|
|
// Call unexpose in case a TVM was used. The call is harmless
|
|
|
|
// if that is not the case.
|
2020-09-13 03:21:07 -05:00
|
|
|
Unexpose(c, disk)
|
2015-02-25 13:03:13 -06:00
|
|
|
|
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:
|
2014-12-29 06:46:54 -06:00
|
|
|
panic(fmt.Sprintf("Unknown export format '%s'", config.Format))
|
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
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:58:26 -06:00
|
|
|
func (StepExport) Cleanup(state multistep.StateBag) {}
|