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

     1  package profitbricks
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/packer/common"
     8  	"github.com/hashicorp/packer/helper/communicator"
     9  	"github.com/hashicorp/packer/helper/multistep"
    10  	"github.com/hashicorp/packer/packer"
    11  )
    12  
    13  const BuilderId = "packer.profitbricks"
    14  
    15  type Builder struct {
    16  	config *Config
    17  	runner multistep.Runner
    18  }
    19  
    20  func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
    21  	c, warnings, errs := NewConfig(raws...)
    22  	if errs != nil {
    23  		return warnings, errs
    24  	}
    25  	b.config = c
    26  
    27  	return warnings, nil
    28  }
    29  
    30  func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
    31  	state := new(multistep.BasicStateBag)
    32  
    33  	state.Put("config", b.config)
    34  	state.Put("hook", hook)
    35  	state.Put("ui", ui)
    36  	steps := []multistep.Step{
    37  		&StepCreateSSHKey{
    38  			Debug:        b.config.PackerDebug,
    39  			DebugKeyPath: fmt.Sprintf("pb_%s", b.config.SnapshotName),
    40  		},
    41  		new(stepCreateServer),
    42  		&communicator.StepConnect{
    43  			Config:    &b.config.Comm,
    44  			Host:      commHost,
    45  			SSHConfig: sshConfig,
    46  		},
    47  		&common.StepProvision{},
    48  		new(stepTakeSnapshot),
    49  	}
    50  
    51  	config := state.Get("config").(*Config)
    52  
    53  	b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
    54  	b.runner.Run(state)
    55  
    56  	if rawErr, ok := state.GetOk("error"); ok {
    57  		return nil, rawErr.(error)
    58  	}
    59  
    60  	artifact := &Artifact{
    61  		snapshotData: config.SnapshotName,
    62  	}
    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  }