From aae36aa4e02700e5108b1fbddfc9f9327d03dc7b Mon Sep 17 00:00:00 2001 From: Alexandre Pujol Date: Fri, 24 Jan 2025 23:32:24 +0100 Subject: [PATCH] test(packer): make image builder simplier. --- tests/Makefile | 5 +- tests/cloud-init/debian-gnome.user-data.yml | 4 +- tests/cloud-init/debian-kde.user-data.yml | 2 +- tests/cloud-init/opensuse-gnome.user-data.yml | 8 +-- tests/cloud-init/opensuse-kde.user-data.yml | 8 +-- .../cloud-init/ubuntu22-desktop.user-data.yml | 11 ++-- .../cloud-init/ubuntu24-desktop.user-data.yml | 11 ++-- tests/packer/archlinux.pkr.hcl | 2 +- tests/packer/builds.pkr.hcl | 55 +++++++------------ tests/packer/clean.sh | 31 ++++------- tests/packer/debian.pkr.hcl | 4 +- tests/packer/init.sh | 11 ++-- tests/packer/opensuse.pkr.hcl | 2 +- tests/packer/ubuntu.pkr.hcl | 8 +-- tests/packer/variables.pkr.hcl | 6 -- 15 files changed, 65 insertions(+), 103 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 8bf5f618..3453ecee 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -12,14 +12,13 @@ flavor ?= disk ?= 10G -VERSION := 0.$(shell git rev-list --count HEAD) -BASE = archlinux debian ubuntu opensuse fedora +BASE = archlinux debian ubuntu22 ubuntu24 opensuse fedora .PHONY: ${BASE} lint $(BASE): @make --directory=../ package dist=${@} - @packer build -force -var version=${VERSION} \ + @packer build -force \ -var disk_size=${disk} -var flavor="${flavor}" \ -only=qemu.${@} packer/ diff --git a/tests/cloud-init/debian-gnome.user-data.yml b/tests/cloud-init/debian-gnome.user-data.yml index 5c95dc23..1c48eb2e 100644 --- a/tests/cloud-init/debian-gnome.user-data.yml +++ b/tests/cloud-init/debian-gnome.user-data.yml @@ -24,10 +24,10 @@ packages: - devscripts - htop - qemu-guest-agent - - spice-vdagent - rsync - - vim + - spice-vdagent - task-gnome-desktop + - vim runcmd: - apt-get update -y diff --git a/tests/cloud-init/debian-kde.user-data.yml b/tests/cloud-init/debian-kde.user-data.yml index c81ced65..e644414f 100644 --- a/tests/cloud-init/debian-kde.user-data.yml +++ b/tests/cloud-init/debian-kde.user-data.yml @@ -24,8 +24,8 @@ packages: - devscripts - htop - qemu-guest-agent - - spice-vdagent - rsync + - spice-vdagent - vim - task-kde-desktop diff --git a/tests/cloud-init/opensuse-gnome.user-data.yml b/tests/cloud-init/opensuse-gnome.user-data.yml index 66966bd6..5e5b197b 100644 --- a/tests/cloud-init/opensuse-gnome.user-data.yml +++ b/tests/cloud-init/opensuse-gnome.user-data.yml @@ -20,19 +20,15 @@ packages: - bash-completion - distribution-release - git + - go - golang-packaging - htop - make - rpmbuild + - rsync - vim write_files: - # Set some bash aliases - - path: /home/${username}/.bashrc - append: true - content: | - [[ -f ~/.bash_aliases ]] && source ~/.bash_aliases - # Setup shared directory - path: /etc/fstab append: true diff --git a/tests/cloud-init/opensuse-kde.user-data.yml b/tests/cloud-init/opensuse-kde.user-data.yml index 66966bd6..5e5b197b 100644 --- a/tests/cloud-init/opensuse-kde.user-data.yml +++ b/tests/cloud-init/opensuse-kde.user-data.yml @@ -20,19 +20,15 @@ packages: - bash-completion - distribution-release - git + - go - golang-packaging - htop - make - rpmbuild + - rsync - vim write_files: - # Set some bash aliases - - path: /home/${username}/.bashrc - append: true - content: | - [[ -f ~/.bash_aliases ]] && source ~/.bash_aliases - # Setup shared directory - path: /etc/fstab append: true diff --git a/tests/cloud-init/ubuntu22-desktop.user-data.yml b/tests/cloud-init/ubuntu22-desktop.user-data.yml index 4c6450a6..75dc6349 100644 --- a/tests/cloud-init/ubuntu22-desktop.user-data.yml +++ b/tests/cloud-init/ubuntu22-desktop.user-data.yml @@ -18,18 +18,19 @@ package_update: true package_upgrade: true package_reboot_if_required: false packages: - - ubuntu-desktop - - linux-generic-hwe-22.04 - - qemu-guest-agent - - spice-vdagent - - terminator - apparmor-profiles - build-essential - config-package-dev - debhelper - devscripts - golang-go + - linux-generic-hwe-22.04 + - qemu-guest-agent - rsync + - spice-vdagent + - terminator + - ubuntu-desktop + - vim snap: commands: diff --git a/tests/cloud-init/ubuntu24-desktop.user-data.yml b/tests/cloud-init/ubuntu24-desktop.user-data.yml index 4fa22941..9f722536 100644 --- a/tests/cloud-init/ubuntu24-desktop.user-data.yml +++ b/tests/cloud-init/ubuntu24-desktop.user-data.yml @@ -20,18 +20,19 @@ package_update: true package_upgrade: true package_reboot_if_required: false packages: - - ubuntu-desktop - - linux-generic-hwe-24.04 - - qemu-guest-agent - - spice-vdagent - - terminator - apparmor-profiles - build-essential - config-package-dev - debhelper - devscripts - golang-go + - linux-generic-hwe-24.04 + - qemu-guest-agent - rsync + - spice-vdagent + - terminator + - ubuntu-desktop + - vim snap: commands: diff --git a/tests/packer/archlinux.pkr.hcl b/tests/packer/archlinux.pkr.hcl index 41a2627d..88a5a1cb 100644 --- a/tests/packer/archlinux.pkr.hcl +++ b/tests/packer/archlinux.pkr.hcl @@ -27,7 +27,7 @@ source "qemu" "archlinux" { cd_label = "cidata" cd_content = { "meta-data" = "" - "user-data" = templatefile("${path.cwd}/packer/init/${source.name}-${var.flavor}.user-data.yml", + "user-data" = templatefile("${path.cwd}/cloud-init/${source.name}-${var.flavor}.user-data.yml", { username = "${var.username}" password = "${var.password}" diff --git a/tests/packer/builds.pkr.hcl b/tests/packer/builds.pkr.hcl index 1c16a6b8..7071c398 100644 --- a/tests/packer/builds.pkr.hcl +++ b/tests/packer/builds.pkr.hcl @@ -12,53 +12,38 @@ build { "source.qemu.ubuntu24", ] - # Upload local files + # Upload artifacts provisioner "file" { - destination = "/tmp" - sources = ["${path.cwd}/packer/src"] - } - - provisioner "file" { - only = ["qemu.archlinux"] - destination = "/tmp/src/" + destination = "/tmp/" sources = [ - "${path.cwd}/../.pkg/apparmor.d-${var.version}-1-x86_64.pkg.tar.zst", + "${path.cwd}/packer/src/", + "${path.cwd}/packer/init.sh", + "${path.cwd}/packer/clean.sh", + "${path.cwd}/../.pkg/", ] } - provisioner "file" { - only = ["qemu.opensuse"] - destination = "/tmp/src/" - sources = ["${path.cwd}/../.pkg/apparmor.d-${var.version}-1.x86_64.rpm"] - } - - provisioner "file" { - only = ["qemu.debian", "qemu.ubuntu22", "qemu.ubuntu24"] - destination = "/tmp/src/" - sources = ["${path.cwd}/../.pkg/apparmor.d_${var.version}-1_amd64.deb"] - } - - # Wait for cloud-init to finish + # Full system provisioning provisioner "shell" { execute_command = "echo '${var.password}' | sudo -S sh -c '{{ .Vars }} {{ .Path }}'" inline = [ + # Wait for cloud-init to finish "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 + + # Ensure cloud-init is successful + "cloud-init status", + + # Remove logs and artifacts so cloud-init can re-run + "cloud-init clean", + + # Install local files and config + "bash /tmp/init.sh", + + # Minimize the image + "bash /tmp/clean.sh", ] } - # Install local files and config - provisioner "shell" { - script = "${path.cwd}/packer/init/init.sh" - execute_command = "echo '${var.password}' | sudo -S sh -c '{{ .Vars }} {{ .Path }}'" - } - - # Minimize the image - provisioner "shell" { - script = "${path.cwd}/packer/init/clean.sh" - execute_command = "echo '${var.password}' | sudo -S sh -c '{{ .Vars }} {{ .Path }}'" - } - post-processor "vagrant" { output = "${var.base_dir}/packer_${var.prefix}${source.name}-${var.flavor}.box" } diff --git a/tests/packer/clean.sh b/tests/packer/clean.sh index 2e1e7b55..8459421a 100644 --- a/tests/packer/clean.sh +++ b/tests/packer/clean.sh @@ -3,7 +3,7 @@ # Copyright (C) 2023-2024 Alexandre Pujol # SPDX-License-Identifier: GPL-2.0-only -set -u +set -eu -o pipefail # shellcheck source=/dev/null _lsb_release() { @@ -46,23 +46,15 @@ _sshdgenkeys() { _EOF } -clean_debian() { - _msg "Apt clean configuration" - - _msg "Full system upgrade" - apt-get update -y - apt-get -qq -y --no-install-recommends upgrade - apt-get -qq -y --no-install-recommends dist-upgrade - - _msg "Clean the apt cache" +clean_apt() { + _msg "Cleaning the apt cache" apt-get -y autoremove --purge apt-get -y autoclean apt-get -y clean } -clean_arch() { - _msg "Pacman clean configuration" - +clean_pacman() { + _msg "Cleaning pacman cache" pacman -Syu --noconfirm pacman -Qdtq | while IFS='' read -r pkg; do pacman -Rsccn --noconfirm "$pkg" @@ -70,16 +62,15 @@ clean_arch() { pacman -Scc --noconfirm } -clean_opensuse() { - _msg "zypper clean configuration" - +clean_zypper() { + _msg "Cleaning zypper cache" zypper update -y zypper clean -y } # Make the image as impersonal as possible. impersonalize() { - _msg "Make the image as impersonal as possible." + _msg "Making the image as impersonal as possible." # Remove remaining pkg file, docs and caches dirs=( @@ -159,16 +150,16 @@ main() { begin=$(_diskused) case "$DISTRIBUTION" in debian | ubuntu) - clean_debian + clean_apt _sshdgenkeys ;; opensuse*) - clean_opensuse + clean_zypper ;; arch) - clean_arch + clean_pacman ;; esac impersonalize diff --git a/tests/packer/debian.pkr.hcl b/tests/packer/debian.pkr.hcl index 7fd176b6..d45ed3d3 100644 --- a/tests/packer/debian.pkr.hcl +++ b/tests/packer/debian.pkr.hcl @@ -6,7 +6,7 @@ source "qemu" "debian" { disk_image = true iso_url = "https://cdimage.debian.org/images/cloud/${var.release.debian.codename}/latest/debian-${var.release.debian.version}-genericcloud-amd64.qcow2" iso_checksum = "file:https://cdimage.debian.org/images/cloud/${var.release.debian.codename}/latest/SHA512SUMS" - iso_target_path = "${var.iso_dir}/debian-cloudimg-amd64.img" + iso_target_path = "${var.iso_dir}/debian-${var.release.debian.codename}-cloudimg-amd64.img" cpu_model = "host" cpus = 6 memory = 4096 @@ -28,7 +28,7 @@ source "qemu" "debian" { cd_label = "cidata" cd_content = { "meta-data" = "" - "user-data" = templatefile("${path.cwd}/packer/init/${source.name}-${var.flavor}.user-data.yml", + "user-data" = templatefile("${path.cwd}/cloud-init/${source.name}-${var.flavor}.user-data.yml", { username = "${var.username}" password = "${var.password}" diff --git a/tests/packer/init.sh b/tests/packer/init.sh index df300c0c..be952966 100644 --- a/tests/packer/init.sh +++ b/tests/packer/init.sh @@ -11,7 +11,7 @@ _lsb_release() { echo "$ID" } DISTRIBUTION="$(_lsb_release)" -readonly SRC=/tmp/src +readonly SRC=/tmp/ readonly DISTRIBUTION main() { @@ -28,23 +28,22 @@ main() { case "$DISTRIBUTION" in arch) pacman --noconfirm -U $SRC/*.pkg.tar.zst - systemctl start apparmor.service ;; debian | ubuntu) - apt-get update -y - apt-get install -y apparmor-profiles build-essential config-package-dev \ - debhelper devscripts htop rsync vim dpkg -i $SRC/*.deb ;; opensuse*) mv "/home/$SUDO_USER/.bash_aliases" "/home/$SUDO_USER/.alias" - zypper install -y bash-completion git go htop make rsync vim rpm -i $SRC/*.rpm ;; esac + + rm -rf /var/cache/apparmor/* + rm -rf /etc/apparmor/earlypolicy/ + systemctl reload apparmor.service } main "$@" diff --git a/tests/packer/opensuse.pkr.hcl b/tests/packer/opensuse.pkr.hcl index 49ba09f7..29649d4b 100644 --- a/tests/packer/opensuse.pkr.hcl +++ b/tests/packer/opensuse.pkr.hcl @@ -30,7 +30,7 @@ source "qemu" "opensuse" { cd_label = "cidata" cd_content = { "meta-data" = "" - "user-data" = templatefile("${path.cwd}/packer/init/${source.name}-${var.flavor}.user-data.yml", + "user-data" = templatefile("${path.cwd}/cloud-init/${source.name}-${var.flavor}.user-data.yml", { username = "${var.username}" password = "${var.password}" diff --git a/tests/packer/ubuntu.pkr.hcl b/tests/packer/ubuntu.pkr.hcl index 052b460d..f6981806 100644 --- a/tests/packer/ubuntu.pkr.hcl +++ b/tests/packer/ubuntu.pkr.hcl @@ -6,7 +6,7 @@ source "qemu" "ubuntu22" { disk_image = true iso_url = "https://cloud-images.ubuntu.com/${var.release.ubuntu22.codename}/current/${var.release.ubuntu22.codename}-server-cloudimg-amd64.img" iso_checksum = "file:https://cloud-images.ubuntu.com/${var.release.ubuntu22.codename}/current/SHA256SUMS" - iso_target_path = "${var.iso_dir}/ubuntu22-cloudimg-amd64.img" + iso_target_path = "${var.iso_dir}/ubuntu-${var.release.ubuntu22.codename}-cloudimg-amd64.img" cpu_model = "host" cpus = 6 memory = 4096 @@ -28,7 +28,7 @@ source "qemu" "ubuntu22" { cd_label = "cidata" cd_content = { "meta-data" = "" - "user-data" = templatefile("${path.cwd}/packer/init/${source.name}-${var.flavor}.user-data.yml", + "user-data" = templatefile("${path.cwd}/cloud-init/${source.name}-${var.flavor}.user-data.yml", { username = "${var.username}" password = "${var.password}" @@ -43,7 +43,7 @@ source "qemu" "ubuntu24" { disk_image = true iso_url = "https://cloud-images.ubuntu.com/${var.release.ubuntu24.codename}/current/${var.release.ubuntu24.codename}-server-cloudimg-amd64.img" iso_checksum = "file:https://cloud-images.ubuntu.com/${var.release.ubuntu24.codename}/current/SHA256SUMS" - iso_target_path = "${var.iso_dir}/ubuntu24-cloudimg-amd64.img" + iso_target_path = "${var.iso_dir}/ubuntu-${var.release.ubuntu24.codename}-cloudimg-amd64.img" cpu_model = "host" cpus = 6 memory = 4096 @@ -65,7 +65,7 @@ source "qemu" "ubuntu24" { cd_label = "cidata" cd_content = { "meta-data" = "" - "user-data" = templatefile("${path.cwd}/packer/init/${source.name}-${var.flavor}.user-data.yml", + "user-data" = templatefile("${path.cwd}/cloud-init/${source.name}-${var.flavor}.user-data.yml", { username = "${var.username}" password = "${var.password}" diff --git a/tests/packer/variables.pkr.hcl b/tests/packer/variables.pkr.hcl index a37c89bf..82251f25 100644 --- a/tests/packer/variables.pkr.hcl +++ b/tests/packer/variables.pkr.hcl @@ -58,12 +58,6 @@ variable "prefix" { default = "aa-" } -variable "version" { - description = "apparmor.d version" - type = string - default = "0.001" -} - variable "flavor" { description = "Distribution flavor to use (server, desktop, gnome, kde...)" type = string