cache/Makefile

219 lines
5.7 KiB
Makefile
Raw Permalink Normal View History

## @author Dmitry Kolesnikov, <dmkolesnikov@gmail.com>
## @copyright (c) 2012 - 2014 Dmitry Kolesnikov. All Rights Reserved
##
## @description
2016-03-20 20:15:12 +00:00
## Makefile to build and release Erlang applications using standard development tools
##
## @version 0.11.8
2012-10-27 20:44:27 +00:00
#####################################################################
##
## application config
##
#####################################################################
PREFIX ?= /usr/local
2016-03-20 20:15:12 +00:00
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 ?= undefined
2016-03-20 20:15:12 +00:00
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
2016-03-20 20:15:12 +00:00
COOKIE?= nocookie
2016-03-20 20:15:12 +00:00
## 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} \
2016-03-20 20:15:12 +00:00
-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
2016-03-20 20:15:12 +00:00
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}\nARG VERSION=\nRUN mkdir ${APP}\nCOPY . ${APP}/\nRUN cd ${APP} && make VSN=\x24{VERSION} && make rel VSN=\x24{VERSION}\n
2016-03-20 20:15:12 +00:00
CTRUN = \
-module(test). \
-export([run/1]). \
run(Spec) -> \
{ok, Test} = file:consult(Spec), \
Error = case lists:keyfind(node, 1, Test) of \
false -> element(2, ct:run_test([{spec, Spec}])); \
2016-03-20 20:15:12 +00:00
true -> ct_master:run(Spec) \
end, \
erlang:halt(Error).
#####################################################################
##
## build
##
#####################################################################
2016-03-20 20:15:12 +00:00
all: rebar3 compile
2012-10-27 20:44:27 +00:00
compile:
2016-03-20 20:15:12 +00:00
@./rebar3 compile
2012-10-27 20:44:27 +00:00
clean:
2016-03-20 20:15:12 +00:00
@./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
2016-03-20 20:15:12 +00:00
@./rebar3 unlock ;\
rm -Rf _build ;\
rm -Rf rebar3
2012-10-27 20:44:27 +00:00
2016-03-20 20:15:12 +00:00
##
## execute unit test
unit: all
2016-03-20 20:15:12 +00:00
@./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
2012-10-27 20:44:27 +00:00
2016-03-20 20:15:12 +00:00
_build/test.beam: _build/test.erl
erlc -o _build $<
_build/test.erl:
echo "${CTRUN}" > $@
2012-10-27 20:44:27 +00:00
#####################################################################
##
2016-03-20 20:15:12 +00:00
## release
##
#####################################################################
2016-03-20 20:15:12 +00:00
rel: ${PKG}.tar.gz
2012-10-27 20:44:27 +00:00
## assemble VM release
ifeq (${PLAT},$(shell uname -s))
2016-03-20 20:15:12 +00:00
${PKG}.tar.gz: relx.config
@./rebar3 tar -n ${APP} -v ${VSN} ;\
cp _build/default/rel/${APP}/${APP}-${VSN}.tar.gz $@ ;\
echo "==> tarball: $@"
2016-03-20 20:15:12 +00:00
relx.config: rel/relx.config.src
@cat $< | sed 's/release/release, {${APP}, "${VSN}"}/' > $@
else
2016-03-20 20:15:12 +00:00
${PKG}.tar.gz: _build/dockermake
@docker build --file=$< --force-rm=true --build-arg="VERSION=${VSN}" --tag=build/${APP}:latest . ;\
2016-03-20 20:15:12 +00:00
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
2016-03-20 20:15:12 +00:00
## build docker image
docker: rel/Dockerfile
docker build \
--build-arg APP=${APP} \
--build-arg VSN=${VSN} \
-t ${URL}/${APP}:${VSN} -f $< .
docker tag ${URL}/${APP}:${VSN} ${URL}/${APP}:${LATEST}
2016-03-20 20:15:12 +00:00
#####################################################################
##
## 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
2016-03-20 20:15:12 +00:00
s3: ${PKG}.bundle
aws s3 cp $< ${S3}/$<
2016-03-20 20:15:12 +00:00
s3-latest: ${PKG}.bundle
aws s3 cp $< ${S3}/${APP}-latest${VARIANT}.bundle
#####################################################################
##
## deploy
##
#####################################################################
ifneq (${host},)
${SSHENV}:
@echo "==> ssh: config keys" ;\
ssh-agent -s > ${SSHENV}
2016-03-20 20:15:12 +00:00
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 ;\
2016-03-20 20:15:12 +00:00
rsync -cav --rsh=ssh --progress $< ${host}:$< ;\
ssh -t ${host} "sudo sh ./$<"
endif
#####################################################################
##
## debug
##
#####################################################################
run:
@erl ${EFLAGS}
2012-10-27 20:44:27 +00:00
benchmark:
@echo "==> benchmark: ${TEST}" ;\
$(BB)/basho_bench -N bb@127.0.0.1 -C nocookie priv/${TEST}.benchmark ;\
$(BB)/priv/summary.r -i tests/current ;\
open tests/current/summary.png
2016-03-20 20:15:12 +00:00
console: ${PKG}.tar.gz
@_build/default/rel/${APP}/bin/${APP} console
#####################################################################
##
## dependencies
##
#####################################################################
2016-03-20 20:15:12 +00:00
rebar3:
@echo "==> install rebar (${REBAR})" ;\
curl -L -O --progress-bar https://github.com/erlang/rebar3/releases/download/${REBAR}/rebar3 ;\
chmod +x $@
2016-03-20 20:15:12 +00:00
.PHONY: test rel deps all pkg