diff --git a/builder/vsphere/common/step_add_floppy.go b/builder/vsphere/common/step_add_floppy.go index f904e59f..82f3b4ed 100644 --- a/builder/vsphere/common/step_add_floppy.go +++ b/builder/vsphere/common/step_add_floppy.go @@ -9,6 +9,8 @@ package common import ( "context" "fmt" + "math/rand" + "time" "github.com/hashicorp/packer-plugin-sdk/multistep" packersdk "github.com/hashicorp/packer-plugin-sdk/packer" @@ -59,7 +61,7 @@ func (s *StepAddFloppy) Run(_ context.Context, state multistep.StateBag) multist d := state.Get("driver").(driver.Driver) if floppyPath, ok := state.GetOk("floppy_path"); ok { - ui.Say("Uploading created floppy image") + ui.Say("Uploading floppy image...") ds, err := d.FindDatastore(s.Datastore, s.Host) if err != nil { @@ -72,14 +74,22 @@ func (s *StepAddFloppy) Run(_ context.Context, state multistep.StateBag) multist return multistep.ActionHalt } - uploadPath := fmt.Sprintf("%v/packer-tmp-created-floppy.flp", vmDir) + // Create a new random number generator + src := rand.NewSource(time.Now().UnixNano()) + r := rand.New(src) + + // Generate a unique ID for the floppy image using the packer-##########.flp. + // This helps avoid conflicts with other floppy images that might be uploaded. + // This naming pattern matches the one used by packer-sdk for generated ISOs. + uniqueID := r.Int63n(9000000000) + 1000000000 + uploadPath := fmt.Sprintf("%v/packer-%d.flp", vmDir, uniqueID) if err := ds.UploadFile(floppyPath.(string), uploadPath, s.Host, s.SetHostForDatastoreUploads); err != nil { state.Put("error", err) return multistep.ActionHalt } state.Put("uploaded_floppy_path", uploadPath) - ui.Say("Adding generated Floppy...") + ui.Say("Adding generated floppy image...") floppyIMGPath := ds.ResolvePath(uploadPath) err = vm.AddFloppy(floppyIMGPath) if err != nil { @@ -89,7 +99,7 @@ func (s *StepAddFloppy) Run(_ context.Context, state multistep.StateBag) multist } if s.Config.FloppyIMGPath != "" { - ui.Say("Adding Floppy image...") + ui.Say("Adding floppy image...") err := vm.AddFloppy(s.Config.FloppyIMGPath) if err != nil { state.Put("error", err) @@ -111,7 +121,7 @@ func (s *StepAddFloppy) Cleanup(state multistep.StateBag) { d := state.Get("driver").(driver.Driver) if UploadedFloppyPath, ok := state.GetOk("uploaded_floppy_path"); ok { - ui.Say("Deleting Floppy image ...") + ui.Say("Deleting floppy image...") ds, err := d.FindDatastore(s.Datastore, s.Host) if err != nil { diff --git a/builder/vsphere/common/step_add_floppy_test.go b/builder/vsphere/common/step_add_floppy_test.go index 275d9bd5..07b36eaf 100644 --- a/builder/vsphere/common/step_add_floppy_test.go +++ b/builder/vsphere/common/step_add_floppy_test.go @@ -6,6 +6,7 @@ package common import ( "context" "fmt" + "regexp" "testing" "github.com/google/go-cmp/cmp" @@ -33,7 +34,7 @@ func TestStepAddFloppy_Run(t *testing.T) { { name: "Add floppy from state floppy path", floppyPath: "floppy/path", - uploadedPath: "vm/dir/packer-tmp-created-floppy.flp", + uploadedPath: "vm/dir/packer-*.flp", step: &StepAddFloppy{ Config: new(FloppyConfig), Datastore: "datastore", @@ -62,7 +63,7 @@ func TestStepAddFloppy_Run(t *testing.T) { expectedDsMock: &driver.DatastoreMock{ UploadFileCalled: true, UploadFileSrc: "floppy/path", - UploadFileDst: "vm/dir/packer-tmp-created-floppy.flp", + UploadFileDst: "vm/dir/packer-*.flp", UploadFileHost: "host", UploadFileSetHost: true, ResolvePathCalled: true, @@ -151,7 +152,7 @@ func TestStepAddFloppy_Run(t *testing.T) { expectedDsMock: &driver.DatastoreMock{ UploadFileCalled: true, UploadFileSrc: "floppy/path", - UploadFileDst: "vm/dir/packer-tmp-created-floppy.flp", + UploadFileDst: "vm/dir/packer-*.flp", UploadFileHost: "host", UploadFileSetHost: true, }, @@ -161,7 +162,7 @@ func TestStepAddFloppy_Run(t *testing.T) { { name: "State floppy path - vm fail to add floppy", floppyPath: "floppy/path", - uploadedPath: "vm/dir/packer-tmp-created-floppy.flp", + uploadedPath: "vm/dir/packer-*.flp", step: &StepAddFloppy{ Config: new(FloppyConfig), Datastore: "datastore", @@ -191,7 +192,7 @@ func TestStepAddFloppy_Run(t *testing.T) { expectedDsMock: &driver.DatastoreMock{ UploadFileCalled: true, UploadFileSrc: "floppy/path", - UploadFileDst: "vm/dir/packer-tmp-created-floppy.flp", + UploadFileDst: "vm/dir/packer-*.flp", UploadFileHost: "host", UploadFileSetHost: true, ResolvePathCalled: true, @@ -268,9 +269,12 @@ func TestStepAddFloppy_Run(t *testing.T) { } } - uploadedPath, _ := state.Get("uploaded_floppy_path").(string) - if uploadedPath != c.uploadedPath { - t.Fatalf("Unexpected uploaded path state %s", uploadedPath) + if c.driverMock.DatastoreMock.UploadFileDst != "" { + pattern := regexp.MustCompile(`vm/dir/packer-(\d{10}|tmp-created-floppy)\.flp`) + if !pattern.MatchString(c.driverMock.DatastoreMock.UploadFileDst) { + t.Fatalf("unexpected UploadFileDst %v", c.driverMock.DatastoreMock.UploadFileDst) + } + c.driverMock.DatastoreMock.UploadFileDst = "vm/dir/packer-*.flp" } if diff := cmp.Diff(c.vmMock, c.expectedVmMock,