-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathmakefile
210 lines (173 loc) · 6.22 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
# Makefile for shipping the container image and setting up
# permissions in Manta. Building with the docker-compose file
# directly works just fine without this.
MAKEFLAGS += --warn-undefined-variables
.DEFAULT_GOAL := build
.PHONY: *
# we get these from CI environment if available, otherwise from git
GIT_COMMIT ?= $(shell git rev-parse --short HEAD)
GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
namespace ?= autopilotpattern
tag := branch-$(shell basename $(GIT_BRANCH))
image := $(namespace)/mysql
testImage := $(namespace)/mysql-testrunner
## Display this help message
help:
@awk '/^##.*$$/,/[a-zA-Z_-]+:/' $(MAKEFILE_LIST) | awk '!(NR%2){print $$0p}{p=$$0}' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' | sort
# ------------------------------------------------
# Target environment configuration
# if you pass `TRACE=1` into the call to `make` then the Python tests will
# run under the `trace` module (provides detailed call logging)
ifndef TRACE
python := python
else
python := python -m trace
endif
# ------------------------------------------------
# Container builds
## Builds the application container image locally
build: build/tester
docker build -t=$(image):$(tag) .
## Build the test running container
build/tester:
docker build -f tests/Dockerfile -t=$(testImage):$(tag) .
## Push the current application container images to the Docker Hub
push:
docker push $(image):$(tag)
docker push $(testImage):$(tag)
## Tag the current images as 'latest' and push them to the Docker Hub
ship:
docker tag $(image):$(tag) $(image):latest
docker tag $(testImage):$(tag) $(testImage):latest
docker tag $(image):$(tag) $(image):latest
docker push $(image):$(tag)
docker push $(image):latest
# ------------------------------------------------
# Run the example stack
## Run the stack under local Compose
run/compose:
cd examples/compose && TAG=$(tag) docker-compose -p my up -d
cd examples/compose && TAG=$(tag) docker-compose -p my logs -f mysql
## Scale up the local Compose stack
run/compose/scale:
cd examples/compose && TAG=$(tag) docker-compose -p my scale mysql=2
# ------------------------------------------------
# Test running
## Pull the container images from the Docker Hub
pull:
docker pull $(image):$(tag)
## Run all tests
test: test/unit test/triton # test/compose
## Run the unit tests inside the mysql container
test/unit:
docker run --rm -w /usr/local/bin \
-e LOG_LEVEL=DEBUG \
$(image):$(tag) \
$(python) test.py
## Run the unit tests with source mounted to the container for local dev
test/unit-src:
docker run --rm -w /usr/local/bin \
-v $(shell pwd)/bin/manager:/usr/local/bin/manager \
-v $(shell pwd)/bin/manage.py:/usr/local/bin/manage.py \
-v $(shell pwd)/bin/test.py:/usr/local/bin/test.py \
-e LOG_LEVEL=DEBUG \
$(image):$(tag) \
$(python) test.py
## Run the integration test runner against Compose locally.
test/compose:
docker run --rm \
-e TAG=$(tag) \
-e GIT_BRANCH=$(GIT_BRANCH) \
-e WORK_DIR=/src \
--network=bridge \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(shell pwd)/tests/compose.sh:/src/compose.sh \
-w /src \
$(testImage):$(tag) /src/compose.sh
test/shell:
docker run --rm -it \
-e TAG=$(tag) \
-e GIT_BRANCH=$(GIT_BRANCH) \
-e WORK_DIR=/src \
--network=bridge \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(shell pwd)/tests/compose.sh:/src/compose.sh \
-w /src \
$(testImage):$(tag) /bin/bash
## Run the integration test runner. Runs locally but targets Triton.
test/triton:
$(call check_var, TRITON_PROFILE, MANTA_USER, MANTA_KEY_ID \
required to run integration tests on Triton.)
docker run --rm \
-e TAG=$(tag) \
-e TRITON_PROFILE=$(TRITON_PROFILE) \
-e MANTA_USER=$(MANTA_USER) \
-e MANTA_KEY_ID=$(MANTA_KEY_ID) \
-e GIT_BRANCH=$(GIT_BRANCH) \
-v ~/.ssh:/root/.ssh:ro \
-v ~/.triton/profiles.d:/root/.triton/profiles.d:ro \
-w /src \
$(testImage):$(tag) /src/triton.sh
# -------------------------------------------------------
## Tear down all project containers
teardown:
docker-compose -p my stop
docker-compose -p my rm -f
## Dump logs for each container to local disk
logs:
docker logs my_consul_1 > consul1.log 2>&1
docker logs my_mysql_1 > mysql1.log 2>&1
docker logs my_mysql_2 > mysql2.log 2>&1
docker logs my_mysql_3 > mysql3.log 2>&1
# -------------------------------------------------------
MANTA_URL ?= https://us-east.manta.joyent.com
MANTA_USER ?= triton_mysql
MANTA_SUBUSER ?= triton_mysql
MANTA_LOGIN ?= triton_mysql
MANTA_ROLE ?= triton_mysql
MANTA_POLICY ?= triton_mysql
## Create user and policies for Manta backups
manta:
# you need to have your SDC_ACCOUNT set
# usage:
# make manta [email protected] PASSWORD=strongpassword
$(call check_var, EMAIL PASSWORD SDC_ACCOUNT, \
Required to create a Manta login.)
ssh-keygen -t rsa -b 4096 -C "${EMAIL}" -f manta
sdc-user create --login=${MANTA_LOGIN} --password=${PASSWORD} --email=${EMAIL}
sdc-user upload-key $(ssh-keygen -E md5 -lf ./manta | awk -F' ' '{gsub("MD5:","");{print $2}}') --name=${MANTA_LOGIN}-key ${MANTA_LOGIN} ./manta.pub
sdc-policy create --name=${MANTA_POLICY} \
--rules='CAN getobject' \
--rules='CAN putobject' \
--rules='CAN putmetadata' \
--rules='CAN putsnaplink' \
--rules='CAN getdirectory' \
--rules='CAN putdirectory'
sdc-role create --name=${MANTA_ROLE} \
--policies=${MANTA_POLICY} \
--members=${MANTA_LOGIN}
mmkdir ${SDC_ACCOUNT}/stor/${MANTA_LOGIN}
mchmod -- +triton_mysql /${SDC_ACCOUNT}/stor/${MANTA_LOGIN}
## Cleans out Manta backups
cleanup:
$(call check_var, SDC_ACCOUNT, Required to cleanup Manta.)
-mrm -r /${SDC_ACCOUNT}/stor/triton-mysql/
mmkdir /${SDC_ACCOUNT}/stor/triton-mysql
mchmod -- +triton_mysql /${SDC_ACCOUNT}/stor/triton-mysql
# -------------------------------------------------------
# helper functions for testing if variables are defined
## Cleanup local backups and log debris
clean:
rm -rf tmp/
find . -name '*.log' -delete
## Print environment for build debugging
debug:
@echo GIT_COMMIT=$(GIT_COMMIT)
@echo GIT_BRANCH=$(GIT_BRANCH)
@echo namespace=$(namespace)
@echo tag=$(tag)
@echo image=$(image)
@echo testImage=$(testImage)
check_var = $(foreach 1,$1,$(__check_var))
__check_var = $(if $(value $1),,\
$(error Missing $1 $(if $(value 2),$(strip $2))))