Skip to content

Commit

Permalink
Merge pull request hashicorp#8690 from daxgames/vsphere_iso_remove_cdrom
Browse files Browse the repository at this point in the history
vsphere iso remove cdroms
  • Loading branch information
SwampDragons authored Feb 13, 2020
2 parents 23fa310 + 78012dc commit 4f7670a
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 2 deletions.
17 changes: 17 additions & 0 deletions builder/vsphere/driver/vm_cdrom.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ func (vm *VirtualMachine) CreateCdrom(c *types.VirtualController) (*types.Virtua
return device, nil
}

func (vm *VirtualMachine) RemoveCdroms() error {
devices, err := vm.Devices()
if err != nil {
return err
}
cdroms := devices.SelectByType((*types.VirtualCdrom)(nil))
if err = vm.RemoveDevice(true, cdroms...); err != nil {
return err
}

sata := devices.SelectByType((*types.VirtualAHCIController)(nil))
if err = vm.RemoveDevice(true, sata...); err != nil {
return err
}
return nil
}

func (vm *VirtualMachine) EjectCdroms() error {
devices, err := vm.Devices()
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion builder/vsphere/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
}

steps = append(steps,
&StepRemoveCDRom{},
&StepRemoveCDRom{
Config: &b.config.RemoveCDRomConfig,
},
&common.StepCreateSnapshot{
CreateSnapshot: b.config.CreateSnapshot,
},
Expand Down
1 change: 1 addition & 0 deletions builder/vsphere/iso/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
packerCommon.ISOConfig `mapstructure:",squash"`

CDRomConfig `mapstructure:",squash"`
RemoveCDRomConfig `mapstructure:",squash"`
FloppyConfig `mapstructure:",squash"`
common.RunConfig `mapstructure:",squash"`
BootConfig `mapstructure:",squash"`
Expand Down
2 changes: 2 additions & 0 deletions builder/vsphere/iso/config.hcl2spec.go

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

21 changes: 20 additions & 1 deletion builder/vsphere/iso/step_remove_cdrom.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:generate struct-markdown
//go:generate mapstructure-to-hcl2 -type RemoveCDRomConfig

package iso

import (
Expand All @@ -7,7 +10,14 @@ import (
"github.com/hashicorp/packer/packer"
)

type StepRemoveCDRom struct{}
type RemoveCDRomConfig struct {
// Remove CD-ROM devices from template. Defaults to `false`.
RemoveCdrom bool `mapstructure:"remove_cdrom"`
}

type StepRemoveCDRom struct {
Config *RemoveCDRomConfig
}

func (s *StepRemoveCDRom) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
Expand All @@ -20,6 +30,15 @@ func (s *StepRemoveCDRom) Run(_ context.Context, state multistep.StateBag) multi
return multistep.ActionHalt
}

if s.Config.RemoveCdrom == true {
ui.Say("Deleting CD-ROM drives...")
err := vm.RemoveCdroms()
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
}

return multistep.ActionContinue
}

Expand Down
30 changes: 30 additions & 0 deletions builder/vsphere/iso/step_remove_cdrom.hcl2spec.go

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- Code generated from the comments of the RemoveCDRomConfig struct in builder/vsphere/iso/step_remove_cdrom.go; DO NOT EDIT MANUALLY -->

- `remove_cdrom` (bool) - Remove CD-ROM devices from template. Defaults to `false`.

0 comments on commit 4f7670a

Please sign in to comment.