test: improve vm generation.

This commit is contained in:
Alexandre Pujol 2023-04-24 12:35:18 +01:00
parent d224aa4e6a
commit 38d310c62a
Failed to generate hash of commit
6 changed files with 54 additions and 26 deletions

View file

@ -20,6 +20,12 @@ boxes:
ram: '6144' ram: '6144'
cpu: '6' cpu: '6'
- name: debian-server
box: aa-debian-server
uefi: true
ram: '6144'
cpu: '6'
- name: opensuse-kde - name: opensuse-kde
box: aa-opensuse-kde box: aa-opensuse-kde
uefi: true uefi: true

View file

@ -20,9 +20,9 @@ source "qemu" "archlinux-gnome" {
disk_detect_zeroes = "unmap" disk_detect_zeroes = "unmap"
disk_discard = "unmap" disk_discard = "unmap"
output_directory = "${var.output}" output_directory = "${var.output}"
vm_name = "${var.prefix}-${source.name}.qcow2" vm_name = "${var.prefix}${source.name}.qcow2"
boot_wait = "10s" boot_wait = "10s"
shutdown_command = "echo ${var.password} | sudo shutdown -hP now" shutdown_command = "echo ${var.password} | sudo -S shutdown -hP now"
cd_label = "cidata" cd_label = "cidata"
cd_content = { cd_content = {
"meta-data" = "" "meta-data" = ""
@ -31,7 +31,7 @@ source "qemu" "archlinux-gnome" {
username = "${var.username}" username = "${var.username}"
password = "${var.password}" password = "${var.password}"
ssh_key = file("${var.ssh_publickey}") ssh_key = file("${var.ssh_publickey}")
hostname = "${var.prefix}-${source.name}" hostname = "${var.prefix}${source.name}"
} }
) )
} }
@ -55,9 +55,9 @@ source "qemu" "archlinux-kde" {
disk_detect_zeroes = "unmap" disk_detect_zeroes = "unmap"
disk_discard = "unmap" disk_discard = "unmap"
output_directory = "${var.output}" output_directory = "${var.output}"
vm_name = "${var.prefix}-${source.name}.qcow2" vm_name = "${var.prefix}${source.name}.qcow2"
boot_wait = "10s" boot_wait = "10s"
shutdown_command = "echo ${var.password} | sudo shutdown -hP now" shutdown_command = "echo ${var.password} | sudo -S shutdown -hP now"
cd_label = "cidata" cd_label = "cidata"
cd_content = { cd_content = {
"meta-data" = "" "meta-data" = ""
@ -66,7 +66,7 @@ source "qemu" "archlinux-kde" {
username = "${var.username}" username = "${var.username}"
password = "${var.password}" password = "${var.password}"
ssh_key = file("${var.ssh_publickey}") ssh_key = file("${var.ssh_publickey}")
hostname = "${var.prefix}-${source.name}" hostname = "${var.prefix}${source.name}"
} }
) )
} }

View file

@ -32,22 +32,26 @@ build {
inline = [ inline = [
"while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for Cloud-Init...'; sleep 20; done", "while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for Cloud-Init...'; sleep 20; done",
"cloud-init clean", # Remove logs and artifacts so cloud-init can re-run "cloud-init clean", # Remove logs and artifacts so cloud-init can re-run
"sh /tmp/src/init.sh"
] ]
} }
provisioner "shell" {
script = "${path.cwd}/packer/init/init.sh"
execute_command = "echo '${var.password}' | sudo -S sh -c '{{ .Vars }} {{ .Path }}'"
}
provisioner "shell" { provisioner "shell" {
script = "${path.cwd}/packer/init/clean.sh" script = "${path.cwd}/packer/init/clean.sh"
execute_command = "echo '${var.password}' | sudo -S sh -c '{{ .Vars }} {{ .Path }}'" execute_command = "echo '${var.password}' | sudo -S sh -c '{{ .Vars }} {{ .Path }}'"
} }
post-processor "vagrant" { post-processor "vagrant" {
output = "${var.iso_dir}/packer_${var.prefix}-${source.name}.box" output = "${var.base_dir}/packer_${var.prefix}${source.name}.box"
} }
post-processor "shell-local" { post-processor "shell-local" {
inline = [ inline = [
"vagrant box add --force --name ${var.prefix}-${source.name} ${var.iso_dir}/packer_${var.prefix}-${source.name}.box" "vagrant box add --force --name ${var.prefix}${source.name} ${var.base_dir}/packer_${var.prefix}${source.name}.box"
] ]
} }

