Files
Packer-Cn/builder/virtualbox/ovf/builder.go
T

191 lines
5.7 KiB
Go
Raw Normal View History

2013-12-21 15:20:15 -08:00
package ovf
import (
"context"
"errors"
"fmt"
2019-12-17 11:25:56 +01:00
"github.com/hashicorp/hcl/v2/hcldec"
2017-04-04 13:39:01 -07:00
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
2018-01-19 16:18:44 -08:00
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 13:39:01 -07:00
"github.com/hashicorp/packer/packer"
2013-12-21 15:20:15 -08:00
)
// Builder implements packer.Builder and builds the actual VirtualBox
// images.
type Builder struct {
2019-12-17 11:25:56 +01:00
config Config
2013-12-21 15:20:15 -08:00
runner multistep.Runner
}
2019-12-17 11:25:56 +01:00
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
2019-12-17 11:25:56 +01:00
warnings, errs := b.config.Prepare(raws...)
2013-12-21 15:20:15 -08:00
if errs != nil {
return nil, warnings, errs
2013-12-21 15:20:15 -08:00
}
return nil, warnings, nil
2013-12-21 15:20:15 -08:00
}
// Run executes a Packer build and returns a packer.Artifact representing
// a VirtualBox appliance.
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
// 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 15:20:15 -08:00
// Set up the state.
state := new(multistep.BasicStateBag)
2019-12-19 17:01:55 +01:00
state.Put("config", &b.config)
state.Put("debug", b.config.PackerDebug)
state.Put("driver", driver)
2013-12-21 15:20:15 -08:00
state.Put("hook", hook)
state.Put("ui", ui)
// Build the steps.
steps := []multistep.Step{
&common.StepOutputDir{
Force: b.config.PackerForce,
Path: b.config.OutputDir,
},
new(vboxcommon.StepSuppressMessages),
2013-12-22 07:58:07 -08:00
&common.StepCreateFloppy{
2016-10-01 09:04:50 +02:00
Files: b.config.FloppyConfig.FloppyFiles,
Directories: b.config.FloppyConfig.FloppyDirectories,
2019-09-12 12:25:22 +00:00
Label: b.config.FloppyConfig.FloppyLabel,
2013-12-22 07:58:07 -08:00
},
new(vboxcommon.StepHTTPIPDiscover),
2015-11-01 14:29:24 -08:00
&common.StepHTTPServer{
HTTPDir: b.config.HTTPDir,
HTTPPortMin: b.config.HTTPPortMin,
HTTPPortMax: b.config.HTTPPortMax,
HTTPAddress: b.config.HTTPAddress,
},
&vboxcommon.StepSshKeyPair{
Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("%s.pem", b.config.PackerBuildName),
Comm: &b.config.Comm,
},
&vboxcommon.StepDownloadGuestAdditions{
GuestAdditionsMode: b.config.GuestAdditionsMode,
GuestAdditionsURL: b.config.GuestAdditionsURL,
GuestAdditionsSHA256: b.config.GuestAdditionsSHA256,
2015-05-27 14:03:56 -07:00
Ctx: b.config.ctx,
},
&common.StepDownload{
Checksum: b.config.Checksum,
Description: "OVF/OVA",
Extension: "ova",
ResultKey: "vm_path",
TargetPath: b.config.TargetPath,
Url: []string{b.config.SourcePath},
},
2013-12-22 15:20:49 -08:00
&StepImport{
2019-12-20 11:50:18 +01:00
Name: b.config.VMName,
ImportFlags: b.config.ImportFlags,
KeepRegistered: b.config.KeepRegistered,
2013-12-22 15:20:49 -08:00
},
&vboxcommon.StepAttachGuestAdditions{
2019-02-08 09:28:10 -08:00
GuestAdditionsMode: b.config.GuestAdditionsMode,
GuestAdditionsInterface: b.config.GuestAdditionsInterface,
},
&vboxcommon.StepConfigureVRDP{
VRDPBindAddress: b.config.VRDPBindAddress,
VRDPPortMin: b.config.VRDPPortMin,
VRDPPortMax: b.config.VRDPPortMax,
},
2013-12-22 08:10:11 -08:00
new(vboxcommon.StepAttachFloppy),
&vboxcommon.StepPortForwarding{
CommConfig: &b.config.CommConfig.Comm,
HostPortMin: b.config.HostPortMin,
HostPortMax: b.config.HostPortMax,
SkipNatMapping: b.config.SkipNatMapping,
2013-12-22 09:08:09 -08:00
},
2013-12-22 09:24:29 -08:00
&vboxcommon.StepVBoxManage{
Commands: b.config.VBoxManage,
2015-05-27 14:03:56 -07:00
Ctx: b.config.ctx,
2013-12-22 09:24:29 -08:00
},
2013-12-22 10:30:12 -08:00
&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,
Comm: &b.config.Comm,
},
&communicator.StepConnect{
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 09:08:09 -08:00
},
2013-12-22 11:50:29 -08:00
&vboxcommon.StepUploadVersion{
Path: *b.config.VBoxVersionFile,
2013-12-22 11:50:29 -08:00
},
&vboxcommon.StepUploadGuestAdditions{
GuestAdditionsMode: b.config.GuestAdditionsMode,
GuestAdditionsPath: b.config.GuestAdditionsPath,
2015-05-27 14:03:56 -07:00
Ctx: b.config.ctx,
},
2013-12-22 09:24:29 -08:00
new(common.StepProvision),
&common.StepCleanupTempKeys{
Comm: &b.config.CommConfig.Comm,
},
2013-12-22 09:37:27 -08:00
&vboxcommon.StepShutdown{
Command: b.config.ShutdownCommand,
Timeout: b.config.ShutdownTimeout,
Delay: b.config.PostShutdownDelay,
DisableShutdown: b.config.DisableShutdown,
ACPIShutdown: b.config.ACPIShutdown,
2013-12-22 09:37:27 -08:00
},
2019-02-08 09:28:10 -08:00
&vboxcommon.StepRemoveDevices{
GuestAdditionsInterface: b.config.GuestAdditionsInterface,
},
&vboxcommon.StepVBoxManage{
Commands: b.config.VBoxManagePost,
2015-05-27 14:03:56 -07:00
Ctx: b.config.ctx,
},
2013-12-22 10:40:39 -08:00
&vboxcommon.StepExport{
Format: b.config.Format,
OutputDir: b.config.OutputDir,
2020-05-05 16:09:05 -07:00
OutputFilename: b.config.OutputFilename,
ExportOpts: b.config.ExportConfig.ExportOpts,
SkipNatMapping: b.config.SkipNatMapping,
SkipExport: b.config.SkipExport,
2013-12-22 10:40:39 -08:00
},
2013-12-21 15:20:15 -08:00
}
// Run the steps.
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(ctx, state)
2013-12-21 15:20:15 -08: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.")
}
generatedData := map[string]interface{}{"generated_data": state.Get("generated_data")}
return vboxcommon.NewArtifact(b.config.OutputDir, generatedData)
2013-12-21 15:20:15 -08:00
}
// Cancel.