2015-05-10 11:02:58 +00:00
## @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
2015-05-10 11:02:58 +00:00
##
2016-03-20 20:15:12 +00:00
## @version 0.11.5
2012-10-27 20:44:27 +00:00
2015-05-10 11:02:58 +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
LATEST ?= latest
2013-07-27 15:28:26 +00:00
2015-05-10 11:02:58 +00:00
## root path to benchmark framework
BB = ../basho_bench
SSHENV = /tmp/ssh-agent.conf
2016-03-20 20:15:12 +00:00
COOKIE ?= nocookie
2013-07-27 15:28:26 +00:00
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" )
2015-05-10 11:02:58 +00:00
EFLAGS = \
-name ${ APP } @${ ADDR } \
2016-03-20 20:15:12 +00:00
-setcookie ${ COOKIE } \
-pa ${ ROOT } /_build/default/lib/*/ebin \
-pa ${ ROOT } /rel \
2015-05-10 11:02:58 +00:00
-kernel inet_dist_listen_min 32100 \
-kernel inet_dist_listen_max 32199 \
+P 1000000 \
+K true +A 160 -sbt ts
2013-04-16 09:15:14 +00:00
2015-05-10 11:02:58 +00:00
## self-extracting bundle wrapper
2016-03-20 20:15:12 +00:00
BUNDLE_INIT = PREFIX = ${ PREFIX } \n REL= ${ PREFIX } /${ REL } \n APP= ${ APP } \n VSN= ${ VSN } \n LINE= ` grep -a -n " BUNDLE: $$ " $$ 0` \n mkdir -p $$ { REL} \n tail -n +$$ ( ( $$ { LINE%%%%:*} + 1) ) $$ 0 | gzip -vdc - | tar -C $$ { REL} -xvf - > /dev/null\n
2015-05-10 11:02:58 +00:00
BUNDLE_FREE = exit\n BUNDLE:\n
2016-03-20 20:15:12 +00:00
BUILDER = FROM ${ VMI } \n RUN mkdir ${ APP } \n COPY . ${ APP } /\n RUN 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( ) .
2015-05-10 11:02:58 +00:00
#####################################################################
##
## build
##
#####################################################################
2016-03-20 20:15:12 +00:00
all : rebar 3 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 ; \
2015-05-10 11:02:58 +00:00
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
2015-05-10 11:02:58 +00:00
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
2015-05-10 11:02:58 +00:00
#####################################################################
##
2016-03-20 20:15:12 +00:00
## release
2015-05-10 11:02:58 +00:00
##
#####################################################################
2016-03-20 20:15:12 +00:00
rel : ${PKG }.tar .gz
2012-10-27 20:44:27 +00:00
2015-05-10 11:02:58 +00:00
## assemble VM release
i f e q ( $ { P L A T } , $( 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: $@ "
2015-05-10 11:02:58 +00:00
2016-03-20 20:15:12 +00:00
relx.config : rel /relx .config .src
@cat $< | sed 's/release/release, {${APP}, "${VSN}"}/' > $@
2015-05-10 11:02:58 +00:00
e l s e
2016-03-20 20:15:12 +00:00
${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 } " > $@
2015-05-10 11:02:58 +00:00
e n d i f
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 -f ${ 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: $@ "
2015-05-10 11:02:58 +00:00
## copy 'package' to s3
2016-03-20 20:15:12 +00:00
s3 : ${PKG }.bundle
aws s3 cp $< ${ S3 } /$<
2015-05-10 11:02:58 +00:00
2016-03-20 20:15:12 +00:00
s3-latest : ${PKG }.bundle
aws s3 cp $< ${ S3 } /${ APP } -latest${ VARIANT } .bundle
2015-05-10 11:02:58 +00:00
#####################################################################
##
## deploy
##
#####################################################################
i f n e q ( $ { h o s t } , )
${SSHENV} :
@echo "==> ssh: config keys" ; \
ssh-agent -s > ${ SSHENV }
2016-03-20 20:15:12 +00:00
node : ${PKG }.bundle ${SSHENV }
2015-05-10 11:02:58 +00:00
@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 ./ $< "
2015-05-10 11:02:58 +00:00
e n d i f
#####################################################################
##
## debug
##
#####################################################################
run :
@erl ${ EFLAGS }
2012-10-27 20:44:27 +00:00
2013-04-16 09:15:14 +00:00
benchmark :
2015-05-10 11:02:58 +00:00
@echo " ==> benchmark: ${ TEST } " ; \
$( BB) /basho_bench -N bb@127.0.0.1 -C nocookie priv/${ TEST } .benchmark ; \
$( BB) /priv/summary.r -i tests/current ; \
2013-04-16 09:15:14 +00:00
open tests/current/summary.png
2016-03-20 20:15:12 +00:00
console : ${PKG }.tar .gz
@_build/default/rel/${ APP } /bin/${ APP } console
2015-05-10 11:02:58 +00:00
#####################################################################
##
## dependencies
##
#####################################################################
2016-03-20 20:15:12 +00:00
rebar3 :
@curl -L -O https://s3.amazonaws.com/rebar3/rebar3 ; \
chmod ugo+x $@
.PHONY : test rel deps all pkg