Skip to content

Commit

Permalink
Merge pull request #65 from ddelnano/ddelnano/sohonetlabs/iso-sr
Browse files Browse the repository at this point in the history
Allow ISO Storage repository to fallback to default SR if unspecified
  • Loading branch information
ddelnano authored Apr 14, 2023
2 parents bb64b86 + 3c5fc29 commit de46bb8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
59 changes: 36 additions & 23 deletions builder/xenserver/common/common_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -287,3 +269,34 @@ func (config CommonConfig) GetISOSR(c *Connection) (xenapi.SRRef, error) {
return srs[0], nil
}
}

func getDefaultSR(c *Connection) (xenapi.SRRef, error) {
var srRef xenapi.SRRef
client := c.GetClient()
hostRef, err := client.Session.GetThisHost(c.session, c.session)

if err != nil {
return srRef, err
}

// 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_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)
}
}

return srRef, errors.New(fmt.Sprintf("failed to find default SR on host '%s'", hostRef))
}
2 changes: 1 addition & 1 deletion builder/xenserver/common/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit de46bb8

Please sign in to comment.