github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/virtualbox/ovf/builder.go (about) 1 package ovf 2 3 import ( 4 "errors" 5 "fmt" 6 "log" 7 8 vboxcommon "github.com/hashicorp/packer/builder/virtualbox/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 VirtualBox 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 VirtualBox 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 VirtualBox 37 driver, err := vboxcommon.NewDriver() 38 if err != nil { 39 return nil, fmt.Errorf("Failed creating VirtualBox 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("cache", cache) 48 state.Put("hook", hook) 49 state.Put("ui", ui) 50 51 // Build the steps. 52 steps := []multistep.Step{ 53 &vboxcommon.StepOutputDir{ 54 Force: b.config.PackerForce, 55 Path: b.config.OutputDir, 56 }, 57 new(vboxcommon.StepSuppressMessages), 58 &common.StepCreateFloppy{ 59 Files: b.config.FloppyConfig.FloppyFiles, 60 Directories: b.config.FloppyConfig.FloppyDirectories, 61 }, 62 &common.StepHTTPServer{ 63 HTTPDir: b.config.HTTPDir, 64 HTTPPortMin: b.config.HTTPPortMin, 65 HTTPPortMax: b.config.HTTPPortMax, 66 }, 67 &vboxcommon.StepDownloadGuestAdditions{ 68 GuestAdditionsMode: b.config.GuestAdditionsMode, 69 GuestAdditionsURL: b.config.GuestAdditionsURL, 70 GuestAdditionsSHA256: b.config.GuestAdditionsSHA256, 71 Ctx: b.config.ctx, 72 }, 73 &common.StepDownload{ 74 Checksum: b.config.Checksum, 75 ChecksumType: b.config.ChecksumType, 76 Description: "OVF/OVA", 77 Extension: "ova", 78 ResultKey: "vm_path", 79 TargetPath: b.config.TargetPath, 80 Url: []string{b.config.SourcePath}, 81 }, 82 &StepImport{ 83 Name: b.config.VMName, 84 ImportFlags: b.config.ImportFlags, 85 }, 86 &vboxcommon.StepAttachGuestAdditions{ 87 GuestAdditionsMode: b.config.GuestAdditionsMode, 88 }, 89 &vboxcommon.StepConfigureVRDP{ 90 VRDPBindAddress: b.config.VRDPBindAddress, 91 VRDPPortMin: b.config.VRDPPortMin, 92 VRDPPortMax: b.config.VRDPPortMax, 93 }, 94 new(vboxcommon.StepAttachFloppy), 95 &vboxcommon.StepForwardSSH{ 96 CommConfig: &b.config.SSHConfig.Comm, 97 HostPortMin: b.config.SSHHostPortMin, 98 HostPortMax: b.config.SSHHostPortMax, 99 SkipNatMapping: b.config.SSHSkipNatMapping, 100 }, 101 &vboxcommon.StepVBoxManage{ 102 Commands: b.config.VBoxManage, 103 Ctx: b.config.ctx, 104 }, 105 &vboxcommon.StepRun{ 106 Headless: b.config.Headless, 107 }, 108 &vboxcommon.StepTypeBootCommand{ 109 BootWait: b.config.BootWait, 110 BootCommand: b.config.FlatBootCommand(), 111 VMName: b.config.VMName, 112 Ctx: b.config.ctx, 113 }, 114 &communicator.StepConnect{ 115 Config: &b.config.SSHConfig.Comm, 116 Host: vboxcommon.CommHost(b.config.SSHConfig.Comm.SSHHost), 117 SSHConfig: vboxcommon.SSHConfigFunc(b.config.SSHConfig), 118 SSHPort: vboxcommon.SSHPort, 119 WinRMPort: vboxcommon.SSHPort, 120 }, 121 &vboxcommon.StepUploadVersion{ 122 Path: *b.config.VBoxVersionFile, 123 }, 124 &vboxcommon.StepUploadGuestAdditions{ 125 GuestAdditionsMode: b.config.GuestAdditionsMode, 126 GuestAdditionsPath: b.config.GuestAdditionsPath, 127 Ctx: b.config.ctx, 128 }, 129 new(common.StepProvision), 130 &vboxcommon.StepShutdown{ 131 Command: b.config.ShutdownCommand, 132 Timeout: b.config.ShutdownTimeout, 133 Delay: b.config.PostShutdownDelay, 134 }, 135 new(vboxcommon.StepRemoveDevices), 136 &vboxcommon.StepVBoxManage{ 137 Commands: b.config.VBoxManagePost, 138 Ctx: b.config.ctx, 139 }, 140 &vboxcommon.StepExport{ 141 Format: b.config.Format, 142 OutputDir: b.config.OutputDir, 143 ExportOpts: b.config.ExportOpts.ExportOpts, 144 SkipNatMapping: b.config.SSHSkipNatMapping, 145 SkipExport: b.config.SkipExport, 146 }, 147 } 148 149 // Run the steps. 150 b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) 151 b.runner.Run(state) 152 153 // Report any errors. 154 if rawErr, ok := state.GetOk("error"); ok { 155 return nil, rawErr.(error) 156 } 157 158 // If we were interrupted or cancelled, then just exit. 159 if _, ok := state.GetOk(multistep.StateCancelled); ok { 160 return nil, errors.New("Build was cancelled.") 161 } 162 163 if _, ok := state.GetOk(multistep.StateHalted); ok { 164 return nil, errors.New("Build was halted.") 165 } 166 167 return vboxcommon.NewArtifact(b.config.OutputDir) 168 } 169 170 // Cancel. 171 func (b *Builder) Cancel() { 172 if b.runner != nil { 173 log.Println("Cancelling the step runner...") 174 b.runner.Cancel() 175 } 176 }