github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/parallels/pvm/builder.go (about) 1 package pvm 2 3 import ( 4 "errors" 5 "fmt" 6 "log" 7 8 parallelscommon "github.com/hashicorp/packer/builder/parallels/common" 9 "github.com/hashicorp/packer/common" 10 "github.com/hashicorp/packer/helper/communicator" 11 "github.com/hashicorp/packer/helper/multistep" 12 "github.com/hashicorp/packer/packer" 13 ) 14 15 // Builder implements packer.Builder and builds the actual Parallels 16 // images. 17 type Builder struct { 18 config *Config 19 runner multistep.Runner 20 } 21 22 // Prepare processes the build configuration parameters. 23 func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { 24 c, warnings, errs := NewConfig(raws...) 25 if errs != nil { 26 return warnings, errs 27 } 28 b.config = c 29 30 return warnings, nil 31 } 32 33 // Run executes a Packer build and returns a packer.Artifact representing 34 // a Parallels appliance. 35 func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { 36 // Create the driver that we'll use to communicate with Parallels 37 driver, err := parallelscommon.NewDriver() 38 if err != nil { 39 return nil, fmt.Errorf("Failed creating Parallels driver: %s", err) 40 } 41 42 // Set up the state. 43 state := new(multistep.BasicStateBag) 44 state.Put("config", b.config) 45 state.Put("debug", b.config.PackerDebug) 46 state.Put("driver", driver) 47 state.Put("hook", hook) 48 state.Put("ui", ui) 49 state.Put("http_port", uint(0)) 50 51 // Build the steps. 52 steps := []multistep.Step{ 53 ¶llelscommon.StepPrepareParallelsTools{ 54 ParallelsToolsMode: b.config.ParallelsToolsMode, 55 ParallelsToolsFlavor: b.config.ParallelsToolsFlavor, 56 }, 57 ¶llelscommon.StepOutputDir{ 58 Force: b.config.PackerForce, 59 Path: b.config.OutputDir, 60 }, 61 &common.StepCreateFloppy{ 62 Files: b.config.FloppyConfig.FloppyFiles, 63 Directories: b.config.FloppyConfig.FloppyDirectories, 64 }, 65 &StepImport{ 66 Name: b.config.VMName, 67 SourcePath: b.config.SourcePath, 68 }, 69 ¶llelscommon.StepAttachParallelsTools{ 70 ParallelsToolsMode: b.config.ParallelsToolsMode, 71 }, 72 new(parallelscommon.StepAttachFloppy), 73 ¶llelscommon.StepPrlctl{ 74 Commands: b.config.Prlctl, 75 Ctx: b.config.ctx, 76 }, 77 ¶llelscommon.StepRun{}, 78 ¶llelscommon.StepTypeBootCommand{ 79 BootCommand: b.config.FlatBootCommand(), 80 BootWait: b.config.BootWait, 81 HostInterfaces: []string{}, 82 VMName: b.config.VMName, 83 Ctx: b.config.ctx, 84 }, 85 &communicator.StepConnect{ 86 Config: &b.config.SSHConfig.Comm, 87 Host: parallelscommon.CommHost, 88 SSHConfig: parallelscommon.SSHConfigFunc(b.config.SSHConfig), 89 }, 90 ¶llelscommon.StepUploadVersion{ 91 Path: b.config.PrlctlVersionFile, 92 }, 93 ¶llelscommon.StepUploadParallelsTools{ 94 ParallelsToolsFlavor: b.config.ParallelsToolsFlavor, 95 ParallelsToolsGuestPath: b.config.ParallelsToolsGuestPath, 96 ParallelsToolsMode: b.config.ParallelsToolsMode, 97 Ctx: b.config.ctx, 98 }, 99 new(common.StepProvision), 100 ¶llelscommon.StepShutdown{ 101 Command: b.config.ShutdownCommand, 102 Timeout: b.config.ShutdownTimeout, 103 }, 104 ¶llelscommon.StepPrlctl{ 105 Commands: b.config.PrlctlPost, 106 Ctx: b.config.ctx, 107 }, 108 ¶llelscommon.StepCompactDisk{ 109 Skip: b.config.SkipCompaction, 110 }, 111 } 112 113 // Run the steps. 114 b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) 115 b.runner.Run(state) 116 117 // Report any errors. 118 if rawErr, ok := state.GetOk("error"); ok { 119 return nil, rawErr.(error) 120 } 121 122 // If we were interrupted or cancelled, then just exit. 123 if _, ok := state.GetOk(multistep.StateCancelled); ok { 124 return nil, errors.New("Build was cancelled.") 125 } 126 127 if _, ok := state.GetOk(multistep.StateHalted); ok { 128 return nil, errors.New("Build was halted.") 129 } 130 131 return parallelscommon.NewArtifact(b.config.OutputDir) 132 } 133 134 // Cancel. 135 func (b *Builder) Cancel() { 136 if b.runner != nil { 137 log.Println("Cancelling the step runner...") 138 b.runner.Cancel() 139 } 140 }