github.com/rothwerx/packer@v0.9.0/builder/vmware/vmx/builder.go (about)

     1  package vmx
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"log"
     7  	"time"
     8  
     9  	"github.com/mitchellh/multistep"
    10  	vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
    11  	"github.com/mitchellh/packer/common"
    12  	"github.com/mitchellh/packer/helper/communicator"
    13  	"github.com/mitchellh/packer/packer"
    14  )
    15  
    16  // Builder implements packer.Builder and builds the actual VMware
    17  // images.
    18  type Builder struct {
    19  	config *Config
    20  	runner multistep.Runner
    21  }
    22  
    23  // Prepare processes the build configuration parameters.
    24  func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
    25  	c, warnings, errs := NewConfig(raws...)
    26  	if errs != nil {
    27  		return warnings, errs
    28  	}
    29  	b.config = c
    30  
    31  	return warnings, nil
    32  }
    33  
    34  // Run executes a Packer build and returns a packer.Artifact representing
    35  // a VirtualBox appliance.
    36  func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
    37  	driver, err := vmwcommon.NewDriver(&b.config.DriverConfig, &b.config.SSHConfig)
    38  	if err != nil {
    39  		return nil, fmt.Errorf("Failed creating VMware driver: %s", err)
    40  	}
    41  
    42  	// Setup the directory
    43  	dir := new(vmwcommon.LocalOutputDir)
    44  	dir.SetOutputDir(b.config.OutputDir)
    45  
    46  	// Set up the state.
    47  	state := new(multistep.BasicStateBag)
    48  	state.Put("config", b.config)
    49  	state.Put("dir", dir)
    50  	state.Put("driver", driver)
    51  	state.Put("hook", hook)
    52  	state.Put("ui", ui)
    53  
    54  	// Build the steps.
    55  	steps := []multistep.Step{
    56  		&vmwcommon.StepPrepareTools{
    57  			RemoteType:        b.config.RemoteType,
    58  			ToolsUploadFlavor: b.config.ToolsUploadFlavor,
    59  		},
    60  		&vmwcommon.StepOutputDir{
    61  			Force: b.config.PackerForce,
    62  		},
    63  		&common.StepCreateFloppy{
    64  			Files: b.config.FloppyFiles,
    65  		},
    66  		&StepCloneVMX{
    67  			OutputDir: b.config.OutputDir,
    68  			Path:      b.config.SourcePath,
    69  			VMName:    b.config.VMName,
    70  		},
    71  		&vmwcommon.StepConfigureVMX{
    72  			CustomData: b.config.VMXData,
    73  		},
    74  		&vmwcommon.StepSuppressMessages{},
    75  		&common.StepHTTPServer{
    76  			HTTPDir:     b.config.HTTPDir,
    77  			HTTPPortMin: b.config.HTTPPortMin,
    78  			HTTPPortMax: b.config.HTTPPortMax,
    79  		},
    80  		&vmwcommon.StepConfigureVNC{
    81  			VNCPortMin: b.config.VNCPortMin,
    82  			VNCPortMax: b.config.VNCPortMax,
    83  		},
    84  		&vmwcommon.StepRun{
    85  			BootWait:           b.config.BootWait,
    86  			DurationBeforeStop: 5 * time.Second,
    87  			Headless:           b.config.Headless,
    88  		},
    89  		&vmwcommon.StepTypeBootCommand{
    90  			BootCommand: b.config.BootCommand,
    91  			VMName:      b.config.VMName,
    92  			Ctx:         b.config.ctx,
    93  		},
    94  		&communicator.StepConnect{
    95  			Config:    &b.config.SSHConfig.Comm,
    96  			Host:      driver.CommHost,
    97  			SSHConfig: vmwcommon.SSHConfigFunc(&b.config.SSHConfig),
    98  		},
    99  		&vmwcommon.StepUploadTools{
   100  			RemoteType:        b.config.RemoteType,
   101  			ToolsUploadFlavor: b.config.ToolsUploadFlavor,
   102  			ToolsUploadPath:   b.config.ToolsUploadPath,
   103  			Ctx:               b.config.ctx,
   104  		},
   105  		&common.StepProvision{},
   106  		&vmwcommon.StepShutdown{
   107  			Command: b.config.ShutdownCommand,
   108  			Timeout: b.config.ShutdownTimeout,
   109  		},
   110  		&vmwcommon.StepCleanFiles{},
   111  		&vmwcommon.StepCompactDisk{
   112  			Skip: b.config.SkipCompaction,
   113  		},
   114  		&vmwcommon.StepConfigureVMX{
   115  			CustomData: b.config.VMXDataPost,
   116  			SkipFloppy: true,
   117  		},
   118  		&vmwcommon.StepCleanVMX{},
   119  	}
   120  
   121  	// Run the steps.
   122  	if b.config.PackerDebug {
   123  		b.runner = &multistep.DebugRunner{
   124  			Steps:   steps,
   125  			PauseFn: common.MultistepDebugFn(ui),
   126  		}
   127  	} else {
   128  		b.runner = &multistep.BasicRunner{Steps: steps}
   129  	}
   130  	b.runner.Run(state)
   131  
   132  	// Report any errors.
   133  	if rawErr, ok := state.GetOk("error"); ok {
   134  		return nil, rawErr.(error)
   135  	}
   136  
   137  	// If we were interrupted or cancelled, then just exit.
   138  	if _, ok := state.GetOk(multistep.StateCancelled); ok {
   139  		return nil, errors.New("Build was cancelled.")
   140  	}
   141  
   142  	if _, ok := state.GetOk(multistep.StateHalted); ok {
   143  		return nil, errors.New("Build was halted.")
   144  	}
   145  
   146  	return vmwcommon.NewLocalArtifact(b.config.OutputDir)
   147  }
   148  
   149  // Cancel.
   150  func (b *Builder) Cancel() {
   151  	if b.runner != nil {
   152  		log.Println("Cancelling the step runner...")
   153  		b.runner.Cancel()
   154  	}
   155  }