github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/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.Host()),
    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  	b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
    50  	b.runner.Run(state)
    51  
    52  	// If there was an error, return that
    53  	if rawErr, ok := state.GetOk("error"); ok {
    54  		return nil, rawErr.(error)
    55  	}
    56  
    57  	// No errors, must've worked
    58  	artifact := &NullArtifact{}
    59  	return artifact, nil
    60  }
    61  
    62  func (b *Builder) Cancel() {
    63  	if b.runner != nil {
    64  		log.Println("Cancelling the step runner...")
    65  		b.runner.Cancel()
    66  	}
    67  }