-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
102 lines (78 loc) · 3.12 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
########################################################################
# To create comma separated lists
########################################################################
null :=
space := $(null) #
comma := ,
########################################################################
# User defined functions
########################################################################
should_become = $(filter $(1),$(ROLE_BECOME_LIST))
become = $(or $(call should_become,$@), $(call should_become,$(r)))
find_vars = $(foreach file, $(wildcard $(1)/*.yml),--extra-vars @$(file))
remove_if = $(if $(1),$(filter-out $(1),$(2)),)
remove_action = $(call remove_if,$(1),$(ACTION_TAGS))
remove_role = $(call remove_if,$(1),$(ROLE_TAGS))
########################################################################
# Binaries and options
########################################################################
SYS_PYTHON := /usr/bin/python3
SYS_PIP := /usr/bin/pip3
VENV := $(CURDIR)/.ansible
VENV_BIN := $(VENV)/bin
VENV_BINS = $(AG) $(AP) $(VENV_PIP)
VENV_PIP := $(VENV_BIN)/pip
VENV_PY_DEPS := ansible
AG := $(VENV_BIN)/ansible-galaxy
AG_FLAGS := install -r requirements.yml
AP := $(VENV_BIN)/ansible-playbook
AP_VARS := $(call find_vars,variables)
AP_OVERRIDES := $(call find_vars,overrides)
AP_BECOME = $(if $(become),--ask-become-pass,)
AP_FLAGS = $(strip $(AP_BECOME) $(strip $(AP_VARS) $(AP_OVERRIDES)))
ROLE_BECOME_LIST := all $(subst local.,,$(shell grep -lR 'become:' roles/*/tasks | cut -d / -f 2))
ROLE_TAGS := $(filter-out tags:,$(shell grep 'tags:' playbook.yml))
ACTION_TAGS := install config update
ACTION_SKIP_TAGS = $(strip $(call remove_action,$@) $(call remove_role,$(r)))
AP_SKIP_TAGS = --skip-tags $(subst $(space),$(comma),$(ACTION_SKIP_TAGS))
AP_TAGS = --tags $(subst $(space),$(comma),$(strip $(@) $(r)))
########################################################################
# Rule definitions
########################################################################
.DEFAULT_GOAL := all
## Install everything
.PHONY: all
all: galaxy | $(VENV_BINS)
$(AP) $(AP_FLAGS) playbook.yml
## Install galaxy requirements
.PHONY: galaxy
galaxy: | $(VENV_BINS)
$(AG) role $(AG_FLAGS)
$(AG) collection $(AG_FLAGS)
## make ACTION [r=ROLE]
.PHONY: $(ACTION_TAGS)
$(ACTION_TAGS): galaxy | $(VENV_BINS)
$(AP) $(AP_FLAGS) $(AP_TAGS) $(AP_SKIP_TAGS) playbook.yml
## make ROLE
.PHONY: $(ROLE_TAGS)
$(ROLE_TAGS): galaxy | $(VENV_BINS)
$(AP) $(AP_FLAGS) $(AP_TAGS) playbook.yml
## list roles
.PHONY: roles
roles:
@printf "%s\n" $(sort $(ROLE_TAGS))
## list roles
.PHONY: actions
actions:
@printf "%s\n" $(sort $(ACTION_TAGS))
$(VENV_BINS):
$(SYS_PIP) install --user virtualenv
$(SYS_PYTHON) -m virtualenv $(VENV)
$(VENV_PIP) install $(VENV_PY_DEPS)
## file=FILE Override files
.PHONY: override
override:
cp variables/$(file).yml overrides/$(file).yml
.PHONY: clean
clean:
rm -rf $(VENV)