github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/null/builder.go (about)

     1  package null
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/hashicorp/packer/common"
     7  	"github.com/hashicorp/packer/helper/communicator"
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  	"github.com/hashicorp/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  
    32  	if b.config.CommConfig.Type != "none" {
    33  		steps = append(steps,
    34  			&communicator.StepConnect{
    35  				Config: &b.config.CommConfig,
    36  				Host:   CommHost(b.config.CommConfig.Host()),
    37  				SSHConfig: SSHConfig(
    38  					b.config.CommConfig.SSHAgentAuth,
    39  					b.config.CommConfig.SSHUsername,
    40  					b.config.CommConfig.SSHPassword,
    41  					b.config.CommConfig.SSHPrivateKey),
    42  			},
    43  		)
    44  	}
    45  
    46  	steps = append(steps,
    47  		new(common.StepProvision),
    48  	)
    49  
    50  	// Setup the state bag and initial state for the steps
    51  	state := new(multistep.BasicStateBag)
    52  	state.Put("config", b.config)
    53  	state.Put("hook", hook)
    54  	state.Put("ui", ui)
    55  
    56  	// Run!
    57  	b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
    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  }