From b2a858fe0f64a3e5ff50f9140eb3e8bf1357a728 Mon Sep 17 00:00:00 2001 From: Dom Del Nano Date: Tue, 22 Dec 2020 14:27:02 -0800 Subject: [PATCH] Ensure that SRs are set via the config file and fix other todos --- builder/xenserver/common/common_config.go | 45 ++++++++++++++----- builder/xenserver/common/step_attach_vdi.go | 1 - builder/xenserver/common/step_upload_vdi.go | 14 ++---- builder/xenserver/iso/builder_test.go | 5 ++- builder/xenserver/iso/step_create_instance.go | 4 +- 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/builder/xenserver/common/common_config.go b/builder/xenserver/common/common_config.go index c372206..42875a1 100644 --- a/builder/xenserver/common/common_config.go +++ b/builder/xenserver/common/common_config.go @@ -21,6 +21,7 @@ type CommonConfig struct { VMName string `mapstructure:"vm_name"` VMDescription string `mapstructure:"vm_description"` SrName string `mapstructure:"sr_name"` + SrISOName string `mapstructure:"sr_iso_name"` FloppyFiles []string `mapstructure:"floppy_files"` NetworkNames []string `mapstructure:"network_names"` ExportNetworkNames []string `mapstructure:"export_network_names"` @@ -222,44 +223,68 @@ func (c CommonConfig) ShouldKeepVM(state multistep.StateBag) bool { } } -func (config CommonConfig) GetSR(c *Connection) (xenapi.SRRecord, error) { - var srRecord xenapi.SRRecord +func (config CommonConfig) GetSR(c *Connection) (xenapi.SRRef, error) { + var srRef xenapi.SRRef if config.SrName == "" { hostRef, err := c.GetClient().Session.GetThisHost(c.session, c.session) if err != nil { - return srRecord, err + return srRef, err } pools, err := c.GetClient().Pool.GetAllRecords(c.session) if err != nil { - return srRecord, err + return srRef, err } for _, pool := range pools { if pool.Master == hostRef { - return c.GetClient().SR.GetRecord(c.session, pool.DefaultSR) + return pool.DefaultSR, nil } } - return srRecord, errors.New(fmt.Sprintf("failed to find default SR on host '%s'", hostRef)) + return srRef, errors.New(fmt.Sprintf("failed to find default SR on host '%s'", hostRef)) } else { // Use the provided name label to find the SR to use srs, err := c.GetClient().SR.GetByNameLabel(c.session, config.SrName) if err != nil { - return srRecord, err + return srRef, err } switch { case len(srs) == 0: - return srRecord, fmt.Errorf("Couldn't find a SR with the specified name-label '%s'", config.SrName) + return srRef, fmt.Errorf("Couldn't find a SR with the specified name-label '%s'", config.SrName) case len(srs) > 1: - return srRecord, fmt.Errorf("Found more than one SR with the name '%s'. The name must be unique", config.SrName) + return srRef, fmt.Errorf("Found more than one SR with the name '%s'. The name must be unique", config.SrName) } - return c.GetClient().SR.GetRecord(c.session, srs[0]) + return srs[0], nil + } +} + +func (config CommonConfig) GetISOSR(c *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) + + if err != nil { + return srRef, err + } + + switch { + case len(srs) == 0: + return srRef, fmt.Errorf("Couldn't find a SR with the specified name-label '%s'", config.SrName) + case len(srs) > 1: + return srRef, fmt.Errorf("Found more than one SR with the name '%s'. The name must be unique", config.SrName) + } + + return srs[0], nil } } diff --git a/builder/xenserver/common/step_attach_vdi.go b/builder/xenserver/common/step_attach_vdi.go index a209c88..4da1074 100644 --- a/builder/xenserver/common/step_attach_vdi.go +++ b/builder/xenserver/common/step_attach_vdi.go @@ -61,7 +61,6 @@ func (self *StepAttachVdi) Cleanup(state multistep.StateBag) { return } - // TODO: What the fuck does this mean? if self.vdi == "" { return } diff --git a/builder/xenserver/common/step_upload_vdi.go b/builder/xenserver/common/step_upload_vdi.go index 0b11779..b5acfd2 100644 --- a/builder/xenserver/common/step_upload_vdi.go +++ b/builder/xenserver/common/step_upload_vdi.go @@ -18,6 +18,7 @@ type StepUploadVdi struct { } func (self *StepUploadVdi) Run(state multistep.StateBag) multistep.StepAction { + config := state.Get("commonconfig").(CommonConfig) ui := state.Get("ui").(packer.Ui) c := state.Get("client").(*Connection) @@ -34,19 +35,10 @@ func (self *StepUploadVdi) Run(state multistep.StateBag) multistep.StepAction { srs, err := c.client.SR.GetAll(c.session) ui.Say(fmt.Sprintf("Step: Found SRs '%v'", srs)) - // TODO (ddelnano): This must be changed to match the ISO Storage repository available - nameLabel := "LocalISO" - // nameLabel := "ISOs" - srs, err = c.client.SR.GetByNameLabel(c.session, nameLabel) + sr, err := config.GetISOSR(c) - if len(srs) != 1 { - ui.Error(fmt.Sprintf("expected to find a single storage repository with name '%s', instead found '%d' storage repositories", nameLabel, len(srs))) - } - - sr := srs[0] - ui.Say(fmt.Sprintf("Step: Found SRs '%v' Choosing: '%v'", srs, sr)) if err != nil { - ui.Error(fmt.Sprintf("Unable to get SR: %s", err.Error())) + ui.Error(fmt.Sprintf("Unable to get SR: %v", err)) return multistep.ActionHalt } diff --git a/builder/xenserver/iso/builder_test.go b/builder/xenserver/iso/builder_test.go index 1a7a19b..21cd065 100644 --- a/builder/xenserver/iso/builder_test.go +++ b/builder/xenserver/iso/builder_test.go @@ -1,9 +1,10 @@ package iso import ( - "github.com/mitchellh/packer/packer" "reflect" "testing" + + "github.com/mitchellh/packer/packer" ) func testConfig() map[string]interface{} { @@ -90,7 +91,7 @@ func TestBuilderPrepare_DiskSize(t *testing.T) { } if b.config.DiskSize != 60000 { - t.Fatalf("bad size: %s", b.config.DiskSize) + t.Fatalf("bad size: %d", b.config.DiskSize) } } diff --git a/builder/xenserver/iso/step_create_instance.go b/builder/xenserver/iso/step_create_instance.go index b11c307..2b39a87 100644 --- a/builder/xenserver/iso/step_create_instance.go +++ b/builder/xenserver/iso/step_create_instance.go @@ -108,7 +108,7 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi return multistep.ActionHalt } - ui.Say(fmt.Sprintf("Using the following SR for the VM: %s", sr.NameLabel)) + ui.Say(fmt.Sprintf("Using the following SR for the VM: %s", sr)) vdi, err := c.GetClient().VDI.Create(c.GetSessionRef(), xenapi.VDIRecord{ NameLabel: "Packer-disk", @@ -116,7 +116,7 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi Type: "user", Sharable: false, ReadOnly: false, - SR: xenapi.SRRef(sr.UUID), + SR: sr, OtherConfig: map[string]string{ "temp": "temp", },