diff options
author | Jonathan Bauer | 2017-05-18 11:17:20 +0200 |
---|---|---|
committer | Jonathan Bauer | 2017-05-18 11:17:20 +0200 |
commit | 5afbd62e39603f43b42f202d4c695a2929b53757 (patch) | |
tree | 08bdc95a2e61d8de9be71a007b540cb4fa11c39e /Makefile | |
parent | slight change in output directory name (diff) | |
download | packer-templates-5afbd62e39603f43b42f202d4c695a2929b53757.tar.gz packer-templates-5afbd62e39603f43b42f202d4c695a2929b53757.tar.xz packer-templates-5afbd62e39603f43b42f202d4c695a2929b53757.zip |
updated TODO
Diffstat (limited to 'Makefile')
-rw-r--r-- | Makefile | 151 |
1 files changed, 126 insertions, 25 deletions
@@ -1,38 +1,139 @@ -# Simple Makefile to build packer templates -# TODO: -# - allow overriding packer variables -# - check for hypervisors (and invalidate targets) -# - provisioning logic -BUILDERS = qemu vmware-iso virtualbox-iso +# Simple Makefile to build base VM images using packer and ansible +# TODO +# * support ssh per user instead of root +# * support building with non-qemu builders and convert to qemu afterwards +# * testing target +# * auto-detect flavors +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 = bwlp +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))) -TARGETS := $(foreach builder, $(BUILDERS), $(foreach template, $(TEMPLATES), $(builder)/$(template))) + +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 list clean -all: list +.PHONY: all help clean +all: help -$(TARGETS): +## +# 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 + $(info ** Success **) - $(info Building '$(@F)' with '$(@D)') - packer build -only=$(@D) \ +## +# 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)' \ - $(@F).json + -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) + $(info ** Success **) -list: - $(info Possible targets:) - @(for F in $(TARGETS); do echo -e "\t$$F" ; done) +## +# Generating boot files +## +# This should use provisioned image +$(BOOTTARGETS): +%/boot: % +# no evil eval tricks... + $(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 + $(info ** Success **) +help: + @echo "General syntax: <template>/<flavor>[/boot]" + @echo + @echo "Detected builders:" + @(for B in $(AVAILABLE_BUILDERS); do echo -e "\t$$B"; done) + @echo + @echo "Base images:" + @(for T in $(BASETARGETS); do echo -e "\t$$T"; done) + @echo + @echo "Provisioning: " + @(for P in $(PROVTARGETS); do echo -e "\t$$P"; done) + @echo + @echo "Generate boot files:" + @(for N in $(BOOTTARGETS); do echo -e "\t$$N"; done) + @echo + +# The builds are directories named after the template name clean: - $(foreach builder,$(BUILDERS),rm -rf $(builder)/;) - -# convenience do-nothing targets for auto-complete -qemu/ubuntu-16.04-amd64: -qemu/centos-7.3-x86_64: -virtualbox-iso/ubuntu-16.04-amd64: -virtualbox-iso/centos-7.3-x86_64: -vmware-iso/ubuntu-16.04-amd64: -vmware-iso/centos-7.3-x86_64: + -$(foreach build_dir,$(TEMPLATES),test -d $(build_dir) && rm -rf $(build_dir);) + |