github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/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/helper/communicator"
    12  	"github.com/mitchellh/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  		&StepImport{
    74  			Name:        b.config.VMName,
    75  			SourcePath:  b.config.SourcePath,
    76  			ImportFlags: b.config.ImportFlags,
    77  		},
    78  		&vboxcommon.StepAttachGuestAdditions{
    79  			GuestAdditionsMode: b.config.GuestAdditionsMode,
    80  		},
    81  		&vboxcommon.StepConfigureVRDP{
    82  			VRDPBindAddress: b.config.VRDPBindAddress,
    83  			VRDPPortMin:     b.config.VRDPPortMin,
    84  			VRDPPortMax:     b.config.VRDPPortMax,
    85  		},
    86  		new(vboxcommon.StepAttachFloppy),
    87  		&vboxcommon.StepForwardSSH{
    88  			CommConfig:     &b.config.SSHConfig.Comm,
    89  			HostPortMin:    b.config.SSHHostPortMin,
    90  			HostPortMax:    b.config.SSHHostPortMax,
    91  			SkipNatMapping: b.config.SSHSkipNatMapping,
    92  		},
    93  		&vboxcommon.StepVBoxManage{
    94  			Commands: b.config.VBoxManage,
    95  			Ctx:      b.config.ctx,
    96  		},
    97  		&vboxcommon.StepRun{
    98  			BootWait: b.config.BootWait,
    99  			Headless: b.config.Headless,
   100  		},
   101  		&vboxcommon.StepTypeBootCommand{
   102  			BootCommand: b.config.BootCommand,
   103  			VMName:      b.config.VMName,
   104  			Ctx:         b.config.ctx,
   105  		},
   106  		&communicator.StepConnect{
   107  			Config:    &b.config.SSHConfig.Comm,
   108  			Host:      vboxcommon.CommHost(b.config.SSHConfig.Comm.SSHHost),
   109  			SSHConfig: vboxcommon.SSHConfigFunc(b.config.SSHConfig),
   110  			SSHPort:   vboxcommon.SSHPort,
   111  		},
   112  		&vboxcommon.StepUploadVersion{
   113  			Path: b.config.VBoxVersionFile,
   114  		},
   115  		&vboxcommon.StepUploadGuestAdditions{
   116  			GuestAdditionsMode: b.config.GuestAdditionsMode,
   117  			GuestAdditionsPath: b.config.GuestAdditionsPath,
   118  			Ctx:                b.config.ctx,
   119  		},
   120  		new(common.StepProvision),
   121  		&vboxcommon.StepShutdown{
   122  			Command: b.config.ShutdownCommand,
   123  			Timeout: b.config.ShutdownTimeout,
   124  			Delay:   b.config.PostShutdownDelay,
   125  		},
   126  		new(vboxcommon.StepRemoveDevices),
   127  		&vboxcommon.StepVBoxManage{
   128  			Commands: b.config.VBoxManagePost,
   129  			Ctx:      b.config.ctx,
   130  		},
   131  		&vboxcommon.StepExport{
   132  			Format:         b.config.Format,
   133  			OutputDir:      b.config.OutputDir,
   134  			ExportOpts:     b.config.ExportOpts.ExportOpts,
   135  			SkipNatMapping: b.config.SSHSkipNatMapping,
   136  		},
   137  	}
   138  
   139  	// Run the steps.
   140  	b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
   141  	b.runner.Run(state)
   142  
   143  	// Report any errors.
   144  	if rawErr, ok := state.GetOk("error"); ok {
   145  		return nil, rawErr.(error)
   146  	}
   147  
   148  	// If we were interrupted or cancelled, then just exit.
   149  	if _, ok := state.GetOk(multistep.StateCancelled); ok {
   150  		return nil, errors.New("Build was cancelled.")
   151  	}
   152  
   153  	if _, ok := state.GetOk(multistep.StateHalted); ok {
   154  		return nil, errors.New("Build was halted.")
   155  	}
   156  
   157  	return vboxcommon.NewArtifact(b.config.OutputDir)
   158  }
   159  
   160  // Cancel.
   161  func (b *Builder) Cancel() {
   162  	if b.runner != nil {
   163  		log.Println("Cancelling the step runner...")
   164  		b.runner.Cancel()
   165  	}
   166  }