Merge pull request #8690 from daxgames/vsphere_iso_remove_cdrom

vsphere iso remove cdroms
This commit is contained in:
Megan Marsh 2020-02-13 11:00:47 -08:00 committed by GitHub
commit 4f7670a1e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 77 additions and 2 deletions

View File

@ -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 {

View File

@ -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,
},

View File

@ -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"`

View File

@ -61,6 +61,7 @@ type FlatConfig struct {
TargetExtension *string `mapstructure:"iso_target_extension" cty:"iso_target_extension"`
CdromType *string `mapstructure:"cdrom_type" cty:"cdrom_type"`
ISOPaths []string `mapstructure:"iso_paths" cty:"iso_paths"`
RemoveCdrom *bool `mapstructure:"remove_cdrom" cty:"remove_cdrom"`
FloppyIMGPath *string `mapstructure:"floppy_img_path" cty:"floppy_img_path"`
FloppyFiles []string `mapstructure:"floppy_files" cty:"floppy_files"`
FloppyDirectories []string `mapstructure:"floppy_dirs" cty:"floppy_dirs"`
@ -180,6 +181,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
"iso_target_extension": &hcldec.AttrSpec{Name: "iso_target_extension", Type: cty.String, Required: false},
"cdrom_type": &hcldec.AttrSpec{Name: "cdrom_type", Type: cty.String, Required: false},
"iso_paths": &hcldec.AttrSpec{Name: "iso_paths", Type: cty.List(cty.String), Required: false},
"remove_cdrom": &hcldec.AttrSpec{Name: "remove_cdrom", Type: cty.Bool, Required: false},
"floppy_img_path": &hcldec.AttrSpec{Name: "floppy_img_path", Type: cty.String, Required: false},
"floppy_files": &hcldec.AttrSpec{Name: "floppy_files", Type: cty.List(cty.String), Required: false},
"floppy_dirs": &hcldec.AttrSpec{Name: "floppy_dirs", Type: cty.List(cty.String), Required: false},

View File

@ -1,3 +1,6 @@
//go:generate struct-markdown
//go:generate mapstructure-to-hcl2 -type RemoveCDRomConfig
package iso
import (
@ -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)
@ -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
}

View File

@ -0,0 +1,30 @@
// Code generated by "mapstructure-to-hcl2 -type RemoveCDRomConfig"; DO NOT EDIT.
package iso
import (
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/zclconf/go-cty/cty"
)
// FlatRemoveCDRomConfig is an auto-generated flat version of RemoveCDRomConfig.
// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.
type FlatRemoveCDRomConfig struct {
RemoveCdrom *bool `mapstructure:"remove_cdrom" cty:"remove_cdrom"`
}
// FlatMapstructure returns a new FlatRemoveCDRomConfig.
// FlatRemoveCDRomConfig is an auto-generated flat version of RemoveCDRomConfig.
// Where the contents a fields with a `mapstructure:,squash` tag are bubbled up.
func (*RemoveCDRomConfig) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } {
return new(FlatRemoveCDRomConfig)
}
// HCL2Spec returns the hcl spec of a RemoveCDRomConfig.
// This spec is used by HCL to read the fields of RemoveCDRomConfig.
// The decoded values from this spec will then be applied to a FlatRemoveCDRomConfig.
func (*FlatRemoveCDRomConfig) HCL2Spec() map[string]hcldec.Spec {
s := map[string]hcldec.Spec{
"remove_cdrom": &hcldec.AttrSpec{Name: "remove_cdrom", Type: cty.Bool, Required: false},
}
return s
}

View File

@ -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`.