-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
118 lines (93 loc) · 3.14 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
-include config/config.mak
build_only ?=
platform_ve ?= proxmox
ifeq ($(filter --jobs,$(MAKEFLAGS)),)
ifeq ($(filter -J,$(MAKEFLAGS)),)
ifeq ($(filter clean,$(MAKECMDGOALS)),)
MAKEFLAGS += --jobs=$(shell nproc || sysctl -n hw.ncpu || echo 1)
endif
endif
endif
GIT_DIR := $(shell git rev-parse --git-dir)
ifeq ($(ansible_groups),)
$(error ansible_groups is not defined)
endif
.PHONY: $(sort $(ansible_groups))
post_build_targets = $(foreach group,$(sort $(ansible_groups)),phony/$(group)/post_build)
.PHONY: $(post_build_targets)
.PHONY: all
.DEFAULT_GOAL: all
all: $(sort $(ansible_groups))
sources = main.pkr.hcl sources.pkr.hcl variables.pkr.hcl \
config/ansible/requirements.yaml plugin.pkr.hcl \
$(wildcard *.pkrvars.hcl)
# allow build aliases for ansible groups and optionally skipping post-build tasks
$(ansible_groups): %: output/%/packer-kvm.img \
$(if $(build_only),,phony/%/post_build)
/tmp/packer/:
mkdir -p /tmp/packer/
.SECONDARY: output/%/packer-kvm.img
output/%/packer-kvm.img: $(sources) config/ansible/%.yaml \
.venv/lock plugin.pkr.hcl.lock /tmp/packer/id_% /tmp/packer/id_%.pub | output/
. .venv/bin/activate && \
packer build --only "$*.qemu.kvm" \
--force \
--var "target=$*" . && \
packer build --only "$*.null.post" \
--var "target=$*" .
rm -f "/tmp/packer/id_$*" "/tmp/packer/id_$*.pub"
.INTERMEDIATE: /tmp/packer/id_%.pub
/tmp/packer/id_% /tmp/packer/id_%.pub: | /tmp/packer/
chmod -R 700 /tmp/packer/
echo y | ssh-keygen -q -N "" -C "" -f "$@" >/dev/null 2>&1
.venv/lock: requirements.txt config/requirements.txt
python3 -m venv .venv/
. .venv/bin/activate && \
python3 -m pip install -U -r requirements.txt -r config/requirements.txt
touch .venv/lock
plugin.pkr.hcl.lock: plugin.pkr.hcl
packer init .
touch "$@"
output/:
mkdir output/
# packer will error if the file doesn't exist, so create a placeholder
define vehost_placeholder
# virtual environment host SSH connection
# (placeholder, replace with your actual values)
source "null" "vehost" {
ssh_host = "127.0.0.1"
ssh_username = "root"
ssh_private_key_file = "/dev/null"
}
endef
vehost.pkr.hcl:
@ printf "%s\n" "Making placeholder file: vehost.pkr.hcl" \
"HINT: replace with your actual values"
$(file >$@,$(vehost_placeholder))
# ambiguous state of vehost, so remote operations should always be out of date
$(post_build_targets): phony/%/post_build: output/%/packer-kvm.img
. .venv/bin/activate && \
packer build --only "veupload.null.*" \
--var "target=$*" . && \
packer build --only "$(platform_ve).null.*" \
--var "target=$*" --parallel-builds=1 .
.PHONY: clean
clean: | /tmp/packer/
find /tmp/packer/ output/ -mindepth 1 -delete
(. .venv/bin/activate && pre-commit uninstall) || true
rm -rf .venv/
.PHONY: install
install: $(GIT_DIR)/hooks/pre-commit
.PHONY: lint
lint:
packer fmt --recursive --check --diff --write=false .
.PHONY: validate
validate: plugin.pkr.hcl.lock
packer validate --no-warn-undeclared-var --syntax-only .
.PHONY: format
format:
packer fmt --recursive --write=true .
$(GIT_DIR)/hooks/pre-commit: .pre-commit-config.yaml .venv/lock
. .venv/bin/activate && \
pre-commit install && \
touch $(GIT_DIR)/hooks/pre-commit