github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/profitbricks/builder.go (about)

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