github.com/kaixiang/packer@v0.5.2-0.20140114230416-1f5786b0d7f1/builder/virtualbox/ovf/builder.go (about) 1 package ovf 2 3 import ( 4 "errors" 5 "fmt" 6 "log" 7 8 "github.com/mitchellh/multistep" 9 vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" 10 "github.com/mitchellh/packer/common" 11 "github.com/mitchellh/packer/packer" 12 ) 13 14 // Builder implements packer.Builder and builds the actual VirtualBox 15 // images. 16 type Builder struct { 17 config *Config 18 runner multistep.Runner 19 } 20 21 // Prepare processes the build configuration parameters. 22 func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { 23 c, warnings, errs := NewConfig(raws...) 24 if errs != nil { 25 return warnings, errs 26 } 27 b.config = c 28 29 return warnings, nil 30 } 31 32 // Run executes a Packer build and returns a packer.Artifact representing 33 // a VirtualBox appliance. 34 func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { 35 // Create the driver that we'll use to communicate with VirtualBox 36 driver, err := vboxcommon.NewDriver() 37 if err != nil { 38 return nil, fmt.Errorf("Failed creating VirtualBox driver: %s", err) 39 } 40 41 // Set up the state. 42 state := new(multistep.BasicStateBag) 43 state.Put("config", b.config) 44 state.Put("driver", driver) 45 state.Put("hook", hook) 46 state.Put("ui", ui) 47 48 // Build the steps. 49 steps := []multistep.Step{ 50 &vboxcommon.StepOutputDir{ 51 Force: b.config.PackerForce, 52 Path: b.config.OutputDir, 53 }, 54 new(vboxcommon.StepSuppressMessages), 55 &common.StepCreateFloppy{ 56 Files: b.config.FloppyFiles, 57 }, 58 &StepImport{ 59 Name: b.config.VMName, 60 SourcePath: b.config.SourcePath, 61 }, 62 /* 63 new(stepAttachGuestAdditions), 64 */ 65 new(vboxcommon.StepAttachFloppy), 66 &vboxcommon.StepForwardSSH{ 67 GuestPort: b.config.SSHPort, 68 HostPortMin: b.config.SSHHostPortMin, 69 HostPortMax: b.config.SSHHostPortMax, 70 }, 71 &vboxcommon.StepVBoxManage{ 72 Commands: b.config.VBoxManage, 73 Tpl: b.config.tpl, 74 }, 75 &vboxcommon.StepRun{ 76 BootWait: b.config.BootWait, 77 Headless: b.config.Headless, 78 }, 79 &common.StepConnectSSH{ 80 SSHAddress: vboxcommon.SSHAddress, 81 SSHConfig: vboxcommon.SSHConfigFunc(b.config.SSHConfig), 82 SSHWaitTimeout: b.config.SSHWaitTimeout, 83 }, 84 &vboxcommon.StepUploadVersion{ 85 Path: b.config.VBoxVersionFile, 86 }, 87 /* 88 new(stepUploadGuestAdditions), 89 */ 90 new(common.StepProvision), 91 &vboxcommon.StepShutdown{ 92 Command: b.config.ShutdownCommand, 93 Timeout: b.config.ShutdownTimeout, 94 }, 95 new(vboxcommon.StepRemoveDevices), 96 &vboxcommon.StepExport{ 97 Format: b.config.Format, 98 OutputDir: b.config.OutputDir, 99 }, 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 vboxcommon.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 }