File: builder.go

package info (click to toggle)
packer 1.6.6%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 33,156 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (124 lines) | stat: -rw-r--r-- 3,836 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package ncloud

import (
	"context"

	"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server"
	"github.com/hashicorp/hcl/v2/hcldec"
	"github.com/hashicorp/packer/packer-plugin-sdk/communicator"
	"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
	"github.com/hashicorp/packer/packer-plugin-sdk/multistep/commonsteps"
	packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
)

// Builder assume this implements packersdk.Builder
type Builder struct {
	config   Config
	stateBag multistep.StateBag
	runner   multistep.Runner
}

func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }

func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
	warnings, errs := b.config.Prepare(raws...)
	if errs != nil {
		return nil, warnings, errs
	}

	b.stateBag = new(multistep.BasicStateBag)

	return nil, warnings, nil
}

func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
	ui.Message("Creating Naver Cloud Platform Connection ...")
	config := Config{
		AccessKey: b.config.AccessKey,
		SecretKey: b.config.SecretKey,
	}

	conn, err := config.Client()
	if err != nil {
		return nil, err
	}

	b.stateBag.Put("hook", hook)
	b.stateBag.Put("ui", ui)

	var steps []multistep.Step

	steps = []multistep.Step{}

	if b.config.Comm.Type == "ssh" {
		steps = []multistep.Step{
			NewStepValidateTemplate(conn, ui, &b.config),
			NewStepCreateLoginKey(conn, ui),
			NewStepCreateServerInstance(conn, ui, &b.config),
			NewStepCreateBlockStorageInstance(conn, ui, &b.config),
			NewStepGetRootPassword(conn, ui, &b.config),
			NewStepCreatePublicIPInstance(conn, ui, &b.config),
			&communicator.StepConnectSSH{
				Config: &b.config.Comm,
				Host: func(stateBag multistep.StateBag) (string, error) {
					return stateBag.Get("PublicIP").(string), nil
				},
				SSHConfig: b.config.Comm.SSHConfigFunc(),
			},
			&commonsteps.StepProvision{},
			&commonsteps.StepCleanupTempKeys{
				Comm: &b.config.Comm,
			},
			NewStepStopServerInstance(conn, ui),
			NewStepCreateServerImage(conn, ui, &b.config),
			NewStepDeleteBlockStorageInstance(conn, ui, &b.config),
			NewStepTerminateServerInstance(conn, ui),
		}
	} else if b.config.Comm.Type == "winrm" {
		steps = []multistep.Step{
			NewStepValidateTemplate(conn, ui, &b.config),
			NewStepCreateLoginKey(conn, ui),
			NewStepCreateServerInstance(conn, ui, &b.config),
			NewStepCreateBlockStorageInstance(conn, ui, &b.config),
			NewStepGetRootPassword(conn, ui, &b.config),
			NewStepCreatePublicIPInstance(conn, ui, &b.config),
			&communicator.StepConnectWinRM{
				Config: &b.config.Comm,
				Host: func(stateBag multistep.StateBag) (string, error) {
					return stateBag.Get("PublicIP").(string), nil
				},
				WinRMConfig: func(state multistep.StateBag) (*communicator.WinRMConfig, error) {
					return &communicator.WinRMConfig{
						Username: b.config.Comm.WinRMUser,
						Password: b.config.Comm.WinRMPassword,
					}, nil
				},
			},
			&commonsteps.StepProvision{},
			NewStepStopServerInstance(conn, ui),
			NewStepCreateServerImage(conn, ui, &b.config),
			NewStepDeleteBlockStorageInstance(conn, ui, &b.config),
			NewStepTerminateServerInstance(conn, ui),
		}
	}

	// Run!
	b.runner = commonsteps.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, b.stateBag)
	b.runner.Run(ctx, b.stateBag)

	// 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
	artifact := &Artifact{
		StateData: map[string]interface{}{"generated_data": b.stateBag.Get("generated_data")},
	}

	if serverImage, ok := b.stateBag.GetOk("memberServerImage"); ok {
		artifact.MemberServerImage = serverImage.(*server.MemberServerImage)
	}

	return artifact, nil
}