summaryrefslogtreecommitdiffstats
path: root/Makefile
blob: b74429e4b709d134892dbd9a17012316293b61ee (plain) (blame)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Simple Makefile to build base VM images using packer and ansible
# TODO
#   * support ssh as user instead of root
#   * testing target
PACKER               ?= packer
ANSIBLE_DIR_BOOT     ?= ansible-dracut
ANSIBLE_DIR_PROV     ?= ansible-roles-prov
HASHER               ?= sha256sum
SHELL                := /usr/bin/env bash
BUILDS               := builds

ARCH                 := $(shell uname -m | sed 's/i686/i386/')

AVAILABLE_BUILDERS   := QEMU
BUILDER_QEMU_EXE     := qemu-system-$(ARCH)
BUILDER_QEMU_NAME    := qemu
BUILDER_QEMU_PROV    := $(BUILDER_QEMU_NAME)

BUILDERS             := qemu

override BUILDER     := qemu
override PROVISIONER := qemu

# The packer templates, detected as *.json (excluding base.json)
TEMPLATES :=        $(patsubst %/,%,$(dir $(wildcard */base.pkrvars.hcl)))

# The provisioning flavors, detected as ansible-<flavor>/setup-<flavor>.yml
# Find all paths beginnig with "ansible-" and and save this suffix in PATTERNS
PATTERNS :=         $(patsubst ansible-%, %, $(wildcard ansible-*))
# Extract <flavor> from ansible-<flavor>/setup-<flavor>.yml using PATTERNS as <flavor>
FLAVORS :=          $(foreach p, $(PATTERNS), \
                        $(patsubst ansible-$(p)/setup-$(p).yml, $(p), \
                            $(wildcard ansible-$(p)/setup-$(p).yml)))
# Extra flavor from variable ANSIBLE_DIR_PROV, using pattern ANSIBLE_DIR_PROV/setup-<flavor>.yml
FLAVORS +=          $(patsubst $(ANSIBLE_DIR_PROV)/setup-%.yml,%, \
                        $(wildcard $(ANSIBLE_DIR_PROV)/setup-*.yml))
FLAVORS :=          $(strip $(FLAVORS))

BASETARGETS :=      $(strip $(foreach t, $(TEMPLATES), $(t)/base))
PROVTARGETS :=      $(strip $(foreach t, $(TEMPLATES), \
                            $(foreach f, $(FLAVORS), $(t)/$(f))))
PROVIMAGES :=       $(strip $(foreach f, $(FLAVORS), \
                            $(foreach t, $(TEMPLATES), $(t)/$(f): %/$(f): %/base)))
REPROVTARGETS :=    $(strip $(foreach t, $(TEMPLATES), \
                                $(foreach f, $(FLAVORS), \
                                    $(filter-out $(t)/$(f).latest, \
                                        $(wildcard $(t)/$(f).*)))))
COMPRESSPOSSIBLE := $(foreach t, $(TEMPLATES), \
                        $(foreach f, $(FLAVORS), $(wildcard $(t)/$(f).*)))
COMPRESSTARGETS :=  $(foreach p, $(COMPRESSPOSSIBLE), $(p)/compress)
BOOTTARGETS :=      $(foreach t, $(TEMPLATES), $(t)/base/boot)
BOOTTARGETS +=      $(foreach p, $(PROVTARGETS), $(p)/boot)
BOOTTARGETS :=      $(strip $(BOOTTARGETS))
CURBOOTTARGETS :=   $(foreach p, $(REPROVTARGETS), $(p)/boot)
BASERMTARGETS :=    $(foreach t, $(BASETARGETS),  rm/$(t))
PROVRMTARGETS :=    $(foreach t, $(REPROVTARGETS),  rm/$(t))

ifndef ANSIBLE_PROV_EXTRA_ARGS
    ANSIBLE_PROV_EXTRA_ARGS :=
endif

