From d8294d3ccd6ec35d56417ec62adb45967bd9f67a Mon Sep 17 00:00:00 2001 From: Heinrich Kruger Date: Wed, 25 May 2022 15:21:38 +0100 Subject: [PATCH 1/3] Use default SR if `sr_iso_name` is not specified --- builder/xenserver/common/common_config.go | 49 +++++++++++++---------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/builder/xenserver/common/common_config.go b/builder/xenserver/common/common_config.go index 4ae5edd..4b8e46e 100644 --- a/builder/xenserver/common/common_config.go +++ b/builder/xenserver/common/common_config.go @@ -223,29 +223,11 @@ func (c CommonConfig) ShouldKeepVM(state multistep.StateBag) bool { } 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 srRef, err - } - - pools, err := c.GetClient().Pool.GetAllRecords(c.session) - - if err != nil { - return srRef, err - } - - for _, pool := range pools { - if pool.Master == hostRef { - return pool.DefaultSR, nil - } - } - - return srRef, errors.New(fmt.Sprintf("failed to find default SR on host '%s'", hostRef)) - + return getDefaultSR(c) } else { + var srRef xenapi.SRRef + // Use the provided name label to find the SR to use srs, err := c.GetClient().SR.GetByNameLabel(c.session, config.SrName) @@ -267,7 +249,7 @@ func (config CommonConfig) GetSR(c *Connection) (xenapi.SRRef, error) { 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") + return getDefaultSR(c) } else { // Use the provided name label to find the SR to use @@ -287,3 +269,26 @@ func (config CommonConfig) GetISOSR(c *Connection) (xenapi.SRRef, error) { return srs[0], nil } } + +func getDefaultSR(c *Connection) (xenapi.SRRef, error) { + var srRef xenapi.SRRef + hostRef, err := c.GetClient().Session.GetThisHost(c.session, c.session) + + if err != nil { + return srRef, err + } + + pools, err := c.GetClient().Pool.GetAllRecords(c.session) + + if err != nil { + return srRef, err + } + + for _, pool := range pools { + if pool.Master == hostRef { + return pool.DefaultSR, nil + } + } + + return srRef, errors.New(fmt.Sprintf("failed to find default SR on host '%s'", hostRef)) +} From 70fd83e8033752682358aa0bf1869344fe663986 Mon Sep 17 00:00:00 2001 From: Heinrich Kruger Date: Thu, 26 May 2022 10:15:40 +0100 Subject: [PATCH 2/3] Work around XenAPI compatibility issue to get default SR --- builder/xenserver/common/common_config.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/builder/xenserver/common/common_config.go b/builder/xenserver/common/common_config.go index 4b8e46e..6a5105c 100644 --- a/builder/xenserver/common/common_config.go +++ b/builder/xenserver/common/common_config.go @@ -272,21 +272,29 @@ func (config CommonConfig) GetISOSR(c *Connection) (xenapi.SRRef, error) { func getDefaultSR(c *Connection) (xenapi.SRRef, error) { var srRef xenapi.SRRef - hostRef, err := c.GetClient().Session.GetThisHost(c.session, c.session) + client := c.GetClient() + hostRef, err := client.Session.GetThisHost(c.session, c.session) if err != nil { return srRef, err } - pools, err := c.GetClient().Pool.GetAllRecords(c.session) + // The current version of the go-xen-api-client does not fully support XenAPI version 8.2 + // In particular, some values for the pool `allowed_operations` are not recognised, resulting + // in a parse error when retrieving pool records. As a workaround, we only fetch pool refs. + pool_refs, err := client.Pool.GetAll(c.session) if err != nil { return srRef, err } - for _, pool := range pools { - if pool.Master == hostRef { - return pool.DefaultSR, nil + for _, pool_ref := range pool_refs { + pool_master, err := client.Pool.GetMaster(c.session, pool_ref) + if err != nil { + return srRef, err + } + if pool_master == hostRef { + return client.Pool.GetDefaultSR(c.session, pool_ref) } } From 3c5fc29b364079f8b09d26de3da9935007856421 Mon Sep 17 00:00:00 2001 From: Dom Del Nano Date: Fri, 14 Apr 2023 00:02:58 -0700 Subject: [PATCH 3/3] Ensure that SrISOName is no longer a required field --- builder/xenserver/common/common_config.go | 2 +- builder/xenserver/common/config.hcl2spec.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/builder/xenserver/common/common_config.go b/builder/xenserver/common/common_config.go index 6a5105c..bf63ae2 100644 --- a/builder/xenserver/common/common_config.go +++ b/builder/xenserver/common/common_config.go @@ -20,7 +20,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"` + SrISOName string `mapstructure:"sr_iso_name" required:"false"` FloppyFiles []string `mapstructure:"floppy_files"` NetworkNames []string `mapstructure:"network_names"` ExportNetworkNames []string `mapstructure:"export_network_names"` diff --git a/builder/xenserver/common/config.hcl2spec.go b/builder/xenserver/common/config.hcl2spec.go index 04d4479..9b3fcb7 100644 --- a/builder/xenserver/common/config.hcl2spec.go +++ b/builder/xenserver/common/config.hcl2spec.go @@ -24,7 +24,7 @@ type FlatConfig struct { VMName *string `mapstructure:"vm_name" cty:"vm_name" hcl:"vm_name"` VMDescription *string `mapstructure:"vm_description" cty:"vm_description" hcl:"vm_description"` SrName *string `mapstructure:"sr_name" cty:"sr_name" hcl:"sr_name"` - SrISOName *string `mapstructure:"sr_iso_name" cty:"sr_iso_name" hcl:"sr_iso_name"` + SrISOName *string `mapstructure:"sr_iso_name" required:"false" cty:"sr_iso_name" hcl:"sr_iso_name"` FloppyFiles []string `mapstructure:"floppy_files" cty:"floppy_files" hcl:"floppy_files"` NetworkNames []string `mapstructure:"network_names" cty:"network_names" hcl:"network_names"` ExportNetworkNames []string `mapstructure:"export_network_names" cty:"export_network_names" hcl:"export_network_names"`