2013-12-21 17:27:00 -05:00
|
|
|
package iso
|
2013-06-11 18:12:45 -04:00
|
|
|
|
|
|
|
import (
|
2013-06-11 23:00:30 -04:00
|
|
|
"errors"
|
2013-06-11 18:45:52 -04:00
|
|
|
"fmt"
|
2014-09-02 12:35:59 -04:00
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
2013-06-11 18:12:45 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-12-21 17:51:38 -05:00
|
|
|
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
|
2013-08-01 15:11:54 -04:00
|
|
|
"github.com/mitchellh/packer/common"
|
2015-06-13 18:08:12 -04:00
|
|
|
"github.com/mitchellh/packer/helper/communicator"
|
2015-05-27 17:01:08 -04:00
|
|
|
"github.com/mitchellh/packer/helper/config"
|
2013-06-11 18:12:45 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2015-05-27 17:01:08 -04:00
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
2013-06-11 18:12:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const BuilderId = "mitchellh.virtualbox"
|
|
|
|
|
|
|
|
type Builder struct {
|
2015-05-27 17:01:08 -04:00
|
|
|
config Config
|
2013-06-11 18:12:45 -04:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:01:08 -04:00
|
|
|
type Config struct {
|
2014-03-12 19:14:44 -04:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2015-11-01 17:29:24 -05:00
|
|
|
common.HTTPConfig `mapstructure:",squash"`
|
2015-10-20 19:27:47 -04:00
|
|
|
common.ISOConfig `mapstructure:",squash"`
|
2016-07-26 15:42:04 -04:00
|
|
|
common.FloppyConfig `mapstructure:",squash"`
|
2014-03-12 19:14:44 -04:00
|
|
|
vboxcommon.ExportConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.ExportOpts `mapstructure:",squash"`
|
|
|
|
vboxcommon.OutputConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.RunConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.ShutdownConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.SSHConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.VBoxManageConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.VBoxManagePostConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.VBoxVersionConfig `mapstructure:",squash"`
|
2013-12-22 12:24:29 -05:00
|
|
|
|
|
|
|
BootCommand []string `mapstructure:"boot_command"`
|
|
|
|
DiskSize uint `mapstructure:"disk_size"`
|
2016-10-03 15:11:21 -04:00
|
|
|
KeepRegistered bool `mapstructure:"keep_registered"`
|
2013-12-22 12:24:29 -05:00
|
|
|
GuestAdditionsMode string `mapstructure:"guest_additions_mode"`
|
|
|
|
GuestAdditionsPath string `mapstructure:"guest_additions_path"`
|
|
|
|
GuestAdditionsURL string `mapstructure:"guest_additions_url"`
|
|
|
|
GuestAdditionsSHA256 string `mapstructure:"guest_additions_sha256"`
|
|
|
|
GuestOSType string `mapstructure:"guest_os_type"`
|
|
|
|
HardDriveInterface string `mapstructure:"hard_drive_interface"`
|
2014-05-23 21:14:24 -04:00
|
|
|
ISOInterface string `mapstructure:"iso_interface"`
|
2013-12-22 12:24:29 -05:00
|
|
|
VMName string `mapstructure:"vm_name"`
|
2013-06-13 13:24:10 -04:00
|
|
|
|
2015-05-27 17:01:08 -04:00
|
|
|
ctx interpolate.Context
|
2013-06-11 18:12:45 -04:00
|
|
|
}
|
|
|
|
|
2013-11-03 00:03:59 -04:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
2015-05-27 17:01:08 -04:00
|
|
|
err := config.Decode(&b.config, &config.DecodeOpts{
|
2015-06-22 12:17:09 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &b.config.ctx,
|
2015-05-27 17:01:08 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"boot_command",
|
|
|
|
"guest_additions_path",
|
|
|
|
"guest_additions_url",
|
|
|
|
"vboxmanage",
|
|
|
|
"vboxmanage_post",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-08-08 19:00:47 -04:00
|
|
|
if err != nil {
|
2013-11-03 00:03:59 -04:00
|
|
|
return nil, err
|
2013-08-08 19:00:47 -04:00
|
|
|
}
|
|
|
|
|
2013-11-03 00:17:21 -04:00
|
|
|
// Accumulate any errors and warnings
|
2015-05-27 17:01:08 -04:00
|
|
|
var errs *packer.MultiError
|
2015-10-20 19:27:47 -04:00
|
|
|
warnings := make([]string, 0)
|
|
|
|
|
|
|
|
isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx)
|
|
|
|
warnings = append(warnings, isoWarnings...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, isoErrs...)
|
|
|
|
|
2015-05-27 17:01:08 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.ExportConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.ExportOpts.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...)
|
2013-12-21 20:38:06 -05:00
|
|
|
errs = packer.MultiErrorAppend(
|
2015-05-27 17:01:08 -04:00
|
|
|
errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...)
|
2015-11-01 17:29:24 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...)
|
2015-05-27 17:01:08 -04:00
|
|
|
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.VBoxManageConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.VBoxManagePostConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.VBoxVersionConfig.Prepare(&b.config.ctx)...)
|
2013-07-13 20:28:56 -04:00
|
|
|
|
2013-06-23 23:43:40 -04:00
|
|
|
if b.config.DiskSize == 0 {
|
|
|
|
b.config.DiskSize = 40000
|
|
|
|
}
|
|
|
|
|
2013-11-02 04:22:56 -04:00
|
|
|
if b.config.GuestAdditionsMode == "" {
|
|
|
|
b.config.GuestAdditionsMode = "upload"
|
|
|
|
}
|
|
|
|
|
2013-06-24 02:05:32 -04:00
|
|
|
if b.config.GuestAdditionsPath == "" {
|
|
|
|
b.config.GuestAdditionsPath = "VBoxGuestAdditions.iso"
|
|
|
|
}
|
|
|
|
|
2013-09-05 14:00:08 -04:00
|
|
|
if b.config.HardDriveInterface == "" {
|
|
|
|
b.config.HardDriveInterface = "ide"
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:57:20 -04:00
|
|
|
if b.config.GuestOSType == "" {
|
|
|
|
b.config.GuestOSType = "Other"
|
|
|
|
}
|
|
|
|
|
2014-05-23 21:14:24 -04:00
|
|
|
if b.config.ISOInterface == "" {
|
|
|
|
b.config.ISOInterface = "ide"
|
|
|
|
}
|
|
|
|
|
2015-06-22 12:25:15 -04:00
|
|
|
if b.config.VMName == "" {
|
|
|
|
b.config.VMName = fmt.Sprintf(
|
|
|
|
"packer-%s-%d", b.config.PackerBuildName, interpolate.InitTime.Unix())
|
|
|
|
}
|
|
|
|
|
2015-01-15 20:53:01 -05:00
|
|
|
if b.config.HardDriveInterface != "ide" && b.config.HardDriveInterface != "sata" && b.config.HardDriveInterface != "scsi" {
|
2013-09-05 15:07:58 -04:00
|
|
|
errs = packer.MultiErrorAppend(
|
2015-01-15 20:53:01 -05:00
|
|
|
errs, errors.New("hard_drive_interface can only be ide, sata, or scsi"))
|
2013-09-05 15:07:58 -04:00
|
|
|
}
|
|
|
|
|
2014-05-23 21:14:24 -04:00
|
|
|
if b.config.ISOInterface != "ide" && b.config.ISOInterface != "sata" {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("iso_interface can only be ide or sata"))
|
|
|
|
}
|
|
|
|
|
2013-11-02 04:22:56 -04:00
|
|
|
validMode := false
|
|
|
|
validModes := []string{
|
2014-05-04 11:56:57 -04:00
|
|
|
vboxcommon.GuestAdditionsModeDisable,
|
|
|
|
vboxcommon.GuestAdditionsModeAttach,
|
|
|
|
vboxcommon.GuestAdditionsModeUpload,
|
2013-11-02 04:22:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, mode := range validModes {
|
|
|
|
if b.config.GuestAdditionsMode == mode {
|
|
|
|
validMode = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !validMode {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("guest_additions_mode is invalid. Must be one of: %v", validModes))
|
|
|
|
}
|
|
|
|
|
2013-07-06 05:28:56 -04:00
|
|
|
if b.config.GuestAdditionsSHA256 != "" {
|
|
|
|
b.config.GuestAdditionsSHA256 = strings.ToLower(b.config.GuestAdditionsSHA256)
|
|
|
|
}
|
|
|
|
|
2013-11-03 00:17:21 -04:00
|
|
|
// 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.")
|
|
|
|
}
|
|
|
|
|
2013-07-19 19:08:25 -04:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2013-11-03 00:17:21 -04:00
|
|
|
return warnings, errs
|
2013-06-11 18:45:52 -04:00
|
|
|
}
|
|
|
|
|
2013-11-03 00:17:21 -04:00
|
|
|
return warnings, nil
|
2013-06-11 18:12:45 -04:00
|
|
|
}
|
|
|
|
|
2013-06-12 19:06:56 -04:00
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
2013-08-13 11:55:33 -04:00
|
|
|
// Create the driver that we'll use to communicate with VirtualBox
|
2013-12-21 18:00:48 -05:00
|
|
|
driver, err := vboxcommon.NewDriver()
|
2013-08-13 11:55:33 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed creating VirtualBox driver: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:45:52 -04:00
|
|
|
steps := []multistep.Step{
|
2014-05-04 11:56:57 -04:00
|
|
|
&vboxcommon.StepDownloadGuestAdditions{
|
|
|
|
GuestAdditionsMode: b.config.GuestAdditionsMode,
|
|
|
|
GuestAdditionsURL: b.config.GuestAdditionsURL,
|
|
|
|
GuestAdditionsSHA256: b.config.GuestAdditionsSHA256,
|
2015-05-27 17:01:08 -04:00
|
|
|
Ctx: b.config.ctx,
|
2014-05-04 11:56:57 -04:00
|
|
|
},
|
2013-08-15 14:05:36 -04:00
|
|
|
&common.StepDownload{
|
|
|
|
Checksum: b.config.ISOChecksum,
|
|
|
|
ChecksumType: b.config.ISOChecksumType,
|
|
|
|
Description: "ISO",
|
2015-06-08 23:42:16 -04:00
|
|
|
Extension: "iso",
|
2015-10-20 19:27:47 -04:00
|
|
|
ResultKey: "iso_path",
|
2015-07-28 07:45:02 -04:00
|
|
|
TargetPath: b.config.TargetPath,
|
2015-10-20 19:27:47 -04:00
|
|
|
Url: b.config.ISOUrls,
|
2013-08-15 14:05:36 -04:00
|
|
|
},
|
2013-12-21 18:20:15 -05:00
|
|
|
&vboxcommon.StepOutputDir{
|
|
|
|
Force: b.config.PackerForce,
|
|
|
|
Path: b.config.OutputDir,
|
|
|
|
},
|
2013-07-09 15:29:40 -04:00
|
|
|
&common.StepCreateFloppy{
|
2016-10-01 03:04:50 -04:00
|
|
|
Files: b.config.FloppyConfig.FloppyFiles,
|
|
|
|
Directories: b.config.FloppyConfig.FloppyDirectories,
|
2013-07-09 15:29:40 -04:00
|
|
|
},
|
2015-11-01 17:29:24 -05:00
|
|
|
&common.StepHTTPServer{
|
2014-09-05 14:52:55 -04:00
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
|
|
|
},
|
2013-12-21 19:04:41 -05:00
|
|
|
new(vboxcommon.StepSuppressMessages),
|
2013-06-11 19:12:19 -04:00
|
|
|
new(stepCreateVM),
|
2013-06-11 19:34:44 -04:00
|
|
|
new(stepCreateDisk),
|
2013-06-11 23:07:11 -04:00
|
|
|
new(stepAttachISO),
|
2014-05-04 11:56:57 -04:00
|
|
|
&vboxcommon.StepAttachGuestAdditions{
|
|
|
|
GuestAdditionsMode: b.config.GuestAdditionsMode,
|
|
|
|
},
|
2015-08-22 18:41:08 -04:00
|
|
|
&vboxcommon.StepConfigureVRDP{
|
2016-05-23 09:24:16 -04:00
|
|
|
VRDPBindAddress: b.config.VRDPBindAddress,
|
|
|
|
VRDPPortMin: b.config.VRDPPortMin,
|
|
|
|
VRDPPortMax: b.config.VRDPPortMax,
|
2015-08-22 18:41:08 -04:00
|
|
|
},
|
2013-12-22 11:10:11 -05:00
|
|
|
new(vboxcommon.StepAttachFloppy),
|
2013-12-22 12:08:09 -05:00
|
|
|
&vboxcommon.StepForwardSSH{
|
2015-06-15 00:47:53 -04:00
|
|
|
CommConfig: &b.config.SSHConfig.Comm,
|
2014-03-27 02:11:34 -04:00
|
|
|
HostPortMin: b.config.SSHHostPortMin,
|
|
|
|
HostPortMax: b.config.SSHHostPortMax,
|
|
|
|
SkipNatMapping: b.config.SSHSkipNatMapping,
|
2013-12-22 12:08:09 -05:00
|
|
|
},
|
2013-12-22 12:24:29 -05:00
|
|
|
&vboxcommon.StepVBoxManage{
|
|
|
|
Commands: b.config.VBoxManage,
|
2015-05-27 17:01:08 -04:00
|
|
|
Ctx: b.config.ctx,
|
2013-12-22 12:24:29 -05:00
|
|
|
},
|
2013-12-22 13:30:12 -05:00
|
|
|
&vboxcommon.StepRun{
|
|
|
|
BootWait: b.config.BootWait,
|
|
|
|
Headless: b.config.Headless,
|
|
|
|
},
|
2014-05-12 22:02:30 -04:00
|
|
|
&vboxcommon.StepTypeBootCommand{
|
|
|
|
BootCommand: b.config.BootCommand,
|
|
|
|
VMName: b.config.VMName,
|
2015-05-27 17:01:08 -04:00
|
|
|
Ctx: b.config.ctx,
|
2014-05-12 22:02:30 -04:00
|
|
|
},
|
2015-06-13 18:08:12 -04:00
|
|
|
&communicator.StepConnect{
|
2015-06-13 19:23:33 -04:00
|
|
|
Config: &b.config.SSHConfig.Comm,
|
2016-08-24 12:30:26 -04:00
|
|
|
Host: vboxcommon.CommHost(b.config.SSHConfig.Comm.SSHHost),
|
2015-06-13 19:23:33 -04:00
|
|
|
SSHConfig: vboxcommon.SSHConfigFunc(b.config.SSHConfig),
|
|
|
|
SSHPort: vboxcommon.SSHPort,
|
2013-07-15 01:17:09 -04:00
|
|
|
},
|
2013-12-22 14:50:29 -05:00
|
|
|
&vboxcommon.StepUploadVersion{
|
|
|
|
Path: b.config.VBoxVersionFile,
|
|
|
|
},
|
2014-05-04 11:56:57 -04:00
|
|
|
&vboxcommon.StepUploadGuestAdditions{
|
|
|
|
GuestAdditionsMode: b.config.GuestAdditionsMode,
|
|
|
|
GuestAdditionsPath: b.config.GuestAdditionsPath,
|
2015-05-27 17:01:08 -04:00
|
|
|
Ctx: b.config.ctx,
|
2014-05-04 11:56:57 -04:00
|
|
|
},
|
2013-07-16 02:44:41 -04:00
|
|
|
new(common.StepProvision),
|
2013-12-22 12:37:27 -05:00
|
|
|
&vboxcommon.StepShutdown{
|
|
|
|
Command: b.config.ShutdownCommand,
|
|
|
|
Timeout: b.config.ShutdownTimeout,
|
2016-10-11 17:43:50 -04:00
|
|
|
Delay: b.config.PostShutdownDelay,
|
2013-12-22 12:37:27 -05:00
|
|
|
},
|
2013-12-22 12:54:00 -05:00
|
|
|
new(vboxcommon.StepRemoveDevices),
|
2014-03-12 19:14:44 -04:00
|
|
|
&vboxcommon.StepVBoxManage{
|
|
|
|
Commands: b.config.VBoxManagePost,
|
2015-05-27 17:01:08 -04:00
|
|
|
Ctx: b.config.ctx,
|
2014-03-12 19:14:44 -04:00
|
|
|
},
|
2013-12-22 13:40:39 -05:00
|
|
|
&vboxcommon.StepExport{
|
2014-03-27 02:11:34 -04:00
|
|
|
Format: b.config.Format,
|
|
|
|
OutputDir: b.config.OutputDir,
|
|
|
|
ExportOpts: b.config.ExportOpts.ExportOpts,
|
|
|
|
SkipNatMapping: b.config.SSHSkipNatMapping,
|
2013-12-22 13:40:39 -05:00
|
|
|
},
|
2013-06-11 18:45:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the state bag
|
2013-08-31 15:44:58 -04:00
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("cache", cache)
|
|
|
|
state.Put("config", &b.config)
|
2016-05-17 04:03:45 -04:00
|
|
|
state.Put("debug", b.config.PackerDebug)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2013-06-11 18:45:52 -04:00
|
|
|
|
|
|
|
// Run
|
2016-09-13 20:04:18 -04:00
|
|
|
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2013-06-11 18:45:52 -04:00
|
|
|
b.runner.Run(state)
|
|
|
|
|
2013-06-20 00:07:53 -04:00
|
|
|
// If there was an error, return that
|
2013-08-31 15:44:58 -04:00
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
2013-06-20 00:07:53 -04:00
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
2013-06-27 22:24:53 -04:00
|
|
|
// If we were interrupted or cancelled, then just exit.
|
2013-08-31 15:44:58 -04:00
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
2013-06-27 22:24:53 -04:00
|
|
|
return nil, errors.New("Build was cancelled.")
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:44:58 -04:00
|
|
|
if _, ok := state.GetOk(multistep.StateHalted); ok {
|
2013-06-27 22:24:53 -04:00
|
|
|
return nil, errors.New("Build was halted.")
|
|
|
|
}
|
|
|
|
|
2013-12-21 17:51:38 -05:00
|
|
|
return vboxcommon.NewArtifact(b.config.OutputDir)
|
2013-06-11 18:12:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Cancel() {
|
2013-06-11 18:45:52 -04:00
|
|
|
if b.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
|
|
|
}
|