github.com/kimor79/packer@v0.8.7-0.20151221212622-d507b18eb4cf/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("driver", driver)
    46  	state.Put("cache", cache)
    47  	state.Put("hook", hook)
    48  	state.Put("ui", ui)
    49  
    50  	// Build the steps.
    51  	steps := []multistep.Step{
    52  		&vboxcommon.StepOutputDir{
    53  			Force: b.config.PackerForce,
    54  			Path:  b.config.OutputDir,
    55  		},
    56  		new(vboxcommon.StepSuppressMessages),
    57  		&common.StepCreateFloppy{
    58  			Files: b.config.FloppyFiles,
    59  		},
    60  		&vboxcommon.StepHTTPServer{
    61  			HTTPDir:     b.config.HTTPDir,
    62  			HTTPPortMin: b.config.HTTPPortMin,
    63  			HTTPPortMax: b.config.HTTPPortMax,
    64  		},
    65  		&vboxcommon.StepDownloadGuestAdditions{
    66  			GuestAdditionsMode:   b.config.GuestAdditionsMode,
    67  			GuestAdditionsURL:    b.config.GuestAdditionsURL,
    68  			GuestAdditionsSHA256: b.config.GuestAdditionsSHA256,
    69  			Ctx:                  b.config.ctx,
    70  		},
    71  		&StepImport{
    72  			Name:        b.config.VMName,
    73  			SourcePath:  b.config.SourcePath,
    74  			ImportFlags: b.config.ImportFlags,
    75  		},
    76  		&vboxcommon.StepAttachGuestAdditions{
    77  			GuestAdditionsMode: b.config.GuestAdditionsMode,
    78  		},
    79  		new(vboxcommon.StepAttachFloppy),
    80  		&vboxcommon.StepForwardSSH{
    81  			CommConfig:     &b.config.SSHConfig.Comm,
    82  			HostPortMin:    b.config.SSHHostPortMin,
    83  			HostPortMax:    b.config.SSHHostPortMax,
    84  			SkipNatMapping: b.config.SSHSkipNatMapping,
    85  		},
    86  		&vboxcommon.StepVBoxManage{
    87  			Commands: b.config.VBoxManage,
    88  			Ctx:      b.config.ctx,
    89  		},
    90  		&vboxcommon.StepRun{
    91  			BootWait: b.config.BootWait,
    92  			Headless: b.config.Headless,
    93  		},
    94  		&vboxcommon.StepTypeBootCommand{
    95  			BootCommand: b.config.BootCommand,
    96  			VMName:      b.config.VMName,
    97  			Ctx:         b.config.ctx,
    98  		},
    99  		&communicator.StepConnect{
   100  			Config:    &b.config.SSHConfig.Comm,
   101  			Host:      vboxcommon.CommHost,
   102  			SSHConfig: vboxcommon.SSHConfigFunc(b.config.SSHConfig),
   103  			SSHPort:   vboxcommon.SSHPort,
   104  		},
   105  		&vboxcommon.StepUploadVersion{
   106  			Path: b.config.VBoxVersionFile,
   107  		},
   108  		&vboxcommon.StepUploadGuestAdditions{
   109  			GuestAdditionsMode: b.config.GuestAdditionsMode,
   110  			GuestAdditionsPath: b.config.GuestAdditionsPath,
   111  			Ctx:                b.config.ctx,
   112  		},
   113  		new(common.StepProvision),
   114  		&vboxcommon.StepShutdown{
   115  			Command: b.config.ShutdownCommand,
   116  			Timeout: b.config.ShutdownTimeout,
   117  		},
   118  		new(vboxcommon.StepRemoveDevices),
   119  		&vboxcommon.StepVBoxManage{
   120  			Commands: b.config.VBoxManagePost,
   121  			Ctx:      b.config.ctx,
   122  		},
   123  		&vboxcommon.StepExport{
   124  			Format:         b.config.Format,
   125  			OutputDir:      b.config.OutputDir,
   126  			ExportOpts:     b.config.ExportOpts.ExportOpts,
   127  			SkipNatMapping: b.config.SSHSkipNatMapping,
   128  		},
   129  	}
   130  
   131  	// Run the steps.
   132  	if b.config.PackerDebug {
   133  		b.runner = &multistep.DebugRunner{
   134  			Steps:   steps,
   135  			PauseFn: common.MultistepDebugFn(ui),
   136  		}
   137  	} else {
   138  		b.runner = &multistep.BasicRunner{Steps: steps}
   139  	}
   140  	b.runner.Run(state)
   141  
   142  	// Report any errors.
   143  	if rawErr, ok := state.GetOk("error"); ok {
   144  		return nil, rawErr.(error)
   145  	}
   146  
   147  	// If we were interrupted or cancelled, then just exit.
   148  	if _, ok := state.GetOk(multistep.StateCancelled); ok {
   149  		return nil, errors.New("Build was cancelled.")
   150  	}
   151  
   152  	if _, ok := state.GetOk(multistep.StateHalted); ok {
   153  		return nil, errors.New("Build was halted.")
   154  	}
   155  
   156  	return vboxcommon.NewArtifact(b.config.OutputDir)
   157  }
   158  
   159  // Cancel.
   160  func (b *Builder) Cancel() {
   161  	if b.runner != nil {
   162  		log.Println("Cancelling the step runner...")
   163  		b.runner.Cancel()
   164  	}
   165  }