Prevent ISO used in iso builder from being uploaded if it already exists

This commit is contained in:
Dom Del Nano 2023-02-26 23:23:28 -08:00
parent 55e3b071e3
commit 0ccc53b8b5
3 changed files with 65 additions and 18 deletions

View File

@ -0,0 +1,44 @@
package common
import (
"context"
"fmt"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
)
type StepFindOrUploadVdi struct {
StepUploadVdi
}
func (self *StepFindOrUploadVdi) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*Connection)
vdiName := self.VdiNameFunc()
ui.Say(fmt.Sprintf("Attemping to find VDI '%s'", vdiName))
vdis, err := c.client.VDI.GetByNameLabel(c.session, vdiName)
if err != nil {
ui.Error(fmt.Sprintf("Failed to find VDI '%s' by name label: %s", vdiName, err.Error()))
return multistep.ActionHalt
}
if len(vdis) > 1 {
ui.Error(fmt.Sprintf("Found more than one VDI with name '%s'. Name must be unique", vdiName))
return multistep.ActionHalt
} else if len(vdis) == 1 {
vdi := vdis[0]
vdiUuid, err := c.client.VDI.GetUUID(c.session, vdi)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get UUID of VDI '%s': %s", vdiName, err.Error()))
return multistep.ActionHalt
}
state.Put(self.VdiUuidKey, vdiUuid)
return multistep.ActionContinue
}
return self.uploadVdi(ctx, state)
}

View File

@ -18,7 +18,7 @@ type StepUploadVdi struct {
VdiUuidKey string
}
func (self *StepUploadVdi) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
func (self *StepUploadVdi) uploadVdi(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("commonconfig").(CommonConfig)
ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*Connection)
@ -33,10 +33,8 @@ 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)
ui.Say(fmt.Sprintf("Step: Found SRs '%v'", srs))
sr, err := config.GetISOSR(c)
ui.Say(fmt.Sprintf("Step: Found SR for upload '%v'", sr))
if err != nil {
ui.Error(fmt.Sprintf("Unable to get SR: %v", err))
@ -96,6 +94,10 @@ func (self *StepUploadVdi) Run(ctx context.Context, state multistep.StateBag) mu
return multistep.ActionContinue
}
func (self *StepUploadVdi) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
return self.uploadVdi(ctx, state)
}
func (self *StepUploadVdi) Cleanup(state multistep.StateBag) {
config := state.Get("commonconfig").(CommonConfig)
ui := state.Get("ui").(packer.Ui)

View File

@ -187,7 +187,6 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p
Url: self.config.ISOUrls,
},
}
steps := []multistep.Step{
&xscommon.StepPrepareOutputDir{
Force: self.config.PackerForce,
@ -212,20 +211,22 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p
},
VdiUuidKey: "floppy_vdi_uuid",
},
&xscommon.StepUploadVdi{
VdiNameFunc: func() string {
if len(self.config.ISOUrls) > 0 {
return path.Base(self.config.ISOUrls[0])
}
return ""
&xscommon.StepFindOrUploadVdi{
xscommon.StepUploadVdi{
VdiNameFunc: func() string {
if len(self.config.ISOUrls) > 0 {
return path.Base(self.config.ISOUrls[0])
}
return ""
},
ImagePathFunc: func() string {
if isoPath, ok := state.GetOk("iso_path"); ok {
return isoPath.(string)
}
return ""
},
VdiUuidKey: "iso_vdi_uuid",
},
ImagePathFunc: func() string {
if isoPath, ok := state.GetOk("iso_path"); ok {
return isoPath.(string)
}
return ""
},
VdiUuidKey: "iso_vdi_uuid",
},
&xscommon.StepFindVdi{
VdiName: self.config.ToolsIsoName,