test(packer): make image builder simplier.

This commit is contained in:
Alexandre Pujol 2025-01-24 23:32:24 +01:00
parent 5b9c1a8fea
commit aae36aa4e0
No known key found for this signature in database
GPG key ID: C5469996F0DF68EC
15 changed files with 65 additions and 103 deletions

View file

@ -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}"

View file

@ -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"
}

View file

@ -3,7 +3,7 @@
# Copyright (C) 2023-2024 Alexandre Pujol <alexandre@pujol.io>
# 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

View file

@ -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}"

View file

@ -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 "$@"

View file

@ -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}"

View file

@ -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}"

View file

@ -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