diff --git a/CHANGES b/CHANGES new file mode 100644 index 000000000..69cd46103 --- /dev/null +++ b/CHANGES @@ -0,0 +1,10 @@ +Changes with emqtt 0.1.1 30 Dec 2012 + + *) Feature: use rebar to generate release + + *) Bugfix: send will msg when network error + +Changes with emqtt 0.1.0 21 Dec 2012 + + *) The first public release. + diff --git a/INSTALL b/INSTALL deleted file mode 100755 index e69de29bb..000000000 diff --git a/Makefile b/Makefile index 1b95d6333..8fc76bff6 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -all: compile +all: deps compile compile: deps ./rebar compile @@ -8,3 +8,9 @@ deps: clean: ./rebar clean + +generate: + ./rebar generate -f + +relclean: + rm -rf rel/emqtt diff --git a/README b/README index e8004f512..d7025601b 100644 --- a/README +++ b/README @@ -30,3 +30,9 @@ logs ==== log/* + + +design +===== + +https://github.com/emqtt/emqtt/wiki diff --git a/docs/cluster.md b/docs/cluster.md deleted file mode 100644 index 281b1e700..000000000 --- a/docs/cluster.md +++ /dev/null @@ -1,12 +0,0 @@ - -Cluster Architecture -==================== - - -Topic: Memory Copy - -Topic ----------- Topic - -Subscriber: Local Node - - diff --git a/docs/desgin.md b/docs/desgin.md deleted file mode 100644 index c3c98819c..000000000 --- a/docs/desgin.md +++ /dev/null @@ -1,13 +0,0 @@ -Erlang TCP -========= - - -One Million TCP Connections -========================== - -http://news.ycombinator.com/item?id=3028547 - -http://www.kegel.com/c10k.html - -http://20bits.com/article/erlang-a-generalized-tcp-server - diff --git a/docs/keepalive.md b/docs/keepalive.md deleted file mode 100644 index 8b1378917..000000000 --- a/docs/keepalive.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/docs/topic.md b/docs/topic.md deleted file mode 100644 index 91755dec5..000000000 --- a/docs/topic.md +++ /dev/null @@ -1,15 +0,0 @@ - -Direct Topic - -or - -Wildchar Topic? - - -a/+/b -a/# -# - -Trie Data Structure - - diff --git a/etc/emqtt.config b/etc/emqtt.config index fd1f7d52a..1d70835d3 100644 --- a/etc/emqtt.config +++ b/etc/emqtt.config @@ -8,7 +8,7 @@ {sasl_error_logger, {file, "log/emqtt_sasl.log"}} ]}, {mnesia, [ - {dir, "var/mnesia"} + {dir, "var/data"} ]}, {lager, [ {error_logger_redirect, false}, diff --git a/log/.placeholder b/log/.placeholder deleted file mode 100644 index e69de29bb..000000000 diff --git a/rebar b/rebar index faac22f97..204e87509 100755 Binary files a/rebar and b/rebar differ diff --git a/rebar.config b/rebar.config index 85d41db0b..4a588f8a3 100644 --- a/rebar.config +++ b/rebar.config @@ -1,7 +1,11 @@ +{require_otp_vsn, "R15"}. + {erl_opts, [debug_info, {parse_transform, lager_transform}]}. {erl_opts, [{i, "include"}]}. +{sub_dirs, ["rel"]}. + {lib_dirs,["lib"]}. {deps_dir, ["lib"]}. diff --git a/rel/files/app.config b/rel/files/app.config new file mode 100644 index 000000000..fd1f7d52a --- /dev/null +++ b/rel/files/app.config @@ -0,0 +1,37 @@ +%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*- +%% ex: ft=erlang ts=4 sw=4 et +[{kernel, + [{start_timer, true}, + {start_pg2, true} + ]}, + {sasl, [ + {sasl_error_logger, {file, "log/emqtt_sasl.log"}} + ]}, + {mnesia, [ + {dir, "var/mnesia"} + ]}, + {lager, [ + {error_logger_redirect, false}, + {crash_log, "log/emqtt_crash.log"}, + {handlers, [ + {lager_console_backend, info}, + {lager_file_backend, [ + {"log/emqtt_error.log", error, 10485760, "$D0", 5}, + {"log/emqtt_info.log", info, 10485760, "$D0", 5} + ]} + ]} + ]}, + {emqtt, [ + {auth, {anonymous, []}}, %internal, anonymous + {listeners, [ + {1883, [ + binary, + {packet, raw}, + {reuseaddr, true}, + {backlog, 128}, + {nodelay, true} + ]} + ]} + ]} +]. + diff --git a/rel/files/emqtt b/rel/files/emqtt new file mode 100755 index 000000000..43d90bcea --- /dev/null +++ b/rel/files/emqtt @@ -0,0 +1,292 @@ +#!/bin/sh +# -*- tab-width:4;indent-tabs-mode:nil -*- +# ex: ts=4 sw=4 et + +RUNNER_SCRIPT_DIR=$(cd ${0%/*} && pwd) + +CALLER_DIR=$PWD + +RUNNER_BASE_DIR=${RUNNER_SCRIPT_DIR%/*} +RUNNER_ETC_DIR=$RUNNER_BASE_DIR/etc +# Note the trailing slash on $PIPE_DIR/ +PIPE_DIR=/tmp/$RUNNER_BASE_DIR/ +RUNNER_USER= + +# Make sure this script is running as the appropriate user +if [ ! -z "$RUNNER_USER" ] && [ `whoami` != "$RUNNER_USER" ]; then + exec sudo -u $RUNNER_USER -i $0 $@ +fi + +# Identify the script name +SCRIPT=`basename $0` + +# Parse out release and erts info +START_ERL=`cat $RUNNER_BASE_DIR/releases/start_erl.data` +ERTS_VSN=${START_ERL% *} +APP_VSN=${START_ERL#* } + +# Use $CWD/vm.args if exists, otherwise releases/APP_VSN/vm.args, or else etc/vm.args +if [ -e "$CALLER_DIR/vm.args" ]; then + VMARGS_PATH=$CALLER_DIR/vm.args + USE_DIR=$CALLER_DIR +else + USE_DIR=$RUNNER_BASE_DIR + if [ -e "$RUNNER_BASE_DIR/releases/$APP_VSN/vm.args" ]; then + VMARGS_PATH="$RUNNER_BASE_DIR/releases/$APP_VSN/vm.args" + else + VMARGS_PATH="$RUNNER_ETC_DIR/vm.args" + fi +fi + +RUNNER_LOG_DIR=$USE_DIR/log +# Make sure log directory exists +mkdir -p $RUNNER_LOG_DIR + +# Use releases/VSN/sys.config if it exists otherwise use etc/app.config +if [ -e "$USE_DIR/sys.config" ]; then + CONFIG_PATH="$USE_DIR/sys.config" +else + if [ -e "$RUNNER_BASE_DIR/releases/$APP_VSN/sys.config" ]; then + CONFIG_PATH="$RUNNER_BASE_DIR/releases/$APP_VSN/sys.config" + else + CONFIG_PATH="$RUNNER_ETC_DIR/app.config" + fi +fi + +# Extract the target node name from node.args +NAME_ARG=`egrep '^-s?name' $VMARGS_PATH` +if [ -z "$NAME_ARG" ]; then + echo "vm.args needs to have either -name or -sname parameter." + exit 1 +fi + +# Extract the name type and name from the NAME_ARG for REMSH +REMSH_TYPE=`echo $NAME_ARG | awk '{print $1}'` +REMSH_NAME=`echo $NAME_ARG | awk '{print $2}'` + +# Note the `date +%s`, used to allow multiple remsh to the same node transparently +REMSH_NAME_ARG="$REMSH_TYPE remsh`date +%s`@`echo $REMSH_NAME | awk -F@ '{print $2}'`" +REMSH_REMSH_ARG="-remsh $REMSH_NAME" + +# Extract the target cookie +COOKIE_ARG=`grep '^-setcookie' $VMARGS_PATH` +if [ -z "$COOKIE_ARG" ]; then + echo "vm.args needs to have a -setcookie parameter." + exit 1 +fi + +# Make sure CWD is set to the right dir +cd $USE_DIR + +# Make sure log directory exists +mkdir -p $USE_DIR/log + + +# Add ERTS bin dir to our path +ERTS_PATH=$RUNNER_BASE_DIR/erts-$ERTS_VSN/bin + +# Setup command to control the node +NODETOOL="$ERTS_PATH/escript $ERTS_PATH/nodetool $NAME_ARG $COOKIE_ARG" + +# Setup remote shell command to control node +REMSH="$ERTS_PATH/erl $REMSH_NAME_ARG $REMSH_REMSH_ARG $COOKIE_ARG" + +# Check the first argument for instructions +case "$1" in + start|start_boot) + # Make sure there is not already a node running + RES=`$NODETOOL ping` + if [ "$RES" = "pong" ]; then + echo "Node is already running!" + exit 1 + fi + case "$1" in + start) + shift + START_OPTION="console" + HEART_OPTION="start" + ;; + start_boot) + shift + START_OPTION="console_boot" + HEART_OPTION="start_boot" + ;; + esac + RUN_PARAM=$(printf "\'%s\' " "$@") + HEART_COMMAND="$RUNNER_BASE_DIR/bin/$SCRIPT $HEART_OPTION $RUN_PARAM" + export HEART_COMMAND + mkdir -p $PIPE_DIR + $ERTS_PATH/run_erl -daemon $PIPE_DIR $RUNNER_LOG_DIR "exec $RUNNER_BASE_DIR/bin/$SCRIPT $START_OPTION $RUN_PARAM" 2>&1 + ;; + + stop) + # Wait for the node to completely stop... + case `uname -s` in + Linux|Darwin|FreeBSD|DragonFly|NetBSD|OpenBSD) + # PID COMMAND + PID=`ps ax -o pid= -o command=|\ + grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'` + ;; + SunOS) + # PID COMMAND + PID=`ps -ef -o pid= -o args=|\ + grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'` + ;; + CYGWIN*) + # UID PID PPID TTY STIME COMMAND + PID=`ps -efW|grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $2}'` + ;; + esac + $NODETOOL stop + ES=$? + if [ "$ES" -ne 0 ]; then + exit $ES + fi + while `kill -0 $PID 2>/dev/null`; + do + sleep 1 + done + ;; + + restart) + ## Restart the VM without exiting the process + $NODETOOL restart + ES=$? + if [ "$ES" -ne 0 ]; then + exit $ES + fi + ;; + + reboot) + ## Restart the VM completely (uses heart to restart it) + $NODETOOL reboot + ES=$? + if [ "$ES" -ne 0 ]; then + exit $ES + fi + ;; + + ping) + ## See if the VM is alive + $NODETOOL ping + ES=$? + if [ "$ES" -ne 0 ]; then + exit $ES + fi + ;; + + attach) + # Make sure a node IS running + RES=`$NODETOOL ping` + ES=$? + if [ "$ES" -ne 0 ]; then + echo "Node is not running!" + exit $ES + fi + + shift + exec $ERTS_PATH/to_erl $PIPE_DIR + ;; + + remote_console) + # Make sure a node IS running + RES=`$NODETOOL ping` + ES=$? + if [ "$ES" -ne 0 ]; then + echo "Node is not running!" + exit $ES + fi + + shift + exec $REMSH + ;; + + upgrade) + if [ -z "$2" ]; then + echo "Missing upgrade package argument" + echo "Usage: $SCRIPT upgrade {package base name}" + echo "NOTE {package base name} MUST NOT include the .tar.gz suffix" + exit 1 + fi + + # Make sure a node IS running + RES=`$NODETOOL ping` + ES=$? + if [ "$ES" -ne 0 ]; then + echo "Node is not running!" + exit $ES + fi + + node_name=`echo $NAME_ARG | awk '{print $2}'` + erlang_cookie=`echo $COOKIE_ARG | awk '{print $2}'` + + $ERTS_PATH/escript $RUNNER_BASE_DIR/bin/install_upgrade.escript $node_name $erlang_cookie $2 + ;; + + console|console_clean|console_boot) + # .boot file typically just $SCRIPT (ie, the app name) + # however, for debugging, sometimes start_clean.boot is useful. + # For e.g. 'setup', one may even want to name another boot script. + case "$1" in + console) BOOTFILE=$SCRIPT ;; + console_clean) BOOTFILE=start_clean ;; + console_boot) + shift + BOOTFILE="$1" + shift + ;; + esac + # Setup beam-required vars + ROOTDIR=$RUNNER_BASE_DIR + BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin + EMU=beam + PROGNAME=`echo $0 | sed 's/.*\\///'` + CMD="$BINDIR/erlexec -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -mode embedded -config $CONFIG_PATH -args_file $VMARGS_PATH" + export EMU + export ROOTDIR + export BINDIR + export PROGNAME + + # Dump environment info for logging purposes + echo "Exec: $CMD" -- ${1+"$@"} + echo "Root: $ROOTDIR" + + # Log the startup + logger -t "$SCRIPT[$$]" "Starting up" + + # Start the VM + exec $CMD -- ${1+"$@"} + ;; + + foreground) + # start up the release in the foreground for use by runit + # or other supervision services + + BOOTFILE=$SCRIPT + FOREGROUNDOPTIONS="-noinput +Bd" + + # Setup beam-required vars + ROOTDIR=$RUNNER_BASE_DIR + BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin + EMU=beam + PROGNAME=`echo $0 | sed 's/.*\///'` + CMD="$BINDIR/erlexec $FOREGROUNDOPTIONS -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -config $CONFIG_PATH -args_file $VMARGS_PATH" + export EMU + export ROOTDIR + export BINDIR + export PROGNAME + + # Dump environment info for logging purposes + echo "Exec: $CMD" -- ${1+"$@"} + echo "Root: $ROOTDIR" + + # Start the VM + exec $CMD -- ${1+"$@"} + ;; + *) + echo "Usage: $SCRIPT {start|start_boot |foreground|stop|restart|reboot|ping|console|console_clean|console_boot |attach|remote_console|upgrade}" + exit 1 + ;; +esac + +exit 0 diff --git a/rel/files/emqtt.cmd b/rel/files/emqtt.cmd new file mode 100644 index 000000000..fc0da7cb2 --- /dev/null +++ b/rel/files/emqtt.cmd @@ -0,0 +1,96 @@ +@setlocal + +@set node_name=emqtt + +@rem Get the absolute path to the parent directory, +@rem which is assumed to be the node root. +@for /F "delims=" %%I in ("%~dp0..") do @set node_root=%%~fI + +@set releases_dir=%node_root%\releases + +@rem Parse ERTS version and release version from start_erl.data +@for /F "usebackq tokens=1,2" %%I in ("%releases_dir%\start_erl.data") do @( + @call :set_trim erts_version %%I + @call :set_trim release_version %%J +) + +@set vm_args=%releases_dir%\%release_version%\vm.args +@set sys_config=%releases_dir%\%release_version%\sys.config +@set node_boot_script=%releases_dir%\%release_version%\%node_name% +@set clean_boot_script=%releases_dir%\%release_version%\start_clean + +@rem extract erlang cookie from vm.args +@for /f "usebackq tokens=1-2" %%I in (`findstr /b \-setcookie "%vm_args%"`) do @set erlang_cookie=%%J + +@set erts_bin=%node_root%\erts-%erts_version%\bin + +@set service_name=%node_name%_%release_version% + +@set erlsrv="%erts_bin%\erlsrv.exe" +@set epmd="%erts_bin%\epmd.exe" +@set escript="%erts_bin%\escript.exe" +@set werl="%erts_bin%\werl.exe" + +@if "%1"=="usage" @goto usage +@if "%1"=="install" @goto install +@if "%1"=="uninstall" @goto uninstall +@if "%1"=="start" @goto start +@if "%1"=="stop" @goto stop +@if "%1"=="restart" @call :stop && @goto start +@if "%1"=="console" @goto console +@if "%1"=="query" @goto query +@if "%1"=="attach" @goto attach +@if "%1"=="upgrade" @goto upgrade +@echo Unknown command: "%1" + +:usage +@echo Usage: %~n0 [install^|uninstall^|start^|stop^|restart^|console^|query^|attach^|upgrade] +@goto :EOF + +:install +@set description=Erlang node %node_name% in %node_root% +@set start_erl=%node_root%\bin\start_erl.cmd +@set args= ++ %node_name% ++ %node_root% +@%erlsrv% add %service_name% -c "%description%" -sname %node_name% -w "%node_root%" -m "%start_erl%" -args "%args%" -stopaction "init:stop()." +@goto :EOF + +:uninstall +@%erlsrv% remove %service_name% +@%epmd% -kill +@goto :EOF + +:start +@%erlsrv% start %service_name% +@goto :EOF + +:stop +@%erlsrv% stop %service_name% +@goto :EOF + +:console +@start "%node_name% console" %werl% -boot "%node_boot_script%" -config "%sys_config%" -args_file "%vm_args%" -sname %node_name% +@goto :EOF + +:query +@%erlsrv% list %service_name% +@exit %ERRORLEVEL% +@goto :EOF + +:attach +@for /f "usebackq" %%I in (`hostname`) do @set hostname=%%I +start "%node_name% attach" %werl% -boot "%clean_boot_script%" -remsh %node_name%@%hostname% -sname console -setcookie %erlang_cookie% +@goto :EOF + +:upgrade +@if "%2"=="" ( + @echo Missing upgrade package argument + @echo Usage: %~n0 upgrade {package base name} + @echo NOTE {package base name} MUST NOT include the .tar.gz suffix + @goto :EOF +) +@%escript% %node_root%\bin\install_upgrade.escript %node_name% %erlang_cookie% %2 +@goto :EOF + +:set_trim +@set %1=%2 +@goto :EOF diff --git a/rel/files/emqtt_ctl b/rel/files/emqtt_ctl new file mode 100755 index 000000000..b056316c1 --- /dev/null +++ b/rel/files/emqtt_ctl @@ -0,0 +1,159 @@ +#!/bin/bash + +RUNNER_SCRIPT_DIR=$(cd ${0%/*} && pwd) + +RUNNER_BASE_DIR=${RUNNER_SCRIPT_DIR%/*} +RUNNER_ETC_DIR=$RUNNER_BASE_DIR/etc +RUNNER_BIN_DIR=$RUNNER_BASE_DIR/bin +RUNNER_LOG_DIR=$RUNNER_BASE_DIR/log + +RUNNER_EBIN_DIR=$RUNNER_BASE_DIR/ebin +RUNNER_USER= + +# Make sure CWD is set to runner base dir +cd $RUNNER_BASE_DIR + +# Extract the target node name from node.args +NAME_ARG=`grep '\-[s]*name' $RUNNER_ETC_DIR/emqtt.args` +if [ -z "$NAME_ARG" ]; then + echo "emqtt.args needs to have either -name or -sname parameter." + exit 1 +fi + +# Learn how to specify node name for connection from remote nodes +echo "$NAME_ARG" | grep '^-sname' > /dev/null 2>&1 +if [ "X$?" = "X0" ]; then + NAME_PARAM="-sname" + NAME_HOST="" +else + NAME_PARAM="-name" + echo "$NAME_ARG" | grep '@.*' > /dev/null 2>&1 + if [ "X$?" = "X0" ]; then + NAME_HOST=`echo "${NAME_ARG}" | sed -e 's/.*\(@.*\)$/\1/'` + else + NAME_HOST="" + fi +fi + +# Extract the target cookie +COOKIE_ARG=`grep '\-setcookie' $RUNNER_ETC_DIR/emqtt.args` +if [ -z "$COOKIE_ARG" ]; then + echo "emqtt.args needs to have a -setcookie parameter." + exit 1 +fi + +# Identify the script name +SCRIPT=`basename $0` + +# Parse out release and erts info +ERLANG_BASE_DIR=/usr/local/lib/erlang +START_ERL=`cat $ERLANG_BASE_DIR/releases/start_erl.data` +ERTS_VSN=${START_ERL% *} +APP_VSN=${START_ERL#* } + +# Add ERTS bin dir to our path +ERTS_PATH=$ERLANG_BASE_DIR/erts-$ERTS_VSN/bin + +# Setup command to control the node +NODETOOL="$ERTS_PATH/escript $RUNNER_BIN_DIR/nodetool $NAME_ARG $COOKIE_ARG" + +# Check the first argument for instructions +case "$1" in + status) + if [ $# -ne 1 ]; then + echo "Usage: $SCRIPT status" + exit 1 + fi + + # Make sure the local node IS running + RES=`$NODETOOL ping` + if [ "$RES" != "pong" ]; then + echo "Node is not running!" + exit 1 + fi + shift + + $NODETOOL rpc emqtt_ctl status $@ + ;; + + cluster_info) + if [ $# -ne 1 ]; then + echo "Usage: $SCRIPT cluster_info" + exit 1 + fi + + # Make sure the local node IS running + RES=`$NODETOOL ping` + if [ "$RES" != "pong" ]; then + echo "Node is not running!" + exit 1 + fi + shift + + $NODETOOL rpc emqtt_ctl cluster_info $@ + ;; + + + cluster) + if [ $# -ne 2 ]; then + echo "Usage: $SCRIPT cluster " + exit 1 + fi + + # Make sure the local node IS running + RES=`$NODETOOL ping` + if [ "$RES" != "pong" ]; then + echo "emqtt is not running!" + exit 1 + fi + shift + + $NODETOOL rpc emqtt_ctl cluster $@ + ;; + + add_user) + if [ $# -ne 3 ]; then + echo "Usage: $SCRIPT add_user " + exit 1 + fi + + # Make sure the local node IS running + RES=`$NODETOOL ping` + if [ "$RES" != "pong" ]; then + echo "emqtt is not running!" + exit 1 + fi + shift + + $NODETOOL rpc emqtt_ctl add_user $@ + ;; + + delete_user) + if [ $# -ne 2 ]; then + echo "Usage: $SCRIPT delete_user " + exit 1 + fi + + # Make sure the local node IS running + RES=`$NODETOOL ping` + if [ "$RES" != "pong" ]; then + echo "emqtt is not running!" + exit 1 + fi + shift + + $NODETOOL rpc emqtt_ctl delete_user $@ + ;; + + *) + echo "Usage: $SCRIPT" + echo " status #query emqtt status" + echo " cluster_info #query cluster nodes" + echo " cluster #cluster node" + echo " add_user #add user" + echo " delete_user #delete user" + exit 1 + ;; + +esac + diff --git a/rel/files/erl b/rel/files/erl new file mode 100755 index 000000000..6f65e3fc9 --- /dev/null +++ b/rel/files/erl @@ -0,0 +1,34 @@ +#!/bin/sh + +## This script replaces the default "erl" in erts-VSN/bin. This is necessary +## as escript depends on erl and in turn, erl depends on having access to a +## bootscript (start.boot). Note that this script is ONLY invoked as a side-effect +## of running escript -- the embedded node bypasses erl and uses erlexec directly +## (as it should). +## +## Note that this script makes the assumption that there is a start_clean.boot +## file available in $ROOTDIR/release/VSN. + +# Determine the abspath of where this script is executing from. +ERTS_BIN_DIR=$(cd ${0%/*} && pwd) + +# Now determine the root directory -- this script runs from erts-VSN/bin, +# so we simply need to strip off two dirs from the end of the ERTS_BIN_DIR +# path. +ROOTDIR=${ERTS_BIN_DIR%/*/*} + +# Parse out release and erts info +START_ERL=`cat $ROOTDIR/releases/start_erl.data` +ERTS_VSN=${START_ERL% *} +APP_VSN=${START_ERL#* } + +BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin +EMU=beam +PROGNAME=`echo $0 | sed 's/.*\\///'` +CMD="$BINDIR/erlexec" +export EMU +export ROOTDIR +export BINDIR +export PROGNAME + +exec $CMD -boot $ROOTDIR/releases/$APP_VSN/start_clean ${1+"$@"} diff --git a/rel/files/install_upgrade.escript b/rel/files/install_upgrade.escript new file mode 100644 index 000000000..56cea1963 --- /dev/null +++ b/rel/files/install_upgrade.escript @@ -0,0 +1,44 @@ +#!/usr/bin/env escript +%%! -noshell -noinput +%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*- +%% ex: ft=erlang ts=4 sw=4 et + +-define(TIMEOUT, 60000). +-define(INFO(Fmt,Args), io:format(Fmt,Args)). + +main([NodeName, Cookie, ReleasePackage]) -> + TargetNode = start_distribution(NodeName, Cookie), + {ok, Vsn} = rpc:call(TargetNode, release_handler, unpack_release, + [ReleasePackage], ?TIMEOUT), + ?INFO("Unpacked Release ~p~n", [Vsn]), + {ok, OtherVsn, Desc} = rpc:call(TargetNode, release_handler, + check_install_release, [Vsn], ?TIMEOUT), + {ok, OtherVsn, Desc} = rpc:call(TargetNode, release_handler, + install_release, [Vsn], ?TIMEOUT), + ?INFO("Installed Release ~p~n", [Vsn]), + ok = rpc:call(TargetNode, release_handler, make_permanent, [Vsn], ?TIMEOUT), + ?INFO("Made Release ~p Permanent~n", [Vsn]); +main(_) -> + init:stop(1). + +start_distribution(NodeName, Cookie) -> + MyNode = make_script_node(NodeName), + {ok, _Pid} = net_kernel:start([MyNode, shortnames]), + erlang:set_cookie(node(), list_to_atom(Cookie)), + TargetNode = make_target_node(NodeName), + case {net_kernel:hidden_connect_node(TargetNode), + net_adm:ping(TargetNode)} of + {true, pong} -> + ok; + {_, pang} -> + io:format("Node ~p not responding to pings.\n", [TargetNode]), + init:stop(1) + end, + TargetNode. + +make_target_node(Node) -> + [_, Host] = string:tokens(atom_to_list(node()), "@"), + list_to_atom(lists:concat([Node, "@", Host])). + +make_script_node(Node) -> + list_to_atom(lists:concat([Node, "_upgrader_", os:getpid()])). diff --git a/rel/files/nodetool b/rel/files/nodetool new file mode 100755 index 000000000..eb08fa45d --- /dev/null +++ b/rel/files/nodetool @@ -0,0 +1,138 @@ +%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*- +%% ex: ft=erlang ts=4 sw=4 et +%% ------------------------------------------------------------------- +%% +%% nodetool: Helper Script for interacting with live nodes +%% +%% ------------------------------------------------------------------- + +main(Args) -> + ok = start_epmd(), + %% Extract the args + {RestArgs, TargetNode} = process_args(Args, [], undefined), + + %% See if the node is currently running -- if it's not, we'll bail + case {net_kernel:hidden_connect_node(TargetNode), net_adm:ping(TargetNode)} of + {true, pong} -> + ok; + {_, pang} -> + io:format("Node ~p not responding to pings.\n", [TargetNode]), + halt(1) + end, + + case RestArgs of + ["ping"] -> + %% If we got this far, the node already responsed to a ping, so just dump + %% a "pong" + io:format("pong\n"); + ["stop"] -> + io:format("~p\n", [rpc:call(TargetNode, init, stop, [], 60000)]); + ["restart"] -> + io:format("~p\n", [rpc:call(TargetNode, init, restart, [], 60000)]); + ["reboot"] -> + io:format("~p\n", [rpc:call(TargetNode, init, reboot, [], 60000)]); + ["rpc", Module, Function | RpcArgs] -> + case rpc:call(TargetNode, list_to_atom(Module), list_to_atom(Function), + [RpcArgs], 60000) of + ok -> + ok; + {badrpc, Reason} -> + io:format("RPC to ~p failed: ~p\n", [TargetNode, Reason]), + halt(1); + _ -> + halt(1) + end; + ["rpcterms", Module, Function, ArgsAsString] -> + case rpc:call(TargetNode, list_to_atom(Module), list_to_atom(Function), + consult(ArgsAsString), 60000) of + {badrpc, Reason} -> + io:format("RPC to ~p failed: ~p\n", [TargetNode, Reason]), + halt(1); + Other -> + io:format("~p\n", [Other]) + end; + Other -> + io:format("Other: ~p\n", [Other]), + io:format("Usage: nodetool {ping|stop|restart|reboot}\n") + end, + net_kernel:stop(). + +process_args([], Acc, TargetNode) -> + {lists:reverse(Acc), TargetNode}; +process_args(["-setcookie", Cookie | Rest], Acc, TargetNode) -> + erlang:set_cookie(node(), list_to_atom(Cookie)), + process_args(Rest, Acc, TargetNode); +process_args(["-name", TargetName | Rest], Acc, _) -> + ThisNode = append_node_suffix(TargetName, "_maint_"), + {ok, _} = net_kernel:start([ThisNode, longnames]), + process_args(Rest, Acc, nodename(TargetName)); +process_args(["-sname", TargetName | Rest], Acc, _) -> + ThisNode = append_node_suffix(TargetName, "_maint_"), + {ok, _} = net_kernel:start([ThisNode, shortnames]), + process_args(Rest, Acc, nodename(TargetName)); +process_args([Arg | Rest], Acc, Opts) -> + process_args(Rest, [Arg | Acc], Opts). + + +start_epmd() -> + [] = os:cmd(epmd_path() ++ " -daemon"), + ok. + +epmd_path() -> + ErtsBinDir = filename:dirname(escript:script_name()), + Name = "epmd", + case os:find_executable(Name, ErtsBinDir) of + false -> + case os:find_executable(Name) of + false -> + io:format("Could not find epmd.~n"), + halt(1); + GlobalEpmd -> + GlobalEpmd + end; + Epmd -> + Epmd + end. + + +nodename(Name) -> + case string:tokens(Name, "@") of + [_Node, _Host] -> + list_to_atom(Name); + [Node] -> + [_, Host] = string:tokens(atom_to_list(node()), "@"), + list_to_atom(lists:concat([Node, "@", Host])) + end. + +append_node_suffix(Name, Suffix) -> + case string:tokens(Name, "@") of + [Node, Host] -> + list_to_atom(lists:concat([Node, Suffix, os:getpid(), "@", Host])); + [Node] -> + list_to_atom(lists:concat([Node, Suffix, os:getpid()])) + end. + + +%% +%% Given a string or binary, parse it into a list of terms, ala file:consult/0 +%% +consult(Str) when is_list(Str) -> + consult([], Str, []); +consult(Bin) when is_binary(Bin)-> + consult([], binary_to_list(Bin), []). + +consult(Cont, Str, Acc) -> + case erl_scan:tokens(Cont, Str, 0) of + {done, Result, Remaining} -> + case Result of + {ok, Tokens, _} -> + {ok, Term} = erl_parse:parse_term(Tokens), + consult([], Remaining, [Term | Acc]); + {eof, _Other} -> + lists:reverse(Acc); + {error, Info, _} -> + {error, Info} + end; + {more, Cont1} -> + consult(Cont1, eof, Acc) + end. diff --git a/rel/files/start_erl.cmd b/rel/files/start_erl.cmd new file mode 100644 index 000000000..c0f20722e --- /dev/null +++ b/rel/files/start_erl.cmd @@ -0,0 +1,40 @@ +@setlocal + +@rem Parse arguments. erlsrv.exe prepends erl arguments prior to first ++. +@rem Other args are position dependent. +@set args="%*" +@for /F "delims=++ tokens=1,2,3" %%I in (%args%) do @( + @set erl_args=%%I + @call :set_trim node_name %%J + @rem Trim spaces from the left of %%K (node_root), which may have spaces inside + @for /f "tokens=* delims= " %%a in ("%%K") do @set node_root=%%a +) + +@set releases_dir=%node_root%\releases + +@rem parse ERTS version and release version from start_erl.dat +@for /F "usebackq tokens=1,2" %%I in ("%releases_dir%\start_erl.data") do @( + @call :set_trim erts_version %%I + @call :set_trim release_version %%J +) + +@set erl_exe="%node_root%\erts-%erts_version%\bin\erl.exe" +@set boot_file="%releases_dir%\%release_version%\%node_name%" + +@if exist "%releases_dir%\%release_version%\sys.config" ( + @set app_config="%releases_dir%\%release_version%\sys.config" +) else ( + @set app_config="%node_root%\etc\app.config" +) + +@if exist "%releases_dir%\%release_version%\vm.args" ( + @set vm_args="%releases_dir%\%release_version%\vm.args" +) else ( + @set vm_args="%node_root%\etc\vm.args" +) + +@%erl_exe% %erl_args% -boot %boot_file% -config %app_config% -args_file %vm_args% + +:set_trim +@set %1=%2 +@goto :EOF diff --git a/rel/files/vm.args b/rel/files/vm.args new file mode 100644 index 000000000..6db0b253c --- /dev/null +++ b/rel/files/vm.args @@ -0,0 +1,20 @@ +## Name of the node +-sname emqtt + +## Cookie for distributed erlang +-setcookie emqtt + +## Heartbeat management; auto-restarts VM if it dies or becomes unresponsive +## (Disabled by default..use with caution!) +##-heart +-smp true + +## Enable kernel poll and a few async threads ++K true ++A 32 + +## Increase number of concurrent ports/sockets +##-env ERL_MAX_PORTS 4096 + +## Tweak GC to run more often +##-env ERL_FULLSWEEP_AFTER 10 diff --git a/rel/reltool.config b/rel/reltool.config new file mode 100644 index 000000000..7970da178 --- /dev/null +++ b/rel/reltool.config @@ -0,0 +1,50 @@ +{sys, [ + {lib_dirs, ["../..", "../lib"]}, + {erts, [{mod_cond, derived}, {app_file, strip}]}, + {app_file, strip}, + {rel, "emqtt", "0.1.1", + [ + kernel, + stdlib, + sasl, + mnesia, + lager, + emqtt + ]}, + {rel, "start_clean", "", + [ + kernel, + stdlib + ]}, + {boot_rel, "emqtt"}, + {profile, embedded}, + {incl_cond, exclude}, + %{mod_cond, derived}, + {excl_archive_filters, [".*"]}, %% Do not archive built libs + {excl_sys_filters, ["^bin/.*", "^erts.*/bin/(dialyzer|typer)", + "^erts.*/(doc|info|include|lib|man|src)"]}, + {excl_app_filters, ["\.gitignore"]}, + {app, kernel, [{incl_cond, include}]}, + {app, stdlib, [{incl_cond, include}]}, + {app, sasl, [{incl_cond, include}]}, + {app, mnesia, [{incl_cond, include}]}, + {app, lager, [{incl_cond, include}]}, + {app, emqtt, [{mod_cond, app}, {incl_cond, include}]} + ]}. + +{target_dir, "emqtt"}. + +{overlay, [ + {mkdir, "log/"}, + {mkdir, "etc/"}, + {mkdir, "var/data/"}, + {copy, "files/erl", "\{\{erts_vsn\}\}/bin/erl"}, + {copy, "files/nodetool", "\{\{erts_vsn\}\}/bin/nodetool"}, + {copy, "files/emqtt", "bin/emqtt"}, + {copy, "files/emqtt_ctl", "bin/emqtt_ctl"}, + {copy, "files/emqtt.cmd", "bin/emqtt.cmd"}, + {copy, "files/start_erl.cmd", "bin/start_erl.cmd"}, + {copy, "files/install_upgrade.escript", "bin/install_upgrade.escript"}, + {copy, "files/app.config", "etc/app.config"}, + {copy, "files/vm.args", "etc/vm.args"} + ]}.