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

     1  package null
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/mitchellh/multistep"
     7  	"github.com/mitchellh/packer/common"
     8  	"github.com/mitchellh/packer/helper/communicator"
     9  	"github.com/mitchellh/packer/packer"
    10  )
    11  
    12  const BuilderId = "fnoeding.null"
    13  
    14  type Builder struct {
    15  	config *Config
    16  	runner multistep.Runner
    17  }
    18  
    19  func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
    20  	c, warnings, errs := NewConfig(raws...)
    21  	if errs != nil {
    22  		return warnings, errs
    23  	}
    24  	b.config = c
    25  
    26  	return warnings, nil
    27  }
    28  
    29  func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
    30  	steps := []multistep.Step{
    31  		&communicator.StepConnect{
    32  			Config: &b.config.CommConfig,
    33  			Host:   CommHost(b.config.CommConfig.SSHHost),
    34  			SSHConfig: SSHConfig(
    35  				b.config.CommConfig.SSHUsername,
    36  				b.config.CommConfig.SSHPassword,
    37  				b.config.CommConfig.SSHPrivateKey),
    38  		},
    39  		&common.StepProvision{},
    40  	}
    41  
    42  	// Setup the state bag and initial state for the steps
    43  	state := new(multistep.BasicStateBag)
    44  	state.Put("config", b.config)
    45  	state.Put("hook", hook)
    46  	state.Put("ui", ui)
    47  
    48  	// Run!
    49  	if b.config.PackerDebug {
    50  		b.runner = &multistep.DebugRunner{
    51  			Steps:   steps,
    52  			PauseFn: common.MultistepDebugFn(ui),
    53  		}
    54  	} else {
    55  		b.runner = &multistep.BasicRunner{Steps: steps}
    56  	}
    57  
    58  	b.runner.Run(state)
    59  
    60  	// If there was an error, return that
    61  	if rawErr, ok := state.GetOk("error"); ok {
    62  		return nil, rawErr.(error)
    63  	}
    64  
    65  	// No errors, must've worked
    66  	artifact := &NullArtifact{}
    67  	return artifact, nil
    68  }
    69  
    70  func (b *Builder) Cancel() {
    71  	if b.runner != nil {
    72  		log.Println("Cancelling the step runner...")
    73  		b.runner.Cancel()
    74  	}
    75  }