github.phpd.cn/hashicorp/packer@v1.3.2/builder/ncloud/builder.go (about) 1 package ncloud 2 3 import ( 4 ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk" 5 "github.com/hashicorp/packer/common" 6 "github.com/hashicorp/packer/helper/communicator" 7 "github.com/hashicorp/packer/helper/multistep" 8 "github.com/hashicorp/packer/packer" 9 ) 10 11 // Builder assume this implements packer.Builder 12 type Builder struct { 13 config *Config 14 stateBag multistep.StateBag 15 runner multistep.Runner 16 } 17 18 func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { 19 c, warnings, errs := NewConfig(raws...) 20 if errs != nil { 21 return warnings, errs 22 } 23 b.config = c 24 25 b.stateBag = new(multistep.BasicStateBag) 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 ui.Message("Creating Naver Cloud Platform Connection ...") 32 conn := ncloud.NewConnection(b.config.AccessKey, b.config.SecretKey) 33 34 b.stateBag.Put("hook", hook) 35 b.stateBag.Put("ui", ui) 36 37 var steps []multistep.Step 38 39 steps = []multistep.Step{} 40 41 if b.config.Comm.Type == "ssh" { 42 steps = []multistep.Step{ 43 NewStepValidateTemplate(conn, ui, b.config), 44 NewStepCreateLoginKey(conn, ui), 45 NewStepCreateServerInstance(conn, ui, b.config), 46 NewStepCreateBlockStorageInstance(conn, ui, b.config), 47 NewStepGetRootPassword(conn, ui), 48 NewStepCreatePublicIPInstance(conn, ui, b.config), 49 &communicator.StepConnectSSH{ 50 Config: &b.config.Comm, 51 Host: func(stateBag multistep.StateBag) (string, error) { 52 return stateBag.Get("PublicIP").(string), nil 53 }, 54 SSHConfig: b.config.Comm.SSHConfigFunc(), 55 }, 56 &common.StepProvision{}, 57 &common.StepCleanupTempKeys{ 58 Comm: &b.config.Comm, 59 }, 60 NewStepStopServerInstance(conn, ui), 61 NewStepCreateServerImage(conn, ui, b.config), 62 NewStepDeleteBlockStorageInstance(conn, ui, b.config), 63 NewStepTerminateServerInstance(conn, ui), 64 } 65 } else if b.config.Comm.Type == "winrm" { 66 steps = []multistep.Step{ 67 NewStepValidateTemplate(conn, ui, b.config), 68 NewStepCreateLoginKey(conn, ui), 69 NewStepCreateServerInstance(conn, ui, b.config), 70 NewStepCreateBlockStorageInstance(conn, ui, b.config), 71 NewStepGetRootPassword(conn, ui), 72 NewStepCreatePublicIPInstance(conn, ui, b.config), 73 &communicator.StepConnectWinRM{ 74 Config: &b.config.Comm, 75 Host: func(stateBag multistep.StateBag) (string, error) { 76 return stateBag.Get("PublicIP").(string), nil 77 }, 78 WinRMConfig: func(state multistep.StateBag) (*communicator.WinRMConfig, error) { 79 return &communicator.WinRMConfig{ 80 Username: b.config.Comm.WinRMUser, 81 Password: state.Get("Password").(string), 82 }, nil 83 }, 84 }, 85 &common.StepProvision{}, 86 NewStepStopServerInstance(conn, ui), 87 NewStepCreateServerImage(conn, ui, b.config), 88 NewStepDeleteBlockStorageInstance(conn, ui, b.config), 89 NewStepTerminateServerInstance(conn, ui), 90 } 91 } 92 93 // Run! 94 b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, b.stateBag) 95 b.runner.Run(b.stateBag) 96 97 // If there was an error, return that 98 if rawErr, ok := b.stateBag.GetOk("Error"); ok { 99 return nil, rawErr.(error) 100 } 101 102 // Build the artifact and return it 103 artifact := &Artifact{} 104 105 if serverImage, ok := b.stateBag.GetOk("memberServerImage"); ok { 106 artifact.ServerImage = serverImage.(*ncloud.ServerImage) 107 } 108 109 return artifact, nil 110 } 111 112 func (b *Builder) Cancel() { 113 b.runner.Cancel() 114 }