generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 39
/
builder.go
228 lines (213 loc) · 7.17 KB
/
builder.go
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package iso
import (
"context"
"errors"
"fmt"
"time"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
vmwcommon "github.com/hashicorp/packer-plugin-vmware/builder/vmware/common"
)
type Builder struct {
config Config
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
}
return nil, warnings, nil
}
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
driver, err := vmwcommon.NewDriver(&b.config.DriverConfig, &b.config.SSHConfig, b.config.VMName)
if err != nil {
return nil, fmt.Errorf("failed creating driver : %s", err)
}
// Before we get deep into the build, make sure ovftool is present and
// credentials are valid, if we're going to use ovftool.
if err := driver.VerifyOvfTool(b.config.SkipExport, b.config.SkipValidateCredentials); err != nil {
return nil, err
}
// Setup the state bag
state := new(multistep.BasicStateBag)
state.Put("config", &b.config)
state.Put("debug", b.config.PackerDebug)
state.Put("driver", driver)
state.Put("hook", hook)
state.Put("ui", ui)
state.Put("sshConfig", &b.config.SSHConfig)
state.Put("driverConfig", &b.config.DriverConfig)
state.Put("temporaryDevices", []string{}) // Devices (in .vmx) created by packer during building
steps := []multistep.Step{
&vmwcommon.StepPrepareTools{
RemoteType: b.config.RemoteType,
ToolsUploadFlavor: b.config.ToolsUploadFlavor,
},
&commonsteps.StepDownload{
Checksum: b.config.ISOChecksum,
Description: "ISO",
Extension: b.config.TargetExtension,
ResultKey: "iso_path",
TargetPath: b.config.TargetPath,
Url: b.config.ISOUrls,
},
&vmwcommon.StepOutputDir{
Force: b.config.PackerForce,
OutputConfig: &b.config.OutputConfig,
RemoteType: b.config.RemoteType,
VMName: b.config.VMName,
},
&commonsteps.StepCreateFloppy{
Files: b.config.FloppyConfig.FloppyFiles,
Directories: b.config.FloppyConfig.FloppyDirectories,
Content: b.config.FloppyConfig.FloppyContent,
Label: b.config.FloppyConfig.FloppyLabel,
},
&commonsteps.StepCreateCD{
Files: b.config.CDConfig.CDFiles,
Content: b.config.CDConfig.CDContent,
Label: b.config.CDConfig.CDLabel,
},
&vmwcommon.StepRemoteUpload{
Key: "floppy_path",
Message: "Uploading Floppy to remote machine...",
DoCleanup: true,
Checksum: "none",
},
&vmwcommon.StepRemoteUpload{
Key: "cd_path",
Message: "Uploading CD to remote machine...",
DoCleanup: true,
Checksum: "none",
},
&vmwcommon.StepRemoteUpload{
Key: "iso_path",
Message: "Uploading ISO to remote machine...",
DoCleanup: b.config.DriverConfig.CleanUpRemoteCache,
Checksum: b.config.ISOChecksum,
},
&vmwcommon.StepCreateDisks{
OutputDir: &b.config.OutputDir,
CreateMainDisk: true,
DiskName: b.config.DiskName,
MainDiskSize: b.config.DiskSize,
AdditionalDiskSize: b.config.AdditionalDiskSize,
DiskAdapterType: b.config.DiskAdapterType,
DiskTypeId: b.config.DiskTypeId,
},
&stepCreateVMX{},
&vmwcommon.StepConfigureVMX{
CustomData: b.config.VMXData,
VMName: b.config.VMName,
DisplayName: b.config.VMXDisplayName,
DiskAdapterType: b.config.DiskAdapterType,
CDROMAdapterType: b.config.CdromAdapterType,
},
&vmwcommon.StepSuppressMessages{},
&vmwcommon.StepHTTPIPDiscover{},
commonsteps.HTTPServerFromHTTPConfig(&b.config.HTTPConfig),
multistep.If(b.config.Comm.Type == "ssh", &communicator.StepSSHKeyGen{
CommConf: &b.config.Comm,
SSHTemporaryKeyPair: b.config.Comm.SSHTemporaryKeyPair,
}),
&vmwcommon.StepConfigureVNC{
Enabled: !b.config.DisableVNC && !b.config.VNCOverWebsocket,
VNCBindAddress: b.config.VNCBindAddress,
VNCPortMin: b.config.VNCPortMin,
VNCPortMax: b.config.VNCPortMax,
VNCDisablePassword: b.config.VNCDisablePassword,
},
&vmwcommon.StepRegister{
Format: b.config.Format,
KeepRegistered: b.config.KeepRegistered,
SkipExport: b.config.SkipExport,
},
&vmwcommon.StepRun{
DurationBeforeStop: 5 * time.Second,
Headless: b.config.Headless,
},
&vmwcommon.StepVNCConnect{
VNCEnabled: !b.config.DisableVNC,
VNCOverWebsocket: b.config.VNCOverWebsocket,
InsecureConnection: b.config.InsecureConnection,
DriverConfig: &b.config.DriverConfig,
},
&vmwcommon.StepVNCBootCommand{
Config: b.config.VNCConfig,
VMName: b.config.VMName,
Ctx: b.config.ctx,
Comm: &b.config.Comm,
},
&communicator.StepConnect{
Config: &b.config.SSHConfig.Comm,
Host: driver.CommHost,
SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
},
&vmwcommon.StepUploadTools{
RemoteType: b.config.RemoteType,
ToolsUploadFlavor: b.config.ToolsUploadFlavor,
ToolsUploadPath: b.config.ToolsUploadPath,
Ctx: b.config.ctx,
},
&commonsteps.StepProvision{},
&commonsteps.StepCleanupTempKeys{
Comm: &b.config.SSHConfig.Comm,
},
&vmwcommon.StepShutdown{
Command: b.config.ShutdownCommand,
Timeout: b.config.ShutdownTimeout,
},
&vmwcommon.StepCleanFiles{},
&vmwcommon.StepCompactDisk{
Skip: b.config.SkipCompaction,
},
&vmwcommon.StepConfigureVMX{
CustomData: b.config.VMXDataPost,
SkipDevices: true,
VMName: b.config.VMName,
DisplayName: b.config.VMXDisplayName,
},
&vmwcommon.StepCleanVMX{
RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet,
VNCEnabled: !b.config.DisableVNC,
},
&vmwcommon.StepCreateSnapshot{
SnapshotName: &b.config.SnapshotName,
},
&vmwcommon.StepUploadVMX{
RemoteType: b.config.RemoteType,
},
&vmwcommon.StepExport{
Format: b.config.Format,
SkipExport: b.config.SkipExport,
VMName: b.config.VMName,
OVFToolOptions: b.config.OVFToolOptions,
OutputDir: &b.config.OutputConfig.OutputDir,
},
}
// Run!
b.runner = commonsteps.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(ctx, state)
// If there was an error, return that
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
// If we were interrupted or cancelled, then just exit.
if _, ok := state.GetOk(multistep.StateCancelled); ok {
return nil, errors.New("build was cancelled")
}
if _, ok := state.GetOk(multistep.StateHalted); ok {
return nil, errors.New("build was halted")
}
// Compile the artifact list
exportOutputPath := state.Get("export_output_path").(string) // set in StepOutputDir
return vmwcommon.NewArtifact(b.config.RemoteType, b.config.Format, exportOutputPath,
b.config.VMName, b.config.SkipExport, b.config.KeepRegistered, state)
}