github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/parallels/pvm/builder.go (about)

     1  package pvm
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"log"
     7  
     8  	"github.com/mitchellh/multistep"
     9  	parallelscommon "github.com/mitchellh/packer/builder/parallels/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 Parallels
    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 Parallels 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 Parallels
    37  	driver, err := parallelscommon.NewDriver()
    38  	if err != nil {
    39  		return nil, fmt.Errorf("Failed creating Paralles 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("hook", hook)
    48  	state.Put("ui", ui)
    49  	state.Put("http_port", uint(0))
    50  
    51  	// Build the steps.
    52  	steps := []multistep.Step{
    53  		&parallelscommon.StepPrepareParallelsTools{
    54  			ParallelsToolsMode:   b.config.ParallelsToolsMode,
    55  			ParallelsToolsFlavor: b.config.ParallelsToolsFlavor,
    56  		},
    57  		&parallelscommon.StepOutputDir{
    58  			Force: b.config.PackerForce,
    59  			Path:  b.config.OutputDir,
    60  		},
    61  		&common.StepCreateFloppy{
    62  			Files:       b.config.FloppyConfig.FloppyFiles,
    63  			Directories: b.config.FloppyConfig.FloppyDirectories,
    64  		},
    65  		&StepImport{
    66  			Name:       b.config.VMName,
    67  			SourcePath: b.config.SourcePath,
    68  		},
    69  		&parallelscommon.StepAttachParallelsTools{
    70  			ParallelsToolsMode: b.config.ParallelsToolsMode,
    71  		},
    72  		new(parallelscommon.StepAttachFloppy),
    73  		&parallelscommon.StepPrlctl{
    74  			Commands: b.config.Prlctl,
    75  			Ctx:      b.config.ctx,
    76  		},
    77  		&parallelscommon.StepRun{
    78  			BootWait: b.config.BootWait,
    79  		},
    80  		&parallelscommon.StepTypeBootCommand{
    81  			BootCommand:    b.config.BootCommand,
    82  			HostInterfaces: []string{},
    83  			VMName:         b.config.VMName,
    84  			Ctx:            b.config.ctx,
    85  		},
    86  		&communicator.StepConnect{
    87  			Config:    &b.config.SSHConfig.Comm,
    88  			Host:      parallelscommon.CommHost,
    89  			SSHConfig: parallelscommon.SSHConfigFunc(b.config.SSHConfig),
    90  		},
    91  		&parallelscommon.StepUploadVersion{
    92  			Path: b.config.PrlctlVersionFile,
    93  		},
    94  		&parallelscommon.StepUploadParallelsTools{
    95  			ParallelsToolsFlavor:    b.config.ParallelsToolsFlavor,
    96  			ParallelsToolsGuestPath: b.config.ParallelsToolsGuestPath,
    97  			ParallelsToolsMode:      b.config.ParallelsToolsMode,
    98  			Ctx:                     b.config.ctx,
    99  		},
   100  		new(common.StepProvision),
   101  		&parallelscommon.StepShutdown{
   102  			Command: b.config.ShutdownCommand,
   103  			Timeout: b.config.ShutdownTimeout,
   104  		},
   105  		&parallelscommon.StepPrlctl{
   106  			Commands: b.config.PrlctlPost,
   107  			Ctx:      b.config.ctx,
   108  		},
   109  	}
   110  
   111  	// Run the steps.
   112  	b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
   113  	b.runner.Run(state)
   114  
   115  	// Report any errors.
   116  	if rawErr, ok := state.GetOk("error"); ok {
   117  		return nil, rawErr.(error)
   118  	}
   119  
   120  	// If we were interrupted or cancelled, then just exit.
   121  	if _, ok := state.GetOk(multistep.StateCancelled); ok {
   122  		return nil, errors.New("Build was cancelled.")
   123  	}
   124  
   125  	if _, ok := state.GetOk(multistep.StateHalted); ok {
   126  		return nil, errors.New("Build was halted.")
   127  	}
   128  
   129  	return parallelscommon.NewArtifact(b.config.OutputDir)
   130  }
   131  
   132  // Cancel.
   133  func (b *Builder) Cancel() {
   134  	if b.runner != nil {
   135  		log.Println("Cancelling the step runner...")
   136  		b.runner.Cancel()
   137  	}
   138  }