2018-01-11 04:57:53 -05:00
|
|
|
package ncloud
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
|
|
|
|
2018-01-11 04:57:53 -05:00
|
|
|
ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk"
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2018-01-11 04:57:53 -05:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2018-01-29 08:41:22 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-29 08:44:24 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2018-01-11 04:57:53 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Builder assume this implements packer.Builder
|
|
|
|
type Builder struct {
|
2019-12-17 05:25:56 -05:00
|
|
|
config Config
|
2018-01-11 04:57:53 -05:00
|
|
|
stateBag multistep.StateBag
|
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2019-12-17 05:25:56 -05:00
|
|
|
warnings, errs := b.config.Prepare(raws...)
|
2018-01-11 04:57:53 -05:00
|
|
|
if errs != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, errs
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
b.stateBag = new(multistep.BasicStateBag)
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, nil
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
2019-03-22 09:53:28 -04:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2018-01-11 04:57:53 -05:00
|
|
|
ui.Message("Creating Naver Cloud Platform Connection ...")
|
|
|
|
conn := ncloud.NewConnection(b.config.AccessKey, b.config.SecretKey)
|
|
|
|
|
|
|
|
b.stateBag.Put("hook", hook)
|
|
|
|
b.stateBag.Put("ui", ui)
|
|
|
|
|
|
|
|
var steps []multistep.Step
|
|
|
|
|
|
|
|
steps = []multistep.Step{}
|
|
|
|
|
2018-01-29 03:29:26 -05:00
|
|
|
if b.config.Comm.Type == "ssh" {
|
2018-01-11 04:57:53 -05:00
|
|
|
steps = []multistep.Step{
|
2019-12-17 05:25:56 -05:00
|
|
|
NewStepValidateTemplate(conn, ui, &b.config),
|
2018-01-11 04:57:53 -05:00
|
|
|
NewStepCreateLoginKey(conn, ui),
|
2019-12-17 05:25:56 -05:00
|
|
|
NewStepCreateServerInstance(conn, ui, &b.config),
|
|
|
|
NewStepCreateBlockStorageInstance(conn, ui, &b.config),
|
|
|
|
NewStepGetRootPassword(conn, ui, &b.config),
|
|
|
|
NewStepCreatePublicIPInstance(conn, ui, &b.config),
|
2018-01-11 04:57:53 -05:00
|
|
|
&communicator.StepConnectSSH{
|
2018-01-29 08:07:32 -05:00
|
|
|
Config: &b.config.Comm,
|
|
|
|
Host: func(stateBag multistep.StateBag) (string, error) {
|
|
|
|
return stateBag.Get("PublicIP").(string), nil
|
|
|
|
},
|
2018-08-24 10:00:40 -04:00
|
|
|
SSHConfig: b.config.Comm.SSHConfigFunc(),
|
2018-01-11 04:57:53 -05:00
|
|
|
},
|
|
|
|
&common.StepProvision{},
|
2018-09-14 14:03:23 -04:00
|
|
|
&common.StepCleanupTempKeys{
|
|
|
|
Comm: &b.config.Comm,
|
|
|
|
},
|
2018-01-11 04:57:53 -05:00
|
|
|
NewStepStopServerInstance(conn, ui),
|
2019-12-17 05:25:56 -05:00
|
|
|
NewStepCreateServerImage(conn, ui, &b.config),
|
|
|
|
NewStepDeleteBlockStorageInstance(conn, ui, &b.config),
|
2018-01-11 04:57:53 -05:00
|
|
|
NewStepTerminateServerInstance(conn, ui),
|
|
|
|
}
|
2018-02-06 20:29:56 -05:00
|
|
|
} else if b.config.Comm.Type == "winrm" {
|
2018-01-11 04:57:53 -05:00
|
|
|
steps = []multistep.Step{
|
2019-12-17 05:25:56 -05:00
|
|
|
NewStepValidateTemplate(conn, ui, &b.config),
|
2018-01-11 04:57:53 -05:00
|
|
|
NewStepCreateLoginKey(conn, ui),
|
2019-12-17 05:25:56 -05:00
|
|
|
NewStepCreateServerInstance(conn, ui, &b.config),
|
|
|
|
NewStepCreateBlockStorageInstance(conn, ui, &b.config),
|
|
|
|
NewStepGetRootPassword(conn, ui, &b.config),
|
|
|
|
NewStepCreatePublicIPInstance(conn, ui, &b.config),
|
2018-01-11 04:57:53 -05:00
|
|
|
&communicator.StepConnectWinRM{
|
|
|
|
Config: &b.config.Comm,
|
|
|
|
Host: func(stateBag multistep.StateBag) (string, error) {
|
2018-01-29 08:07:32 -05:00
|
|
|
return stateBag.Get("PublicIP").(string), nil
|
2018-01-11 04:57:53 -05:00
|
|
|
},
|
|
|
|
WinRMConfig: func(state multistep.StateBag) (*communicator.WinRMConfig, error) {
|
|
|
|
return &communicator.WinRMConfig{
|
|
|
|
Username: b.config.Comm.WinRMUser,
|
2018-12-21 03:53:52 -05:00
|
|
|
Password: b.config.Comm.WinRMPassword,
|
2018-01-11 04:57:53 -05:00
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&common.StepProvision{},
|
|
|
|
NewStepStopServerInstance(conn, ui),
|
2019-12-17 05:25:56 -05:00
|
|
|
NewStepCreateServerImage(conn, ui, &b.config),
|
|
|
|
NewStepDeleteBlockStorageInstance(conn, ui, &b.config),
|
2018-01-11 04:57:53 -05:00
|
|
|
NewStepTerminateServerInstance(conn, ui),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run!
|
2018-01-29 03:29:26 -05:00
|
|
|
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, b.stateBag)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, b.stateBag)
|
2018-01-11 04:57:53 -05:00
|
|
|
|
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := b.stateBag.GetOk("Error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the artifact and return it
|
2020-01-30 05:27:58 -05:00
|
|
|
artifact := &Artifact{
|
|
|
|
StateData: map[string]interface{}{"generated_data": b.stateBag.Get("generated_data")},
|
|
|
|
}
|
2018-01-11 04:57:53 -05:00
|
|
|
|
|
|
|
if serverImage, ok := b.stateBag.GetOk("memberServerImage"); ok {
|
|
|
|
artifact.ServerImage = serverImage.(*ncloud.ServerImage)
|
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|