2013-12-21 18:20:15 -05:00
|
|
|
package ovf
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
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
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2017-04-04 16:39:01 -04:00
|
|
|
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep/commonsteps"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-12-21 18:20:15 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Builder implements packer.Builder and builds the actual VirtualBox
|
|
|
|
// images.
|
|
|
|
type Builder struct {
|
2019-12-17 05:25:56 -05:00
|
|
|
config Config
|
2013-12-21 18:20:15 -05:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2019-12-17 05:25:56 -05:00
|
|
|
warnings, errs := b.config.Prepare(raws...)
|
2013-12-21 18:20:15 -05:00
|
|
|
if errs != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, errs
|
2013-12-21 18:20:15 -05:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, nil
|
2013-12-21 18:20:15 -05:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:17:11 -05:00
|
|
|
// Run executes a Packer build and returns a packersdk.Artifact representing
|
2013-12-21 18:20:15 -05:00
|
|
|
// a VirtualBox appliance.
|
2020-11-19 18:10:00 -05:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.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)
|
2019-12-19 11:01:55 -05:00
|
|
|
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{
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.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),
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepCreateFloppy{
|
2016-10-01 03:04:50 -04:00
|
|
|
Files: b.config.FloppyConfig.FloppyFiles,
|
|
|
|
Directories: b.config.FloppyConfig.FloppyDirectories,
|
2019-09-12 08:25:22 -04:00
|
|
|
Label: b.config.FloppyConfig.FloppyLabel,
|
2013-12-22 10:58:07 -05:00
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepCreateCD{
|
2020-09-11 17:15:17 -04:00
|
|
|
Files: b.config.CDConfig.CDFiles,
|
|
|
|
Label: b.config.CDConfig.CDLabel,
|
|
|
|
},
|
2020-02-07 04:38:48 -05:00
|
|
|
new(vboxcommon.StepHTTPIPDiscover),
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepHTTPServer{
|
2014-09-05 14:52:55 -04:00
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
2020-05-27 16:34:24 -04:00
|
|
|
HTTPAddress: b.config.HTTPAddress,
|
2014-09-05 14:52:55 -04:00
|
|
|
},
|
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
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepDownload{
|
2020-05-28 05:02:09 -04:00
|
|
|
Checksum: b.config.Checksum,
|
|
|
|
Description: "OVF/OVA",
|
|
|
|
Extension: "ova",
|
|
|
|
ResultKey: "vm_path",
|
|
|
|
TargetPath: b.config.TargetPath,
|
|
|
|
Url: []string{b.config.SourcePath},
|
2016-10-10 22:16:25 -04:00
|
|
|
},
|
2013-12-22 18:20:49 -05:00
|
|
|
&StepImport{
|
2019-12-20 05:50:18 -05:00
|
|
|
Name: b.config.VMName,
|
|
|
|
ImportFlags: b.config.ImportFlags,
|
|
|
|
KeepRegistered: b.config.KeepRegistered,
|
2013-12-22 18:20:49 -05:00
|
|
|
},
|
2020-09-11 17:15:17 -04:00
|
|
|
&vboxcommon.StepAttachISOs{
|
2020-09-14 13:40:07 -04:00
|
|
|
AttachBootISO: false,
|
2020-09-11 17:15:17 -04:00
|
|
|
ISOInterface: b.config.GuestAdditionsInterface,
|
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),
|
2020-01-09 11:37:30 -05:00
|
|
|
&vboxcommon.StepPortForwarding{
|
|
|
|
CommConfig: &b.config.CommConfig.Comm,
|
|
|
|
HostPortMin: b.config.HostPortMin,
|
|
|
|
HostPortMax: b.config.HostPortMax,
|
|
|
|
SkipNatMapping: b.config.SkipNatMapping,
|
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{
|
2020-01-09 11:37:30 -05:00
|
|
|
Config: &b.config.CommConfig.Comm,
|
|
|
|
Host: vboxcommon.CommHost(b.config.CommConfig.Comm.SSHHost),
|
|
|
|
SSHConfig: b.config.CommConfig.Comm.SSHConfigFunc(),
|
|
|
|
SSHPort: vboxcommon.CommPort,
|
|
|
|
WinRMPort: vboxcommon.CommPort,
|
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
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
new(commonsteps.StepProvision),
|
|
|
|
&commonsteps.StepCleanupTempKeys{
|
2020-01-09 11:37:30 -05:00
|
|
|
Comm: &b.config.CommConfig.Comm,
|
2018-09-14 14:03:23 -04:00
|
|
|
},
|
2013-12-22 12:37:27 -05:00
|
|
|
&vboxcommon.StepShutdown{
|
2019-12-05 08:34:56 -05:00
|
|
|
Command: b.config.ShutdownCommand,
|
|
|
|
Timeout: b.config.ShutdownTimeout,
|
|
|
|
Delay: b.config.PostShutdownDelay,
|
|
|
|
DisableShutdown: b.config.DisableShutdown,
|
2020-01-09 11:36:19 -05:00
|
|
|
ACPIShutdown: b.config.ACPIShutdown,
|
2013-12-22 12:37:27 -05:00
|
|
|
},
|
2020-09-11 17:15:17 -04:00
|
|
|
&vboxcommon.StepRemoveDevices{},
|
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,
|
2020-05-05 19:09:05 -04:00
|
|
|
OutputFilename: b.config.OutputFilename,
|
2019-06-19 10:34:14 -04:00
|
|
|
ExportOpts: b.config.ExportConfig.ExportOpts,
|
2020-01-09 11:37:30 -05:00
|
|
|
SkipNatMapping: b.config.SkipNatMapping,
|
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.
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2013-12-21 18:20:15 -05:00
|
|
|
|
|
|
|
// 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.")
|
|
|
|
}
|
|
|
|
|
2020-01-30 05:27:58 -05:00
|
|
|
generatedData := map[string]interface{}{"generated_data": state.Get("generated_data")}
|
|
|
|
return vboxcommon.NewArtifact(b.config.OutputDir, generatedData)
|
2013-12-21 18:20:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel.
|