-
Notifications
You must be signed in to change notification settings - Fork 9
/
Makefile
232 lines (201 loc) · 5.79 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
## @author Dmitry Kolesnikov, <dmkolesnikov@gmail.com>
## @copyright (c) 2012 - 2014 Dmitry Kolesnikov. All Rights Reserved
##
## @description
## Makefile to build and release Erlang applications using
## rebar, reltool, etc (see README for details)
##
## application version schema (based on semantic version)
## ${APP}-${VSN}+${GIT}.${ARCH}.${PLAT}
##
## @version 0.7.1
.PHONY: test rel deps all pkg
#####################################################################
##
## application config
##
#####################################################################
ROOT = `pwd`
PREFIX ?= /usr/local
APP ?= $(notdir $(CURDIR))
ARCH?= $(shell uname -m)
PLAT?= $(shell uname -s)
HEAD?= $(shell git rev-parse --short HEAD)
TAG = ${HEAD}.${ARCH}.${PLAT}
TEST?= priv/${APP}.benchmark
S3 =
GIT ?=
VMI =
NET ?= en0
USER =
PASS =
## root path to benchmark framework
BB = ../basho_bench
SSHENV = /tmp/ssh-agent.conf
ADDR = $(shell ifconfig ${NET} | sed -En 's/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')
BRANCH = $(shell git symbolic-ref --short -q HEAD)
## erlang flags (make run only)
EFLAGS = \
-name ${APP}@${ADDR} \
-setcookie nocookie \
-pa ./ebin \
-pa deps/*/ebin \
-pa apps/*/ebin \
-kernel inet_dist_listen_min 32100 \
-kernel inet_dist_listen_max 32199 \
+P 1000000 \
+K true +A 160 -sbt ts
#####################################################################
##
## internal config
##
#####################################################################
ifeq ($(wildcard rel/reltool.config),)
REL =
VSN =
TAR =
PKG =
else
REL = $(shell cat rel/reltool.config | sed -n 's/{target_dir,.*\"\(.*\)\"}./\1/p')
VSN = $(shell echo ${REL} | sed -n 's/.*-\(.*\)/\1/p')
ifeq (${VSN},)
VSN = $(shell cat rel/reltool.config | sed -n 's/.*{rel,.*\".*\",.*\"\(.*\)\".*/\1/p')
endif
ifeq (${config},)
RFLAGS =
VARIANT =
else
VARIANT = $(addprefix ., $(notdir $(basename ${config})))
RFLAGS = target_dir=${REL}${VARIANT} overlay_vars=${ROOT}/${config}
endif
ifeq (${VSN},)
TAR = ${REL}${VARIANT}+${TAG}.tgz
PKG = ${REL}${VARIANT}+${TAG}.bundle
else
TAR = ${REL}-${VSN}${VARIANT}+${TAG}.tgz
PKG = ${REL}-${VSN}${VARIANT}+${TAG}.bundle
endif
endif
## self-extracting bundle wrapper
BUNDLE_INIT = PREFIX=${PREFIX}\nREL=${PREFIX}/${REL}${VARIANT}\nAPP=${APP}\nVSN=${VSN}\nLINE=\`grep -a -n 'BUNDLE:\x24' \x240\`\ntail -n +\x24(( \x24{LINE\x25\x25:*} + 1)) \x240 | gzip -vdc - | tar -C ${PREFIX} -xvf - > /dev/null\n
BUNDLE_FREE = exit\nBUNDLE:\n
BUILDER = cd /tmp && git clone -b ${BRANCH} ${GIT}/${APP} && cd /tmp/${APP} && make && make rel && sleep 300
#####################################################################
##
## build
##
#####################################################################
all: rebar deps compile
compile:
@./rebar compile
deps:
@./rebar get-deps
clean:
@./rebar clean ; \
rm -rf test.*-temp-data ; \
rm -rf tests ; \
rm -rf log ; \
rm -f *.tgz ; \
rm -f *.bundle
distclean: clean
@./rebar delete-deps
test: all
@./rebar skip_deps=true eunit
docs:
@./rebar skip_deps=true doc
#####################################################################
##
## release
##
#####################################################################
ifneq (${REL},)
rel: ${TAR}
## assemble VM release
ifeq (${PLAT},$(shell uname -s))
${TAR}:
@./rebar generate ${RFLAGS}; \
cd rel ; tar -zcf ../${TAR} ${REL}${VARIANT}/; cd - ;\
echo "==> tarball: ${TAR}"
else
ifneq (${VMI},)
${TAR}:
@echo "==> docker run ${VMI}" ;\
K=`test ${PASS} && cat ${PASS}` ;\
A=`test ${USER} && echo "mkdir -p /root/.ssh && echo \"$$K\" > /root/.ssh/id_rsa && chmod 0700 /root/.ssh/id_rsa && echo -e \"Host *\n\tUser ${USER}\n\tStrictHostKeyChecking no\n\" > /root/.ssh/config &&"` ;\
I=`docker run -d -t ${VMI} /bin/sh -c "$$A ${BUILDER}"` ;\
(docker attach $$I &) ;\
docker cp $$I:/tmp/${APP}/${TAR} . 1> /dev/null 2>&1 ;\
while [ $$? -ne 0 ] ;\
do \
sleep 10 ;\
docker cp $$I:/tmp/${APP}/${TAR} . 1> /dev/null 2>&1 ;\
done ;\
docker kill $$I ;\
docker rm $$I
endif
endif
## package VM release to executable bundle
pkg: rel/deploy.sh ${TAR}
@printf "${BUNDLE_INIT}" > ${PKG} ; \
cat rel/deploy.sh >> ${PKG} ; \
printf "${BUNDLE_FREE}" >> ${PKG} ; \
cat ${TAR} >> ${PKG} ; \
chmod ugo+x ${PKG} ; \
echo "==> bundle: ${PKG}"
## copy 'package' to s3
## copy 'package' to s3
s3: ${PKG}
aws s3 cp ${PKG} ${S3}/${APP}+${TAG}${VARIANT}.bundle
s3-latest: ${PKG}
aws s3 cp ${PKG} ${S3}/${APP}+latest${VARIANT}.bundle
endif
#####################################################################
##
## deploy
##
#####################################################################
ifneq (${host},)
${SSHENV}:
@echo "==> ssh: config keys" ;\
ssh-agent -s > ${SSHENV}
node: ${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 ${PKG} ${host}:${PKG} ;\
ssh -t ${host} "sudo sh ./${PKG}"
endif
#####################################################################
##
## debug
##
#####################################################################
run:
@erl ${EFLAGS}
benchmark:
@echo "==> benchmark: ${TEST}" ;\
$(BB)/basho_bench -N bb@127.0.0.1 -C nocookie ${TEST} ;\
$(BB)/priv/summary.r -i tests/current ;\
open tests/current/summary.png
ifneq (${REL},)
start:
@./rel/${REL}${VARIANT}/bin/${APP} start
stop:
@./rel/${REL}${VARIANT}/bin/${APP} stop
console:
@./rel/${REL}${VARIANT}/bin/${APP} console
attach:
@./rel/${REL}${VARIANT}/bin/${APP} attach
endif
#####################################################################
##
## dependencies
##
#####################################################################
rebar:
@curl -L -O https://raw.githubusercontent.com/wiki/basho/rebar/rebar ; \
chmod ugo+x rebar