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