github.phpd.cn/hashicorp/packer@v1.3.2/builder/oneandone/builder.go (about) 1 package oneandone 2 3 import ( 4 "errors" 5 "fmt" 6 "log" 7 8 "github.com/hashicorp/packer/common" 9 "github.com/hashicorp/packer/helper/communicator" 10 "github.com/hashicorp/packer/helper/multistep" 11 "github.com/hashicorp/packer/packer" 12 ) 13 14 const BuilderId = "packer.oneandone" 15 16 type Builder struct { 17 config *Config 18 runner multistep.Runner 19 } 20 21 func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { 22 c, warnings, errs := NewConfig(raws...) 23 if errs != nil { 24 return warnings, errs 25 } 26 b.config = c 27 28 return warnings, nil 29 } 30 31 func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { 32 33 state := new(multistep.BasicStateBag) 34 35 state.Put("config", b.config) 36 state.Put("hook", hook) 37 state.Put("ui", ui) 38 39 steps := []multistep.Step{ 40 &StepCreateSSHKey{ 41 Debug: b.config.PackerDebug, 42 DebugKeyPath: fmt.Sprintf("oneandone_%s", b.config.SnapshotName), 43 }, 44 new(stepCreateServer), 45 &communicator.StepConnect{ 46 Config: &b.config.Comm, 47 Host: commHost, 48 SSHConfig: b.config.Comm.SSHConfigFunc(), 49 }, 50 &common.StepProvision{}, 51 &common.StepCleanupTempKeys{ 52 Comm: &b.config.Comm, 53 }, 54 new(stepTakeSnapshot), 55 } 56 57 b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) 58 b.runner.Run(state) 59 60 if rawErr, ok := state.GetOk("error"); ok { 61 return nil, rawErr.(error) 62 } 63 64 if temp, ok := state.GetOk("snapshot_name"); ok { 65 b.config.SnapshotName = temp.(string) 66 } 67 68 artifact := &Artifact{ 69 snapshotName: b.config.SnapshotName, 70 } 71 72 if id, ok := state.GetOk("snapshot_id"); ok { 73 artifact.snapshotId = id.(string) 74 } else { 75 return nil, errors.New("Image creation has failed.") 76 } 77 78 return artifact, nil 79 } 80 81 func (b *Builder) Cancel() { 82 if b.runner != nil { 83 log.Println("Cancelling the step runner...") 84 b.runner.Cancel() 85 } 86 }