From 0ccc53b8b5ac9bf4fba8f8517853a314f524b521 Mon Sep 17 00:00:00 2001 From: Dom Del Nano Date: Sun, 26 Feb 2023 23:23:28 -0800 Subject: [PATCH] Prevent ISO used in iso builder from being uploaded if it already exists --- .../common/step_find_or_upload_vdi.go | 44 +++++++++++++++++++ builder/xenserver/common/step_upload_vdi.go | 10 +++-- builder/xenserver/iso/builder.go | 29 ++++++------ 3 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 builder/xenserver/common/step_find_or_upload_vdi.go diff --git a/builder/xenserver/common/step_find_or_upload_vdi.go b/builder/xenserver/common/step_find_or_upload_vdi.go new file mode 100644 index 0000000..37b8453 --- /dev/null +++ b/builder/xenserver/common/step_find_or_upload_vdi.go @@ -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) +} diff --git a/builder/xenserver/common/step_upload_vdi.go b/builder/xenserver/common/step_upload_vdi.go index 67a00f3..006e919 100644 --- a/builder/xenserver/common/step_upload_vdi.go +++ b/builder/xenserver/common/step_upload_vdi.go @@ -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) diff --git a/builder/xenserver/iso/builder.go b/builder/xenserver/iso/builder.go index a9e37f1..e31b0d9 100644 --- a/builder/xenserver/iso/builder.go +++ b/builder/xenserver/iso/builder.go @@ -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,