ifdef DEBUG
    VERBOSE := 1
    ifeq ($(DEBUG),STEP)
        override PACKER_OPTS += -debug
    else
        override PACKER_OPTS += -on-error=ask
    endif
endif

ifdef FORCE
    override PACKER_OPTS += -force
endif

ifdef WINDOW
    override PACKER_OPTS += -var headless=false
endif

override PACKER_OPTS += -warn-on-undeclared-var

# We support parallel Provisioning packer builds.
# To ensure data consistency we save the used ansible roles before executing in
# its own environment, tagged by the current date & time.
TIMESTAMP := $(shell date "+%Y-%m-%d_%H-%M-%S")

ifdef VERBOSE
    $(info timestamp:            $(TIMESTAMP))
    $(info )
    $(info packer: executable:   $(PACKER))
    $(info packer: options:      $(PACKER_OPTS))
    $(info hasher:               $(HASHER))
    $(info )
    $(info ansible: boot:        $(ANSIBLE_DIR_BOOT))
    $(info ansible: flavors:     $(FLAVORS))
    $(info )
    $(info builder: available:   $(AVAILABLE_BUILDERS))
    $(info builder: chosen:      $(BUILDER))
    $(info )
    $(info targets: base:        $(BASETARGETS))
    $(info targets: boot:        $(BOOTTARGETS))
    $(info targets: provision:   $(PROVTARGETS))
    $(info targets: reprovision: $(REPROVTARGETS))
    $(info )
    override PACKER := PACKER_LOG=1 $(PACKER)
endif

.PHONY: help clean_except_last clean_bases clean_all clean_failed backinglock $(REPROVTARGETS)
help:

# Creating base images. When DRACUT_INIT is specified, the dracut module
# repository will be initially cloned and its required components (dnbd3,
# xmount, libxmount-qemu) will be pre-build to accelerate subsequent builds.
$(BASETARGETS):
	$(info ** Building template '$(@D)' using '$(BUILDER)' **)
	$(eval INIT_TAG := $(if $(DRACUT_INIT), install, untagged))
	$(PACKER) build -only=base.$(BUILDER).ansible \
		$(PACKER_OPTS) \
		-var-file=$(@D)/base.pkrvars.hcl \
		-var http_dir=$(@D)/http \
		-var output_directory=$(BUILDS)/$(@D)/base \
		-var playbook_file=$(ANSIBLE_DIR_BOOT)/slx-builder.yml \
		-var vm_name=$(@D) \
		-var extra_ansible_args=-t,$(INIT_TAG) \
		./
	$(HASHER) $(BUILDS)/$(@D)/base/$(@D) >$(BUILDS)/$(@D)/base/CHECKSUM

# Provisioning images
$(PROVIMAGES)

