github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/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 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 if b.config.PackerDebug { 53 b.runner = &multistep.DebugRunner{ 54 Steps: steps, 55 PauseFn: common.MultistepDebugFn(ui), 56 } 57 } else { 58 b.runner = &multistep.BasicRunner{Steps: steps} 59 } 60 61 b.runner.Run(state) 62 63 if rawErr, ok := state.GetOk("error"); ok { 64 return nil, rawErr.(error) 65 } 66 67 artifact := &Artifact{ 68 snapshotData: config.SnapshotName, 69 } 70 return artifact, nil 71 } 72 73 func (b *Builder) Cancel() { 74 if b.runner != nil { 75 log.Println("Cancelling the step runner...") 76 b.runner.Cancel() 77 } 78 }