github.phpd.cn/hashicorp/packer@v1.3.2/builder/parallels/pvm/builder.go (about)

     1  package pvm
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"log"
     7  
     8  	parallelscommon "github.com/hashicorp/packer/builder/parallels/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 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 Parallels 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  		&parallelscommon.StepTypeBootCommand{
    79  			BootCommand:    b.config.FlatBootCommand(),
    80  			BootWait:       b.config.BootWait,
    81  			HostInterfaces: []string{},
    82  			VMName:         b.config.VMName,
    83  			Ctx:            b.config.ctx,
    84  			GroupInterval:  b.config.BootConfig.BootGroupInterval,
    85  		},
    86  		&communicator.StepConnect{
    87  			Config:    &b.config.SSHConfig.Comm,
    88  			Host:      parallelscommon.CommHost,
    89  			SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
    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  		&common.StepCleanupTempKeys{
   106  			Comm: &b.config.SSHConfig.Comm,
   107  		},
   108  		&parallelscommon.StepPrlctl{
   109  			Commands: b.config.PrlctlPost,
   110  			Ctx:      b.config.ctx,
   111  		},
   112  		&parallelscommon.StepCompactDisk{
   113  			Skip: b.config.SkipCompaction,
   114  		},
   115  	}
   116  
   117  	// Run the steps.
   118  	b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
   119  	b.runner.Run(state)
   120  
   121  	// Report any errors.
   122  	if rawErr, ok := state.GetOk("error"); ok {
   123  		return nil, rawErr.(error)
   124  	}
   125  
   126  	// If we were interrupted or cancelled, then just exit.
   127  	if _, ok := state.GetOk(multistep.StateCancelled); ok {
   128  		return nil, errors.New("Build was cancelled.")
   129  	}
   130  
   131  	if _, ok := state.GetOk(multistep.StateHalted); ok {
   132  		return nil, errors.New("Build was halted.")
   133  	}
   134  
   135  	return parallelscommon.NewArtifact(b.config.OutputDir)
   136  }
   137  
   138  // Cancel.
   139  func (b *Builder) Cancel() {
   140  	if b.runner != nil {
   141  		log.Println("Cancelling the step runner...")
   142  		b.runner.Cancel()
   143  	}
   144  }