Ensure that SRs are set via the config file and fix other todos

This commit is contained in:
Dom Del Nano 2020-12-22 14:27:02 -08:00
parent 70adca2259
commit b2a858fe0f
5 changed files with 43 additions and 26 deletions

View File

@ -21,6 +21,7 @@ type CommonConfig struct {
VMName string `mapstructure:"vm_name"` VMName string `mapstructure:"vm_name"`
VMDescription string `mapstructure:"vm_description"` VMDescription string `mapstructure:"vm_description"`
SrName string `mapstructure:"sr_name"` SrName string `mapstructure:"sr_name"`
SrISOName string `mapstructure:"sr_iso_name"`
FloppyFiles []string `mapstructure:"floppy_files"` FloppyFiles []string `mapstructure:"floppy_files"`
NetworkNames []string `mapstructure:"network_names"` NetworkNames []string `mapstructure:"network_names"`
ExportNetworkNames []string `mapstructure:"export_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) { func (config CommonConfig) GetSR(c *Connection) (xenapi.SRRef, error) {
var srRecord xenapi.SRRecord var srRef xenapi.SRRef
if config.SrName == "" { if config.SrName == "" {
hostRef, err := c.GetClient().Session.GetThisHost(c.session, c.session) hostRef, err := c.GetClient().Session.GetThisHost(c.session, c.session)
if err != nil { if err != nil {
return srRecord, err return srRef, err
} }
pools, err := c.GetClient().Pool.GetAllRecords(c.session) pools, err := c.GetClient().Pool.GetAllRecords(c.session)
if err != nil { if err != nil {
return srRecord, err return srRef, err
} }
for _, pool := range pools { for _, pool := range pools {
if pool.Master == hostRef { 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 { } else {
// Use the provided name label to find the SR to use // 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.session, config.SrName)
if err != nil { if err != nil {
return srRecord, err return srRef, err
} }
switch { switch {
case len(srs) == 0: 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: 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
} }
} }

View File

@ -61,7 +61,6 @@ func (self *StepAttachVdi) Cleanup(state multistep.StateBag) {
return return
} }
// TODO: What the fuck does this mean?
if self.vdi == "" { if self.vdi == "" {
return return
} }

View File

@ -18,6 +18,7 @@ type StepUploadVdi struct {
} }
func (self *StepUploadVdi) Run(state multistep.StateBag) multistep.StepAction { func (self *StepUploadVdi) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("commonconfig").(CommonConfig)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*Connection) 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) srs, err := c.client.SR.GetAll(c.session)
ui.Say(fmt.Sprintf("Step: Found SRs '%v'", srs)) ui.Say(fmt.Sprintf("Step: Found SRs '%v'", srs))
// TODO (ddelnano): This must be changed to match the ISO Storage repository available sr, err := config.GetISOSR(c)
nameLabel := "LocalISO"
// nameLabel := "ISOs"
srs, err = c.client.SR.GetByNameLabel(c.session, nameLabel)
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 { 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 return multistep.ActionHalt
} }

View File

@ -1,9 +1,10 @@
package iso package iso
import ( import (
"github.com/mitchellh/packer/packer"
"reflect" "reflect"
"testing" "testing"
"github.com/mitchellh/packer/packer"
) )
func testConfig() map[string]interface{} { func testConfig() map[string]interface{} {
@ -90,7 +91,7 @@ func TestBuilderPrepare_DiskSize(t *testing.T) {
} }
if b.config.DiskSize != 60000 { if b.config.DiskSize != 60000 {
t.Fatalf("bad size: %s", b.config.DiskSize) t.Fatalf("bad size: %d", b.config.DiskSize)
} }
} }

View File

@ -108,7 +108,7 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi
return multistep.ActionHalt 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{ vdi, err := c.GetClient().VDI.Create(c.GetSessionRef(), xenapi.VDIRecord{
NameLabel: "Packer-disk", NameLabel: "Packer-disk",
@ -116,7 +116,7 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi
Type: "user", Type: "user",
Sharable: false, Sharable: false,
ReadOnly: false, ReadOnly: false,
SR: xenapi.SRRef(sr.UUID), SR: sr,
OtherConfig: map[string]string{ OtherConfig: map[string]string{
"temp": "temp", "temp": "temp",
}, },