$(PROVTARGETS) $(REPROVTARGETS):
	$(eval FLAVOR := 			$(basename $(@F)))
	$(eval ANSIBLE_DIR_PROV :=	$(if ansible-$(FLAVOR)/setup-$(FLAVOR).yml,\
									ansible-$(FLAVOR)))
	$(eval VERSION := 			$(if $(suffix $(@)), $(subst .,,$(suffix $(@))), 0))
	$(eval BASE_IMAGE :=		$(if $(wildcard $(BUILDS)/$(@)/$(@D)-$(FLAVOR)),\
									$(BUILDS)/$(@)/$(@D)-$(FLAVOR),\
									$(BUILDS)/$(@D)/base/$(@D)))
	$(eval BUILD_DIR :=		 	$(BUILDS)/$(@D)/$(FLAVOR).$(TIMESTAMP))
	$(info ** Provisioning '$(@D)' with '$(FLAVOR)' **)
	@mkdir -p $(BUILD_DIR)
	@cp -r `readlink -f $(ANSIBLE_DIR_PROV)` $(BUILD_DIR)/$(ANSIBLE_DIR_PROV)
	@ln -sfn $(FLAVOR).$(TIMESTAMP) $(BUILDS)/$(@D)/$(FLAVOR).latest
	@ln -sfr $(BUILD_DIR) $(dir $(BASE_IMAGE))/$(TIMESTAMP).backinglock
	@-cp -r $(BUILDS)/$@/$(ANSIBLE_DIR_PROV) $(BUILD_DIR)
	@-cp -r $(BUILDS)/$@/$(ANSIBLE_DIR_PROV).* $(BUILD_DIR)
	$(PACKER) build -only=flavor.$(BUILDER).ansible \
		$(PACKER_OPTS) \
		-var disk_image=true \
		-var iso_url=$(BASE_IMAGE) \
		-var iso_checksum=file:$(dir $(BASE_IMAGE))CHECKSUM \
		-var output_directory=$(BUILD_DIR)/image \
		-var playbook_file=$(BUILD_DIR)/$(ANSIBLE_DIR_PROV)/setup-$(FLAVOR).yml \
		-var vm_name=$(@D)-$(FLAVOR) \
		-var extra_ansible_args=$(ANSIBLE_PROV_EXT		RA_ARGS) \
		./
	$(HASHER) $(BUILD_DIR)/image/$(@D)-$(FLAVOR) \
		>$(BUILD_DIR)/image/CHECKSUM

# Generating boot files
$(BOOTTARGETS): %/boot: %

