Encapsulate xen client properly

This commit is contained in:
flx5 2021-10-01 12:41:54 +02:00
parent cdd65fb230
commit f24ca7f84d
22 changed files with 116 additions and 95 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/hashicorp/packer-plugin-sdk/shutdowncommand"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
xenapi "github.com/terra-farm/go-xen-api-client"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
)
type CommonConfig struct {
@ -153,16 +154,16 @@ func (c CommonConfig) ShouldKeepVM(state multistep.StateBag) bool {
}
}
func (config CommonConfig) GetSR(c *Connection) (xenapi.SRRef, error) {
func (config CommonConfig) GetSR(c *xen.Connection) (xenapi.SRRef, error) {
var srRef xenapi.SRRef
if config.SrName == "" {
hostRef, err := c.GetClient().Session.GetThisHost(c.session, c.session)
hostRef, err := c.GetClient().Session.GetThisHost(c.GetSessionRef(), c.GetSessionRef())
if err != nil {
return srRef, err
}
pools, err := c.GetClient().Pool.GetAllRecords(c.session)
pools, err := c.GetClient().Pool.GetAllRecords(c.GetSessionRef())
if err != nil {
return srRef, err
@ -178,7 +179,7 @@ func (config CommonConfig) GetSR(c *Connection) (xenapi.SRRef, error) {
} else {
// Use the provided name label to find the SR to use
srs, err := c.GetClient().SR.GetByNameLabel(c.session, config.SrName)
srs, err := c.GetClient().SR.GetByNameLabel(c.GetSessionRef(), config.SrName)
if err != nil {
return srRef, err
@ -195,14 +196,14 @@ func (config CommonConfig) GetSR(c *Connection) (xenapi.SRRef, error) {
}
}
func (config CommonConfig) GetISOSR(c *Connection) (xenapi.SRRef, error) {
func (config CommonConfig) GetISOSR(c *xen.Connection) (xenapi.SRRef, error) {
var srRef xenapi.SRRef
if config.SrISOName == "" {
return srRef, errors.New("sr_iso_name must be specified in the packer configuration")
} else {
// Use the provided name label to find the SR to use
srs, err := c.GetClient().SR.GetByNameLabel(c.session, config.SrName)
srs, err := c.GetClient().SR.GetByNameLabel(c.GetSessionRef(), config.SrName)
if err != nil {
return srRef, err

View File

@ -3,6 +3,7 @@ package common
import (
"crypto/tls"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"log"
"net/http"
"net/url"
@ -27,14 +28,14 @@ func appendQuery(urlstring, k, v string) (string, error) {
func HTTPUpload(import_url string, fh *os.File, state multistep.StateBag) (result string, err error) {
ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
task, err := c.client.Task.Create(c.session, "packer-task", "Packer task")
task, err := c.GetClient().Task.Create(c.GetSessionRef(), "packer-task", "Packer task")
if err != nil {
err = fmt.Errorf("Unable to create task: %s", err.Error())
return
}
defer c.client.Task.Destroy(c.session, task)
defer c.GetClient().Task.Destroy(c.GetSessionRef(), task)
import_task_url, err := appendQuery(import_url, "task_id", string(task))
if err != nil {
@ -76,13 +77,13 @@ func HTTPUpload(import_url string, fh *os.File, state multistep.StateBag) (resul
logIteration := 0
err = InterruptibleWait{
Predicate: func() (bool, error) {
status, err := c.client.Task.GetStatus(c.session, task)
status, err := c.GetClient().Task.GetStatus(c.GetSessionRef(), task)
if err != nil {
return false, fmt.Errorf("Failed to get task status: %s", err.Error())
}
switch status {
case xsclient.TaskStatusTypePending:
progress, err := c.client.Task.GetProgress(c.session, task)
progress, err := c.GetClient().Task.GetProgress(c.GetSessionRef(), task)
if err != nil {
return false, fmt.Errorf("Failed to get progress: %s", err.Error())
}
@ -94,7 +95,7 @@ func HTTPUpload(import_url string, fh *os.File, state multistep.StateBag) (resul
case xsclient.TaskStatusTypeSuccess:
return true, nil
case xsclient.TaskStatusTypeFailure:
errorInfo, err := c.client.Task.GetErrorInfo(c.session, task)
errorInfo, err := c.GetClient().Task.GetErrorInfo(c.GetSessionRef(), task)
if err != nil {
errorInfo = []string{fmt.Sprintf("furthermore, failed to get error info: %s", err.Error())}
}
@ -116,7 +117,7 @@ func HTTPUpload(import_url string, fh *os.File, state multistep.StateBag) (resul
return
}
result, err = c.client.Task.GetResult(c.session, task)
result, err = c.GetClient().Task.GetResult(c.GetSessionRef(), task)
if err != nil {
err = fmt.Errorf("Error getting result: %s", err.Error())
return

View File

@ -3,6 +3,7 @@ package common
import (
"context"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"log"
"github.com/hashicorp/packer-plugin-sdk/multistep"
@ -19,7 +20,7 @@ type StepAttachVdi struct {
func (self *StepAttachVdi) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
log.Printf("Running attach vdi for key %s\n", self.VdiUuidKey)
var vdiUuid string
@ -31,20 +32,20 @@ func (self *StepAttachVdi) Run(ctx context.Context, state multistep.StateBag) mu
}
var err error
self.vdi, err = c.client.VDI.GetByUUID(c.session, vdiUuid)
self.vdi, err = c.GetClient().VDI.GetByUUID(c.GetSessionRef(), vdiUuid)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VDI from UUID '%s': %s", vdiUuid, err.Error()))
return multistep.ActionHalt
}
uuid := state.Get("instance_uuid").(string)
instance, err := c.client.VM.GetByUUID(c.session, uuid)
instance, err := c.GetClient().VM.GetByUUID(c.GetSessionRef(), uuid)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
return multistep.ActionHalt
}
err = ConnectVdi(c, instance, self.vdi, self.VdiType)
err = xen.ConnectVdi(c, instance, self.vdi, self.VdiType)
if err != nil {
ui.Error(fmt.Sprintf("Error attaching VDI '%s': '%s'", vdiUuid, err.Error()))
return multistep.ActionHalt
@ -57,7 +58,7 @@ func (self *StepAttachVdi) Run(ctx context.Context, state multistep.StateBag) mu
func (self *StepAttachVdi) Cleanup(state multistep.StateBag) {
config := state.Get("commonconfig").(CommonConfig)
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
if config.ShouldKeepVM(state) {
return
}
@ -67,7 +68,7 @@ func (self *StepAttachVdi) Cleanup(state multistep.StateBag) {
}
uuid := state.Get("instance_uuid").(string)
vmRef, err := c.client.VM.GetByUUID(c.session, uuid)
vmRef, err := c.GetClient().VM.GetByUUID(c.GetSessionRef(), uuid)
if err != nil {
log.Printf("Unable to get VM from UUID '%s': %s", uuid, err.Error())
return
@ -75,7 +76,7 @@ func (self *StepAttachVdi) Cleanup(state multistep.StateBag) {
vdiUuid := state.Get(self.VdiUuidKey).(string)
err = DisconnectVdi(c, vmRef, self.vdi)
err = xen.DisconnectVdi(c, vmRef, self.vdi)
if err != nil {
log.Printf("Unable to disconnect VDI '%s': %s", vdiUuid, err.Error())
return

View File

@ -3,6 +3,7 @@ package common
import (
"context"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
@ -11,13 +12,13 @@ import (
type StepBootWait struct{}
func (self *StepBootWait) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
config := state.Get("commonconfig").(CommonConfig)
ui := state.Get("ui").(packer.Ui)
instance, _ := c.client.VM.GetByUUID(c.session, state.Get("instance_uuid").(string))
instance, _ := c.GetClient().VM.GetByUUID(c.GetSessionRef(), state.Get("instance_uuid").(string))
ui.Say("Unpausing VM " + state.Get("instance_uuid").(string))
Unpause(c, instance)
xen.Unpause(c, instance)
if int64(config.BootWait) > 0 {
ui.Say(fmt.Sprintf("Waiting %s for boot...", config.BootWait))

View File

@ -3,6 +3,7 @@ package common
import (
"context"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"log"
"github.com/hashicorp/packer-plugin-sdk/multistep"
@ -15,7 +16,7 @@ type StepDetachVdi struct {
func (self *StepDetachVdi) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
var vdiUuid string
if vdiUuidRaw, ok := state.GetOk(self.VdiUuidKey); ok {
@ -25,20 +26,20 @@ func (self *StepDetachVdi) Run(ctx context.Context, state multistep.StateBag) mu
return multistep.ActionContinue
}
vdi, err := c.client.VDI.GetByUUID(c.session, vdiUuid)
vdi, err := c.GetClient().VDI.GetByUUID(c.GetSessionRef(), vdiUuid)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VDI from UUID '%s': %s", vdiUuid, err.Error()))
return multistep.ActionHalt
}
uuid := state.Get("instance_uuid").(string)
instance, err := c.client.VM.GetByUUID(c.session, uuid)
instance, err := c.GetClient().VM.GetByUUID(c.GetSessionRef(), uuid)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
return multistep.ActionHalt
}
err = DisconnectVdi(c, instance, vdi)
err = xen.DisconnectVdi(c, instance, vdi)
if err != nil {
ui.Error(fmt.Sprintf("Unable to detach VDI '%s': %s", vdiUuid, err.Error()))
//return multistep.ActionHalt

View File

@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"io"
"net"
"net/http"
@ -85,33 +86,33 @@ func downloadFile(url, filename string, ui packer.Ui) (err error) {
func (StepExport) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("commonconfig").(CommonConfig)
ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
instance_uuid := state.Get("instance_uuid").(string)
suffix := ".vhd"
extrauri := "&format=vhd"
instance, err := c.client.VM.GetByUUID(c.session, instance_uuid)
instance, err := c.GetClient().VM.GetByUUID(c.GetSessionRef(), instance_uuid)
if err != nil {
ui.Error(fmt.Sprintf("Could not get VM with UUID '%s': %s", instance_uuid, err.Error()))
return multistep.ActionHalt
}
if len(config.ExportNetworkNames) > 0 {
vifs, err := c.client.VM.GetVIFs(c.session, instance)
vifs, err := c.GetClient().VM.GetVIFs(c.GetSessionRef(), instance)
if err != nil {
ui.Error(fmt.Sprintf("Error occured getting VIFs: %s", err.Error()))
return multistep.ActionHalt
}
for _, vif := range vifs {
err := c.client.VIF.Destroy(c.session, vif)
err := c.GetClient().VIF.Destroy(c.GetSessionRef(), vif)
if err != nil {
ui.Error(fmt.Sprintf("Destroy vif fail: '%s': %s", vif, err.Error()))
return multistep.ActionHalt
}
}
for i, networkNameLabel := range config.ExportNetworkNames {
networks, err := c.client.Network.GetByNameLabel(c.session, networkNameLabel)
networks, err := c.GetClient().Network.GetByNameLabel(c.GetSessionRef(), networkNameLabel)
if err != nil {
ui.Error(fmt.Sprintf("Error occured getting Network by name-label: %s", err.Error()))
@ -129,7 +130,7 @@ func (StepExport) Run(ctx context.Context, state multistep.StateBag) multistep.S
//we need the VIF index string
vifIndexString := fmt.Sprintf("%d", i)
_, err = ConnectNetwork(c, networks[0], instance, vifIndexString)
_, err = xen.ConnectNetwork(c, networks[0], instance, vifIndexString)
if err != nil {
ui.Say(err.Error())
@ -197,27 +198,27 @@ func (StepExport) Run(ctx context.Context, state multistep.StateBag) multistep.S
case "vdi_vhd":
// export the disks
disks, err := GetDisks(c, instance)
disks, err := xen.GetDisks(c, instance)
if err != nil {
ui.Error(fmt.Sprintf("Could not get VM disks: %s", err.Error()))
return multistep.ActionHalt
}
for _, disk := range disks {
disk_uuid, err := c.client.VDI.GetUUID(c.session, disk)
disk_uuid, err := c.GetClient().VDI.GetUUID(c.GetSessionRef(), disk)
if err != nil {
ui.Error(fmt.Sprintf("Could not get disk with UUID '%s': %s", disk_uuid, err.Error()))
return multistep.ActionHalt
}
// Work out XenServer version
hosts, err := c.client.Host.GetAll(c.session)
hosts, err := c.GetClient().Host.GetAll(c.GetSessionRef())
if err != nil {
ui.Error(fmt.Sprintf("Could not retrieve hosts in the pool: %s", err.Error()))
return multistep.ActionHalt
}
host := hosts[0]
host_software_versions, err := c.client.Host.GetSoftwareVersion(c.session, host)
host_software_versions, err := c.GetClient().Host.GetSoftwareVersion(c.GetSessionRef(), host)
xs_version := host_software_versions["product_version"]
if err != nil {
@ -231,7 +232,7 @@ func (StepExport) Run(ctx context.Context, state multistep.StateBag) multistep.S
if xs_version <= "6.5.0" && config.Format == "vdi_vhd" {
// Export the VHD using a Transfer VM
disk_export_url, err = Expose(c, disk, "vhd")
disk_export_url, err = xen.Expose(c, disk, "vhd")
if err != nil {
ui.Error(fmt.Sprintf("Failed to expose disk %s: %s", disk_uuid, err.Error()))
@ -264,7 +265,7 @@ func (StepExport) Run(ctx context.Context, state multistep.StateBag) multistep.S
// Call unexpose in case a TVM was used. The call is harmless
// if that is not the case.
Unexpose(c, disk)
xen.Unexpose(c, disk)
}

View File

@ -3,6 +3,7 @@ package common
import (
"context"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
@ -16,14 +17,14 @@ type StepFindVdi struct {
func (self *StepFindVdi) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
// Ignore if VdiName is not specified
if self.VdiName == "" {
return multistep.ActionContinue
}
vdis, err := c.client.VDI.GetByNameLabel(c.session, self.VdiName)
vdis, err := c.GetClient().VDI.GetByNameLabel(c.GetSessionRef(), self.VdiName)
switch {
case len(vdis) == 0:
@ -36,7 +37,7 @@ func (self *StepFindVdi) Run(ctx context.Context, state multistep.StateBag) mult
vdi := vdis[0]
vdiUuid, err := c.client.VDI.GetUUID(c.session, vdi)
vdiUuid, err := c.GetClient().VDI.GetUUID(c.GetSessionRef(), vdi)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get UUID of VDI '%s': %s", self.VdiName, err.Error()))
return multistep.ActionHalt

View File

@ -6,6 +6,7 @@ import (
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/proxy"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"net"
"strconv"
)
@ -16,7 +17,7 @@ type StepGetVNCPort struct {
func (self *StepGetVNCPort) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
xenClient := state.Get("client").(*Connection)
xenClient := state.Get("client").(*xen.Connection)
xenProxy := state.Get("xen_proxy").(proxy.XenProxy)
config := state.Get("commonconfig").(CommonConfig)

View File

@ -3,6 +3,7 @@ package common
import (
"context"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
@ -12,24 +13,24 @@ type StepSetVmHostSshAddress struct{}
func (self *StepSetVmHostSshAddress) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
ui := state.Get("ui").(packer.Ui)
ui.Say("Step: Set SSH address to VM host IP")
uuid := state.Get("instance_uuid").(string)
instance, err := c.client.VM.GetByUUID(c.session, uuid)
instance, err := c.GetClient().VM.GetByUUID(c.GetSessionRef(), uuid)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
return multistep.ActionHalt
}
host, err := c.client.VM.GetResidentOn(c.session, instance)
host, err := c.GetClient().VM.GetResidentOn(c.GetSessionRef(), instance)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM Host for VM '%s': %s", uuid, err.Error()))
}
address, err := c.client.Host.GetAddress(c.session, host)
address, err := c.GetClient().Host.GetAddress(c.GetSessionRef(), host)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get address from VM Host: %s", err.Error()))
}

View File

@ -3,6 +3,7 @@ package common
import (
"context"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
@ -12,16 +13,16 @@ type StepSetVmToTemplate struct{}
func (StepSetVmToTemplate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
instance_uuid := state.Get("instance_uuid").(string)
instance, err := c.client.VM.GetByUUID(c.session, instance_uuid)
instance, err := c.GetClient().VM.GetByUUID(c.GetSessionRef(), instance_uuid)
if err != nil {
ui.Error(fmt.Sprintf("Could not get VM with UUID '%s': %s", instance_uuid, err.Error()))
return multistep.ActionHalt
}
err = c.client.VM.SetIsATemplate(c.session, instance, true)
err = c.GetClient().VM.SetIsATemplate(c.GetSessionRef(), instance, true)
if err != nil {
ui.Error(fmt.Sprintf("failed to set VM '%s' as a template with error: %v", instance_uuid, err))

View File

@ -3,6 +3,7 @@ package common
import (
"context"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"log"
"time"
@ -16,10 +17,10 @@ type StepShutdown struct{}
func (StepShutdown) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("commonconfig").(CommonConfig)
ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
instance_uuid := state.Get("instance_uuid").(string)
instance, err := c.client.VM.GetByUUID(c.session, instance_uuid)
instance, err := c.GetClient().VM.GetByUUID(c.GetSessionRef(), instance_uuid)
if err != nil {
ui.Error(fmt.Sprintf("Could not get VM with UUID '%s': %s", instance_uuid, err.Error()))
return multistep.ActionHalt
@ -45,7 +46,7 @@ func (StepShutdown) Run(ctx context.Context, state multistep.StateBag) multistep
err = InterruptibleWait{
Predicate: func() (bool, error) {
power_state, err := c.client.VM.GetPowerState(c.session, instance)
power_state, err := c.GetClient().VM.GetPowerState(c.GetSessionRef(), instance)
return power_state == xenapi.VMPowerStateHalted, err
},
PredicateInterval: 5 * time.Second,
@ -60,7 +61,7 @@ func (StepShutdown) Run(ctx context.Context, state multistep.StateBag) multistep
} else {
ui.Message("Attempting to cleanly shutdown the VM...")
err = c.client.VM.CleanShutdown(c.session, instance)
err = c.GetClient().VM.CleanShutdown(c.GetSessionRef(), instance)
if err != nil {
ui.Error(fmt.Sprintf("Could not shut down VM: %s", err.Error()))
return false
@ -72,7 +73,7 @@ func (StepShutdown) Run(ctx context.Context, state multistep.StateBag) multistep
if !success {
ui.Say("WARNING: Forcing hard shutdown of the VM...")
err = c.client.VM.HardShutdown(c.session, instance)
err = c.GetClient().VM.HardShutdown(c.GetSessionRef(), instance)
if err != nil {
ui.Error(fmt.Sprintf("Could not hard shut down VM -- giving up: %s", err.Error()))
return multistep.ActionHalt

View File

@ -2,6 +2,7 @@ package common
import (
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"log"
"time"
@ -24,19 +25,19 @@ type StepStartOnHIMN struct{}
func (self *StepStartOnHIMN) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
ui.Say("Step: Start VM on the Host Internal Mangement Network")
uuid := state.Get("instance_uuid").(string)
instance, err := c.client.VM.GetByUUID(c.session, uuid)
instance, err := c.GetClient().VM.GetByUUID(c.GetSessionRef(), uuid)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
return multistep.ActionHalt
}
// Find the HIMN Ref
networks, err := c.client.Network.GetByNameLabel(c.session, "Host internal management network")
networks, err := c.GetClient().Network.GetByNameLabel(c.GetSessionRef(), "Host internal management network")
if err != nil || len(networks) == 0 {
ui.Error("Unable to find a host internal management network")
ui.Error(err.Error())
@ -46,7 +47,7 @@ func (self *StepStartOnHIMN) Run(state multistep.StateBag) multistep.StepAction
himn := networks[0]
// Create a VIF for the HIMN
himn_vif, err := ConnectNetwork(c, himn, instance, "0")
himn_vif, err := xen.ConnectNetwork(c, himn, instance, "0")
if err != nil {
ui.Error("Error creating VIF")
ui.Error(err.Error())
@ -54,14 +55,14 @@ func (self *StepStartOnHIMN) Run(state multistep.StateBag) multistep.StepAction
}
// Start the VM
c.client.VM.Start(c.session, instance, false, false)
c.GetClient().VM.Start(c.GetSessionRef(), instance, false, false)
var himn_iface_ip string = ""
// Obtain the allocated IP
err = InterruptibleWait{
Predicate: func() (found bool, err error) {
ips, err := c.client.Network.GetAssignedIps(c.session, himn)
ips, err := c.GetClient().Network.GetAssignedIps(c.GetSessionRef(), himn)
if err != nil {
return false, fmt.Errorf("Can't get assigned IPs: %s", err.Error())
}

View File

@ -3,6 +3,7 @@ package common
import (
"context"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
@ -14,40 +15,40 @@ type StepStartVmPaused struct {
func (self *StepStartVmPaused) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(Config)
ui.Say("Step: Start VM Paused")
uuid := state.Get("instance_uuid").(string)
instance, err := c.client.VM.GetByUUID(c.session, uuid)
instance, err := c.GetClient().VM.GetByUUID(c.GetSessionRef(), uuid)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
return multistep.ActionHalt
}
// note that here "cd" means boot from hard drive ('c') first, then CDROM ('d')
err = c.client.VM.SetHVMBootPolicy(c.session, instance, "BIOS order")
err = c.GetClient().VM.SetHVMBootPolicy(c.GetSessionRef(), instance, "BIOS order")
if err != nil {
ui.Error(fmt.Sprintf("Unable to set HVM boot params: %s", err.Error()))
return multistep.ActionHalt
}
err = c.client.VM.SetHVMBootParams(c.session, instance, map[string]string{"order": "cd", "firmware": config.Firmware})
err = c.GetClient().VM.SetHVMBootParams(c.GetSessionRef(), instance, map[string]string{"order": "cd", "firmware": config.Firmware})
if err != nil {
ui.Error(fmt.Sprintf("Unable to set HVM boot params: %s", err.Error()))
return multistep.ActionHalt
}
err = c.client.VM.Start(c.session, instance, true, false)
err = c.GetClient().VM.Start(c.GetSessionRef(), instance, 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 := c.client.VM.GetDomid(c.session, instance)
domid, err := c.GetClient().VM.GetDomid(c.GetSessionRef(), instance)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get domid of VM with UUID '%s': %s", uuid, err.Error()))
return multistep.ActionHalt

View File

@ -3,6 +3,7 @@ package common
import (
"context"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"log"
"net"
"os"
@ -23,7 +24,7 @@ type StepUploadVdi struct {
func (self *StepUploadVdi) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("commonconfig").(CommonConfig)
ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
imagePath := self.ImagePathFunc()
vdiName := self.VdiNameFunc()
@ -35,7 +36,7 @@ func (self *StepUploadVdi) Run(ctx context.Context, state multistep.StateBag) mu
ui.Say(fmt.Sprintf("Step: Upload VDI '%s'", vdiName))
// Create VDI for the image
srs, err := c.client.SR.GetAll(c.session)
srs, err := c.GetClient().SR.GetAll(c.GetSessionRef())
ui.Say(fmt.Sprintf("Step: Found SRs '%v'", srs))
sr, err := config.GetISOSR(c)
@ -62,7 +63,7 @@ func (self *StepUploadVdi) Run(ctx context.Context, state multistep.StateBag) mu
// Create the VDI
// vdi, err := sr.CreateVdi(vdiName, fileLength)
vdi, err := c.client.VDI.Create(c.session, xenapi.VDIRecord{
vdi, err := c.GetClient().VDI.Create(c.GetSessionRef(), xenapi.VDIRecord{
NameLabel: vdiName,
VirtualSize: int(fileLength),
Type: "user",
@ -78,7 +79,7 @@ func (self *StepUploadVdi) Run(ctx context.Context, state multistep.StateBag) mu
return multistep.ActionHalt
}
vdiUuid, err := c.client.VDI.GetUUID(c.session, vdi)
vdiUuid, err := c.GetClient().VDI.GetUUID(c.GetSessionRef(), vdi)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get UUID of VDI '%s': %s", vdiName, err.Error()))
return multistep.ActionHalt
@ -101,7 +102,7 @@ func (self *StepUploadVdi) Run(ctx context.Context, state multistep.StateBag) mu
func (self *StepUploadVdi) Cleanup(state multistep.StateBag) {
config := state.Get("commonconfig").(CommonConfig)
ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
vdiName := self.VdiNameFunc()
@ -121,7 +122,7 @@ func (self *StepUploadVdi) Cleanup(state multistep.StateBag) {
return
}
vdi, err := c.client.VDI.GetByUUID(c.session, vdiUuid)
vdi, err := c.GetClient().VDI.GetByUUID(c.GetSessionRef(), vdiUuid)
if err != nil {
ui.Error(fmt.Sprintf("Can't get VDI '%s': %s", vdiUuid, err.Error()))
return
@ -131,7 +132,7 @@ func (self *StepUploadVdi) Cleanup(state multistep.StateBag) {
// so try several times
for i := 0; i < 3; i++ {
log.Printf("Trying to destroy VDI...")
err = c.client.VDI.Destroy(c.session, vdi)
err = c.GetClient().VDI.Destroy(c.GetSessionRef(), vdi)
if err == nil {
break
}

View File

@ -3,6 +3,7 @@ package common
import (
"context"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"time"
"github.com/hashicorp/packer-plugin-sdk/multistep"
@ -17,7 +18,7 @@ type StepWaitForIP struct {
func (self *StepWaitForIP) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
config := state.Get("commonconfig").(CommonConfig)
// Respect static configuration
@ -29,7 +30,7 @@ func (self *StepWaitForIP) Run(ctx context.Context, state multistep.StateBag) mu
ui.Say("Step: Wait for VM's IP to become known to us.")
uuid := state.Get("instance_uuid").(string)
instance, err := c.client.VM.GetByUUID(c.session, uuid)
instance, err := c.GetClient().VM.GetByUUID(c.GetSessionRef(), uuid)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
return multistep.ActionHalt
@ -56,12 +57,12 @@ func (self *StepWaitForIP) Run(ctx context.Context, state multistep.StateBag) mu
if config.IPGetter == "auto" || config.IPGetter == "tools" {
// Look for PV IP
m, err := c.client.VM.GetGuestMetrics(c.session, instance)
m, err := c.GetClient().VM.GetGuestMetrics(c.GetSessionRef(), instance)
if err != nil {
return false, err
}
if m != "" {
metrics, err := c.client.VMGuestMetrics.GetRecord(c.session, m)
metrics, err := c.GetClient().VMGuestMetrics.GetRecord(c.GetSessionRef(), m)
if err != nil {
return false, err
}

View File

@ -2,6 +2,7 @@ package common
import (
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"log"
"github.com/hashicorp/packer-plugin-sdk/multistep"
@ -11,20 +12,20 @@ type VmCleanup struct{}
func (self *VmCleanup) Cleanup(state multistep.StateBag) {
config := state.Get("commonconfig").(CommonConfig)
c := state.Get("client").(*Connection)
c := state.Get("client").(*xen.Connection)
if config.ShouldKeepVM(state) {
return
}
uuid := state.Get("instance_uuid").(string)
instance, err := c.client.VM.GetByUUID(c.session, uuid)
instance, err := c.GetClient().VM.GetByUUID(c.GetSessionRef(), uuid)
if err != nil {
log.Printf(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
return
}
err = c.client.VM.HardShutdown(c.session, instance)
err = c.GetClient().VM.HardShutdown(c.GetSessionRef(), instance)
if err != nil {
log.Printf(fmt.Sprintf("Unable to force shutdown VM '%s': %s", uuid, err.Error()))
}

View File

@ -7,6 +7,7 @@ import (
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/mitchellh/go-vnc"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/proxy"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"io"
"log"
"net"
@ -16,10 +17,10 @@ import (
)
func GetVNCConsoleLocation(state multistep.StateBag) (string, error) {
xenClient := state.Get("client").(*Connection)
xenClient := state.Get("client").(*xen.Connection)
config := state.Get("commonconfig").(CommonConfig)
vmRef, err := xenClient.client.VM.GetByNameLabel(xenClient.session, config.VMName)
vmRef, err := xenClient.GetClient().VM.GetByNameLabel(xenClient.GetSessionRef(), config.VMName)
if err != nil {
return "", err
@ -29,7 +30,7 @@ func GetVNCConsoleLocation(state multistep.StateBag) (string, error) {
return "", fmt.Errorf("expected to find a single VM, instead found '%d'. Ensure the VM name is unique", len(vmRef))
}
consoles, err := xenClient.client.VM.GetConsoles(xenClient.session, vmRef[0])
consoles, err := xenClient.GetClient().VM.GetConsoles(xenClient.GetSessionRef(), vmRef[0])
if err != nil {
return "", err
@ -39,7 +40,7 @@ func GetVNCConsoleLocation(state multistep.StateBag) (string, error) {
return "", fmt.Errorf("expected to find a VM console, instead found '%d'. Ensure there is only one console", len(consoles))
}
location, err := xenClient.client.Console.GetLocation(xenClient.session, consoles[0])
location, err := xenClient.GetClient().Console.GetLocation(xenClient.GetSessionRef(), consoles[0])
if err != nil {
return "", err
@ -49,7 +50,7 @@ func GetVNCConsoleLocation(state multistep.StateBag) (string, error) {
}
func CreateVNCConnection(state multistep.StateBag, location string) (net.Conn, error) {
xenClient := state.Get("client").(*Connection)
xenClient := state.Get("client").(*xen.Connection)
xenProxy := state.Get("xen_proxy").(proxy.XenProxy)
target, err := GetTcpAddressFromURL(location)

View File

@ -1,4 +1,4 @@
package common
package xen
import (
"encoding/xml"

View File

@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"path"
"strings"
"time"
@ -159,7 +160,7 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, warns []stri
}
func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
c, err := xscommon.NewXenAPIClient(self.config.HostIp, self.config.HostPort, self.config.Username, self.config.Password)
c, err := xen.NewXenAPIClient(self.config.HostIp, self.config.HostPort, self.config.Username, self.config.Password)
if err != nil {
return nil, err

View File

@ -3,6 +3,7 @@ package iso
import (
"context"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"log"
"github.com/hashicorp/packer-plugin-sdk/multistep"
@ -19,7 +20,7 @@ type stepCreateInstance struct {
func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
c := state.Get("client").(*xscommon.Connection)
c := state.Get("client").(*xen.Connection)
config := state.Get("config").(xscommon.Config)
ui := state.Get("ui").(packer.Ui)
@ -136,7 +137,7 @@ func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBa
self.vdis[i] = &vdi
err = xscommon.ConnectVdi(c, instance, vdi, xsclient.VbdTypeDisk)
err = xen.ConnectVdi(c, instance, vdi, xsclient.VbdTypeDisk)
if err != nil {
ui.Error(fmt.Sprintf("Unable to connect packer disk VDI: %s", err.Error()))
return multistep.ActionHalt
@ -177,7 +178,7 @@ func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBa
}
log.Printf("Creating VIF on network '%s' on VM '%s'\n", network, instance)
_, err = xscommon.ConnectNetwork(c, network, instance, "0")
_, err = xen.ConnectNetwork(c, network, instance, "0")
if err != nil {
ui.Error(fmt.Sprintf("Failed to create VIF with error: %v", err))
@ -206,7 +207,7 @@ func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBa
//we need the VIF index string
vifIndexString := fmt.Sprintf("%d", i)
_, err = xscommon.ConnectNetwork(c, networks[0], instance, vifIndexString)
_, err = xen.ConnectNetwork(c, networks[0], instance, vifIndexString)
if err != nil {
ui.Say(fmt.Sprintf("Failed to connect VIF with error: %v", err.Error()))
@ -233,7 +234,7 @@ func (self *stepCreateInstance) Cleanup(state multistep.StateBag) {
}
ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*xscommon.Connection)
c := state.Get("client").(*xen.Connection)
if self.instance != nil {
ui.Say("Destroying VM")

View File

@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"time"
"github.com/hashicorp/hcl/v2/hcldec"
@ -89,7 +90,7 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, warns []stri
func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
//Setup XAPI client
c, err := xscommon.NewXenAPIClient(self.config.HostIp, self.config.HostPort, self.config.Username, self.config.Password)
c, err := xen.NewXenAPIClient(self.config.HostIp, self.config.HostPort, self.config.Username, self.config.Password)
if err != nil {
return nil, err

View File

@ -3,6 +3,7 @@ package xva
import (
"context"
"fmt"
"github.com/xenserver/packer-builder-xenserver/builder/xenserver/common/xen"
"net"
"os"
"strconv"
@ -20,7 +21,7 @@ type stepImportInstance struct {
func (self *stepImportInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
c := state.Get("client").(*xscommon.Connection)
c := state.Get("client").(*xen.Connection)
config := state.Get("config").(xscommon.Config)
ui := state.Get("ui").(packer.Ui)