Added Makefile

This commit is contained in:
Héctor Molinero Fernández 2020-09-29 23:02:32 +02:00
parent 02218ab176
commit 4c3f2038be
7 changed files with 75 additions and 16 deletions

View File

@ -12,7 +12,7 @@
2. Build the server image with Packer. 2. Build the server image with Packer.
```sh ```sh
cd ./packer/ cd ./packer/
packer build ./ packer build -only=hcloud.main ./
``` ```
3. Copy `./terraform/terraform.tfvars.sample` file to `./terraform/terraform.tfvars` and fill it 3. Copy `./terraform/terraform.tfvars.sample` file to `./terraform/terraform.tfvars` and fill it

2
packer/.gitignore vendored
View File

@ -1,5 +1,5 @@
packer_cache/ packer_cache/
qemu/dist/ dist/
packer.auto.pkrvars.hcl packer.auto.pkrvars.hcl

55
packer/Makefile Normal file
View File

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

View File

@ -1,6 +1,8 @@
build { build {
sources = [ sources = [
"source.hcloud.main" "source.hcloud.main",
"source.digitalocean.main",
"source.qemu.main"
] ]
provisioner "file" { provisioner "file" {

View File

@ -5,5 +5,5 @@ disable_root: false
chpasswd: { list: ["root:toor"], expire: false } chpasswd: { list: ["root:toor"], expire: false }
runcmd: 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 - systemctl restart ssh.service

View File

@ -3,26 +3,28 @@
set -eu set -eu
export LC_ALL=C export LC_ALL=C
SRC_DIR=$(dirname "$(readlink -f "$0")") SRC_DIR=$(dirname "$(dirname "$(readlink -f "$0")")")
TMP_DIR=$(mktemp -d) TMP_DIR=$(mktemp -d)
trap 'rm -rf "${TMP_DIR:?}"' EXIT
CLOUDIMG_DISK=${SRC_DIR:?}/dist/wireguard.qcow2 ORIGINAL_DISK=${SRC_DIR:?}/dist/qemu/wireguard.qcow2
SNAPSHOT_DISK=${TMP_DIR:?}/cloudinit-snapshot.qcow2 SNAPSHOT_DISK=${TMP_DIR:?}/snapshot.qcow2
USERDATA_DISK=${TMP_DIR:?}/cloudinit-seed.img
USERDATA_YAML=${TMP_DIR:?}/user-data
# Create a snapshot image to preserve the original cloud-image USERDATA_DISK=${TMP_DIR:?}/seed.img
qemu-img create -b "${CLOUDIMG_DISK:?}" -f qcow2 "${SNAPSHOT_DISK:?}" 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 qemu-img resize "${SNAPSHOT_DISK:?}" +2G
# Create a seed image with metadata using cloud-localds # 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:?}" cloud-localds "${USERDATA_DISK:?}" "${USERDATA_YAML:?}"
# Remove keys from the known_hosts file # Remove keys from the known_hosts file
ssh-keygen -R '[127.0.0.1]:1122' ssh-keygen -R '[127.0.0.1]:1122' 2>/dev/null
ssh-keygen -R '[localhost]:1122' ssh-keygen -R '[localhost]:1122' 2>/dev/null
# hostfwd helper # hostfwd helper
hostfwd() { printf ',hostfwd=%s::%s-:%s' "$@"; } hostfwd() { printf ',hostfwd=%s::%s-:%s' "$@"; }

View File

@ -45,7 +45,7 @@ source "qemu" "main" {
vm_name = "wireguard.qcow2" vm_name = "wireguard.qcow2"
http_directory = "./qemu/http/" http_directory = "./qemu/http/"
output_directory = "./qemu/dist/" output_directory = "./dist/qemu/"
accelerator = "kvm" accelerator = "kvm"
cpus = 1 cpus = 1