2014-04-06 13:21:22 -04:00
|
|
|
package pvm
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2014-04-06 13:21:22 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-06-13 18:43:27 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2017-04-04 16:39:01 -04:00
|
|
|
parallelscommon "github.com/hashicorp/packer/builder/parallels/common"
|
2020-12-01 18:30:31 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/communicator"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep/commonsteps"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2014-04-06 13:21:22 -04:00
|
|
|
)
|
|
|
|
|
2020-12-01 16:42:11 -05:00
|
|
|
// Builder implements packersdk.Builder and builds the actual Parallels
|
2014-04-06 13:21:22 -04:00
|
|
|
// images.
|
|
|
|
type Builder struct {
|
2019-12-17 05:25:56 -05:00
|
|
|
config Config
|
2014-04-06 13:21:22 -04: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...)
|
2014-04-06 13:21:22 -04:00
|
|
|
if errs != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, errs
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, nil
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:17:11 -05:00
|
|
|
// Run executes a Packer build and returns a packersdk.Artifact representing
|
2014-04-06 13:21:22 -04:00
|
|
|
// a Parallels appliance.
|
2020-11-19 18:10:00 -05:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
|
2014-04-06 13:21:22 -04:00
|
|
|
// Create the driver that we'll use to communicate with Parallels
|
|
|
|
driver, err := parallelscommon.NewDriver()
|
|
|
|
if err != nil {
|
2017-03-28 20:45:01 -04:00
|
|
|
return nil, fmt.Errorf("Failed creating Parallels driver: %s", err)
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the state.
|
|
|
|
state := new(multistep.BasicStateBag)
|
2019-12-19 11:01:55 -05:00
|
|
|
state.Put("config", &b.config)
|
2016-05-17 17:10:10 -04:00
|
|
|
state.Put("debug", b.config.PackerDebug)
|
2014-04-06 13:21:22 -04:00
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2019-03-19 09:47:21 -04:00
|
|
|
state.Put("http_port", 0)
|
2014-04-06 13:21:22 -04:00
|
|
|
|
|
|
|
// Build the steps.
|
|
|
|
steps := []multistep.Step{
|
2014-09-02 04:42:12 -04:00
|
|
|
¶llelscommon.StepPrepareParallelsTools{
|
|
|
|
ParallelsToolsMode: b.config.ParallelsToolsMode,
|
|
|
|
ParallelsToolsFlavor: b.config.ParallelsToolsFlavor,
|
|
|
|
},
|
2014-04-06 13:21:22 -04:00
|
|
|
¶llelscommon.StepOutputDir{
|
|
|
|
Force: b.config.PackerForce,
|
|
|
|
Path: b.config.OutputDir,
|
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepCreateFloppy{
|
2016-10-11 17:43:50 -04:00
|
|
|
Files: b.config.FloppyConfig.FloppyFiles,
|
2016-09-28 00:31:42 -04:00
|
|
|
Directories: b.config.FloppyConfig.FloppyDirectories,
|
2019-09-12 08:25:22 -04:00
|
|
|
Label: b.config.FloppyConfig.FloppyLabel,
|
2014-04-06 13:21:22 -04:00
|
|
|
},
|
|
|
|
&StepImport{
|
|
|
|
Name: b.config.VMName,
|
|
|
|
SourcePath: b.config.SourcePath,
|
|
|
|
},
|
|
|
|
¶llelscommon.StepAttachParallelsTools{
|
2014-09-02 05:28:04 -04:00
|
|
|
ParallelsToolsMode: b.config.ParallelsToolsMode,
|
2014-04-06 13:21:22 -04:00
|
|
|
},
|
|
|
|
new(parallelscommon.StepAttachFloppy),
|
|
|
|
¶llelscommon.StepPrlctl{
|
|
|
|
Commands: b.config.Prlctl,
|
2015-05-27 16:51:24 -04:00
|
|
|
Ctx: b.config.ctx,
|
2014-04-06 13:21:22 -04:00
|
|
|
},
|
2018-04-12 19:32:22 -04:00
|
|
|
¶llelscommon.StepRun{},
|
2014-05-13 17:39:00 -04:00
|
|
|
¶llelscommon.StepTypeBootCommand{
|
2018-04-16 00:54:02 -04:00
|
|
|
BootCommand: b.config.FlatBootCommand(),
|
2018-04-12 19:32:22 -04:00
|
|
|
BootWait: b.config.BootWait,
|
2014-05-13 17:39:00 -04:00
|
|
|
HostInterfaces: []string{},
|
|
|
|
VMName: b.config.VMName,
|
2015-05-27 16:51:24 -04:00
|
|
|
Ctx: b.config.ctx,
|
2018-08-22 14:25:43 -04:00
|
|
|
GroupInterval: b.config.BootConfig.BootGroupInterval,
|
2014-05-13 17:39:00 -04:00
|
|
|
},
|
2015-06-13 18:43:27 -04:00
|
|
|
&communicator.StepConnect{
|
2015-06-13 19:23:33 -04:00
|
|
|
Config: &b.config.SSHConfig.Comm,
|
2019-07-02 16:56:28 -04:00
|
|
|
Host: parallelscommon.CommHost(b.config.SSHConfig.Comm.SSHHost),
|
2018-08-22 12:23:23 -04:00
|
|
|
SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
|
2014-04-06 13:21:22 -04:00
|
|
|
},
|
|
|
|
¶llelscommon.StepUploadVersion{
|
|
|
|
Path: b.config.PrlctlVersionFile,
|
|
|
|
},
|
|
|
|
¶llelscommon.StepUploadParallelsTools{
|
2014-09-02 05:28:04 -04:00
|
|
|
ParallelsToolsFlavor: b.config.ParallelsToolsFlavor,
|
2014-04-06 13:21:22 -04:00
|
|
|
ParallelsToolsGuestPath: b.config.ParallelsToolsGuestPath,
|
|
|
|
ParallelsToolsMode: b.config.ParallelsToolsMode,
|
2015-05-27 16:51:24 -04:00
|
|
|
Ctx: b.config.ctx,
|
2014-04-06 13:21:22 -04:00
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
new(commonsteps.StepProvision),
|
2014-04-06 13:21:22 -04:00
|
|
|
¶llelscommon.StepShutdown{
|
|
|
|
Command: b.config.ShutdownCommand,
|
|
|
|
Timeout: b.config.ShutdownTimeout,
|
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepCleanupTempKeys{
|
2018-09-14 14:03:23 -04:00
|
|
|
Comm: &b.config.SSHConfig.Comm,
|
|
|
|
},
|
2015-05-03 05:18:48 -04:00
|
|
|
¶llelscommon.StepPrlctl{
|
|
|
|
Commands: b.config.PrlctlPost,
|
2015-06-22 15:46:13 -04:00
|
|
|
Ctx: b.config.ctx,
|
2015-05-03 05:18:48 -04:00
|
|
|
},
|
2018-04-26 08:09:17 -04:00
|
|
|
¶llelscommon.StepCompactDisk{
|
|
|
|
Skip: b.config.SkipCompaction,
|
|
|
|
},
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the steps.
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2014-04-06 13:21:22 -04: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.")
|
|
|
|
}
|
|
|
|
|
2020-01-30 05:27:58 -05:00
|
|
|
generatedData := map[string]interface{}{"generated_data": state.Get("generated_data")}
|
|
|
|
return parallelscommon.NewArtifact(b.config.OutputDir, generatedData)
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel.
|