summaryrefslogtreecommitdiffstats
path: root/Makefile
blob: 4e5f7d3b628d7c0bac75684323259c5481ee6b1d (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
# 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
# the "provisioning" flavor, expects a 'setup-<flavor>.yml' playbook
# in the 'ansible-roles' submodule! This will likely change...
FLAVORS := $(patsubst $(ANSIBLE_DIR)/setup-%.yml,%, $(wildcard $(ANSIBLE_DIR)/setup-*.yml))
SUPPORTED_BUILDERS = qemu virtualbox-iso vmware-iso
# check which hypervisors are available
ifeq ($(shell which qemu-system-$(shell uname -m | sed 's/i686/i386/') 2>&1 > /dev/null && echo $$?), 0)
    AVAILABLE_BUILDERS += qemu
    BUILDER := qemu
endif
ifndef BUILDER
    ifeq ($(shell which virtualbox 2>&1 > /dev/null && echo $$?), 0)
        AVAILABLE_BUILDERS += virtualbox-iso
        BUILDER := virtualbox-iso
    endif
endif
ifndef BUILDER
    ifeq ($(shell which vmplayer 2>&1 > /dev/null && echo $$?), 0)
        AVAILABLE_BUILDERS += vmware-iso
        BUILDER := vmware-iso
    endif
endif
ifndef BUILDER
    $(error No usable builder found! Please install a hypervisor...)
endif
MISSING_BUILDERS = $(filter-out $(AVAILABLE_BUILDERS),$(SUPPORTED_BUILDERS))
ifneq ($(words $(MISSING_BUILDERS)), 0)
    $(info Builders missing on this system: $(MISSING_BUILDERS))
    $(info If you wish to use these, install the corresponding hypervisor and retry.)
    $(info )
endif

TEMPLATES := $(basename $(filter-out base.json,$(wildcard *.json)))

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)

PACKER_OPTS := -var-file=base.json
ifdef DEBUG
    PACKER_OPTS += -debug
    PACKER_OPTS += -var='headless=false'
endif

.PHONY: all help clean
all: help

##
#       Creating base images
##
$(BASETARGETS):
	ifndef ROOTPW
		$(error ROOTPW is not set)
	endif
	$(info ** Building template '$(@D)' using '$(BUILDER)' **)
	$(PACKER) build -only=$(BUILDER) \
		$(PACKER_OPTS) \
		-var='vm_name=$(@D)' \
		$(@D).json
	@test -f output-$(@D)-$(BUILDER)/$(@D) || false
	@-test -d $(@D)/base && rm -rf $(@D)/base
	@-mkdir $(@D)
	@mv output-$(@D)-$(BUILDER) $(@D)/base
	@mv $(@D)/base/$(@D) $(@D)/base/image
	@echo "** Success **"

##
#       Provisioning images
##
# This should still only use base images
$(PROVTARGETS):
$(foreach flav, $(FLAVORS), %/$(flav)): %/base
	$(info ** Provisioning '$(@D)' with '$(@F)' **)
	$(PACKER) build -only=$(BUILDER) \
		$(PACKER_OPTS) \
		-var='vm_name=$(@F)' \
		-var='image_dir=$(@D)' \
		-var='image_name=base/image' \
		-var='playbook=setup-$(@F).yml' \
		$(ANSIBLE_DIR)/run-playbook-only.json
	@test -f output-$(@D)/$(@F) || false
	@-test -d $(@D)/$(@F) && rm -rf $(@D)/$(@F)
	@-mkdir $(@D)/$(@F)
	@mv output-$(@D)/$(@F) $(@D)/$(@F)/image
	@rmdir output-$(@D)
	@echo "** Success **"

##
#		Generating boot files
##
# This should use provisioned image
$(BOOTTARGETS):
%/boot: %
	$(info ** Generating boot files for '$(patsubst %/,%,$(dir $(@D))):$(notdir $(@D))' **)
	$(PACKER) build -only=$(BUILDER) \
		$(PACKER_OPTS) \
		-var='vm_name=$(notdir $(@D)).tmp' \
		-var='image_dir=$(patsubst %/,%,$(dir $(@D)))/$(notdir $(@D))' \
		-var='image_name=image' \
		-var='playbook=build-dracut-initramfs.yml' \
		$(ANSIBLE_DIR)/run-playbook-only.json
	@test -f $(ANSIBLE_DIR)/boot_files/initramfs || false
	@-test -d $(patsubst %/,%,$(dir $(@D)))/$(notdir $(@D))/boot && \
		rm -rf $(patsubst %/,%,$(dir $(@D)))/$(notdir $(@D))/boot
	@mv $(ANSIBLE_DIR)/boot_files $(patsubst %/,%,$(dir $(@D)))/$(notdir $(@D))/boot
	ifndef DEBUG
		@rm -rf output-$(patsubst %/,%,$(dir $(@D)))/
	endif
	@echo "** Success **"

help:
	@echo "General syntax: <template>/<flavor>[/boot]"
	@echo
	@echo "Detected builders:"
	@(for B in $(AVAILABLE_BUILDERS); do printf "\t%s\n" "$$B"; done)
	@echo
	@echo "Base images:"
	@(for T in $(BASETARGETS); do printf "\t%s\n" "$$T"; done)
	@echo
	@echo "Provisioning: "
	@(for P in $(PROVTARGETS); do printf "\t%s\n" "$$P"; done)
	@echo
	@echo "Generate boot files:"
	@(for N in $(BOOTTARGETS); do printf "\t%s\n" "$$N"; done)
	@echo

# The builds are directories named after the template name
clean:
	-$(foreach build_dir,$(TEMPLATES),test -d $(build_dir) && rm -rf $(build_dir);)