View file

@ -4,9 +4,9 @@
source "qemu" "ubuntu-server" { source "qemu" "ubuntu-server" {
disk_image = true disk_image = true
iso_url = "https://cloud-images.ubuntu.com/${var.release.ubuntu}/current/${var.release.ubuntu}-server-cloudimg-amd64.img" iso_url = "https://cloud-images.ubuntu.com/${var.release.ubuntu.codename}/current/${var.release.ubuntu.codename}-server-cloudimg-amd64.img"
iso_checksum = "file:https://cloud-images.ubuntu.com/${var.release.ubuntu}/current/SHA256SUMS" iso_checksum = "file:https://cloud-images.ubuntu.com/${var.release.ubuntu.codename}/current/SHA256SUMS"
iso_target_path = "${var.iso_dir}/${source.name}-cloudimg-amd64.img" iso_target_path = "${var.iso_dir}/ubuntu-cloudimg-amd64.img"
cpus = 4 cpus = 4
memory = 2048 memory = 2048
disk_size = "${var.disk_size}" disk_size = "${var.disk_size}"
@ -20,7 +20,7 @@ source "qemu" "ubuntu-server" {
disk_detect_zeroes = "unmap" disk_detect_zeroes = "unmap"
disk_discard = "unmap" disk_discard = "unmap"
output_directory = "${var.output}/" output_directory = "${var.output}/"
vm_name = "${var.prefix}-${source.name}.qcow2" vm_name = "${var.prefix}${source.name}.qcow2"
boot_wait = "10s" boot_wait = "10s"
firmware = "/usr/share/edk2-ovmf/x64/OVMF_CODE.fd" firmware = "/usr/share/edk2-ovmf/x64/OVMF_CODE.fd"
shutdown_command = "echo ${var.password} | sudo -S /sbin/shutdown -hP now" shutdown_command = "echo ${var.password} | sudo -S /sbin/shutdown -hP now"
@ -32,7 +32,7 @@ source "qemu" "ubuntu-server" {
username = "${var.username}" username = "${var.username}"
password = "${var.password}" password = "${var.password}"
ssh_key = file("${var.ssh_publickey}") ssh_key = file("${var.ssh_publickey}")
hostname = "${var.prefix}-${source.name}" hostname = "${var.prefix}${source.name}"
} }
) )
} }

View file

@ -40,6 +40,12 @@ variable "iso_dir" {
default = "/var/lib/libvirt/images" default = "/var/lib/libvirt/images"
} }
variable "base_dir" {
description = "Final packer image output directory"
type = string
default = "/var/lib/libvirt/images"
}
variable "output" { variable "output" {
description = "Output build directory" description = "Output build directory"
type = string type = string
@ -49,7 +55,7 @@ variable "output" {
variable "prefix" { variable "prefix" {
description = "Image name prefix" description = "Image name prefix"
type = string type = string
default = "aa" default = "aa-"
} }
variable "version" { variable "version" {
@ -58,18 +64,30 @@ variable "version" {
default = "0.001" default = "0.001"
} }
variable "release" {
description = "Distribution release to use"
type = map(string)
default = {
"ubuntu" : "jammy", # 22.04 LTS
"debian" : "bullseye", # 11
"opensuse" : "9",
}
}
variable "flavor" { variable "flavor" {
description = "Distribution flavor to use (-desktop, -gnome, -kde...)" description = "Distribution flavor to use (server, desktop, gnome, kde...)"
type = string type = string
default = "" default = ""
} }
variable "release" {
description = "Distribution metadata to use"
type = map(object({
codename = string
version = string
}))
default = {
"ubuntu" : {
codename = "jammy",
version = "22.04.2",
},
"debian" : {
codename = "bullseye",
version = "11",
}
"opensuse" : {
codename = "tumbleweed",
version = "",
}
}
}