github.com/yoctocloud/packer@v0.6.2-0.20160520224004-e11a0a18423f/builder/parallels/pvm/builder.go (about) 1 package pvm 2 3 import ( 4 "errors" 5 "fmt" 6 "log" 7 8 "github.com/mitchellh/multistep" 9 parallelscommon "github.com/mitchellh/packer/builder/parallels/common" 10 "github.com/mitchellh/packer/common" 11 "github.com/mitchellh/packer/helper/communicator" 12 "github.com/mitchellh/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 Paralles 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.FloppyFiles, 63 }, 64 &StepImport{ 65 Name: b.config.VMName, 66 SourcePath: b.config.SourcePath, 67 }, 68 ¶llelscommon.StepAttachParallelsTools{ 69 ParallelsToolsMode: b.config.ParallelsToolsMode, 70 }, 71 new(parallelscommon.StepAttachFloppy), 72 ¶llelscommon.StepPrlctl{ 73 Commands: b.config.Prlctl, 74 Ctx: b.config.ctx, 75 }, 76 ¶llelscommon.StepRun{ 77 BootWait: b.config.BootWait, 78 }, 79 ¶llelscommon.StepTypeBootCommand{ 80 BootCommand: b.config.BootCommand, 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 } 109 110 // Run the steps. 111 if b.config.PackerDebug { 112 pauseFn := common.MultistepDebugFn(ui) 113 state.Put("pauseFn", pauseFn) 114 b.runner = &multistep.DebugRunner{ 115 Steps: steps, 116 PauseFn: pauseFn, 117 } 118 } else { 119 b.runner = &multistep.BasicRunner{Steps: steps} 120 } 121 b.runner.Run(state) 122 123 // Report any errors. 124 if rawErr, ok := state.GetOk("error"); ok { 125 return nil, rawErr.(error) 126 } 127 128 // If we were interrupted or cancelled, then just exit. 129 if _, ok := state.GetOk(multistep.StateCancelled); ok { 130 return nil, errors.New("Build was cancelled.") 131 } 132 133 if _, ok := state.GetOk(multistep.StateHalted); ok { 134 return nil, errors.New("Build was halted.") 135 } 136 137 return parallelscommon.NewArtifact(b.config.OutputDir) 138 } 139 140 // Cancel. 141 func (b *Builder) Cancel() { 142 if b.runner != nil { 143 log.Println("Cancelling the step runner...") 144 b.runner.Cancel() 145 } 146 }