github.com/homburg/packer@v0.6.1-0.20140528012651-1dcaf1716848/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  
    47  	// Build the steps.
    48  	steps := []multistep.Step{
    49  		&parallelscommon.StepOutputDir{
    50  			Force: b.config.PackerForce,
    51  			Path:  b.config.OutputDir,
    52  		},
    53  		&common.StepCreateFloppy{
    54  			Files: b.config.FloppyFiles,
    55  		},
    56  		&StepImport{
    57  			Name:       b.config.VMName,
    58  			SourcePath: b.config.SourcePath,
    59  		},
    60  		&parallelscommon.StepAttachParallelsTools{
    61  			ParallelsToolsHostPath: b.config.ParallelsToolsHostPath,
    62  			ParallelsToolsMode:     b.config.ParallelsToolsMode,
    63  		},
    64  		new(parallelscommon.StepAttachFloppy),
    65  		&parallelscommon.StepPrlctl{
    66  			Commands: b.config.Prlctl,
    67  			Tpl:      b.config.tpl,
    68  		},
    69  		&parallelscommon.StepRun{
    70  			BootWait: b.config.BootWait,
    71  			Headless: b.config.Headless,
    72  		},
    73  		&common.StepConnectSSH{
    74  			SSHAddress:     parallelscommon.SSHAddress,
    75  			SSHConfig:      parallelscommon.SSHConfigFunc(b.config.SSHConfig),
    76  			SSHWaitTimeout: b.config.SSHWaitTimeout,
    77  		},
    78  		&parallelscommon.StepUploadVersion{
    79  			Path: b.config.PrlctlVersionFile,
    80  		},
    81  		&parallelscommon.StepUploadParallelsTools{
    82  			ParallelsToolsGuestPath: b.config.ParallelsToolsGuestPath,
    83  			ParallelsToolsHostPath:  b.config.ParallelsToolsHostPath,
    84  			ParallelsToolsMode:      b.config.ParallelsToolsMode,
    85  			Tpl:                     b.config.tpl,
    86  		},
    87  		new(common.StepProvision),
    88  		&parallelscommon.StepShutdown{
    89  			Command: b.config.ShutdownCommand,
    90  			Timeout: b.config.ShutdownTimeout,
    91  		},
    92  		new(parallelscommon.StepRemoveDevices),
    93  	}
    94  
    95  	// Run the steps.
    96  	if b.config.PackerDebug {
    97  		b.runner = &multistep.DebugRunner{
    98  			Steps:   steps,
    99  			PauseFn: common.MultistepDebugFn(ui),
   100  		}
   101  	} else {
   102  		b.runner = &multistep.BasicRunner{Steps: steps}
   103  	}
   104  	b.runner.Run(state)
   105  
   106  	// Report any errors.
   107  	if rawErr, ok := state.GetOk("error"); ok {
   108  		return nil, rawErr.(error)
   109  	}
   110  
   111  	// If we were interrupted or cancelled, then just exit.
   112  	if _, ok := state.GetOk(multistep.StateCancelled); ok {
   113  		return nil, errors.New("Build was cancelled.")
   114  	}
   115  
   116  	if _, ok := state.GetOk(multistep.StateHalted); ok {
   117  		return nil, errors.New("Build was halted.")
   118  	}
   119  
   120  	return parallelscommon.NewArtifact(b.config.OutputDir)
   121  }
   122  
   123  // Cancel.
   124  func (b *Builder) Cancel() {
   125  	if b.runner != nil {
   126  		log.Println("Cancelling the step runner...")
   127  		b.runner.Cancel()
   128  	}
   129  }