github.phpd.cn/hashicorp/packer@v1.3.2/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: b.config.Comm.SSHConfigFunc(), 46 }, 47 &common.StepProvision{}, 48 &common.StepCleanupTempKeys{ 49 Comm: &b.config.Comm, 50 }, 51 new(stepTakeSnapshot), 52 } 53 54 config := state.Get("config").(*Config) 55 56 b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) 57 b.runner.Run(state) 58 59 if rawErr, ok := state.GetOk("error"); ok { 60 return nil, rawErr.(error) 61 } 62 63 artifact := &Artifact{ 64 snapshotData: config.SnapshotName, 65 } 66 return artifact, nil 67 } 68 69 func (b *Builder) Cancel() { 70 if b.runner != nil { 71 log.Println("Cancelling the step runner...") 72 b.runner.Cancel() 73 } 74 }