-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
301 lines (260 loc) · 9.04 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
include .mk
ANSIBLE_CONFIG ?= ansible/ansible.cfg
AWS_BUILD_INSTANCE_TYPE ?= t2.micro
AWS_BUILD_SUBNET_ID ?=
AWS_BUILD_VPC_ID ?=
PWD = $(shell pwd)
REGION ?= eu-west-1
ROLE ?=
USERNAME ?=
VENV ?= $(PWD)/.venv
VERSION ?= $(shell git describe --tags)
VERSION_ROLE ?= $(shell (cat .artefacts/$(ROLE).yml 2>/dev/null || echo "$(ROLE): { version: no-service }") | shyaml get-value '$(ROLE).version')
# Ansible 2.2.1 introduced a bug with paths for "local" connections
export HOME
PATH := $(VENV)/bin:$(PWD)/vendor:$(PWD)/bin:$(shell printenv PATH)
SHELL := env PATH=$(PATH) /bin/bash
export ANSIBLE_CONFIG
export AWS_DEFAULT_REGION=$(REGION)
export AWS_REGION=$(REGION)
export PATH
.PHONY: .phony
## Prints this help
help:
@awk -v skip=1 \
'/^##/ { sub(/^[#[:blank:]]*/, "", $$0); doc_h=$$0; doc=""; skip=0; next } \
skip { next } \
/^#/ { doc=doc "\n" substr($$0, 2); next } \
/:/ { sub(/:.*/, "", $$0); printf "\033[34m%-30s\033[0m\033[1m%s\033[0m %s\n\n", $$0, doc_h, doc; skip=1 }' \
$(MAKEFILE_LIST)
## Initialise local project
init: .git/.local-hooks-installed $(VENV)
## Sudo for AWS Roles
# Usage: $(make aws-sudo TOKEN=123789)
aws-sudo: $(VENV)
@(printenv TOKEN > /dev/null && aws-sudo -m $(TOKEN) $(PROFILE) ) || ( \
aws-sudo $(PROFILE) \
)
## Install vendor dependencies such as Packer
vendor: vendor/packer vendor/jq
# install Packer
vendor/packer:
mkdir -p vendor
bash bin/install_local_packer 1.0.4
# install JQ
vendor/jq:
mkdir -p vendor
bash bin/install_local_jq 1.5
## Lint all the code
# Usage: make lint
lint: lint.python lint.bash lint.ansible
## Lint Python scripts
lint.python: $(VENV)
@echo "Lint Python code..."
@grep -Rn python bin \
| grep ":1:" \
| sed -E 's/([^:]*):.*/\1/' \
| xargs -I% pep8 %
## Lint Ansible roles and playbooks
lint.ansible: $(VENV)
@echo "Lint Ansible Roles..."
@find ansible/roles/service -name "*.yml" -not -path "*/files/*.yml" -print0 | \
xargs -n1 -0 -I% \
ansible-lint % \
--exclude=ansible/roles/vendor
@echo "Ansible Syntax Check..."
$(MAKE) ansible.check ROLE=base_image
$(MAKE) ansible.check ROLE=hippo_authoring
$(MAKE) ansible.check ROLE=hippo_delivery
## Lint Bash scripts
lint.bash:
@which shellcheck > /dev/null || (\
echo "please install shellcheck: https://github.com/koalaman/shellcheck#user-content-installing" \
&& exit 1 \
)
@echo "Lint Bash code..."
@grep -Rn "/bash" bin \
| grep ":1:" \
| sed -E 's/([^:]*):.*/\1/' \
| xargs -I% bash -c 'cd $$(dirname %) && shellcheck -x $(PWD)/%'
## Check syntax for given ROLE
# Usage: make ansible.check ROLE=hippo_authoring
ansible.check: $(VENV) ansible/roles/vendor
ansible-playbook --syntax-check \
--inventory ansible/inventories/localhost \
--extra-vars hosts=localhost \
--extra-vars role=$(ROLE) \
--extra-vars root_dir=$(PWD)/ansible \
--extra-vars @$(PWD)/vagrant/config.yml \
$(EXTRAS) \
ansible/playbooks/ami.yml
## Builds an AMI using an Ansible role
#
# BUILD_NAME must be specified, in GoCD this should be the
# job number, outside of GoCD a dummy handle should be used
# so as to not collide with GoCD build numbers
#
# Usage: make ami ROLE=bastion BUILD_NAME=test1
ami: .artefacts .log vendor/packer vendor/jq $(VENV) ansible/roles/vendor
packer build \
-var 'aws_instance_type=$(AWS_BUILD_INSTANCE_TYPE)' \
-var 'aws_region=$(REGION)' \
-var 'aws_subnet_id=$(AWS_BUILD_SUBNET_ID)' \
-var 'aws_vpc_id=$(AWS_BUILD_VPC_ID)' \
-var 'base_ami_id=$(shell get_base_ami_id $(ROLE))' \
-var 'build_name=$(shell get_build_name $(BUILD_NAME))' \
-var 'build_version=$(VERSION)-$(VERSION_ROLE)' \
-var 'role=$(ROLE)' \
-var 'root_dir=$(PWD)/ansible' \
"packer/ami.json"
rm -rf self.tgz
ami.artefacts:
$(MAKE) .artefacts/$(ROLE)-$(shell cat .artefacts/$(ROLE).yml | shyaml get-value $(ROLE).version).tgz ROLE=$(ROLE)
## Debug AMI build process
# Usage: make ami_debug ROLE=api_storelocator USERNAME=iam.key.name
ami.debug: .artefacts vendor/packer vendor/jq $(VENV) ansible/roles/vendor
packer build -debug \
-var 'aws_instance_type=$(AWS_BUILD_INSTANCE_TYPE)' \
-var 'aws_region=$(REGION)' \
-var 'aws_subnet_id=$(AWS_BUILD_SUBNET_ID)' \
-var 'aws_vpc_id=$(AWS_BUILD_VPC_ID)' \
-var 'base_ami_id=$(shell get_base_ami_id $(ROLE))' \
-var 'build_name=$(shell ./bin/get_build_name $(BUILD_NAME))' \
-var 'build_version=$(VERSION)-$(VERSION_ROLE)' \
-var 'disable_stop_instance=true' \
-var 'role=$(ROLE)' \
-var 'root_dir=$(PWD)/ansible' \
-var 'ssh_agent_auth=true' \
-var 'ssh_keypair_name=$(USERNAME)' \
"packer/ami.json"
## Builds and `ssh` to given machine.
# Startup and (re)provision local VM and then `ssh` to it for given ROLE.
# Usage: make vagrant ROLE=hippo_delivery
# make vagrant ROLE=hippo_delivery MODE=configure
vagrant: $(VENV) vagrant.build .phony
vagrant ssh
## Builds VM (only provision)
vagrant.build: ansible/roles/vendor
@printenv ROLE || ( \
echo "please specify ROLE, example: make vagrant ROLE=hippo_delivery" \
&& exit 1 \
)
vagrant up --no-provision
MODE="$(MODE)" vagrant provision
@echo "- - - - - - - - - -"
@echo " Build Finished"
@echo "- - - - - - - - - -"
## Watch changes and rebuild local VM
# This require `entr` (brew install entr)
# Usage: make vagrant.watch ROLE=hippo_delivery
vagrant.watch:
while sleep 1; do \
find ansible/ \
vagrant/ \
Vagrantfile \
| entr -d $(MAKE) lint vagrant.build ROLE=$(ROLE); \
done
## Executes given command on vagrant box
# Usage: make vagrant.exec ROLE=hippo_delivery COMMAND="echo 'Hello world!'"
vagrant.exec:
@printenv ROLE || ( \
echo "please specify ROLE, example: make vagrant ROLE=hippo_delivery" \
&& exit 1 \
)
vagrant up --no-provision
vagrant ssh -c "$(COMMAND)"
## Runs simple command on a given local VM.
# Usage: make vagrant.ssh ROLE=hippo_delivery
# make vagrant.status ROLE=hippo_delivery
# make vagrant.halt ROLE=hippo_delivery
# make vagrant.destroy ROLE=hippo_delivery
vagrant.%:
@printenv ROLE || ( \
echo "please specify ROLE, example: make vagrant ROLE=hippo_delivery" \
&& exit 1 \
)
MODE=$(MODE) vagrant $(subst vagrant.,,$@)
## Builds a vagrant VirtualBox ".box"
# This gives you working vagrant image box that you can share with others.
# Usage:
# make box ROLE=api_kong
box: .artefacts/ubuntu-16.04.3-server-amd64.iso $(VENV) vendor/packer ansible/roles/vendor
@mkdir -p .artefacts/boxes
packer build \
-var 'role=$(ROLE)' \
-var 'root_dir=$(PWD)/ansible' \
"packer/vagrant.json"
##
# usage: make build-deploy V=v1.1.5
build-deploy:
o=$$(head -n2 .artefacts/hippo_delivery.yml; echo " version: $(V)"); echo "$$o" > .artefacts/hippo_delivery.yml
o=$$(head -n2 .artefacts/hippo_authoring.yml; echo " version: $(V)"); echo "$$o" > .artefacts/hippo_authoring.yml
unset VERSION_ROLE && make ami.artefacts ami ROLE=hippo_authoring
unset VERSION_ROLE && make ami.artefacts ami ROLE=hippo_delivery
cp .artefacts/hippo_*.yml ../ps-deploy/.artefacts/
cd ../ps-deploy && make stack ROLE=hippo_authoring
cd ../ps-deploy && make stack ROLE=hippo_delivery
## Create new version tag based on the nearest tag
version.bumpup:
@git tag $$((git describe --abbrev=0 --tags | grep $$(cat .version) || echo $$(cat .version).-1) | perl -pe 's/^(v(\d+\.)*)(-?\d+)(.*)$$/$$1.($$3+1).$$4/e')
$(MAKE) version.print
## Prints current version
version.print:
@echo "- - -"
@echo "Current version: $(VERSION)"
@echo "- - -"
## Configure localhost
# ! DO NOT RUN LOCALLY !
# This target is designed to be used for bootstraping machine in given environment.
# Usage:
# make ansible_configure_local ROLE=bastion
ansible_configure_local:
cd ansible && ansible-playbook \
-i inventories/localhost \
-e role=$(ROLE) \
-e root_dir=/bootstrap/ansible \
-e @/bootstrap/environment.yml \
--tags=configure \
playbooks/bootstrap.yml
## Delete all downloaded and generated files
clean:
rm -rf ansible/roles/vendor
rm -rf $(VENV)
find . -name "*.retry" | xargs rm
# shortcurt
.venv: $(VENV)
# setup Virtualenv
$(VENV):
@which virtualenv > /dev/null || (\
echo "please install virtualenv: http://docs.python-guide.org/en/latest/dev/virtualenvs/" \
&& exit 1 \
)
virtualenv $(VENV)
.venv/bin/pip install -r ansible/requirements.txt --ignore-installed
# This ensures that on python < 2.7.9 we can accept SNI https connections
.venv/bin/pip install -U urllib3[secure]
virtualenv --relocatable $(VENV)
ansible/roles/vendor: $(VENV) .phony
ansible-galaxy install -p ansible/roles/vendor -r ansible/roles/vendor.yml --ignore-errors
# generates empty .mk file if not present
.mk:
touch .mk
# install hooks and local git config
.git/.local-hooks-installed:
@bash .git-local/install
.artefacts:
mkdir -p .artefacts
.artefacts/%.yml: .artefacts
bin/create_latest_artifact $@
# Downloads ubuntu xenial iso image (for vagrant box build)
.artefacts/ubuntu-16.04.3-server-amd64.iso:
curl -Lo .artefacts/ubuntu-16.04.3-server-amd64.iso \
http://releases.ubuntu.com/16.04/ubuntu-16.04.3-server-amd64.iso
# Downloads Hippo distribution tgz
.artefacts/$(ROLE)-%.tgz:
aws s3 cp \
s3://artefacts.ps.digital.nhs.uk/$(ROLE)/$(patsubst .artefacts/$(ROLE)-%.tgz,%,$@)/website.tgz \
$@
.log:
mkdir -p .log
.phony: