From 4a2726395fb08a9df0e0f3497f578df19a7e4b44 Mon Sep 17 00:00:00 2001 From: Fatih Acar Date: Tue, 27 Feb 2024 13:56:16 +0100 Subject: [PATCH] feat(packaging): add packer build Signed-off-by: Fatih Acar --- packaging/packer/ansible-galaxy.sh | 9 ++ packaging/packer/ansible-playbook.sh | 9 ++ packaging/packer/http/meta-data | 0 packaging/packer/http/user-data | 29 +++++++ packaging/packer/plugins.pkr.hcl | 12 +++ packaging/packer/ubuntu.pkr.hcl | 123 +++++++++++++++++++++++++++ 6 files changed, 182 insertions(+) create mode 100755 packaging/packer/ansible-galaxy.sh create mode 100755 packaging/packer/ansible-playbook.sh create mode 100644 packaging/packer/http/meta-data create mode 100644 packaging/packer/http/user-data create mode 100644 packaging/packer/plugins.pkr.hcl create mode 100644 packaging/packer/ubuntu.pkr.hcl diff --git a/packaging/packer/ansible-galaxy.sh b/packaging/packer/ansible-galaxy.sh new file mode 100755 index 0000000000..4c0128a7f9 --- /dev/null +++ b/packaging/packer/ansible-galaxy.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +cd ansible + +if ! poetry run ansible-galaxy --version >/dev/null; then + poetry install --no-root +fi + +poetry run ansible-galaxy "$@" diff --git a/packaging/packer/ansible-playbook.sh b/packaging/packer/ansible-playbook.sh new file mode 100755 index 0000000000..f09d4be732 --- /dev/null +++ b/packaging/packer/ansible-playbook.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +cd ansible + +if ! poetry run ansible-playbook --version >/dev/null; then + poetry install --no-root +fi + +poetry run ansible-playbook "$@" diff --git a/packaging/packer/http/meta-data b/packaging/packer/http/meta-data new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packaging/packer/http/user-data b/packaging/packer/http/user-data new file mode 100644 index 0000000000..b2fe0c7057 --- /dev/null +++ b/packaging/packer/http/user-data @@ -0,0 +1,29 @@ +#cloud-config +autoinstall: + version: 1 + locale: en_US + keyboard: + layout: us + ssh: + install-server: true + allow-pw: true + packages: + - qemu-guest-agent + late-commands: + - | + if [ -d /sys/firmware/efi ]; then + apt-get install -y efibootmgr + efibootmgr -o $(efibootmgr | perl -n -e '/Boot(.+)\* ubuntu/ && print $1') + fi + user-data: + preserve_hostname: false + hostname: infrahub + package_upgrade: true + timezone: UTC + users: + - name: admin + passwd: $6$xyz$74AlwKA3Z5n2L6ujMzm/zQXHCluA4SRc2mBfO2/O5uUc2yM2n2tnbBMi/IVRLJuKwfjrLZjAT7agVfiK7arSy/ + groups: [adm, cdrom, dip, plugdev, lxd, sudo] + lock-passwd: false + sudo: ALL=(ALL) NOPASSWD:ALL + shell: /bin/bash diff --git a/packaging/packer/plugins.pkr.hcl b/packaging/packer/plugins.pkr.hcl new file mode 100644 index 0000000000..191c01745f --- /dev/null +++ b/packaging/packer/plugins.pkr.hcl @@ -0,0 +1,12 @@ +packer { + required_plugins { + qemu = { + version = "~> 1" + source = "github.com/hashicorp/qemu" + } + ansible = { + version = "~> 1" + source = "github.com/hashicorp/ansible" + } + } +} diff --git a/packaging/packer/ubuntu.pkr.hcl b/packaging/packer/ubuntu.pkr.hcl new file mode 100644 index 0000000000..17015e6733 --- /dev/null +++ b/packaging/packer/ubuntu.pkr.hcl @@ -0,0 +1,123 @@ +locals { + infrahub_version = var.infrahub_version == "" ? "0.11" : var.infrahub_version + vm_template_name = var.vm_template_name == "" ? "infrahub-${local.infrahub_version}-ubuntu-22.04.qcow2" : var.vm_template_name +} + +variable "infrahub_version" { + type = string + default = "" +} + +variable "vm_template_name" { + type = string + default = "" +} + +source "qemu" "custom_image" { + # Boot Commands when Loading the ISO file with OVMF.fd file (Tianocore) / GrubV2 + boot_command = [ + "", + "e", + "", + " autoinstall ds=nocloud-net\\;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/", + "" + ] + boot_wait = "5s" + + output_directory = "output" + http_directory = "http" + iso_url = "https://releases.ubuntu.com/jammy/ubuntu-22.04.4-live-server-amd64.iso" + iso_checksum = "file:https://releases.ubuntu.com/jammy/SHA256SUMS" + memory = 4096 + + ssh_password = "packerubuntu" + ssh_username = "admin" + ssh_timeout = "20m" + + headless = true + accelerator = "kvm" + format = "qcow2" + disk_compression = true + + disk_image = false + disk_size = "20G" + + cpu_model = "host" + sockets = 1 + cpus = 8 + cores = 8 + threads = 1 + + qemu_img_args { + convert = ["-W"] # should speedup compression according to: https://gitlab.com/qemu-project/qemu/-/issues/80#note_1669835297 + } + + vm_name = local.vm_template_name +} + +build { + sources = ["source.qemu.custom_image"] + provisioner "shell" { + inline = ["while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for Cloud-Init...'; sleep 1; done"] + } + + provisioner "ansible" { + user = build.User + playbook_file = "${path.cwd}/ansible/bootstrap_infra.yml" + command = "./ansible-playbook.sh" + ansible_env_vars = [ + "ANSIBLE_HOST_KEY_CHECKING=False" + ] + + extra_arguments = [ + "-e", "infrahub_dir=/opt/infrahub", + "-e", "expose_database_ports=false", + "-e", "expose_message_queue_ports=false", + "-e", "NEO4J_PASSWORD=admin", + "-e", "RABBITMQ_PASSWORD=admin", + "-e", "INFRAHUB_CONTAINER_REGISTRY=9r2s1098.c1.gra9.container-registry.ovh.net", + "-e", "INFRAHUB_VERSION=${local.infrahub_version}", + "-e", "INFRAHUB_PRODUCTION=false", + "-e", "INFRAHUB_SECURITY_INITIAL_ADMIN_TOKEN=1b93a1e6-b14a-4c5b-b16e-e154d6ed05f4", + "-e", "INFRAHUB_SECURITY_SECRET_KEY=1b93a1e6-b14a-4c5b-b16e-e154d6ed05f4", + ] + + + galaxy_command = "./ansible-galaxy.sh" + galaxy_file = "${path.cwd}/ansible/requirements.yml" + } + + provisioner "ansible" { + user = build.User + playbook_file = "${path.cwd}/ansible/bootstrap_monitoring_stack.yml" + command = "./ansible-playbook.sh" + ansible_env_vars = [ + "ANSIBLE_HOST_KEY_CHECKING=False" + ] + + extra_arguments = [ + "-e", "node_exporter_web_listen_address=127.0.0.1:9100", + "-e", "install_vector=true", + "-e", "monitor_infrahub=true", + "-e", "GRAFANA_ROOT_URL=''", + ] + + + galaxy_command = "./ansible-galaxy.sh" + galaxy_file = "${path.cwd}/ansible/requirements.yml" + } + + provisioner "shell" { + inline = [ + "sudo sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT=\".*\"/GRUB_CMDLINE_LINUX_DEFAULT=\"\"/' /etc/default/grub", + "sudo update-grub2", + "sudo passwd -d ${build.User}", + "sudo apt-get -y autoremove --purge", + "sudo apt-get -y clean", + "sudo apt-get -y autoclean", + "sudo cloud-init clean -l -s -c all", + "sudo sync", + ] + } +} +