github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/builder/null/builder.go (about)

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