vsphere-clone use common step_add_cdrom
This commit is contained in:
parent
778d77e4a2
commit
b46f587450
|
@ -58,7 +58,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
|||
&common.StepConfigureHardware{
|
||||
Config: &b.config.HardwareConfig,
|
||||
},
|
||||
&StepAddCDRom{
|
||||
&common.StepAddCDRom{
|
||||
Config: &b.config.CDRomConfig,
|
||||
},
|
||||
&common.StepConfigParams{
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
package clone
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/hashicorp/packer/helper/multistep"
|
||||
"github.com/hashicorp/packer/packer"
|
||||
)
|
||||
|
||||
func basicStateBag() *multistep.BasicStateBag {
|
||||
state := new(multistep.BasicStateBag)
|
||||
state.Put("ui", &packer.BasicUi{
|
||||
Reader: new(bytes.Buffer),
|
||||
Writer: new(bytes.Buffer),
|
||||
})
|
||||
return state
|
||||
}
|
|
@ -23,7 +23,7 @@ type Config struct {
|
|||
common.HardwareConfig `mapstructure:",squash"`
|
||||
common.ConfigParamsConfig `mapstructure:",squash"`
|
||||
|
||||
CDRomConfig `mapstructure:",squash"`
|
||||
common.CDRomConfig `mapstructure:",squash"`
|
||||
common.RemoveCDRomConfig `mapstructure:",squash"`
|
||||
common.FloppyConfig `mapstructure:",squash"`
|
||||
common.RunConfig `mapstructure:",squash"`
|
||||
|
|
|
@ -61,6 +61,7 @@ type FlatConfig struct {
|
|||
ToolsSyncTime *bool `mapstructure:"tools_sync_time" cty:"tools_sync_time" hcl:"tools_sync_time"`
|
||||
ToolsUpgradePolicy *bool `mapstructure:"tools_upgrade_policy" cty:"tools_upgrade_policy" hcl:"tools_upgrade_policy"`
|
||||
CdromType *string `mapstructure:"cdrom_type" cty:"cdrom_type" hcl:"cdrom_type"`
|
||||
ISOPaths []string `mapstructure:"iso_paths" cty:"iso_paths" hcl:"iso_paths"`
|
||||
RemoveCdrom *bool `mapstructure:"remove_cdrom" cty:"remove_cdrom" hcl:"remove_cdrom"`
|
||||
FloppyIMGPath *string `mapstructure:"floppy_img_path" cty:"floppy_img_path" hcl:"floppy_img_path"`
|
||||
FloppyFiles []string `mapstructure:"floppy_files" cty:"floppy_files" hcl:"floppy_files"`
|
||||
|
@ -194,6 +195,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
|
|||
"tools_sync_time": &hcldec.AttrSpec{Name: "tools_sync_time", Type: cty.Bool, Required: false},
|
||||
"tools_upgrade_policy": &hcldec.AttrSpec{Name: "tools_upgrade_policy", Type: cty.Bool, 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},
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
//go:generate struct-markdown
|
||||
//go:generate mapstructure-to-hcl2 -type CDRomConfig
|
||||
|
||||
package clone
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/packer/builder/vsphere/driver"
|
||||
"github.com/hashicorp/packer/helper/multistep"
|
||||
"github.com/hashicorp/packer/packer"
|
||||
)
|
||||
|
||||
type CDRomConfig struct {
|
||||
// Which controller to use. Example: `sata`. Defaults to `ide`.
|
||||
CdromType string `mapstructure:"cdrom_type"`
|
||||
}
|
||||
|
||||
type StepAddCDRom struct {
|
||||
Config *CDRomConfig
|
||||
}
|
||||
|
||||
func (c *CDRomConfig) Prepare() []error {
|
||||
var errs []error
|
||||
|
||||
if c.CdromType != "" && c.CdromType != "ide" && c.CdromType != "sata" {
|
||||
errs = append(errs, fmt.Errorf("'cdrom_type' must be 'ide' or 'sata'"))
|
||||
}
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
func (s *StepAddCDRom) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
vm := state.Get("vm").(driver.VirtualMachine)
|
||||
|
||||
if s.Config.CdromType == "sata" {
|
||||
if _, err := vm.FindSATAController(); err == driver.ErrNoSataController {
|
||||
ui.Say("Adding SATA controller...")
|
||||
if err := vm.AddSATAController(); err != nil {
|
||||
state.Put("error", fmt.Errorf("error adding SATA controller: %v", err))
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add our custom CD, if it exists
|
||||
if cd_path, _ := state.Get("cd_path").(string); cd_path != "" {
|
||||
if err := vm.AddCdrom(s.Config.CdromType, cd_path); err != nil {
|
||||
state.Put("error", fmt.Errorf("error mounting a CD '%v': %v", cd_path, err))
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
}
|
||||
|
||||
return multistep.ActionContinue
|
||||
}
|
||||
|
||||
func (s *StepAddCDRom) Cleanup(state multistep.StateBag) {}
|
|
@ -1,30 +0,0 @@
|
|||
// Code generated by "mapstructure-to-hcl2 -type CDRomConfig"; DO NOT EDIT.
|
||||
package clone
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/hcl/v2/hcldec"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
)
|
||||
|
||||
// FlatCDRomConfig is an auto-generated flat version of CDRomConfig.
|
||||
// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.
|
||||
type FlatCDRomConfig struct {
|
||||
CdromType *string `mapstructure:"cdrom_type" cty:"cdrom_type" hcl:"cdrom_type"`
|
||||
}
|
||||
|
||||
// FlatMapstructure returns a new FlatCDRomConfig.
|
||||
// FlatCDRomConfig is an auto-generated flat version of CDRomConfig.
|
||||
// Where the contents a fields with a `mapstructure:,squash` tag are bubbled up.
|
||||
func (*CDRomConfig) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } {
|
||||
return new(FlatCDRomConfig)
|
||||
}
|
||||
|
||||
// HCL2Spec returns the hcl spec of a CDRomConfig.
|
||||
// This spec is used by HCL to read the fields of CDRomConfig.
|
||||
// The decoded values from this spec will then be applied to a FlatCDRomConfig.
|
||||
func (*FlatCDRomConfig) HCL2Spec() map[string]hcldec.Spec {
|
||||
s := map[string]hcldec.Spec{
|
||||
"cdrom_type": &hcldec.AttrSpec{Name: "cdrom_type", Type: cty.String, Required: false},
|
||||
}
|
||||
return s
|
||||
}
|
|
@ -1,200 +0,0 @@
|
|||
package clone
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"github.com/hashicorp/packer/builder/vsphere/driver"
|
||||
"github.com/hashicorp/packer/helper/multistep"
|
||||
)
|
||||
|
||||
func TestCDRomConfig_Prepare(t *testing.T) {
|
||||
// Data validation
|
||||
tc := []struct {
|
||||
name string
|
||||
config *CDRomConfig
|
||||
fail bool
|
||||
expectedErrMsg string
|
||||
}{
|
||||
{
|
||||
name: "Should not fail for empty config",
|
||||
config: new(CDRomConfig),
|
||||
fail: false,
|
||||
expectedErrMsg: "",
|
||||
},
|
||||
{
|
||||
name: "Valid cdroom type ide",
|
||||
config: &CDRomConfig{CdromType: "ide"},
|
||||
fail: false,
|
||||
expectedErrMsg: "",
|
||||
},
|
||||
{
|
||||
name: "Valid cdroom type sata",
|
||||
config: &CDRomConfig{CdromType: "ide"},
|
||||
fail: false,
|
||||
expectedErrMsg: "",
|
||||
},
|
||||
{
|
||||
name: "Invalid cdroom type",
|
||||
config: &CDRomConfig{CdromType: "invalid"},
|
||||
fail: true,
|
||||
expectedErrMsg: "'cdrom_type' must be 'ide' or 'sata'",
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range tc {
|
||||
errs := c.config.Prepare()
|
||||
if c.fail {
|
||||
if len(errs) == 0 {
|
||||
t.Fatalf("Config preprare should fail")
|
||||
}
|
||||
if errs[0].Error() != c.expectedErrMsg {
|
||||
t.Fatalf("Expected error message: %s but was '%s'", c.expectedErrMsg, errs[0].Error())
|
||||
}
|
||||
} else {
|
||||
if len(errs) != 0 {
|
||||
t.Fatalf("Config preprare should not fail")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStepAddCDRom_Run(t *testing.T) {
|
||||
tc := []struct {
|
||||
name string
|
||||
state *multistep.BasicStateBag
|
||||
step *StepAddCDRom
|
||||
vmMock *driver.VirtualMachineMock
|
||||
expectedAction multistep.StepAction
|
||||
expectedVmMock *driver.VirtualMachineMock
|
||||
fail bool
|
||||
errMessage string
|
||||
}{
|
||||
{
|
||||
name: "CDRom SATA type with all cd paths set",
|
||||
state: cdPathStateBag(),
|
||||
step: &StepAddCDRom{
|
||||
Config: &CDRomConfig{
|
||||
CdromType: "sata",
|
||||
},
|
||||
},
|
||||
vmMock: new(driver.VirtualMachineMock),
|
||||
expectedAction: multistep.ActionContinue,
|
||||
expectedVmMock: &driver.VirtualMachineMock{
|
||||
FindSATAControllerCalled: true,
|
||||
AddCdromCalled: true,
|
||||
AddCdromCalledTimes: 1,
|
||||
AddCdromTypes: []string{"sata"},
|
||||
AddCdromPaths: []string{"cd/path"},
|
||||
},
|
||||
fail: false,
|
||||
errMessage: "",
|
||||
},
|
||||
{
|
||||
name: "Add SATA Controller",
|
||||
state: basicStateBag(),
|
||||
step: &StepAddCDRom{
|
||||
Config: &CDRomConfig{
|
||||
CdromType: "sata",
|
||||
},
|
||||
},
|
||||
vmMock: &driver.VirtualMachineMock{
|
||||
FindSATAControllerErr: driver.ErrNoSataController,
|
||||
},
|
||||
expectedAction: multistep.ActionContinue,
|
||||
expectedVmMock: &driver.VirtualMachineMock{
|
||||
FindSATAControllerCalled: true,
|
||||
FindSATAControllerErr: driver.ErrNoSataController,
|
||||
AddSATAControllerCalled: true,
|
||||
},
|
||||
fail: false,
|
||||
errMessage: "",
|
||||
},
|
||||
{
|
||||
name: "Fail to add SATA Controller",
|
||||
state: basicStateBag(),
|
||||
step: &StepAddCDRom{
|
||||
Config: &CDRomConfig{
|
||||
CdromType: "sata",
|
||||
},
|
||||
},
|
||||
vmMock: &driver.VirtualMachineMock{
|
||||
FindSATAControllerErr: driver.ErrNoSataController,
|
||||
AddSATAControllerErr: fmt.Errorf("AddSATAController error"),
|
||||
},
|
||||
expectedAction: multistep.ActionHalt,
|
||||
expectedVmMock: &driver.VirtualMachineMock{
|
||||
FindSATAControllerCalled: true,
|
||||
AddSATAControllerCalled: true,
|
||||
},
|
||||
fail: true,
|
||||
errMessage: fmt.Sprintf("error adding SATA controller: %v", fmt.Errorf("AddSATAController error")),
|
||||
},
|
||||
{
|
||||
name: "IDE CDRom Type and Iso Path set",
|
||||
state: basicStateBag(),
|
||||
step: &StepAddCDRom{
|
||||
Config: &CDRomConfig{
|
||||
CdromType: "ide",
|
||||
},
|
||||
},
|
||||
vmMock: new(driver.VirtualMachineMock),
|
||||
expectedAction: multistep.ActionContinue,
|
||||
expectedVmMock: new(driver.VirtualMachineMock),
|
||||
fail: false,
|
||||
errMessage: "",
|
||||
},
|
||||
{
|
||||
name: "Fail to add cdrom from state cd_path",
|
||||
state: cdPathStateBag(),
|
||||
step: &StepAddCDRom{
|
||||
Config: new(CDRomConfig),
|
||||
},
|
||||
vmMock: &driver.VirtualMachineMock{
|
||||
AddCdromErr: fmt.Errorf("AddCdrom error"),
|
||||
},
|
||||
expectedAction: multistep.ActionHalt,
|
||||
expectedVmMock: &driver.VirtualMachineMock{
|
||||
AddCdromCalled: true,
|
||||
AddCdromCalledTimes: 1,
|
||||
AddCdromTypes: []string{""},
|
||||
AddCdromPaths: []string{"cd/path"},
|
||||
},
|
||||
fail: true,
|
||||
errMessage: fmt.Sprintf("error mounting a CD 'cd/path': %v", fmt.Errorf("AddCdrom error")),
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range tc {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
c.state.Put("vm", c.vmMock)
|
||||
if action := c.step.Run(context.TODO(), c.state); action != c.expectedAction {
|
||||
t.Fatalf("unexpected action %v", action)
|
||||
}
|
||||
err, ok := c.state.Get("error").(error)
|
||||
if ok {
|
||||
if err.Error() != c.errMessage {
|
||||
t.Fatalf("unexpected error %s", err.Error())
|
||||
}
|
||||
} else {
|
||||
if c.fail {
|
||||
t.Fatalf("expected to fail but it didn't")
|
||||
}
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(c.vmMock, c.expectedVmMock,
|
||||
cmpopts.IgnoreInterfaces(struct{ error }{})); diff != "" {
|
||||
t.Fatalf("unexpected VirtualMachine calls: %s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func cdPathStateBag() *multistep.BasicStateBag {
|
||||
state := basicStateBag()
|
||||
state.Put("cd_path", "cd/path")
|
||||
return state
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
//go:generate struct-markdown
|
||||
//go:generate mapstructure-to-hcl2 -type CDRomConfig
|
||||
|
||||
package iso
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
|
@ -1,5 +1,5 @@
|
|||
// Code generated by "mapstructure-to-hcl2 -type CDRomConfig"; DO NOT EDIT.
|
||||
package iso
|
||||
package common
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/hcl/v2/hcldec"
|
|
@ -1,4 +1,4 @@
|
|||
package iso
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
@ -96,7 +96,7 @@ func TestStepAddCDRom_Run(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "Add SATA Controller",
|
||||
state: basicStateBag(),
|
||||
state: basicStateBag(nil),
|
||||
step: &StepAddCDRom{
|
||||
Config: &CDRomConfig{
|
||||
CdromType: "sata",
|
||||
|
@ -116,7 +116,7 @@ func TestStepAddCDRom_Run(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "Fail to add SATA Controller",
|
||||
state: basicStateBag(),
|
||||
state: basicStateBag(nil),
|
||||
step: &StepAddCDRom{
|
||||
Config: &CDRomConfig{
|
||||
CdromType: "sata",
|
||||
|
@ -136,7 +136,7 @@ func TestStepAddCDRom_Run(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "IDE CDRom Type and Iso Path set",
|
||||
state: basicStateBag(),
|
||||
state: basicStateBag(nil),
|
||||
step: &StepAddCDRom{
|
||||
Config: &CDRomConfig{
|
||||
CdromType: "ide",
|
||||
|
@ -156,7 +156,7 @@ func TestStepAddCDRom_Run(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "Fail to add cdrom from ISOPaths",
|
||||
state: basicStateBag(),
|
||||
state: basicStateBag(nil),
|
||||
step: &StepAddCDRom{
|
||||
Config: &CDRomConfig{
|
||||
ISOPaths: []string{"iso/path"},
|
||||
|
@ -222,14 +222,14 @@ func TestStepAddCDRom_Run(t *testing.T) {
|
|||
}
|
||||
|
||||
func cdAndIsoRemotePathStateBag() *multistep.BasicStateBag {
|
||||
state := basicStateBag()
|
||||
state := basicStateBag(nil)
|
||||
state.Put("iso_remote_path", "remote/path")
|
||||
state.Put("cd_path", "cd/path")
|
||||
return state
|
||||
}
|
||||
|
||||
func isoRemotePathStateBag() *multistep.BasicStateBag {
|
||||
state := basicStateBag()
|
||||
state := basicStateBag(nil)
|
||||
state.Put("iso_remote_path", "remote/path")
|
||||
return state
|
||||
}
|
|
@ -65,7 +65,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
|||
&common.StepConfigureHardware{
|
||||
Config: &b.config.HardwareConfig,
|
||||
},
|
||||
&StepAddCDRom{
|
||||
&common.StepAddCDRom{
|
||||
Config: &b.config.CDRomConfig,
|
||||
},
|
||||
&common.StepConfigParams{
|
||||
|
|
|
@ -25,7 +25,7 @@ type Config struct {
|
|||
|
||||
packerCommon.ISOConfig `mapstructure:",squash"`
|
||||
|
||||
CDRomConfig `mapstructure:",squash"`
|
||||
common.CDRomConfig `mapstructure:",squash"`
|
||||
common.RemoveCDRomConfig `mapstructure:",squash"`
|
||||
common.FloppyConfig `mapstructure:",squash"`
|
||||
common.RunConfig `mapstructure:",squash"`
|
||||
|
|
|
@ -204,7 +204,7 @@ can be done via environment variable:
|
|||
|
||||
@include 'common/CDConfig-not-required.mdx'
|
||||
|
||||
@include 'builder/vsphere/clone/CDRomConfig-not-required.mdx'
|
||||
@include 'builder/vsphere/common/CDRomConfig-not-required.mdx'
|
||||
|
||||
### Communicator configuration
|
||||
|
||||
|
|
|
@ -172,7 +172,7 @@ iso_paths = [
|
|||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
@include 'builder/vsphere/iso/CDRomConfig-not-required.mdx'
|
||||
@include 'builder/vsphere/common/CDRomConfig-not-required.mdx'
|
||||
|
||||
@include 'common/CDConfig.mdx'
|
||||
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
<!-- Code generated from the comments of the CDRomConfig struct in builder/vsphere/clone/step_add_cdrom.go; DO NOT EDIT MANUALLY -->
|
||||
|
||||
- `cdrom_type` (string) - Which controller to use. Example: `sata`. Defaults to `ide`.
|
|
@ -1,4 +1,4 @@
|
|||
<!-- Code generated from the comments of the CDRomConfig struct in builder/vsphere/iso/step_add_cdrom.go; DO NOT EDIT MANUALLY -->
|
||||
<!-- Code generated from the comments of the CDRomConfig struct in builder/vsphere/common/step_add_cdrom.go; DO NOT EDIT MANUALLY -->
|
||||
|
||||
- `cdrom_type` (string) - Which controller to use. Example: `sata`. Defaults to `ide`.
|
||||
|
Loading…
Reference in New Issue