-
Notifications
You must be signed in to change notification settings - Fork 3
/
clone-test.pkr.hcl
243 lines (210 loc) · 5.73 KB
/
clone-test.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
packer {
required_plugins {
sshkey = {
version = ">= 1.0.1"
source = "github.com/ivoronin/sshkey"
}
vsphere = {
version = ">= 1.1.1"
source = "github.com/hashicorp/vsphere"
}
}
}
variable "template_manifest" {
type = string
default = ""
}
variable "ssh_username" {
type = string
default = ""
}
variable "template" {
type = string
default = ""
}
variable "ssh_public_key" {
type = string
default = ""
}
variable "ssh_agent_auth" {
type = bool
default = false
}
// 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 = ""
}
// customizations
variable "vm_name" {
type = string
default = "packer-clone-test"
}
variable "manifest_output" {
type = string
default = ""
}
variable "bootconfig_type" {
description = "cloudinit or ignition"
type = string
default = "cloudinit"
}
data "sshkey" "install" {
name = "base-image-test"
}
locals {
manifestfile = var.template_manifest != "" ? file(var.template_manifest) : ""
builds = local.manifestfile != "" ? lookup(jsondecode(local.manifestfile), "builds", []) : []
build = length(local.builds) > 0 ? local.builds[0] : convert("{}", object)
custom_data = contains(keys(local.build), "custom_data") ? local.build["custom_data"] : {}
template = lookup(local.custom_data, "template_name", var.template)
ssh_username = lookup(local.custom_data, "ssh_username", var.ssh_username)
bootconfig_type = lookup(local.custom_data, "bootconfig_type", var.bootconfig_type)
base_cloudinit = <<EOF
#cloud-config
users:
- name: ${local.ssh_username}
sudo: ALL=(ALL) NOPASSWD:ALL
groups: sudo, wheel
lock_passwd: true
ssh_authorized_keys:
- ${var.ssh_public_key == "" ? data.sshkey.install.public_key : var.ssh_public_key}
EOF
cloudinit_metadata = <<EOF
instance-id: ${var.vm_name}
local-hostname: ${var.vm_name}
network:
version: 2
ethernets:
nics:
match:
name: ens*
dhcp4: yes
nics:
match:
name: eth*
dhcp4: yes
EOF
base_ignition = <<EOF
{
"ignition": {
"config": {},
"security": {
"tls": {}
},
"timeouts": {},
"version": "2.3.0"
},
"networkd": {},
"passwd": {
"users": [
{
"name": "core",
"sshAuthorizedKeys": [
"${var.ssh_public_key == "" ? data.sshkey.install.public_key : var.ssh_public_key}"
]
}
]
}
}
EOF
cloud_init_configuration_parameters = {
"guestinfo.userdata" = base64encode(local.base_cloudinit),
"guestinfo.userdata.encoding" = "base64",
"guestinfo.metadata" = base64encode(local.cloudinit_metadata)
"guestinfo.metadata.encoding" = "base64"
}
ignition_configuration_parameters = {
"guestinfo.ignition.config.data" = base64encode(local.base_ignition),
"guestinfo.ignition.config.data.encoding" = "base64",
}
configuration_parameters = local.bootconfig_type == "cloudinit" ? local.cloud_init_configuration_parameters : local.ignition_configuration_parameters
}
source "vsphere-clone" "clone_test" {
CPUs = 1
RAM = 2048
cluster = var.vsphere_cluster
communicator = "ssh"
cpu_cores = 1
datacenter = var.vsphere_datacenter
datastore = var.vsphere_datastore
folder = var.vsphere_folder
insecure_connection = var.vsphere_insecure_connection
network = var.vsphere_network
password = var.vsphere_password
ssh_private_key_file = data.sshkey.install.private_key_path
ssh_timeout = "60m"
ssh_username = local.ssh_username
ssh_agent_auth = var.ssh_agent_auth
template = local.template
username = var.vsphere_user
vcenter_server = var.vcenter_server
vm_name = var.vm_name
resource_pool = var.vsphere_resource_pool
// # once the test is done we don't need the vm
// destroy = true
disk_size = 40960
// cd_label = "cidata"
// cd_content = {
// "/user-data" = local.base_cloudinit,
// "/user-data.txt" = local.base_cloudinit,
// "/meta-data" = "",
// }
configuration_parameters = local.configuration_parameters
create_snapshot = false
convert_to_template = false
}
build {
sources = ["source.vsphere-clone.clone_test"]
provisioner "shell" {
execute_command = "sudo su -m root -c 'sh -eux {{.Path}}'"
scripts = [
"${path.root}/tests/test_login.sh",
"${path.root}/tests/test_disk.sh"
]
}
post-processor "manifest" {
output = var.manifest_output == "" ? "manifests/test/${var.vm_name}.json" : var.manifest_output
strip_path = true
custom_data = {
template = local.template
template_manifest = var.template_manifest
template_name = join("/", [var.vsphere_folder, var.vm_name])
ssh_username = local.ssh_username
datacenter = var.vsphere_datacenter
}
}
}