-
Notifications
You must be signed in to change notification settings - Fork 18
/
Makefile
234 lines (196 loc) · 6.11 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
## @author Dmitry Kolesnikov, <[email protected]>
## @copyright (c) 2012 - 2014 Dmitry Kolesnikov. All Rights Reserved
##
## @description
## Makefile to build and release Erlang applications using standard development tools
##
## @version 0.11.6
#####################################################################
##
## application config
##
#####################################################################
PREFIX ?= /usr/local
APP ?= $(notdir $(CURDIR))
ARCH ?= $(shell uname -m)
PLAT ?= $(shell uname -s)
VSN ?= $(shell test -z "`git status --porcelain`" && git describe --tags --long | sed -e 's/-g[0-9a-f]*//' | sed -e 's/-0//' || echo "`git describe --abbrev=0 --tags`-SNAPSHOT")
REL = ${APP}-${VSN}
PKG ?= ${REL}+${ARCH}.${PLAT}
TEST ?= ${APP}
S3 ?=
VMI ?= fogfish/erlang
NET ?= lo0
URL ?= registry.opensource.zalan.do/hunt
LATEST ?= latest
## rebar version (no spaces at end)
REBAR ?= 3.3.2
## root path to benchmark framework
BB = ../basho_bench
SSHENV = /tmp/ssh-agent.conf
COOKIE?= nocookie
## erlang runtime flags use by `make run`
ROOT = $(shell pwd)
ADDR = $(shell ifconfig ${NET} | sed -En 's/^${NET}:.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p' && echo "127.0.0.1")
EFLAGS = \
-name ${APP}@${ADDR} \
-setcookie ${COOKIE} \
-pa ${ROOT}/_build/default/lib/*/ebin \
-pa ${ROOT}/rel \
-kernel inet_dist_listen_min 32100 \
-kernel inet_dist_listen_max 32199 \
+P 1000000 \
+K true +A 160 -sbt ts
## self-extracting bundle wrapper
BUNDLE_INIT = PREFIX=${PREFIX}\nREL=${PREFIX}/${REL}\nAPP=${APP}\nVSN=${VSN}\nLINE=`grep -a -n "BUNDLE:$$" $$0`\nmkdir -p $${REL}\ntail -n +$$(( $${LINE%%%%:*} + 1)) $$0 | gzip -vdc - | tar -C $${REL} -xvf - > /dev/null\n
BUNDLE_FREE = exit\nBUNDLE:\n
BUILDER = FROM ${VMI}\nRUN mkdir ${APP}\nCOPY . ${APP}/\nRUN cd ${APP} && make && make rel\n
CTRUN = \
-module(test). \
-export([run/1]). \
run(Spec) -> \
{ok, Test} = file:consult(Spec), \
case lists:keyfind(node, 1, Test) of \
false -> ct:run_test([{spec, Spec}]); \
true -> ct_master:run(Spec) \
end, \
erlang:halt().
#####################################################################
##
## build
##
#####################################################################
all: rebar3 compile
compile:
@./rebar3 compile
clean:
@./rebar3 clean ;\
rm -Rf _build/default/rel ;\
rm -rf test.*-temp-data ;\
rm -rf tests ;\
rm -rf log ;\
rm -f relx.config ;\
rm -f *.tar.gz ;\
rm -f *.bundle
distclean: clean
@./rebar3 unlock ;\
rm -Rf _build ;\
rm -Rf rebar3
##
## execute unit test
unit: all
@./rebar3 skip_deps=true eunit
##
## execute common test and terminate node
test: _build/test.beam
@mkdir -p /tmp/test/${APP} ;\
erl ${EFLAGS} -pa _build/ -pa test/ -run test run test/${TEST}.config
_build/test.beam: _build/test.erl
erlc -o _build $<
_build/test.erl:
echo "${CTRUN}" > $@
#####################################################################
##
## release
##
#####################################################################
rel: ${PKG}.tar.gz
## assemble VM release
ifeq (${PLAT},$(shell uname -s))
${PKG}.tar.gz: relx.config
@./rebar3 tar -n ${APP} -v ${VSN} ;\
cp _build/default/rel/${APP}/${APP}-${VSN}.tar.gz $@ ;\
echo "==> tarball: $@"
relx.config: rel/relx.config.src
@cat $< | sed 's/release/release, {${APP}, "${VSN}"}/' > $@
else
${PKG}.tar.gz: _build/dockermake
@docker build --file=$< --force-rm=true --tag=build/${APP}:latest . ;\
I=`docker create build/${APP}:latest` ;\
docker cp $$I:/${APP}/$@ $@ ;\
docker rm -f $$I ;\
docker rmi build/${APP}:latest ;\
test -f $@ && echo "==> tarball: $@"
_build/dockermake:
@echo "${BUILDER}" > $@
endif
## build docker image
docker: rel/Dockerfile scm-source.json
docker build \
--build-arg APP=${APP} \
--build-arg VSN=${VSN} \
-t ${URL}/${APP}:${VSN} -f $< .
docker tag ${URL}/${APP}:${VSN} ${URL}/${APP}:${LATEST}
#####################################################################
##
## package / bundle
##
#####################################################################
pkg: ${PKG}.tar.gz ${PKG}.bundle
${PKG}.bundle: rel/deploy.sh
@printf '${BUNDLE_INIT}' > $@ ;\
cat $< >> $@ ;\
printf '${BUNDLE_FREE}' >> $@ ;\
cat ${PKG}.tar.gz >> $@ ;\
chmod ugo+x $@ ;\
echo "==> bundle: $@"
## copy 'package' to s3
s3: ${PKG}.bundle
aws s3 cp $< ${S3}/$<
s3-latest: ${PKG}.bundle
aws s3 cp $< ${S3}/${APP}-latest${VARIANT}.bundle
#####################################################################
##
## deploy
##
#####################################################################
ifneq (${host},)
${SSHENV}:
@echo "==> ssh: config keys" ;\
ssh-agent -s > ${SSHENV}
node: ${PKG}.bundle ${SSHENV}
@echo "==> deploy: ${host}" ;\
. ${SSHENV} ;\
k=`basename ${pass}` ;\
l=`ssh-add -l | grep $$k` ;\
if [ -z "$$l" ] ; then \
ssh-add ${pass} ;\
fi ;\
rsync -cav --rsh=ssh --progress $< ${host}:$< ;\
ssh -t ${host} "sudo sh ./$<"
endif
#####################################################################
##
## debug
##
#####################################################################
run:
@erl ${EFLAGS}
benchmark:
@echo "==> benchmark: ${TEST}" ;\
$(BB)/basho_bench -N [email protected] -C nocookie priv/${TEST}.benchmark ;\
$(BB)/priv/summary.r -i tests/current ;\
open tests/current/summary.png
console: ${PKG}.tar.gz
@_build/default/rel/${APP}/bin/${APP} console
#####################################################################
##
## dependencies
##
#####################################################################
rebar3:
@echo "==> install rebar (${REBAR})" ;\
curl -L -O --progress-bar https://github.com/erlang/rebar3/releases/download/${REBAR}/rebar3 ;\
chmod +x $@
#####################################################################
##
## stups extension
##
#####################################################################
scm-source.json: FORCE
@R=`git rev-parse HEAD`;\
S=`git status --porcelain | awk 1 ORS=' '`;\
if [ -n "$$S" ]; then R="$$R (locally modified)"; fi; \
echo "{\"url\": \"https://github.com/zalando/typhoon.git\", \"revision\": \"$$R\", \"author\": \"${USER}\", \"status\": \"$$S\"}" > $@
FORCE:
.PHONY: test rel deps all pkg