Files
Packer-Cn/builder/parallels/pvm/builder.go
T

142 lines
4.1 KiB
Go
Raw Normal View History

package pvm
import (
"context"
"errors"
"fmt"
2015-06-13 18:43:27 -04:00
2019-12-17 11:25:56 +01:00
"github.com/hashicorp/hcl/v2/hcldec"
2017-04-04 13:39:01 -07:00
parallelscommon "github.com/hashicorp/packer/builder/parallels/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"
)
// Builder implements packer.Builder and builds the actual Parallels
// images.
type Builder struct {
2019-12-17 11:25:56 +01:00
config Config
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...)
if errs != nil {
return nil, warnings, errs
}
return nil, warnings, nil
}
// Run executes a Packer build and returns a packer.Artifact representing
// a Parallels 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 Parallels
driver, err := parallelscommon.NewDriver()
if err != nil {
2017-03-28 17:45:01 -07:00
return nil, fmt.Errorf("Failed creating Parallels driver: %s", err)
}
// 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)
state.Put("hook", hook)
state.Put("ui", ui)
2019-03-19 14:47:21 +01:00
state.Put("http_port", 0)
// Build the steps.
steps := []multistep.Step{
&parallelscommon.StepPrepareParallelsTools{
ParallelsToolsMode: b.config.ParallelsToolsMode,
ParallelsToolsFlavor: b.config.ParallelsToolsFlavor,
},
&parallelscommon.StepOutputDir{
Force: b.config.PackerForce,
Path: b.config.OutputDir,
},
&common.StepCreateFloppy{
2016-10-11 23:43: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,
},
&StepImport{
Name: b.config.VMName,
SourcePath: b.config.SourcePath,
},
&parallelscommon.StepAttachParallelsTools{
ParallelsToolsMode: b.config.ParallelsToolsMode,
},
new(parallelscommon.StepAttachFloppy),
&parallelscommon.StepPrlctl{
Commands: b.config.Prlctl,
2015-05-27 13:51:24 -07:00
Ctx: b.config.ctx,
},
&parallelscommon.StepRun{},
&parallelscommon.StepTypeBootCommand{
2018-04-15 21:54:02 -07:00
BootCommand: b.config.FlatBootCommand(),
BootWait: b.config.BootWait,
HostInterfaces: []string{},
VMName: b.config.VMName,
2015-05-27 13:51:24 -07:00
Ctx: b.config.ctx,
GroupInterval: b.config.BootConfig.BootGroupInterval,
},
2015-06-13 18:43:27 -04:00
&communicator.StepConnect{
2015-06-13 19:23:33 -04:00
Config: &b.config.SSHConfig.Comm,
Host: parallelscommon.CommHost(b.config.SSHConfig.Comm.SSHHost),
SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
},
&parallelscommon.StepUploadVersion{
Path: b.config.PrlctlVersionFile,
},
&parallelscommon.StepUploadParallelsTools{
ParallelsToolsFlavor: b.config.ParallelsToolsFlavor,
ParallelsToolsGuestPath: b.config.ParallelsToolsGuestPath,
ParallelsToolsMode: b.config.ParallelsToolsMode,
2015-05-27 13:51:24 -07:00
Ctx: b.config.ctx,
},
new(common.StepProvision),
&parallelscommon.StepShutdown{
Command: b.config.ShutdownCommand,
Timeout: b.config.ShutdownTimeout,
},
&common.StepCleanupTempKeys{
Comm: &b.config.SSHConfig.Comm,
},
&parallelscommon.StepPrlctl{
Commands: b.config.PrlctlPost,
2015-06-22 12:46:13 -07:00
Ctx: b.config.ctx,
},
&parallelscommon.StepCompactDisk{
Skip: b.config.SkipCompaction,
},
}
// Run the steps.
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(ctx, 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.")
}
generatedData := map[string]interface{}{"generated_data": state.Get("generated_data")}
return parallelscommon.NewArtifact(b.config.OutputDir, generatedData)
}
// Cancel.