summaryrefslogtreecommitdiffstats
path: root/Makefile
blob: d1f8426fee6e67aa9bf089d889382bd78062db93 (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
# 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             ?= ansible-roles
HASHER                  ?= sha256sum
SHELL                   := /usr/bin/env bash


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

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

BUILDER_VMWARE_EXE      := vmware
BUILDER_VMWARE_NAME     := vmware-iso
BUILDER_VMWARE_PROV     := vmware-vmx

BUILDER_VIRTUALBOX_EXE  := virtualbox
BUILDER_VIRTUALBOX_NAME := virtualbox-iso
BUILDER_VIRTUALBOX_PROV := virtualbox-ovf

BUILDERS                := QEMU VMWARE VIRTUALBOX

ifndef BUILDER
    $(foreach cur, $(BUILDERS),\
        $(if $(shell command -v $(BUILDER_$(cur)_EXE)),\
            $(eval AVAILABLE_BUILDERS += $(BUILDER_$(cur)_NAME))))

    BUILDER := $(firstword $(AVAILABLE_BUILDERS))
endif

ifndef PROVISIONER
    $(foreach cur, $(BUILDERS),\
        $(if $(filter $(BUILDER),$(BUILDER_$(cur)_NAME)),\
            $(eval PROVISIONER := $(BUILDER_$(cur)_PROV))))
endif


# The packer templates, detected as *.json (excluding base.json)
TEMPLATES := $(basename $(filter-out base.json,$(wildcard *.json)))

# The provisioning flavors, detected as ansible-roles/setup-<flavor>.yml
FLAVORS := $(patsubst $(ANSIBLE_DIR)/setup-%.yml,%, $(wildcard $(ANSIBLE_DIR)/setup-*.yml))

BASETARGETS := $(foreach template, $(TEMPLATES), $(template)/base)
PROVTARGETS := $(foreach template, $(TEMPLATES), $(foreach flavor, $(FLAVORS), $(template)/$(flavor)))
BOOTTARGETS := $(foreach template, $(TEMPLATES), $(template)/base/boot)
BOOTTARGETS += $(foreach prov, $(PROVTARGETS), $(prov)/boot)


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


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


# 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")


# The ROOTPW is only needed for the base, boot and provisioning targets.
# In every other case it must not be checked as it should be possible to call
# help or cleanup without the need of defining the password.
PW_NEEDED := $(filter $(strip $(PROVTARGETS) $(BOOTTARGETS)),$(MAKECMDGOALS))
ifneq ($(PW_NEEDED),)
    ifeq ($(strip $(ROOTPW)),)
        $(error No root password is set, set it as ROOTPW in your environment.)
    else
        $(foreach cur,$(PW_NEEDED),\
            $(eval cur_dir := $(firstword $(subst /, ,$(cur)))/base)\
            $(if $(shell test -d "$(cur_dir)" && echo yes),\
                $(shell echo "$(ROOTPW)" | $(HASHER) --check --status "$(cur_dir)/rootpw.$(HASHER)")\
                $(if $(filter 1,$(.SHELLSTATUS)),\
                    $(error The wrong ROOTPW is set. Please correct it))))
    endif
endif


ifdef VERBOSE
    $(info root password:        $(ROOTPW))
    $(info hasher:               $(HASHER))
    $(info )
    $(info timestamp:            $(TIMESTAMP))
    $(info )
    $(info packer: executable:   $(PACKER))
    $(info packer: options:      $(PACKER_OPTS))
    $(info )
    $(info ansible: directory:   $(ANSIBLE_DIR))
    $(info )
    $(info builder: available:   $(AVAILABLE_BUILDERS))
    $(info builder: chosen:      $(BUILDER))
    $(info )
    $(info targets: base:        $(strip $(BASETARGETS)))
    $(info targets: boot:        $(strip $(BOOTTARGETS)))
    $(info targets: provision:   $(strip $(PROVTARGETS)))
    $(info )
endif


.PHONY: help clean_except_last clean_bases clean_all clean_failed
help:


# Creating base images
$(BASETARGETS):
	$(info ** Building template '$(@D)' using '$(BUILDER)' **)
	$(PACKER) build -only=$(BUILDER) \
	    $(PACKER_OPTS) \
	    -var='vm_name=rootfs-image' \
	    -var='output_directory=$(@D)/base' \
	    $(@D).json
	@echo "$(ROOTPW)" | $(HASHER) > $(@D)/base/rootpw.$(HASHER)


# Provisioning images
$(PROVTARGETS):
$(foreach flav, $(FLAVORS), %/$(flav)): %/base
	$(eval BUILD_DIR := $(@D)/$(@F).$(TIMESTAMP))
	$(info ** Provisioning '$(@D)' with '$(@F)' **)
	@mkdir -p $(BUILD_DIR)
	@cp -r $(ANSIBLE_DIR) $(BUILD_DIR)/$(ANSIBLE_DIR)
	@ln -sfn $(@F).$(TIMESTAMP) $(@D)/$(@F).latest
	$(PACKER) build -only=$(PROVISIONER) \
	    $(PACKER_OPTS) \
	    -var='vm_name=rootfs-image' \
	    -var='output_directory=$(BUILD_DIR)/build' \
	    -var='base_image=$(@D)/base/rootfs-image' \
	    -var='playbook=setup-$(@F).yml' \
	    $(BUILD_DIR)/$(ANSIBLE_DIR)/run-playbook-only.json


# Generating boot files
$(BOOTTARGETS):
%/boot: %
	$(eval BUILD_DIR := $(if $(filter base,$(notdir $(@D))),\
	                       $(@D),\
	                       $(@D).$(TIMESTAMP)/build))
	$(eval ANSIBLE_DIR_CUR := $(if $(filter base,$(notdir $(@D))),\
	                               $(ANSIBLE_DIR),\
	                               $(@D).$(TIMESTAMP)/$(ANSIBLE_DIR)))
	$(info ** Generating boot files for '$(BUILD_DIR)')
	$(PACKER) build -only=$(PROVISIONER) \
	    $(PACKER_OPTS) \
	    -var='vm_name=rootfs-image.tmp' \
	    -var='output_directory=$(BUILD_DIR)/tmp' \
	    -var='base_image=$(BUILD_DIR)/rootfs-image' \
	    -var='playbook=build-dracut-initramfs.yml' \
	    $(ANSIBLE_DIR_CUR)/run-playbook-only.json
	@mv $(ANSIBLE_DIR_CUR)/boot_files $(BUILD_DIR)/boot
	$(if $(DEBUG),,@rm -rf $(BUILD_DIR)/tmp)


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"
	@echo
	@echo "Base images targets:"
	@for T in $(BASETARGETS); do printf "\t%s\n" "$$T"; done
	@echo
	@echo "Provisioning images targets: "
	@for P in $(PROVTARGETS); do printf "\t%s\n" "$$P"; done
	@echo
	@echo "Generate boot files targets:"
	@printf "\t<{base,provisioning}_target>/boot\n"
	@echo
	@echo "Available options are:"
	@printf "\tANSIBLE_DIR: Set directory with ansible roles   (def: ansible-roles)\n"
	@printf "\tBUILDER:     Set an ISO builder, do not autodetect\n"
	@printf "\tPROVISIONER: Set a provisioning builder, do not autodetect\n"
	@printf "\tDEBUG:       Enable debug mode in packer        (includes VERBOSE)\n"
	@printf "\t  DEBUG=     Enable enhanced on-error handling\n"
	@printf "\t  DEBUG=STEP Enable step by step debugging in packer\n"
	@printf "\tHASHER:      Set wanted hasher                  (def: sha256sum)\n"
	@printf "\tPACKER:      Set packer executable              (def: packer)\n"
	@printf "\tPACKER_OPTS: Set packer options\n"
	@printf "\tROOTPW:      Set root password for output image\n"
	@printf "\tVERBOSE:     Enable verbose output\n"
	@printf "\tWINDOW:      Disable headless mode\n"
	@echo
	@echo "Clean targets are:"
	@printf "\tclean_except_last\n"
	@printf "\tclean_failed\n"
	@printf "\tclean_bases\n"
	@printf "\tclean_all\n"