github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/oneandone/builder.go (about) 1 package oneandone 2 3 import ( 4 "errors" 5 "fmt" 6 "github.com/hashicorp/packer/common" 7 "github.com/hashicorp/packer/helper/communicator" 8 "github.com/hashicorp/packer/packer" 9 "github.com/mitchellh/multistep" 10 "log" 11 ) 12 13 const BuilderId = "packer.oneandone" 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 32 state := new(multistep.BasicStateBag) 33 34 state.Put("config", b.config) 35 state.Put("hook", hook) 36 state.Put("ui", ui) 37 38 steps := []multistep.Step{ 39 &StepCreateSSHKey{ 40 Debug: b.config.PackerDebug, 41 DebugKeyPath: fmt.Sprintf("oneandone_%s", b.config.SnapshotName), 42 }, 43 new(stepCreateServer), 44 &communicator.StepConnect{ 45 Config: &b.config.Comm, 46 Host: commHost, 47 SSHConfig: sshConfig, 48 }, 49 &common.StepProvision{}, 50 new(stepTakeSnapshot), 51 } 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 if temp, ok := state.GetOk("snapshot_name"); ok { 61 b.config.SnapshotName = temp.(string) 62 } 63 64 artifact := &Artifact{ 65 snapshotName: b.config.SnapshotName, 66 } 67 68 if id, ok := state.GetOk("snapshot_id"); ok { 69 artifact.snapshotId = id.(string) 70 } else { 71 return nil, errors.New("Image creation has failed.") 72 } 73 74 return artifact, nil 75 } 76 77 func (b *Builder) Cancel() { 78 if b.runner != nil { 79 log.Println("Cancelling the step runner...") 80 b.runner.Cancel() 81 } 82 }