aboutsummaryrefslogtreecommitdiffstats
path: root/src/rcl_prv_assembler.erl
diff options
context:
space:
mode:
authorTristan Sloughter <[email protected]>2013-03-23 13:22:47 -0500
committerTristan Sloughter <[email protected]>2013-03-23 13:22:47 -0500
commite00d3d31a2da2b56b2a34a3b55c89c543f199f70 (patch)
treebee05d3026bda7079443b35be7399a9d0b214b78 /src/rcl_prv_assembler.erl
parent991ad4d7129f4ce941ea528daa9d329231ea829c (diff)
downloadrelx-e00d3d31a2da2b56b2a34a3b55c89c543f199f70.tar.gz
relx-e00d3d31a2da2b56b2a34a3b55c89c543f199f70.tar.bz2
relx-e00d3d31a2da2b56b2a34a3b55c89c543f199f70.zip
add extended start script
Diffstat (limited to 'src/rcl_prv_assembler.erl')
-rw-r--r--src/rcl_prv_assembler.erl481
1 files changed, 477 insertions, 4 deletions
diff --git a/src/rcl_prv_assembler.erl b/src/rcl_prv_assembler.erl
index 1559c2d..1709b3b 100644
--- a/src/rcl_prv_assembler.erl
+++ b/src/rcl_prv_assembler.erl
@@ -218,9 +218,14 @@ write_bin_file(State, Release, OutputDir, RelDir) ->
VsnRel = filename:join(BinDir, RelName ++ "-" ++ RelVsn),
BareRel = filename:join(BinDir, RelName),
ErlOpts = rcl_state:get(State, erl_opts, ""),
- StartFile = bin_file_contents(RelName, RelVsn,
- rcl_release:erts(Release),
- ErlOpts),
+ StartFile = case rcl_state:get(State, extended_start_script, false) of
+ false ->
+ bin_file_contents(RelName, RelVsn,
+ rcl_release:erts(Release),
+ ErlOpts);
+ true ->
+ extended_bin_file_contents(RelName, RelVsn, rcl_release:erts(Release), ErlOpts)
+ end,
%% We generate the start script by default, unless the user
%% tells us not too
case rcl_state:get(State, generate_start_script, true) of
@@ -232,6 +237,7 @@ write_bin_file(State, Release, OutputDir, RelDir) ->
ok = file:write_file(BareRel, StartFile),
ok = file:change_mode(BareRel, 8#777)
end,
+ ok = file:write_file(filename:join(RelDir, "vm.args"), vm_args_file(RelName)),
copy_or_generate_sys_config_file(State, Release, OutputDir, RelDir).
%% @doc copy config/sys.config or generate one to releases/VSN/sys.config
@@ -289,7 +295,11 @@ include_erts(State, Release, OutputDir, RelDir) ->
case rcl_state:get(State, extended_start_script, false) of
true ->
ok = ec_file:copy(filename:join([Prefix, "bin", "start_clean.boot"]),
- filename:join([OutputDir, "bin", "start_clean.boot"]));
+ filename:join([OutputDir, "bin", "start_clean.boot"])),
+ NodeToolFile = nodetool_contents(),
+ NodeTool = filename:join([LocalErts, "bin", "nodetool"]),
+ ok = file:write_file(NodeTool, NodeToolFile),
+ ok = file:change_mode(NodeTool, 8#777);
false ->
ok
end,
@@ -414,3 +424,466 @@ export LD_LIBRARY_PATH=$ERTS_DIR/lib
cd $ROOTDIR
$BINDIR/erlexec $ERL_OPTS $SYS_CONFIG -boot $REL_DIR/$REL_NAME $@">>].
+
+extended_bin_file_contents(RelName, RelVsn, ErtsVsn, ErlOpts) ->
+ [<<"#!/bin/sh
+
+set -e
+
+SCRIPT_DIR=`dirname $0`
+RELEASE_ROOT_DIR=`cd $SCRIPT_DIR/.. && pwd`
+REL_NAME=">>, RelName, <<"
+REL_VSN=">>, RelVsn, <<"
+ERTS_VSN=">>, ErtsVsn, <<"
+REL_DIR=$RELEASE_ROOT_DIR/releases/$REL_NAME-$REL_VSN
+ERL_OPTS=">>, ErlOpts, <<"
+PIPE_DIR=/tmp/$REL_DIR/
+
+find_erts_dir() {
+ local erts_dir=$RELEASE_ROOT_DIR/erts-$ERTS_VSN
+ if [ -d \"$erts_dir\" ]; then
+ ERTS_DIR=$erts_dir;
+ ROOTDIR=$RELEASE_ROOT_DIR
+ else
+ local erl=`which erl`
+ local erl_root=`$erl -noshell -eval \"io:format(\\\"~s\\\", [code:root_dir()]).\" -s init stop`
+ ERTS_DIR=$erl_root/erts-$ERTS_VSN
+ ROOTDIR=$erl_root
+ fi
+
+}
+
+find_sys_config() {
+ local possible_sys=$REL_DIR/sys.config
+ if [ -f \"$possible_sys\" ]; then
+ SYS_CONFIG=\"-config $possible_sys\"
+ fi
+}
+
+# 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=$REL_DIR
+ if [ -e \"$REL_DIR/vm.args\" ]; then
+ VMARGS_PATH=\"$REL_DIR/vm.args\"
+ else
+ VMARGS_PATH=\"$REL_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 \"$REL_DIR/sys.config\" ]; then
+ CONFIG_PATH=\"$REL_DIR/sys.config\"
+ else
+ CONFIG_PATH=\"$REL_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
+
+# Setup remote shell command to control node
+REMSH=\"$ERTS_PATH/erl $REMSH_NAME_ARG $REMSH_REMSH_ARG $COOKIE_ARG\"
+
+find_erts_dir
+find_sys_config
+export ROOTDIR=$RELEASE_ROOT_DIR
+export BINDIR=$ERTS_DIR/bin
+export EMU=beam
+export PROGNAME=erl
+export LD_LIBRARY_PATH=$ERTS_DIR/lib
+
+cd $ROOTDIR
+
+# Setup command to control the node
+NODETOOL=\"$ERTS_DIR/bin/escript $ERTS_DIR/bin/nodetool $NAME_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=\"$SCRIPT_DIR/bin/$REL_NAME $HEART_OPTION $RUN_PARAM\"
+ export HEART_COMMAND
+ mkdir -p $PIPE_DIR
+ $ERTS_DIR/bin/run_erl -daemon $PIPE_DIR $RUNNER_LOG_DIR \"exec $RELEASE_ROOT_DIR/bin/$REL_NAME $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 \"$SCRIPT_DIR/.*/[b]eam\"|awk '{print $1}'`
+ ;;
+ SunOS)
+ # PID COMMAND
+ PID=`ps -ef -o pid= -o args=|
+ grep \"$SCRIPT_DIR/.*/[b]eam\"|awk '{print $1}'`
+ ;;
+ CYGWIN*)
+ # UID PID PPID TTY STIME COMMAND
+ PID=`ps -efW|grep \"$SCRIPT_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_DIR/bin/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: $REL_NAME 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_DIR/bin/escript $SCRIPT_DIR/bin/install_upgrade.escript $node_name $erlang_cookie $2
+ ;;
+
+ console|console_clean|console_boot)
+ # .boot file typically just $REL_NAME (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=$REL_NAME ;;
+ console_clean) BOOTFILE=start_clean ;;
+ console_boot)
+ shift
+ BOOTFILE=\"$1\"
+ shift
+ ;;
+ esac
+ # Setup beam-required vars
+ ROOTDIR=$RELEASE_ROOT_DIR
+ BINDIR=$RELEASE_ROOT_DIR/erts-$ERTS_VSN/bin
+ EMU=beam
+ PROGNAME=`echo $0 | sed 's/.*\\///'`
+ CMD=\"$BINDIR/erlexec -boot $REL_DIR/$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 \"$REL_NAME[$$]\" \"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=$REL_NAME
+ FOREGROUNDOPTIONS=\"-noinput +Bd\"
+
+ # Setup beam-required vars
+ ROOTDIR=$RELEASE_ROOT_DIR
+ BINDIR=$RELEASE_ROOT_DIR/erts-$ERTS_VSN/bin
+ EMU=beam
+ PROGNAME=`echo $0 | sed 's/.*///'`
+ CMD=\"$BINDIR/erlexec $FOREGROUNDOPTIONS -boot $REL_DIR/$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: $REL_NAME {start|start_boot <file>|foreground|stop|restart|reboot|ping|console|console_clean|console_boot <file>|attach|remote_console|upgrade}\"
+ exit 1
+ ;;
+esac
+
+exit 0">>].
+
+nodetool_contents() ->
+ [<<"%% -*- 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.">>].
+
+vm_args_file(RelName) ->
+ [<<"## Name of the node
+-name ">>, RelName, <<"@127.0.0.1
+
+## Cookie for distributed erlang
+-setcookie ">>, RelName, <<"
+
+## Heartbeat management; auto-restarts VM if it dies or becomes unresponsive
+## (Disabled by default..use with caution!)
+##-heart
+
+## Enable kernel poll and a few async threads
+##+K true
+##+A 5
+
+## Increase number of concurrent ports/sockets
+##-env ERL_MAX_PORTS 4096
+
+## Tweak GC to run more often
+##-env ERL_FULLSWEEP_AFTER 10">>].