2013-12-21 18:20:15 -05:00
|
|
|
package ovf
|
|
|
|
|
|
|
|
import (
|
2013-12-21 20:38:06 -05:00
|
|
|
"errors"
|
2013-12-22 20:04:26 -05:00
|
|
|
"fmt"
|
2013-12-21 20:38:06 -05:00
|
|
|
"log"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
|
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-12-21 18:20:15 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Builder implements packer.Builder and builds the actual VirtualBox
|
|
|
|
// images.
|
|
|
|
type Builder struct {
|
|
|
|
config *Config
|
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare processes the build configuration parameters.
|
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
|
|
|
c, warnings, errs := NewConfig(raws...)
|
|
|
|
if errs != nil {
|
|
|
|
return warnings, errs
|
|
|
|
}
|
|
|
|
b.config = c
|
|
|
|
|
|
|
|
return warnings, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes a Packer build and returns a packer.Artifact representing
|
|
|
|
// a VirtualBox appliance.
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2013-12-22 20:04:26 -05:00
|
|
|
// Create the driver that we'll use to communicate with VirtualBox
|
|
|
|
driver, err := vboxcommon.NewDriver()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed creating VirtualBox driver: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-12-21 18:20:15 -05:00
|
|
|
// Set up the state.
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", b.config)
|
2016-05-17 04:03:45 -04:00
|
|
|
state.Put("debug", b.config.PackerDebug)
|
2013-12-22 20:04:26 -05:00
|
|
|
state.Put("driver", driver)
|
2013-12-21 18:20:15 -05:00
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
// Build the steps.
|
|
|
|
steps := []multistep.Step{
|
2019-01-11 18:06:36 -05:00
|
|
|
&common.StepOutputDir{
|
2013-12-21 19:04:41 -05:00
|
|
|
Force: b.config.PackerForce,
|
|
|
|
Path: b.config.OutputDir,
|
|
|
|
},
|
2013-12-21 20:38:06 -05:00
|
|
|
new(vboxcommon.StepSuppressMessages),
|
2013-12-22 10:58:07 -05:00
|
|
|
&common.StepCreateFloppy{
|
2016-10-01 03:04:50 -04:00
|
|
|
Files: b.config.FloppyConfig.FloppyFiles,
|
|
|
|
Directories: b.config.FloppyConfig.FloppyDirectories,
|
2013-12-22 10:58:07 -05: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,
|
|
|
|
},
|
2019-02-03 12:20:52 -05:00
|
|
|
&vboxcommon.StepSshKeyPair{
|
|
|
|
Debug: b.config.PackerDebug,
|
2019-02-04 12:47:18 -05:00
|
|
|
DebugKeyPath: fmt.Sprintf("%s.pem", b.config.PackerBuildName),
|
2019-02-03 12:20:52 -05:00
|
|
|
Comm: &b.config.Comm,
|
|
|
|
},
|
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:03:56 -04:00
|
|
|
Ctx: b.config.ctx,
|
2014-05-04 11:56:57 -04:00
|
|
|
},
|
2016-10-10 22:16:25 -04:00
|
|
|
&common.StepDownload{
|
|
|
|
Checksum: b.config.Checksum,
|
|
|
|
ChecksumType: b.config.ChecksumType,
|
|
|
|
Description: "OVF/OVA",
|
|
|
|
Extension: "ova",
|
|
|
|
ResultKey: "vm_path",
|
|
|
|
TargetPath: b.config.TargetPath,
|
|
|
|
Url: []string{b.config.SourcePath},
|
|
|
|
},
|
2013-12-22 18:20:49 -05:00
|
|
|
&StepImport{
|
2014-09-05 13:23:37 -04:00
|
|
|
Name: b.config.VMName,
|
|
|
|
ImportFlags: b.config.ImportFlags,
|
2013-12-22 18:20:49 -05:00
|
|
|
},
|
2014-05-04 11:56:57 -04:00
|
|
|
&vboxcommon.StepAttachGuestAdditions{
|
2019-02-08 12:28:10 -05:00
|
|
|
GuestAdditionsMode: b.config.GuestAdditionsMode,
|
|
|
|
GuestAdditionsInterface: b.config.GuestAdditionsInterface,
|
2014-05-04 11:56:57 -04:00
|
|
|
},
|
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:03:56 -04:00
|
|
|
Ctx: b.config.ctx,
|
2013-12-22 12:24:29 -05:00
|
|
|
},
|
2013-12-22 13:30:12 -05:00
|
|
|
&vboxcommon.StepRun{
|
|
|
|
Headless: b.config.Headless,
|
|
|
|
},
|
2014-05-12 22:02:30 -04:00
|
|
|
&vboxcommon.StepTypeBootCommand{
|
2018-08-22 14:25:43 -04:00
|
|
|
BootWait: b.config.BootWait,
|
|
|
|
BootCommand: b.config.FlatBootCommand(),
|
|
|
|
VMName: b.config.VMName,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
GroupInterval: b.config.BootConfig.BootGroupInterval,
|
2019-02-03 12:20:52 -05:00
|
|
|
Comm: &b.config.Comm,
|
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),
|
2018-08-22 11:02:23 -04:00
|
|
|
SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
|
2015-06-13 19:23:33 -04:00
|
|
|
SSHPort: vboxcommon.SSHPort,
|
2016-12-23 10:55:10 -05:00
|
|
|
WinRMPort: vboxcommon.SSHPort,
|
2013-12-22 12:08:09 -05:00
|
|
|
},
|
2013-12-22 14:50:29 -05:00
|
|
|
&vboxcommon.StepUploadVersion{
|
2017-03-16 21:04:36 -04:00
|
|
|
Path: *b.config.VBoxVersionFile,
|
2013-12-22 14:50:29 -05:00
|
|
|
},
|
2014-05-04 11:56:57 -04:00
|
|
|
&vboxcommon.StepUploadGuestAdditions{
|
|
|
|
GuestAdditionsMode: b.config.GuestAdditionsMode,
|
|
|
|
GuestAdditionsPath: b.config.GuestAdditionsPath,
|
2015-05-27 17:03:56 -04:00
|
|
|
Ctx: b.config.ctx,
|
2014-05-04 11:56:57 -04:00
|
|
|
},
|
2013-12-22 12:24:29 -05:00
|
|
|
new(common.StepProvision),
|
2018-09-14 14:03:23 -04:00
|
|
|
&common.StepCleanupTempKeys{
|
|
|
|
Comm: &b.config.SSHConfig.Comm,
|
|
|
|
},
|
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
|
|
|
},
|
2019-02-08 12:28:10 -05:00
|
|
|
&vboxcommon.StepRemoveDevices{
|
|
|
|
GuestAdditionsInterface: b.config.GuestAdditionsInterface,
|
|
|
|
},
|
2014-03-12 19:14:44 -04:00
|
|
|
&vboxcommon.StepVBoxManage{
|
|
|
|
Commands: b.config.VBoxManagePost,
|
2015-05-27 17:03:56 -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,
|
2016-12-29 08:19:59 -05:00
|
|
|
SkipExport: b.config.SkipExport,
|
2013-12-22 13:40:39 -05:00
|
|
|
},
|
2013-12-21 18:20:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the steps.
|
2016-09-13 20:04:18 -04:00
|
|
|
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2013-12-21 18:20:15 -05:00
|
|
|
b.runner.Run(state)
|
|
|
|
|
|
|
|
// Report any errors.
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were interrupted or cancelled, then just exit.
|
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
|
|
|
return nil, errors.New("Build was cancelled.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk(multistep.StateHalted); ok {
|
|
|
|
return nil, errors.New("Build was halted.")
|
|
|
|
}
|
|
|
|
|
2013-12-21 20:38:06 -05:00
|
|
|
return vboxcommon.NewArtifact(b.config.OutputDir)
|
2013-12-21 18:20:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel.
|
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
|
|
|
}
|