2020-01-07 19:59:31 -05:00
|
|
|
//go:generate struct-markdown
|
|
|
|
//go:generate mapstructure-to-hcl2 -type CDRomConfig
|
|
|
|
|
2018-01-24 09:56:14 -05:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
2018-10-31 17:42:24 -04:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-02-14 11:42:29 -05:00
|
|
|
|
2020-01-07 19:59:31 -05:00
|
|
|
"github.com/hashicorp/packer/builder/vsphere/driver"
|
2018-10-31 17:42:24 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-24 09:56:14 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CDRomConfig struct {
|
2020-01-07 19:59:31 -05:00
|
|
|
// Which controller to use. Example: `sata`. Defaults to `ide`.
|
|
|
|
CdromType string `mapstructure:"cdrom_type"`
|
|
|
|
// List of datastore paths to ISO files that will be mounted to the VM.
|
|
|
|
// Example: `"[datastore1] ISO/ubuntu.iso"`.
|
|
|
|
ISOPaths []string `mapstructure:"iso_paths"`
|
2018-01-24 09:56:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type StepAddCDRom struct {
|
2018-02-01 06:47:09 -05:00
|
|
|
Config *CDRomConfig
|
2018-01-24 09:56:14 -05:00
|
|
|
}
|
|
|
|
|
2018-10-29 20:18:57 -04:00
|
|
|
func (c *CDRomConfig) Prepare() []error {
|
|
|
|
var errs []error
|
|
|
|
|
2018-10-31 17:42:24 -04:00
|
|
|
if c.CdromType != "" && c.CdromType != "ide" && c.CdromType != "sata" {
|
2018-10-29 20:18:57 -04:00
|
|
|
errs = append(errs, fmt.Errorf("'cdrom_type' must be 'ide' or 'sata'"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2018-04-25 07:22:38 -04:00
|
|
|
func (s *StepAddCDRom) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-01-24 09:56:14 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-02-02 13:04:32 -05:00
|
|
|
vm := state.Get("vm").(*driver.VirtualMachine)
|
2018-01-24 09:56:14 -05:00
|
|
|
|
2018-10-29 20:18:57 -04:00
|
|
|
if s.Config.CdromType == "sata" {
|
2019-07-08 10:55:24 -04:00
|
|
|
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
|
|
|
|
}
|
2018-10-29 20:18:57 -04:00
|
|
|
}
|
2018-02-26 16:17:48 -05:00
|
|
|
}
|
|
|
|
|
2020-01-13 14:43:02 -05:00
|
|
|
ui.Say("Mounting ISO images...")
|
2020-02-13 14:03:07 -05:00
|
|
|
if path, ok := state.GetOk("iso_remote_path"); ok {
|
|
|
|
if err := vm.AddCdrom(s.Config.CdromType, path.(string)); err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("error mounting an image '%v': %v", path, err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 09:07:09 -05:00
|
|
|
if len(s.Config.ISOPaths) > 0 {
|
|
|
|
for _, path := range s.Config.ISOPaths {
|
|
|
|
if err := vm.AddCdrom(s.Config.CdromType, path); err != nil {
|
2019-07-16 16:23:17 -04:00
|
|
|
state.Put("error", fmt.Errorf("error mounting an image '%v': %v", path, err))
|
2019-01-04 09:07:09 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 09:56:14 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-04-25 05:23:37 -04:00
|
|
|
func (s *StepAddCDRom) Cleanup(state multistep.StateBag) {}
|