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"
|
2014-09-02 12:35:59 -04:00
|
|
|
"math/rand"
|
|
|
|
"time"
|
2013-12-21 20:38:06 -05:00
|
|
|
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
|
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/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.
|
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
2014-09-02 12:35:59 -04:00
|
|
|
// Seed the random number generator
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
|
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)
|
2013-12-22 20:04:26 -05:00
|
|
|
state.Put("driver", driver)
|
2014-05-04 11:56:57 -04:00
|
|
|
state.Put("cache", cache)
|
2013-12-21 18:20:15 -05:00
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
// Build the steps.
|
|
|
|
steps := []multistep.Step{
|
2013-12-21 19:04:41 -05:00
|
|
|
&vboxcommon.StepOutputDir{
|
|
|
|
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{
|
|
|
|
Files: b.config.FloppyFiles,
|
|
|
|
},
|
2014-09-05 14:52:55 -04:00
|
|
|
&vboxcommon.StepHTTPServer{
|
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
|
|
|
},
|
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
|
|
|
},
|
2013-12-22 18:20:49 -05:00
|
|
|
&StepImport{
|
2014-09-05 13:23:37 -04:00
|
|
|
Name: b.config.VMName,
|
|
|
|
SourcePath: b.config.SourcePath,
|
|
|
|
ImportFlags: b.config.ImportFlags,
|
2013-12-22 18:20:49 -05:00
|
|
|
},
|
2014-05-04 11:56:57 -04:00
|
|
|
&vboxcommon.StepAttachGuestAdditions{
|
|
|
|
GuestAdditionsMode: b.config.GuestAdditionsMode,
|
|
|
|
},
|
2013-12-22 11:10:11 -05:00
|
|
|
new(vboxcommon.StepAttachFloppy),
|
2013-12-22 12:08:09 -05:00
|
|
|
&vboxcommon.StepForwardSSH{
|
2014-03-27 02:11:34 -04:00
|
|
|
GuestPort: b.config.SSHPort,
|
|
|
|
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{
|
|
|
|
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:03:56 -04:00
|
|
|
Ctx: b.config.ctx,
|
2014-05-12 22:02:30 -04:00
|
|
|
},
|
2013-12-22 12:08:09 -05:00
|
|
|
&common.StepConnectSSH{
|
|
|
|
SSHAddress: vboxcommon.SSHAddress,
|
|
|
|
SSHConfig: vboxcommon.SSHConfigFunc(b.config.SSHConfig),
|
|
|
|
SSHWaitTimeout: b.config.SSHWaitTimeout,
|
|
|
|
},
|
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: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),
|
2013-12-22 12:37:27 -05:00
|
|
|
&vboxcommon.StepShutdown{
|
|
|
|
Command: b.config.ShutdownCommand,
|
|
|
|
Timeout: b.config.ShutdownTimeout,
|
|
|
|
},
|
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: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,
|
2013-12-22 13:40:39 -05:00
|
|
|
},
|
2013-12-21 18:20:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the steps.
|
|
|
|
if b.config.PackerDebug {
|
|
|
|
b.runner = &multistep.DebugRunner{
|
|
|
|
Steps: steps,
|
|
|
|
PauseFn: common.MultistepDebugFn(ui),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
b.runner = &multistep.BasicRunner{Steps: steps}
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|