2019-02-16 08:32:22 -05:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
2019-05-11 09:12:40 -04:00
|
|
|
"context"
|
2019-02-16 08:32:22 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2019-02-16 08:32:22 -05:00
|
|
|
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
|
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Builder implements packer.Builder and builds the actual VirtualBox
|
|
|
|
// images.
|
|
|
|
type Builder struct {
|
2020-01-07 10:55:50 -05:00
|
|
|
config Config
|
2019-02-16 08:32:22 -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...)
|
2019-02-16 08:32:22 -05:00
|
|
|
if errs != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, errs
|
2019-02-16 08:32:22 -05:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, nil
|
2019-02-16 08:32:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes a Packer build and returns a packer.Artifact representing
|
|
|
|
// a VirtualBox appliance.
|
2019-05-11 09:12:40 -04:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2019-02-16 08:32:22 -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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the state.
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("debug", b.config.PackerDebug)
|
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
// Build the steps.
|
|
|
|
steps := []multistep.Step{
|
|
|
|
new(vboxcommon.StepSuppressMessages),
|
|
|
|
&common.StepCreateFloppy{
|
|
|
|
Files: b.config.FloppyConfig.FloppyFiles,
|
|
|
|
Directories: b.config.FloppyConfig.FloppyDirectories,
|
|
|
|
},
|
2020-09-11 17:15:17 -04:00
|
|
|
&common.StepCreateCD{
|
|
|
|
Files: b.config.CDConfig.CDFiles,
|
|
|
|
Label: b.config.CDConfig.CDLabel,
|
|
|
|
},
|
2019-02-16 08:32:22 -05:00
|
|
|
&StepSetSnapshot{
|
|
|
|
Name: b.config.VMName,
|
|
|
|
AttachSnapshot: b.config.AttachSnapshot,
|
2019-03-10 10:38:38 -04:00
|
|
|
KeepRegistered: b.config.KeepRegistered,
|
2019-02-16 08:32:22 -05:00
|
|
|
},
|
2020-02-07 04:38:48 -05:00
|
|
|
new(vboxcommon.StepHTTPIPDiscover),
|
2019-02-16 08:32:22 -05:00
|
|
|
&common.StepHTTPServer{
|
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
2020-05-27 16:34:24 -04:00
|
|
|
HTTPAddress: b.config.HTTPAddress,
|
2019-02-16 08:32:22 -05:00
|
|
|
},
|
|
|
|
&vboxcommon.StepDownloadGuestAdditions{
|
|
|
|
GuestAdditionsMode: b.config.GuestAdditionsMode,
|
|
|
|
GuestAdditionsURL: b.config.GuestAdditionsURL,
|
|
|
|
GuestAdditionsSHA256: b.config.GuestAdditionsSHA256,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
},
|
|
|
|
&StepImport{
|
|
|
|
Name: b.config.VMName,
|
|
|
|
},
|
2020-09-11 17:15:17 -04:00
|
|
|
&vboxcommon.StepAttachISOs{
|
|
|
|
AttachBootIso: false,
|
|
|
|
ISOInterface: b.config.GuestAdditionsInterface,
|
|
|
|
GuestAdditionsMode: b.config.GuestAdditionsMode,
|
|
|
|
GuestAdditionsInterface: b.config.GuestAdditionsInterface,
|
2019-02-16 08:32:22 -05:00
|
|
|
},
|
|
|
|
&vboxcommon.StepConfigureVRDP{
|
|
|
|
VRDPBindAddress: b.config.VRDPBindAddress,
|
|
|
|
VRDPPortMin: b.config.VRDPPortMin,
|
|
|
|
VRDPPortMax: b.config.VRDPPortMax,
|
|
|
|
},
|
|
|
|
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,
|
2019-02-16 08:32:22 -05:00
|
|
|
},
|
|
|
|
&vboxcommon.StepVBoxManage{
|
|
|
|
Commands: b.config.VBoxManage,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
},
|
|
|
|
&vboxcommon.StepRun{
|
|
|
|
Headless: b.config.Headless,
|
|
|
|
},
|
|
|
|
&vboxcommon.StepTypeBootCommand{
|
|
|
|
BootWait: b.config.BootWait,
|
|
|
|
BootCommand: b.config.FlatBootCommand(),
|
|
|
|
VMName: b.config.VMName,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
GroupInterval: b.config.BootConfig.BootGroupInterval,
|
2019-03-10 10:38:38 -04:00
|
|
|
Comm: &b.config.Comm,
|
2019-02-16 08:32:22 -05: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,
|
2019-02-16 08:32:22 -05:00
|
|
|
},
|
|
|
|
&vboxcommon.StepUploadVersion{
|
|
|
|
Path: *b.config.VBoxVersionFile,
|
|
|
|
},
|
|
|
|
&vboxcommon.StepUploadGuestAdditions{
|
|
|
|
GuestAdditionsMode: b.config.GuestAdditionsMode,
|
|
|
|
GuestAdditionsPath: b.config.GuestAdditionsPath,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
},
|
|
|
|
new(common.StepProvision),
|
|
|
|
&common.StepCleanupTempKeys{
|
2020-01-09 11:37:30 -05:00
|
|
|
Comm: &b.config.CommConfig.Comm,
|
2019-02-16 08:32:22 -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,
|
2019-02-16 08:32:22 -05:00
|
|
|
},
|
2020-09-11 17:15:17 -04:00
|
|
|
&vboxcommon.StepRemoveDevices{},
|
2019-02-16 08:32:22 -05:00
|
|
|
&vboxcommon.StepVBoxManage{
|
|
|
|
Commands: b.config.VBoxManagePost,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
},
|
|
|
|
&StepCreateSnapshot{
|
|
|
|
Name: b.config.VMName,
|
|
|
|
TargetSnapshot: b.config.TargetSnapshot,
|
|
|
|
},
|
|
|
|
&vboxcommon.StepExport{
|
|
|
|
Format: b.config.Format,
|
|
|
|
OutputDir: b.config.OutputDir,
|
2020-05-05 19:09:05 -04:00
|
|
|
OutputFilename: b.config.OutputFilename,
|
2019-08-21 06:28:34 -04:00
|
|
|
ExportOpts: b.config.ExportOpts,
|
2020-01-09 11:37:30 -05:00
|
|
|
SkipNatMapping: b.config.SkipNatMapping,
|
2019-02-16 08:32:22 -05:00
|
|
|
SkipExport: b.config.SkipExport,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-03-10 10:38:38 -04:00
|
|
|
if !b.config.SkipExport {
|
|
|
|
steps = append(steps, nil)
|
|
|
|
copy(steps[1:], steps)
|
|
|
|
steps[0] = &common.StepOutputDir{
|
|
|
|
Force: b.config.PackerForce,
|
|
|
|
Path: b.config.OutputDir,
|
|
|
|
}
|
|
|
|
}
|
2019-02-16 08:32:22 -05:00
|
|
|
// Run the steps.
|
|
|
|
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-05-11 09:12:40 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2019-02-16 08:32:22 -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 {
|
2019-08-03 07:43:11 -04:00
|
|
|
return nil, errors.New("build was cancelled")
|
2019-02-16 08:32:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk(multistep.StateHalted); ok {
|
2019-08-03 07:43:11 -04:00
|
|
|
return nil, errors.New("build was halted")
|
2019-02-16 08:32:22 -05:00
|
|
|
}
|
|
|
|
|
2019-03-10 10:38:38 -04:00
|
|
|
if b.config.SkipExport {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-08-03 07:43:11 -04:00
|
|
|
|
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)
|
2019-02-16 08:32:22 -05:00
|
|
|
}
|