github.com/mitchellh/packer@v1.3.2/builder/vmware/vmx/builder.go (about)

     1  package vmx
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"log"
     7  	"time"
     8  
     9  	vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
    10  	"github.com/hashicorp/packer/common"
    11  	"github.com/hashicorp/packer/helper/communicator"
    12  	"github.com/hashicorp/packer/helper/multistep"
    13  	"github.com/hashicorp/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 VMware image.
    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("debug", b.config.PackerDebug)
    50  	state.Put("dir", dir)
    51  	state.Put("driver", driver)
    52  	state.Put("hook", hook)
    53  	state.Put("ui", ui)
    54  
    55  	// Build the steps.
    56  	steps := []multistep.Step{
    57  		&vmwcommon.StepPrepareTools{
    58  			RemoteType:        b.config.RemoteType,
    59  			ToolsUploadFlavor: b.config.ToolsUploadFlavor,
    60  		},
    61  		&vmwcommon.StepOutputDir{
    62  			Force: b.config.PackerForce,
    63  		},
    64  		&common.StepCreateFloppy{
    65  			Files:       b.config.FloppyConfig.FloppyFiles,
    66  			Directories: b.config.FloppyConfig.FloppyDirectories,
    67  		},
    68  		&StepCloneVMX{
    69  			OutputDir: b.config.OutputDir,
    70  			Path:      b.config.SourcePath,
    71  			VMName:    b.config.VMName,
    72  			Linked:    b.config.Linked,
    73  		},
    74  		&vmwcommon.StepConfigureVMX{
    75  			CustomData: b.config.VMXData,
    76  		},
    77  		&vmwcommon.StepSuppressMessages{},
    78  		&common.StepHTTPServer{
    79  			HTTPDir:     b.config.HTTPDir,
    80  			HTTPPortMin: b.config.HTTPPortMin,
    81  			HTTPPortMax: b.config.HTTPPortMax,
    82  		},
    83  		&vmwcommon.StepConfigureVNC{
    84  			Enabled:            !b.config.DisableVNC,
    85  			VNCBindAddress:     b.config.VNCBindAddress,
    86  			VNCPortMin:         b.config.VNCPortMin,
    87  			VNCPortMax:         b.config.VNCPortMax,
    88  			VNCDisablePassword: b.config.VNCDisablePassword,
    89  		},
    90  		&vmwcommon.StepRun{
    91  			DurationBeforeStop: 5 * time.Second,
    92  			Headless:           b.config.Headless,
    93  		},
    94  		&vmwcommon.StepTypeBootCommand{
    95  			BootWait:    b.config.BootWait,
    96  			VNCEnabled:  !b.config.DisableVNC,
    97  			BootCommand: b.config.FlatBootCommand(),
    98  			VMName:      b.config.VMName,
    99  			Ctx:         b.config.ctx,
   100  			KeyInterval: b.config.VNCConfig.BootKeyInterval,
   101  		},
   102  		&communicator.StepConnect{
   103  			Config:    &b.config.SSHConfig.Comm,
   104  			Host:      driver.CommHost,
   105  			SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
   106  		},
   107  		&vmwcommon.StepUploadTools{
   108  			RemoteType:        b.config.RemoteType,
   109  			ToolsUploadFlavor: b.config.ToolsUploadFlavor,
   110  			ToolsUploadPath:   b.config.ToolsUploadPath,
   111  			Ctx:               b.config.ctx,
   112  		},
   113  		&common.StepProvision{},
   114  		&common.StepCleanupTempKeys{
   115  			Comm: &b.config.SSHConfig.Comm,
   116  		},
   117  		&vmwcommon.StepShutdown{
   118  			Command: b.config.ShutdownCommand,
   119  			Timeout: b.config.ShutdownTimeout,
   120  		},
   121  		&vmwcommon.StepCleanFiles{},
   122  		&vmwcommon.StepCompactDisk{
   123  			Skip: b.config.SkipCompaction,
   124  		},
   125  		&vmwcommon.StepConfigureVMX{
   126  			CustomData: b.config.VMXDataPost,
   127  			SkipFloppy: true,
   128  		},
   129  		&vmwcommon.StepCleanVMX{
   130  			RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet,
   131  			VNCEnabled:               !b.config.DisableVNC,
   132  		},
   133  	}
   134  
   135  	// Run the steps.
   136  	b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
   137  	b.runner.Run(state)
   138  
   139  	// Report any errors.
   140  	if rawErr, ok := state.GetOk("error"); ok {
   141  		return nil, rawErr.(error)
   142  	}
   143  
   144  	// If we were interrupted or cancelled, then just exit.
   145  	if _, ok := state.GetOk(multistep.StateCancelled); ok {
   146  		return nil, errors.New("Build was cancelled.")
   147  	}
   148  
   149  	if _, ok := state.GetOk(multistep.StateHalted); ok {
   150  		return nil, errors.New("Build was halted.")
   151  	}
   152  
   153  	return vmwcommon.NewLocalArtifact(b.config.VMName, b.config.OutputDir)
   154  }
   155  
   156  // Cancel.
   157  func (b *Builder) Cancel() {
   158  	if b.runner != nil {
   159  		log.Println("Cancelling the step runner...")
   160  		b.runner.Cancel()
   161  	}
   162  }