From 4c3f2038be5602d487224033b1c0057ebbbd9994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Molinero=20Fern=C3=A1ndez?= Date: Tue, 29 Sep 2020 23:02:32 +0200 Subject: [PATCH] Added Makefile --- README.md | 2 +- packer/.gitignore | 2 +- packer/Makefile | 55 +++++++++++++++++++++++++++++++++ packer/build.pkr.hcl | 4 ++- packer/qemu/http/seed/user-data | 2 +- packer/qemu/start-vm.sh | 24 +++++++------- packer/sources.pkr.hcl | 2 +- 7 files changed, 75 insertions(+), 16 deletions(-) create mode 100644 packer/Makefile diff --git a/README.md b/README.md index f8c787c..f63f081 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ 2. Build the server image with Packer. ```sh cd ./packer/ - packer build ./ + packer build -only=hcloud.main ./ ``` 3. Copy `./terraform/terraform.tfvars.sample` file to `./terraform/terraform.tfvars` and fill it diff --git a/packer/.gitignore b/packer/.gitignore index 6de0335..456b978 100644 --- a/packer/.gitignore +++ b/packer/.gitignore @@ -1,5 +1,5 @@ packer_cache/ -qemu/dist/ +dist/ packer.auto.pkrvars.hcl diff --git a/packer/Makefile b/packer/Makefile new file mode 100644 index 0000000..4823516 --- /dev/null +++ b/packer/Makefile @@ -0,0 +1,55 @@ +#!/usr/bin/make -f + +SHELL := /bin/sh +.SHELLFLAGS := -eu -c + +PACKER := $(shell command -v packer 2>/dev/null) + +PACKER_WORK_DIR := ./ +PACKER_CACHE_DIR := ./packer_cache/ +PACKER_HCLOUD_OUT := ./dist/hcloud/wireguard.log +PACKER_DIGITALOCEAN_OUT := ./dist/digitalocean/wireguard.log +PACKER_QEMU_OUT := ./dist/qemu/wireguard.qcow2 + +################################################## +## "all" target +################################################## + +.PHONY: all +all: build + +################################################## +## "build" target +################################################## + +.PHONY: build +build: build-hcloud build-digitalocean build-qemu + +.PHONY: build-hcloud +build-hcloud: $(PACKER_HCLOUD_OUT) + +$(PACKER_HCLOUD_OUT): + mkdir -p '$(dir $(PACKER_HCLOUD_OUT))' + '$(PACKER)' build -force -only=hcloud.main '$(PACKER_WORK_DIR)' 2>&1 | tee '$(PACKER_HCLOUD_OUT)' + +.PHONY: build-digitalocean +build-hcloud: $(PACKER_DIGITALOCEAN_OUT) + +$(PACKER_DIGITALOCEAN_OUT): + mkdir -p '$(dir $(PACKER_DIGITALOCEAN_OUT))' + '$(PACKER)' build -force -only=digitalocean.main '$(PACKER_WORK_DIR)' 2>&1 | tee '$(PACKER_DIGITALOCEAN_OUT)' + +.PHONY: build-qemu +build-qemu: $(PACKER_QEMU_OUT) + +$(PACKER_QEMU_OUT): + mkdir -p '$(dir $(PACKER_QEMU_OUT))' + '$(PACKER)' build -force -only=qemu.main '$(PACKER_WORK_DIR)' + +################################################## +## "clean" target +################################################## + +.PHONY: clean +clean: + rm -rf '$(PACKER_HCLOUD_OUT)' '$(PACKER_DIGITALOCEAN_OUT)' '$(PACKER_QEMU_OUT)' '$(PACKER_CACHE_DIR)' diff --git a/packer/build.pkr.hcl b/packer/build.pkr.hcl index 64662a3..1758410 100644 --- a/packer/build.pkr.hcl +++ b/packer/build.pkr.hcl @@ -1,6 +1,8 @@ build { sources = [ - "source.hcloud.main" + "source.hcloud.main", + "source.digitalocean.main", + "source.qemu.main" ] provisioner "file" { diff --git a/packer/qemu/http/seed/user-data b/packer/qemu/http/seed/user-data index 0513619..38b9a4e 100644 --- a/packer/qemu/http/seed/user-data +++ b/packer/qemu/http/seed/user-data @@ -5,5 +5,5 @@ disable_root: false chpasswd: { list: ["root:toor"], expire: false } runcmd: - - printf '%s\n' 'PermitRootLogin yes' >> /etc/ssh/sshd_config + - sed -ni '/^PermitRootLogin\s/!p;$aPermitRootLogin yes' /etc/ssh/sshd_config - systemctl restart ssh.service diff --git a/packer/qemu/start-vm.sh b/packer/qemu/start-vm.sh index 2215b43..c9d318e 100755 --- a/packer/qemu/start-vm.sh +++ b/packer/qemu/start-vm.sh @@ -3,26 +3,28 @@ set -eu export LC_ALL=C -SRC_DIR=$(dirname "$(readlink -f "$0")") +SRC_DIR=$(dirname "$(dirname "$(readlink -f "$0")")") TMP_DIR=$(mktemp -d) -trap 'rm -rf "${TMP_DIR:?}"' EXIT -CLOUDIMG_DISK=${SRC_DIR:?}/dist/wireguard.qcow2 -SNAPSHOT_DISK=${TMP_DIR:?}/cloudinit-snapshot.qcow2 -USERDATA_DISK=${TMP_DIR:?}/cloudinit-seed.img -USERDATA_YAML=${TMP_DIR:?}/user-data +ORIGINAL_DISK=${SRC_DIR:?}/dist/qemu/wireguard.qcow2 +SNAPSHOT_DISK=${TMP_DIR:?}/snapshot.qcow2 -# Create a snapshot image to preserve the original cloud-image -qemu-img create -b "${CLOUDIMG_DISK:?}" -f qcow2 "${SNAPSHOT_DISK:?}" +USERDATA_DISK=${TMP_DIR:?}/seed.img +USERDATA_YAML=${SRC_DIR:?}/qemu/http/seed/user-data + +# Remove temporary files on exit +trap 'rm -rf "${TMP_DIR:?}"; trap - EXIT; exit 0' EXIT TERM INT HUP + +# Create a snapshot image to preserve the original image +qemu-img create -b "${ORIGINAL_DISK:?}" -f qcow2 "${SNAPSHOT_DISK:?}" qemu-img resize "${SNAPSHOT_DISK:?}" +2G # Create a seed image with metadata using cloud-localds -printf '%s\n' '#cloud-config' 'runcmd: ["ssh-import-id gh:hectorm"]' > "${USERDATA_YAML:?}" cloud-localds "${USERDATA_DISK:?}" "${USERDATA_YAML:?}" # Remove keys from the known_hosts file -ssh-keygen -R '[127.0.0.1]:1122' -ssh-keygen -R '[localhost]:1122' +ssh-keygen -R '[127.0.0.1]:1122' 2>/dev/null +ssh-keygen -R '[localhost]:1122' 2>/dev/null # hostfwd helper hostfwd() { printf ',hostfwd=%s::%s-:%s' "$@"; } diff --git a/packer/sources.pkr.hcl b/packer/sources.pkr.hcl index 261333f..82d7d75 100644 --- a/packer/sources.pkr.hcl +++ b/packer/sources.pkr.hcl @@ -45,7 +45,7 @@ source "qemu" "main" { vm_name = "wireguard.qcow2" http_directory = "./qemu/http/" - output_directory = "./qemu/dist/" + output_directory = "./dist/qemu/" accelerator = "kvm" cpus = 1