2020-01-07 16:59:31 -08:00
|
|
|
//go:generate struct-markdown
|
|
|
|
//go:generate mapstructure-to-hcl2 -type CDRomConfig
|
|
|
|
|
2020-09-22 11:32:25 +02:00
|
|
|
package common
|
2018-01-24 17:56:14 +03:00
|
|
|
|
|
|
|
import (
|
2018-11-01 00:42:24 +03:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-02-14 11:42:29 -05:00
|
|
|
|
2020-01-07 16:59:31 -08:00
|
|
|
"github.com/hashicorp/packer/builder/vsphere/driver"
|
2018-11-01 00:42:24 +03:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-24 17:56:14 +03:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CDRomConfig struct {
|
2020-01-07 16:59:31 -08:00
|
|
|
// Which controller to use. Example: `sata`. Defaults to `ide`.
|
|
|
|
CdromType string `mapstructure:"cdrom_type"`
|
2020-08-24 16:54:30 +02:00
|
|
|
// List of Datastore or Content Library paths to ISO files that will be mounted to the VM.
|
|
|
|
// Here's an HCL2 example:
|
|
|
|
// ```hcl
|
|
|
|
// iso_paths = [
|
|
|
|
// "[datastore1] ISO/ubuntu.iso",
|
|
|
|
// "Packer Library Test/ubuntu-16.04.6-server-amd64/ubuntu-16.04.6-server-amd64.iso"
|
|
|
|
// ]
|
|
|
|
// ```
|
2020-01-07 16:59:31 -08:00
|
|
|
ISOPaths []string `mapstructure:"iso_paths"`
|
2018-01-24 17:56:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type StepAddCDRom struct {
|
2018-02-01 14:47:09 +03:00
|
|
|
Config *CDRomConfig
|
2018-01-24 17:56:14 +03:00
|
|
|
}
|
|
|
|
|
2018-10-30 03:18:57 +03:00
|
|
|
func (c *CDRomConfig) Prepare() []error {
|
|
|
|
var errs []error
|
|
|
|
|
2018-11-01 00:42:24 +03:00
|
|
|
if c.CdromType != "" && c.CdromType != "ide" && c.CdromType != "sata" {
|
2018-10-30 03:18:57 +03:00
|
|
|
errs = append(errs, fmt.Errorf("'cdrom_type' must be 'ide' or 'sata'"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2018-04-25 14:22:38 +03:00
|
|
|
func (s *StepAddCDRom) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-01-24 17:56:14 +03:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2020-09-18 09:57:01 +02:00
|
|
|
vm := state.Get("vm").(driver.VirtualMachine)
|
2018-01-24 17:56:14 +03:00
|
|
|
|
2018-10-30 03:18:57 +03:00
|
|
|
if s.Config.CdromType == "sata" {
|
2019-07-08 17:55:24 +03: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-30 03:18:57 +03:00
|
|
|
}
|
2018-02-27 00:17:48 +03:00
|
|
|
}
|
|
|
|
|
2020-01-13 11:43:02 -08: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 14:59:51 -07:00
|
|
|
// Add our custom CD, if it exists
|
2020-09-18 09:57:01 +02:00
|
|
|
if cd_path, _ := state.Get("cd_path").(string); cd_path != "" {
|
|
|
|
s.Config.ISOPaths = append(s.Config.ISOPaths, cd_path)
|
2020-09-14 14:59:51 -07:00
|
|
|
}
|
|
|
|
|
2019-01-04 15:07:09 +01: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 23:23:17 +03:00
|
|
|
state.Put("error", fmt.Errorf("error mounting an image '%v': %v", path, err))
|
2019-01-04 15:07:09 +01:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 17:56:14 +03:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-04-25 11:23:37 +02:00
|
|
|
func (s *StepAddCDRom) Cleanup(state multistep.StateBag) {}
|