-
Notifications
You must be signed in to change notification settings - Fork 5
/
vsphere.pkr.hcl
491 lines (419 loc) · 14.2 KB
/
vsphere.pkr.hcl
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
packer {
required_plugins {
sshkey = {
version = ">= 1.0.1"
source = "github.com/ivoronin/sshkey"
}
vsphere = {
version = ">= 1.1.1"
source = "github.com/hashicorp/vsphere"
}
}
}
// conntextion details
variable "vcenter_server" {
type = string
default = "${env("VSPHERE_SERVER")}"
}
variable "vsphere_user" {
type = string
default = "${env("VSPHERE_USERNAME")}"
}
variable "vsphere_password" {
type = string
default = "${env("VSPHERE_PASSWORD")}"
}
variable "vsphere_insecure_connection" {
type = string
default = "false"
}
// vsphere related details
variable "vsphere_cluster" {
type = string
}
variable "vsphere_datastore" {
type = string
}
variable "vsphere_datacenter" {
type = string
}
variable "vsphere_network" {
type = string
}
variable "vsphere_resource_pool" {
type = string
default = ""
}
variable "vsphere_folder" {
type = string
default = ""
}
// image build related details
variable "distribution" {
type = string
}
variable "distribution_version" {
type = string
}
variable "iso_url" {
type = string
default = ""
}
variable "iso_paths" {
type = list(string)
default = []
}
variable "iso_checksum" {
type = string
default = null
}
// customizations
variable "vm_name" {
type = string
default = ""
description = "set the vm name (which will be the name of the template)"
}
variable "vm_name_prefix" {
type = string
default = "d2iq-base-"
description = "add a prefix to the name"
}
variable "vm_name_postfix" {
type = string
default = "$$D2iQDefault$$"
description = "add a postfix to the name"
}
variable "vsphere_guest_os_type" {
type = string
default = ""
}
variable "cpu" {
type = string
default = "4"
}
variable "cpu_cores" {
type = string
default = "1"
}
variable "disk_size" {
type = number
default = 20480
}
variable "memory" {
type = string
default = "8192"
}
variable "disk_thin_provisioned" {
type = bool
default = true
}
variable "firmware" {
type = string
default = "bios"
}
variable "ssh_timeout" {
type = string
default = "30m"
}
variable "ssh_username" {
type = string
default = ""
}
variable "dry_run" {
type = bool
default = false
}
variable "manifest_output" {
type = string
default = ""
}
variable "rhn_username" {
type = string
default = "${env("RHN_USERNAME")}"
}
variable "rhn_password" {
type = string
default = "${env("RHN_PASSWORD")}"
}
variable "rhn_subscription_key" {
type = string
default = "${env("RHN_SUBSCRIPTION_KEY")}"
}
variable "rhn_subscription_org" {
type = string
default = "${env("RHN_SUBSCRIPTION_ORG")}"
}
variable "iso_path_entry" {
default = ""
}
variable "bootconfig_type" {
default = "cloudinit"
}
data "sshkey" "install" {
name = "base-image-build"
}
# All locals variables are generated from variables that uses expressions
# that are not allowed in HCL2 variables.
# Read the documentation for locals blocks here:
# https://www.packer.io/docs/templates/hcl_templates/blocks/locals
locals {
vm_name_postfix = var.vm_name_postfix == "$$D2iQDefault$$" ? "-${regex_replace(timestamp(), "[- TZ:]", "")}" : var.vm_name_postfix
vm_name_var = var.vm_name == "" ? "${var.distribution}-${var.distribution_version}" : var.vm_name
vm_name = "${var.vm_name_prefix}${local.vm_name_var}${local.vm_name_postfix}"
# lookup by <distro_name>-<distro_version> fallback to <distro_version>
distro_version_bootfile_lookup = {
"RHEL-7" = "${path.root}/bootfiles/rhel/rhel7.ks"
"RHEL" = "${path.root}/bootfiles/rhel/rhel8.ks"
"RockyLinux" = "${path.root}/bootfiles/rocky/rocky.ks"
"RockyLinux-8.7" = "${path.root}/bootfiles/rocky/rocky-vault.ks"
"RockyLinux-9.1" = "${path.root}/bootfiles/rocky/rocky-vault.ks"
"CentOS" = "${path.root}/bootfiles/centos/centos7.ks"
"OracleLinux" = "${path.root}/bootfiles/oraclelinux/oraclelinux9.ks"
"Ubuntu" = "${path.root}/bootfiles/ubuntu/autoinstall.yaml"
"Ubuntu-18.04" = "${path.root}/bootfiles/ubuntu/preseed.cfg"
"Flatcar" = "${path.root}/bootfiles/flatcar/bootfile.sh.tmpl"
}
el_old_bootcommand = [
"<tab><wait>",
" ks=hd:sr1:/bootfile.cfg<enter>"
]
el_bootcommand = [
"<tab><wait>",
" inst.ks=hd:sr1:/bootfile.cfg<enter>"
]
ubuntu_bionic_bootcommand = [
"<wait>e<down><down><down><end><wait>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
"<bs><bs><bs><bs><bs><bs>",
"/install/vmlinuz",
" priority=critical locale=en_US",
" file=/media/BOOTFILE.CFG",
"<f10>"
]
ubuntu_bootcommand = [
"<esc><esc><esc><esc><esc><esc><esc><esc>",
"<esc><wait>",
"<esc><wait>",
"linux /casper/vmlinuz autoinstall ds=nocloud text",
"<enter><wait>",
"initrd /casper/initrd",
"<enter><wait>",
"boot",
"<enter>"
]
ubuntu_jammy_bootcommand = [
"c<wait>",
"linux /casper/vmlinuz --- autoinstall ds=nocloud text",
"<enter><wait>",
"initrd /casper/initrd",
"<enter><wait>",
"boot",
"<enter>"
]
flatcar_bootcommand = [
"<wait><wait><wait>",
"sudo mkdir /bootfiles<enter>",
"sudo mount -o loop /dev/sr1 /bootfiles<enter>",
"sudo bash /bootfiles/bootfile.sh<enter>",
]
# lookup by <distro_name>-<distro_version> fallback to <distro_name>
distro_boot_command_lookup = {
"RHEL-7" = local.el_old_bootcommand
"RHEL" = local.el_bootcommand
"CentOS" = local.el_old_bootcommand
"RockyLinux" = local.el_bootcommand
"OracleLinux" = local.el_bootcommand
"Ubuntu-18.04" = local.ubuntu_bionic_bootcommand
"Ubuntu-20.04" = local.ubuntu_bootcommand
"Ubuntu-22.04" = local.ubuntu_jammy_bootcommand
"Flatcar" = local.flatcar_bootcommand
}
default_firmware = "bios"
# lookup by <distro_name>-<distro_version> fallback to <distro_version> fallback to local.default_firmware
distro_firmware_lookup = {
"Ubuntu" = "efi-secure"
}
default_bootwait = "10s"
# lookup by <distro_name>-<distro_version> fallback to <distro_version> fallback to local.default_bootwait
distro_bootwait_lookup = {
"Ubuntu" = "1s"
}
# lookup by <distro_name>-<distro_version> fallback to <distro_version> fallback to ""
distro_cd_label_lookup = {
"Ubuntu" = "cidata",
"Flatcar" = "cidata"
}
default_vsphere_guest_os_type = "otherlinux64guest"
distro_vsphere_guest_os_type_lookup = {
"Ubuntu" = "ubuntu64Guest",
"CentOS" = "centos64Guest",
"RHEL" = "rhel7_64Guest"
"RockyLinux" = "centos64Guest"
"OracleLinux" = "oracleLinux64Guest"
"Flatcar" = "otherlinux64Guest"
}
# lookup by <distro_name>-<distro_version> fallback to <distro_name>
distro_default_ssh_username = {
"Ubuntu" = "ubuntu",
"CentOS" = "centos",
"RHEL" = "eluser"
"RockyLinux" = "rockstar"
"OracleLinux" = "opc"
"Flatcar" = "core"
}
boot_command_distro = lookup(local.distro_boot_command_lookup, "${var.distribution}", [""])
boot_command = lookup(local.distro_boot_command_lookup, "${var.distribution}-${var.distribution_version}", local.boot_command_distro)
firmware = lookup(local.distro_firmware_lookup, "${var.distribution}-${var.distribution_version}", lookup(local.distro_firmware_lookup, "${var.distribution}", local.default_firmware))
boot_wait = lookup(local.distro_bootwait_lookup, "${var.distribution}-${var.distribution_version}", lookup(local.distro_bootwait_lookup, "${var.distribution}", local.default_bootwait))
cd_label = lookup(local.distro_cd_label_lookup, "${var.distribution}-${var.distribution_version}", lookup(local.distro_cd_label_lookup, "${var.distribution}", ""))
fallback_username = "user"
distro_ssh_username = lookup(local.distro_default_ssh_username, "${var.distribution}-${var.distribution_version}", lookup(local.distro_default_ssh_username, "${var.distribution}", local.fallback_username))
ssh_username = var.ssh_username != "" ? var.ssh_username : local.distro_ssh_username
distro_vsphere_guest_os_type = var.vsphere_guest_os_type != "" ? var.vsphere_guest_os_type : lookup(local.distro_vsphere_guest_os_type_lookup, "${var.distribution}-${var.distribution_version}", lookup(local.distro_vsphere_guest_os_type_lookup, "${var.distribution}", local.default_vsphere_guest_os_type))
bootfile_name = lookup(local.distro_version_bootfile_lookup, "${var.distribution}-${var.distribution_version}", lookup(local.distro_version_bootfile_lookup, "${var.distribution}", ""))
bootfile = templatefile(local.bootfile_name, {
ssh_username = local.ssh_username
public_key = data.sshkey.install.public_key
sha512_fake_pw = sha512(uuidv4())
distribution = var.distribution
distribution_version = var.distribution_version
})
}
source "vsphere-iso" "baseimage" {
CPUs = var.cpu
RAM = var.memory
cluster = var.vsphere_cluster
disk_controller_type = ["pvscsi"]
guest_os_type = local.distro_vsphere_guest_os_type
network_adapters {
network_card = "vmxnet3"
network = var.vsphere_network
}
boot_wait = local.boot_wait
boot_command = local.boot_command
firmware = local.firmware
cd_content = {
"/bootfile.cfg" = local.bootfile,
"/bootfile.sh" = local.bootfile,
# make it cloud-config compatible
"/user-data" = local.bootfile,
"/meta-data" = "",
"/authorized_keys" = data.sshkey.install.public_key
}
cd_label = local.cd_label
floppy_content = {
"/BOOTFILE.CFG" = local.bootfile,
# make it cloud-config compatible
"/user-data" = local.bootfile,
"/meta-data" = "",
"/authorized_keys" = data.sshkey.install.public_key
}
storage {
disk_size = var.disk_size
disk_thin_provisioned = var.disk_thin_provisioned
}
communicator = "ssh"
cpu_cores = var.cpu_cores
datacenter = var.vsphere_datacenter
resource_pool = var.vsphere_resource_pool
datastore = var.vsphere_datastore
folder = var.vsphere_folder
insecure_connection = var.vsphere_insecure_connection
iso_url = var.iso_url
iso_paths = var.iso_path_entry != "" ? [ var.iso_path_entry ] : var.iso_paths
iso_checksum = var.iso_checksum
password = var.vsphere_password
ssh_private_key_file = data.sshkey.install.private_key_path
ssh_clear_authorized_keys = true
ssh_key_exchange_algorithms = ["curve25519-sha256@libssh.org", "ecdh-sha2-nistp256", "ecdh-sha2-nistp384", "ecdh-sha2-nistp521", "diffie-hellman-group14-sha1", "diffie-hellman-group1-sha1"]
ssh_timeout = var.ssh_timeout
ssh_username = local.ssh_username
username = var.vsphere_user
vcenter_server = var.vcenter_server
vm_name = local.vm_name
create_snapshot = !var.dry_run
convert_to_template = !var.dry_run
}
locals {
distro_build_scripts = {
"Ubuntu" = [
"${path.root}/scripts/ubuntu/install_open_vm_tools.sh",
"${path.root}/scripts/ubuntu/post_cleanup.sh",
],
"CentOS" = [
"${path.root}/scripts/el/el7_ensure_python.sh",
"${path.root}/scripts/el/install_open_vm_tools.sh",
"${path.root}/scripts/common/cloudinit_from_source.sh",
"${path.root}/scripts/el/cleanup_yum.sh"
],
"RHEL-7" = [
"${path.root}/scripts/el/rhn_add_subscription.sh",
"${path.root}/scripts/el/install_open_vm_tools.sh",
"${path.root}/scripts/el/install_cloud_tools.sh",
"${path.root}/scripts/el/el7_install_cloud_init-vmware-ds.sh",
"${path.root}/scripts/el/cleanup_yum.sh",
"${path.root}/scripts/el/rhn_remove_subscription.sh"
],
"RHEL" = [
"${path.root}/scripts/el/rhn_add_subscription.sh",
"${path.root}/scripts/el/install_open_vm_tools.sh",
"${path.root}/scripts/el/install_cloud_tools.sh",
"${path.root}/scripts/el/cleanup_dnf.sh",
"${path.root}/scripts/el/rhn_remove_subscription.sh"
],
"RockyLinux" = [
"${path.root}/scripts/el/install_open_vm_tools.sh",
"${path.root}/scripts/el/cleanup_dnf.sh"
],
"OracleLinux" = [
"${path.root}/scripts/el/install_open_vm_tools.sh",
"${path.root}/scripts/el/cleanup_dnf.sh"
],
"Flatcar" = [
"${path.root}/scripts/flatcar/no_autologin.sh",
"${path.root}/scripts/flatcar/clean.sh",
]
}
common_pre_distro = []
common_post_distro = [
"${path.root}/scripts/common/clean_authorized_keys.sh",
"${path.root}/scripts/common/finish_cloud_init.sh",
"${path.root}/scripts/common/zero_disk.sh"
]
build_scripts_distro = lookup(local.distro_build_scripts, "${var.distribution}-${var.distribution_version}", lookup(local.distro_build_scripts, "${var.distribution}", []))
build_scripts = concat(local.common_pre_distro, local.build_scripts_distro, local.common_post_distro)
}
build {
sources = ["source.vsphere-iso.baseimage"]
provisioner "shell" {
env = {
BASE_IMAGE_SSH_USER = local.ssh_username
RHN_USERNAME = var.rhn_username
RHN_PASSWORD = var.rhn_password
RHN_SUBSCRIPTION_KEY = var.rhn_subscription_key
RHN_SUBSCRIPTION_ORG = var.rhn_subscription_org
}
execute_command = "sudo su -m root -c '{{ .Vars }} {{.Path}}'"
scripts = local.build_scripts
}
post-processor "manifest" {
output = var.manifest_output == "" ? "manifests/${local.vm_name}.json" : var.manifest_output
strip_path = true
custom_data = {
iso_url = var.iso_url
iso_checksum = var.iso_checksum
template_name = join("/", [var.vsphere_folder, local.vm_name])
ssh_username = local.ssh_username
datacenter = var.vsphere_datacenter
distribution = var.distribution
distribution_version = var.distribution_version
resource_pool = var.vsphere_resource_pool
bootconfig_type = var.bootconfig_type
}
}
}