github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/profitbricks/builder.go (about)

     1  package profitbricks
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/common"
     7  	"github.com/mitchellh/packer/helper/communicator"
     8  	"github.com/mitchellh/packer/packer"
     9  	"log"
    10  )
    11  
    12  const BuilderId = "packer.profitbricks"
    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  
    31  	steps := []multistep.Step{
    32  		&StepCreateSSHKey{
    33  			Debug:        b.config.PackerDebug,
    34  			DebugKeyPath: fmt.Sprintf("pb_%s", b.config.SnapshotName),
    35  		},
    36  		new(stepCreateServer),
    37  		&communicator.StepConnect{
    38  			Config:    &b.config.Comm,
    39  			Host:      commHost,
    40  			SSHConfig: sshConfig,
    41  		},
    42  		&common.StepProvision{},
    43  		new(stepTakeSnapshot),
    44  	}
    45  
    46  	state := new(multistep.BasicStateBag)
    47  
    48  	state.Put("config", b.config)
    49  	state.Put("hook", hook)
    50  	state.Put("ui", ui)
    51  	config := state.Get("config").(*Config)
    52  
    53  	if b.config.PackerDebug {
    54  		b.runner = &multistep.DebugRunner{
    55  			Steps:   steps,
    56  			PauseFn: common.MultistepDebugFn(ui),
    57  		}
    58  	} else {
    59  		b.runner = &multistep.BasicRunner{Steps: steps}
    60  	}
    61  
    62  	b.runner.Run(state)
    63  
    64  	if rawErr, ok := state.GetOk("error"); ok {
    65  		return nil, rawErr.(error)
    66  	}
    67  
    68  	artifact := &Artifact{
    69  		snapshotData: config.SnapshotName,
    70  	}
    71  	return artifact, nil
    72  }
    73  
    74  func (b *Builder) Cancel() {
    75  	if b.runner != nil {
    76  		log.Println("Cancelling the step runner...")
    77  		b.runner.Cancel()
    78  	}
    79  }