github.com/yoctocloud/packer@v0.6.2-0.20160520224004-e11a0a18423f/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("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.FloppyFiles,
    66  		},
    67  		&StepCloneVMX{
    68  			OutputDir: b.config.OutputDir,
    69  			Path:      b.config.SourcePath,
    70  			VMName:    b.config.VMName,
    71  		},
    72  		&vmwcommon.StepConfigureVMX{
    73  			CustomData: b.config.VMXData,
    74  		},
    75  		&vmwcommon.StepSuppressMessages{},
    76  		&common.StepHTTPServer{
    77  			HTTPDir:     b.config.HTTPDir,
    78  			HTTPPortMin: b.config.HTTPPortMin,
    79  			HTTPPortMax: b.config.HTTPPortMax,
    80  		},
    81  		&vmwcommon.StepConfigureVNC{
    82  			VNCPortMin: b.config.VNCPortMin,
    83  			VNCPortMax: b.config.VNCPortMax,
    84  		},
    85  		&vmwcommon.StepRun{
    86  			BootWait:           b.config.BootWait,
    87  			DurationBeforeStop: 5 * time.Second,
    88  			Headless:           b.config.Headless,
    89  		},
    90  		&vmwcommon.StepTypeBootCommand{
    91  			BootCommand: b.config.BootCommand,
    92  			VMName:      b.config.VMName,
    93  			Ctx:         b.config.ctx,
    94  		},
    95  		&communicator.StepConnect{
    96  			Config:    &b.config.SSHConfig.Comm,
    97  			Host:      driver.CommHost,
    98  			SSHConfig: vmwcommon.SSHConfigFunc(&b.config.SSHConfig),
    99  		},
   100  		&vmwcommon.StepUploadTools{
   101  			RemoteType:        b.config.RemoteType,
   102  			ToolsUploadFlavor: b.config.ToolsUploadFlavor,
   103  			ToolsUploadPath:   b.config.ToolsUploadPath,
   104  			Ctx:               b.config.ctx,
   105  		},
   106  		&common.StepProvision{},
   107  		&vmwcommon.StepShutdown{
   108  			Command: b.config.ShutdownCommand,
   109  			Timeout: b.config.ShutdownTimeout,
   110  		},
   111  		&vmwcommon.StepCleanFiles{},
   112  		&vmwcommon.StepCompactDisk{
   113  			Skip: b.config.SkipCompaction,
   114  		},
   115  		&vmwcommon.StepConfigureVMX{
   116  			CustomData: b.config.VMXDataPost,
   117  			SkipFloppy: true,
   118  		},
   119  		&vmwcommon.StepCleanVMX{},
   120  	}
   121  
   122  	// Run the steps.
   123  	if b.config.PackerDebug {
   124  		pauseFn := common.MultistepDebugFn(ui)
   125  		state.Put("pauseFn", pauseFn)
   126  		b.runner = &multistep.DebugRunner{
   127  			Steps:   steps,
   128  			PauseFn: pauseFn,
   129  		}
   130  	} else {
   131  		b.runner = &multistep.BasicRunner{Steps: steps}
   132  	}
   133  	b.runner.Run(state)
   134  
   135  	// Report any errors.
   136  	if rawErr, ok := state.GetOk("error"); ok {
   137  		return nil, rawErr.(error)
   138  	}
   139  
   140  	// If we were interrupted or cancelled, then just exit.
   141  	if _, ok := state.GetOk(multistep.StateCancelled); ok {
   142  		return nil, errors.New("Build was cancelled.")
   143  	}
   144  
   145  	if _, ok := state.GetOk(multistep.StateHalted); ok {
   146  		return nil, errors.New("Build was halted.")
   147  	}
   148  
   149  	return vmwcommon.NewLocalArtifact(b.config.OutputDir)
   150  }
   151  
   152  // Cancel.
   153  func (b *Builder) Cancel() {
   154  	if b.runner != nil {
   155  		log.Println("Cancelling the step runner...")
   156  		b.runner.Cancel()
   157  	}
   158  }