$(BOOTTARGETS) $(CURBOOTTARGETS):
	$(eval FLAVOR := 	$(notdir $(@D)))
	$(eval BASE := 		$(patsubst %/, %, $(dir $(@D))))
	$(eval BASE_DIR :=	$(if $(filter base, $(notdir $(BUILDS)/$(@D))),\
							,\
							$(if $(wildcard $(BUILDS)/$(@D)/image/.),\
								$(BUILDS)/$(@D),\
								$(BUILDS)/$(@D).$(TIMESTAMP))))
	$(eval BUILD_DIR := $(if $(BASE_DIR)/image,\
							$(BASE_DIR)/image,\
							$(BUILDS)/$(@D)))
	$(eval BUILD_DIR :=	$(if $(filter $(@), $(CURBOOTTARGETS)),\
							$(BASE_DIR)/image,\
							$(BUILD_DIR)))
	$(info ** Generating boot files for '$(BUILD_DIR)')
	$(PACKER) build -only=flavor.$(BUILDER).ansible \
		$(PACKER_OPTS) \
		-var disk_image=true \
		-var iso_url=$(BUILD_DIR)/$(BASE)-$(FLAVOR) \
		-var iso_checksum=file:$(dir $(BUILD_DIR))image/CHECKSUM \
		-var output_directory=$(BUILD_DIR)/tmp \
		-var playbook_file=$(ANSIBLE_DIR_BOOT)/slx-builder.yml \
		-var vm_name=$(BASE)-$(FLAVOR).tmp \
		-var extra_ansible_args=-t,install,-t,build \
		-var extra_ansible_args=$(ANSIBLE_PROV_EXTRA_ARGS) \
		./
	@mkdir -p $(BUILD_DIR)/boot
	@mv -f $(ANSIBLE_DIR_BOOT)/boot_files/* $(BUILD_DIR)/boot
	$(if $(DEBUG),,@rm -rf $(BUILD_DIR)/tmp)

# Safe removal of images
$(BASERMTARGETS):
rm/%: %/*.backinglock
	$(eval BUILD_DIR := $(subst rm/,,$(@)))
	@rm -rf $(BUILD_DIR)

$(PROVRMTARGETS):
rm/%: %/build/*.backinglock
	$(eval BUILD_DIR := 	$(subst rm/,,$(@))/build)
	$(eval FATHER_BUILD := 	$(dir $(shell qemu-img info $(BUILD_DIR)/rootfs-image | grep "backing file" | cut -d\  -f3-)))
	$(eval BUILD_TIME := 	$(subst .,,$(suffix $(@))))
	@rm -rf $(FATHER_BUILD)/$(BUILD_TIME).backinglock
	@rm -rf $(BUILD_DIR)

%.backinglock: backinglock
	@qemu-img convert -f qcow2 $(@)/build/rootfs-image -O qcow2 $(@)/build/rootfs-image.tmp
	@rm $(@)/build/rootfs-image
	@mv $(@)/build/rootfs-image.tmp $(@)/build/rootfs-image
	@rm $(@)

backinglock:

clean_except_last:
	@-$(foreach template,$(TEMPLATES),\
		$(eval exclusions := $(shell test -d $(template) && \
										find $(template) \
											-maxdepth 1 \
											-type l \
											-print0 \
										| xargs -r -0 -n1 readlink))\
		test -d $(template) && \
			find $(template)/* \
				-maxdepth 0 \
				-type d \
				$(foreach file,$(exclusions),-not -name $(file) ) \
				-not -name base \
				-print0 \
			| xargs -r -0 -n1 rm -rf; )

clean_failed:
	@-$(foreach template,$(TEMPLATES),\
		test -d $(template) && \
			find $(template)/* \
				-maxdepth 0 \
				-type d \
				-not -name base \
				-print0 \
			| xargs -r -0 -n1 -i \
				$(SHELL) -c 'test -d "{}/build" || rm -rf "{}"'; )

clean_bases:
	@-$(foreach template,$(TEMPLATES),\
		test -d $(template) && rm -rf $(template)/base;)

clean_all:
	@-$(foreach template,$(TEMPLATES),\
		test -d $(template) && rm -rf $(template);)

help:
	@printf "Usage:\n\tmake <template>/<flavor>[/boot]\n"
	@printf "\n"
	@printf "Base images targets:\n"
	@for T in $(BASETARGETS); do printf "\t%s\n" "$$T"; done
	@printf "\n"
	@printf "Provisioning images targets:\n"
	@for P in $(PROVTARGETS); do printf "\t%s\n" "$$P"; done
	@for P in $(REPROVTARGETS); do printf "\t%s\n" "$$P"; done
	@printf "\n"
	@printf "Generate boot files targets:\n"
	@printf "\t<{base,provisioning}_target>/boot\n"
	@printf "\n"
	@printf "Commiting backing files and Compressing targets:"
	@for C in $(COMPRESSTARGETS); do printf "\t%s\n" "$$C"; done
	@printf "\n"
	@printf "For safely removing targets:\n"
	@printf "\trm/<target>\n"
	@printf "\n"
	@printf "Available options are:\n"
	@printf "\tANSIBLE_DIR_BOOT:\tSet directory with ansible roles for building initramfs (def: ansible-dracut)\n"
	@printf "\tANSIBLE_DIR_PROV:\tSet directory with ansible roles for provisioning (def: ansible-roles-prov)\n"
	@printf "\tDEBUG:\t\t\tEnable debug mode in packer (sets VERBOSE=1)\n"
	@printf "\t\tDEBUG=1\t\tEnable enhanced on-error handling\n"
	@printf "\t\tDEBUG=STEP\tEnable step by step debugging in packer\n"
	@printf "\tFORCE=1:\t\tOverwrite existing builds\n"
	@printf "\tPACKER:\t\t\tSet packer executable (def: packer)\n"
	@printf "\tPACKER_OPTS:\t\tSet packer options\n"
	@printf "\tVERBOSE=1:\t\tEnable verbose output\n"
	@printf "\tWINDOW:\t\t\tDisable headless mode\n"
	@printf "\tPKR_VAR_<VAR>=<VAL>:\tSet a packer variable <VAR>=<VAL>, e.g. PKR_VAR_ssh_password='secret'\n"
	@printf "\n"
	@printf "Clean targets are:\n"
	@printf "\tclean_except_last\n"
	@printf "\tclean_failed\n"
	@printf "\tclean_bases\n"
	@printf "\tclean_all\n"