2014-04-06 13:21:22 -04:00
|
|
|
package pvm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
parallelscommon "github.com/mitchellh/packer/builder/parallels/common"
|
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Builder implements packer.Builder and builds the actual Parallels
|
|
|
|
// 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 Parallels appliance.
|
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
|
|
|
// Create the driver that we'll use to communicate with Parallels
|
|
|
|
driver, err := parallelscommon.NewDriver()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed creating Paralles driver: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the state.
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", b.config)
|
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2014-05-13 17:39:00 -04:00
|
|
|
state.Put("http_port", uint(0))
|
2014-04-06 13:21:22 -04:00
|
|
|
|
|
|
|
// Build the steps.
|
|
|
|
steps := []multistep.Step{
|
|
|
|
¶llelscommon.StepOutputDir{
|
|
|
|
Force: b.config.PackerForce,
|
|
|
|
Path: b.config.OutputDir,
|
|
|
|
},
|
|
|
|
&common.StepCreateFloppy{
|
|
|
|
Files: b.config.FloppyFiles,
|
|
|
|
},
|
|
|
|
&StepImport{
|
|
|
|
Name: b.config.VMName,
|
|
|
|
SourcePath: b.config.SourcePath,
|
|
|
|
},
|
|
|
|
¶llelscommon.StepAttachParallelsTools{
|
|
|
|
ParallelsToolsHostPath: b.config.ParallelsToolsHostPath,
|
|
|
|
ParallelsToolsMode: b.config.ParallelsToolsMode,
|
|
|
|
},
|
|
|
|
new(parallelscommon.StepAttachFloppy),
|
|
|
|
¶llelscommon.StepPrlctl{
|
|
|
|
Commands: b.config.Prlctl,
|
|
|
|
Tpl: b.config.tpl,
|
|
|
|
},
|
|
|
|
¶llelscommon.StepRun{
|
|
|
|
BootWait: b.config.BootWait,
|
|
|
|
Headless: b.config.Headless,
|
|
|
|
},
|
2014-05-13 17:39:00 -04:00
|
|
|
¶llelscommon.StepTypeBootCommand{
|
|
|
|
BootCommand: b.config.BootCommand,
|
|
|
|
HostInterfaces: []string{},
|
|
|
|
VMName: b.config.VMName,
|
|
|
|
Tpl: b.config.tpl,
|
|
|
|
},
|
2014-04-06 13:21:22 -04:00
|
|
|
&common.StepConnectSSH{
|
|
|
|
SSHAddress: parallelscommon.SSHAddress,
|
|
|
|
SSHConfig: parallelscommon.SSHConfigFunc(b.config.SSHConfig),
|
|
|
|
SSHWaitTimeout: b.config.SSHWaitTimeout,
|
|
|
|
},
|
|
|
|
¶llelscommon.StepUploadVersion{
|
|
|
|
Path: b.config.PrlctlVersionFile,
|
|
|
|
},
|
|
|
|
¶llelscommon.StepUploadParallelsTools{
|
|
|
|
ParallelsToolsGuestPath: b.config.ParallelsToolsGuestPath,
|
|
|
|
ParallelsToolsHostPath: b.config.ParallelsToolsHostPath,
|
|
|
|
ParallelsToolsMode: b.config.ParallelsToolsMode,
|
|
|
|
Tpl: b.config.tpl,
|
|
|
|
},
|
|
|
|
new(common.StepProvision),
|
|
|
|
¶llelscommon.StepShutdown{
|
|
|
|
Command: b.config.ShutdownCommand,
|
|
|
|
Timeout: b.config.ShutdownTimeout,
|
|
|
|
},
|
|
|
|
new(parallelscommon.StepRemoveDevices),
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return parallelscommon.NewArtifact(b.config.OutputDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel.
|
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
|
|
|
}
|