packer-cn/builder/vmware/iso/builder.go

472 lines
13 KiB
Go
Raw Normal View History

2013-12-24 00:58:41 -05:00
package iso
import (
"errors"
"fmt"
"io/ioutil"
2013-06-05 20:52:37 -04:00
"log"
2013-06-06 18:12:54 -04:00
"os"
"strconv"
"time"
2015-05-27 17:16:28 -04:00
2017-04-04 16:39:01 -04:00
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
"github.com/hashicorp/packer/common"
2018-04-18 17:53:59 -04:00
"github.com/hashicorp/packer/common/bootcommand"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
)
const BuilderIdESX = "mitchellh.vmware-esx"
type Builder struct {
2015-05-27 17:16:28 -04:00
config Config
runner multistep.Runner
}
2015-05-27 17:16:28 -04:00
type Config struct {
2013-12-26 17:31:23 -05:00
common.PackerConfig `mapstructure:",squash"`
common.HTTPConfig `mapstructure:",squash"`
common.ISOConfig `mapstructure:",squash"`
2016-07-26 15:42:04 -04:00
common.FloppyConfig `mapstructure:",squash"`
bootcommand.VNCConfig `mapstructure:",squash"`
vmwcommon.DriverConfig `mapstructure:",squash"`
2013-12-26 17:31:23 -05:00
vmwcommon.OutputConfig `mapstructure:",squash"`
vmwcommon.RunConfig `mapstructure:",squash"`
vmwcommon.ShutdownConfig `mapstructure:",squash"`
vmwcommon.SSHConfig `mapstructure:",squash"`
vmwcommon.ToolsConfig `mapstructure:",squash"`
2013-12-26 17:31:23 -05:00
vmwcommon.VMXConfig `mapstructure:",squash"`
2013-12-26 17:14:19 -05:00
// disk drives
2017-02-27 16:34:53 -05:00
AdditionalDiskSize []uint `mapstructure:"disk_additional_size"`
DiskAdapterType string `mapstructure:"disk_adapter_type"`
2017-02-27 16:34:53 -05:00
DiskName string `mapstructure:"vmdk_name"`
DiskSize uint `mapstructure:"disk_size"`
DiskTypeId string `mapstructure:"disk_type_id"`
Format string `mapstructure:"format"`
// cdrom drive
CdromAdapterType string `mapstructure:"cdrom_adapter_type"`
// platform information
2017-02-27 16:34:53 -05:00
GuestOSType string `mapstructure:"guest_os_type"`
Version string `mapstructure:"version"`
VMName string `mapstructure:"vm_name"`
// Network adapter and type
NetworkAdapterType string `mapstructure:"network_adapter_type"`
Network string `mapstructure:"network"`
// device presence
2017-02-27 16:34:53 -05:00
Sound bool `mapstructure:"sound"`
USB bool `mapstructure:"usb"`
// communication ports
2017-02-27 16:34:53 -05:00
Serial string `mapstructure:"serial"`
Parallel string `mapstructure:"parallel"`
// booting a guest
KeepRegistered bool `mapstructure:"keep_registered"`
OVFToolOptions []string `mapstructure:"ovftool_options"`
2015-06-15 15:40:34 -04:00
SkipCompaction bool `mapstructure:"skip_compaction"`
SkipExport bool `mapstructure:"skip_export"`
2015-06-15 15:40:34 -04:00
VMXDiskTemplatePath string `mapstructure:"vmx_disk_template_path"`
VMXTemplatePath string `mapstructure:"vmx_template_path"`
// remote vsphere
RemoteType string `mapstructure:"remote_type"`
RemoteDatastore string `mapstructure:"remote_datastore"`
RemoteCacheDatastore string `mapstructure:"remote_cache_datastore"`
RemoteCacheDirectory string `mapstructure:"remote_cache_directory"`
RemoteHost string `mapstructure:"remote_host"`
RemotePort uint `mapstructure:"remote_port"`
RemoteUser string `mapstructure:"remote_username"`
RemotePassword string `mapstructure:"remote_password"`
RemotePrivateKey string `mapstructure:"remote_private_key_file"`
CommConfig communicator.Config `mapstructure:",squash"`
2015-05-27 17:16:28 -04:00
ctx interpolate.Context
}
2013-11-03 00:03:59 -04:00
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
2015-05-27 17:16:28 -04:00
err := config.Decode(&b.config, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &b.config.ctx,
2015-05-27 17:16:28 -04:00
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{
"boot_command",
"tools_upload_path",
},
},
}, raws...)
2013-08-08 19:18:01 -04:00
if err != nil {
2013-11-03 00:03:59 -04:00
return nil, err
2013-08-08 19:18:01 -04:00
}
2015-05-27 17:16:28 -04:00
// Accumulate any errors and warnings
var errs *packer.MultiError
warnings := make([]string, 0)
isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx)
warnings = append(warnings, isoWarnings...)
errs = packer.MultiErrorAppend(errs, isoErrs...)
errs = packer.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...)
2015-05-27 17:16:28 -04:00
errs = packer.MultiErrorAppend(errs, b.config.DriverConfig.Prepare(&b.config.ctx)...)
2013-12-25 18:01:57 -05:00
errs = packer.MultiErrorAppend(errs,
2015-05-27 17:16:28 -04:00
b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...)
errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.ToolsConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.VMXConfig.Prepare(&b.config.ctx)...)
2016-07-26 15:42:04 -04:00
errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.VNCConfig.Prepare(&b.config.ctx)...)
if b.config.DiskName == "" {
b.config.DiskName = "disk"
}
if b.config.DiskSize == 0 {
b.config.DiskSize = 40000
}
if b.config.DiskAdapterType == "" {
// Default is lsilogic
b.config.DiskAdapterType = "lsilogic"
}
if !b.config.SkipCompaction {
if b.config.RemoteType == "esx5" {
if b.config.DiskTypeId == "" {
b.config.SkipCompaction = true
}
}
}
if b.config.DiskTypeId == "" {
// Default is growable virtual disk split in 2GB files.
b.config.DiskTypeId = "1"
if b.config.RemoteType == "esx5" {
b.config.DiskTypeId = "zeroedthick"
}
}
if b.config.RemoteType == "esx5" {
if b.config.DiskTypeId != "thin" && !b.config.SkipCompaction {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("skip_compaction must be 'true' for disk_type_id: %s", b.config.DiskTypeId))
}
}
if b.config.GuestOSType == "" {
b.config.GuestOSType = "other"
}
if b.config.VMName == "" {
b.config.VMName = fmt.Sprintf("packer-%s", b.config.PackerBuildName)
}
if b.config.Version == "" {
b.config.Version = "9"
}
if b.config.RemoteUser == "" {
b.config.RemoteUser = "root"
}
if b.config.RemoteDatastore == "" {
b.config.RemoteDatastore = "datastore1"
}
if b.config.RemoteCacheDatastore == "" {
b.config.RemoteCacheDatastore = b.config.RemoteDatastore
}
if b.config.RemoteCacheDirectory == "" {
b.config.RemoteCacheDirectory = "packer_cache"
}
if b.config.RemotePort == 0 {
b.config.RemotePort = 22
}
2017-10-09 20:12:33 -04:00
if b.config.VMXTemplatePath != "" {
if err := b.validateVMXTemplatePath(); err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("vmx_template_path is invalid: %s", err))
}
}
if b.config.Network == "" {
b.config.Network = "nat"
}
if !b.config.Sound {
b.config.Sound = false
}
if !b.config.USB {
b.config.USB = false
}
// Remote configuration validation
if b.config.RemoteType != "" {
if b.config.RemoteHost == "" {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("remote_host must be specified"))
}
if b.config.RemoteType != "esx5" {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Only 'esx5' value is accepted for remote_type"))
}
}
if b.config.Format == "" {
b.config.Format = "ovf"
}
if !(b.config.Format == "ova" || b.config.Format == "ovf" || b.config.Format == "vmx") {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("format must be one of ova, ovf, or vmx"))
}
if b.config.RemoteType == "esx5" && b.config.SkipExport != true && b.config.RemotePassword == "" {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("exporting the vm (with ovftool) requires that you set a value for remote_password"))
}
// Warnings
if b.config.ShutdownCommand == "" {
warnings = append(warnings,
"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
"will forcibly halt the virtual machine, which may result in data loss.")
}
2017-10-09 20:12:33 -04:00
if b.config.Headless && b.config.DisableVNC {
warnings = append(warnings,
"Headless mode uses VNC to retrieve output. Since VNC has been disabled,\n"+
"you won't be able to see any output.")
}
2013-07-19 19:08:25 -04:00
if errs != nil && len(errs.Errors) > 0 {
return warnings, errs
2013-06-06 15:19:38 -04:00
}
return warnings, nil
}
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
driver, err := NewDriver(&b.config)
if err != nil {
return nil, fmt.Errorf("Failed creating VMware driver: %s", err)
}
// Determine the output dir implementation
var dir OutputDir
switch d := driver.(type) {
case OutputDir:
dir = d
default:
dir = new(vmwcommon.LocalOutputDir)
}
exportOutputPath := b.config.OutputDir
if b.config.RemoteType != "" {
b.config.OutputDir = b.config.VMName
}
dir.SetOutputDir(b.config.OutputDir)
// Setup the state bag
state := new(multistep.BasicStateBag)
state.Put("cache", cache)
state.Put("config", &b.config)
state.Put("debug", b.config.PackerDebug)
state.Put("dir", dir)
state.Put("driver", driver)
state.Put("hook", hook)
state.Put("ui", ui)
steps := []multistep.Step{
&vmwcommon.StepPrepareTools{
2014-05-10 00:12:14 -04:00
RemoteType: b.config.RemoteType,
ToolsUploadFlavor: b.config.ToolsUploadFlavor,
},
&common.StepDownload{
Checksum: b.config.ISOChecksum,
ChecksumType: b.config.ISOChecksumType,
Description: "ISO",
Extension: b.config.TargetExtension,
ResultKey: "iso_path",
TargetPath: b.config.TargetPath,
Url: b.config.ISOUrls,
},
&vmwcommon.StepOutputDir{
Force: b.config.PackerForce,
},
&common.StepCreateFloppy{
2016-10-11 17:43:50 -04:00
Files: b.config.FloppyConfig.FloppyFiles,
Directories: b.config.FloppyConfig.FloppyDirectories,
},
&stepRemoteUpload{
Key: "floppy_path",
Message: "Uploading Floppy to remote machine...",
DoCleanup: true,
},
&stepRemoteUpload{
Key: "iso_path",
Message: "Uploading ISO to remote machine...",
},
&stepCreateDisk{},
&stepCreateVMX{},
&vmwcommon.StepConfigureVMX{
CustomData: b.config.VMXData,
},
&vmwcommon.StepSuppressMessages{},
&common.StepHTTPServer{
2014-09-05 14:59:46 -04:00
HTTPDir: b.config.HTTPDir,
HTTPPortMin: b.config.HTTPPortMin,
HTTPPortMax: b.config.HTTPPortMax,
},
2014-09-05 15:10:40 -04:00
&vmwcommon.StepConfigureVNC{
2017-10-09 20:12:33 -04:00
Enabled: !b.config.DisableVNC,
VNCBindAddress: b.config.VNCBindAddress,
VNCPortMin: b.config.VNCPortMin,
VNCPortMax: b.config.VNCPortMax,
VNCDisablePassword: b.config.VNCDisablePassword,
2014-09-05 15:10:40 -04:00
},
&StepRegister{
Format: b.config.Format,
},
2013-12-24 20:12:43 -05:00
&vmwcommon.StepRun{
DurationBeforeStop: 5 * time.Second,
Headless: b.config.Headless,
},
&vmwcommon.StepTypeBootCommand{
BootWait: b.config.BootWait,
2017-10-09 20:12:33 -04:00
VNCEnabled: !b.config.DisableVNC,
BootCommand: b.config.FlatBootCommand(),
VMName: b.config.VMName,
2015-05-27 17:16:28 -04:00
Ctx: b.config.ctx,
KeyInterval: b.config.VNCConfig.BootKeyInterval,
},
2015-06-13 18:52:44 -04:00
&communicator.StepConnect{
Config: &b.config.SSHConfig.Comm,
Host: driver.CommHost,
SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
},
&vmwcommon.StepUploadTools{
2014-05-10 00:12:14 -04:00
RemoteType: b.config.RemoteType,
ToolsUploadFlavor: b.config.ToolsUploadFlavor,
2014-05-10 00:12:14 -04:00
ToolsUploadPath: b.config.ToolsUploadPath,
2015-05-27 17:16:28 -04:00
Ctx: b.config.ctx,
},
&common.StepProvision{},
&common.StepCleanupTempKeys{
Comm: &b.config.SSHConfig.Comm,
},
2013-12-25 01:33:49 -05:00
&vmwcommon.StepShutdown{
Command: b.config.ShutdownCommand,
2013-12-26 17:31:23 -05:00
Timeout: b.config.ShutdownTimeout,
2013-12-25 01:33:49 -05:00
},
2013-12-24 20:17:58 -05:00
&vmwcommon.StepCleanFiles{},
&vmwcommon.StepCompactDisk{
Skip: b.config.SkipCompaction,
},
&vmwcommon.StepConfigureVMX{
CustomData: b.config.VMXDataPost,
2014-09-04 00:27:54 -04:00
SkipFloppy: true,
},
&vmwcommon.StepCleanVMX{
RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet,
2017-10-12 19:38:18 -04:00
VNCEnabled: !b.config.DisableVNC,
},
&StepUploadVMX{
2015-05-27 17:16:28 -04:00
RemoteType: b.config.RemoteType,
},
&StepExport{
Format: b.config.Format,
SkipExport: b.config.SkipExport,
OutputDir: exportOutputPath,
},
}
// Run!
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(state)
2013-06-20 00:20:48 -04:00
// If there was an error, return that
2013-08-31 15:50:25 -04:00
if rawErr, ok := state.GetOk("error"); ok {
2013-06-20 00:20:48 -04:00
return nil, rawErr.(error)
}
2013-06-06 18:12:54 -04:00
// If we were interrupted or cancelled, then just exit.
2013-08-31 15:50:25 -04:00
if _, ok := state.GetOk(multistep.StateCancelled); ok {
2013-06-20 00:20:48 -04:00
return nil, errors.New("Build was cancelled.")
2013-06-06 18:12:54 -04:00
}
2013-08-31 15:50:25 -04:00
if _, ok := state.GetOk(multistep.StateHalted); ok {
2013-06-20 00:20:48 -04:00
return nil, errors.New("Build was halted.")
2013-06-06 18:12:54 -04:00
}
// Compile the artifact list
var files []string
if b.config.RemoteType != "" && b.config.Format != "" && !b.config.SkipExport {
dir = new(vmwcommon.LocalOutputDir)
dir.SetOutputDir(exportOutputPath)
files, err = dir.ListFiles()
} else {
files, err = state.Get("dir").(OutputDir).ListFiles()
}
if err != nil {
return nil, err
2013-06-06 18:12:54 -04:00
}
// Set the proper builder ID
builderId := vmwcommon.BuilderId
if b.config.RemoteType != "" {
builderId = BuilderIdESX
}
config := make(map[string]string)
config[ArtifactConfKeepRegistered] = strconv.FormatBool(b.config.KeepRegistered)
config[ArtifactConfFormat] = b.config.Format
config[ArtifactConfSkipExport] = strconv.FormatBool(b.config.SkipExport)
return &Artifact{
builderId: builderId,
2017-07-27 20:59:11 -04:00
id: b.config.VMName,
dir: dir,
f: files,
config: config,
}, nil
}
func (b *Builder) Cancel() {
2013-06-05 20:52:37 -04:00
if b.runner != nil {
log.Println("Cancelling the step runner...")
b.runner.Cancel()
}
}
func (b *Builder) validateVMXTemplatePath() error {
f, err := os.Open(b.config.VMXTemplatePath)
if err != nil {
return err
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return err
}
2015-05-27 17:16:28 -04:00
return interpolate.Validate(string(data), &b.config.ctx)
}