From f2fe3631a093f5d45aeef129ead37562ded9ec9c Mon Sep 17 00:00:00 2001 From: nuex Date: Fri, 21 Feb 2014 15:16:26 -0500 Subject: load scripts from files and templates --- src/rlx_prv_assembler.erl | 644 ++-------------------------------------------- 1 file changed, 25 insertions(+), 619 deletions(-) diff --git a/src/rlx_prv_assembler.erl b/src/rlx_prv_assembler.erl index 33be3e0..255e0da 100644 --- a/src/rlx_prv_assembler.erl +++ b/src/rlx_prv_assembler.erl @@ -646,637 +646,43 @@ ensure_not_exist(RelConfPath) -> end. erl_script(ErtsVsn) -> - [<<"#!/bin/sh -set -e - -SCRIPT_DIR=`dirname $0` -ROOTDIR=`cd $SCRIPT_DIR/../../ && pwd` -BINDIR=$ROOTDIR/erts-">>, ErtsVsn, <<"/bin -EMU=beam -PROGNAME=`echo $0 | sed 's/.*\\///'` -export EMU -export ROOTDIR -export BINDIR -export PROGNAME -exec \"$BINDIR/erlexec\" ${1+\"$@\"} -">>]. - + render(erl_script, [{erts_vsn, ErtsVsn}]). + 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_VSN -ERL_OPTS=">>, ErlOpts, <<" - -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 -} - -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:$LD_LIBRARY_PATH - -cd $ROOTDIR - -$BINDIR/erlexec $ERL_OPTS $SYS_CONFIG -boot $REL_DIR/$REL_NAME $@">>]. + render(bin, [{rel_name, RelName}, {rel_vsn, RelVsn}, {erts_vsn, ErtsVsn}, + {erl_opts, ErlOpts}]). 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_VSN -ERL_OPTS=">>, ErlOpts, <<" -PIPE_DIR=/tmp/erl_pipes/">>, RelName, <<"/ - -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 \"$RELEASE_ROOT_DIR/vm.args\" ]; then - VMARGS_PATH=$RELEASE_ROOT_DIR/vm.args - USE_DIR=$RELEASE_ROOT_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=$RELEASE_ROOT_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 -boot start_clean\" - -# 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 - -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:$LD_LIBRARY_PATH - -cd $ROOTDIR - -# Setup remote shell command to control node -REMSH=\"$BINDIR/erl $REMSH_NAME_ARG $REMSH_REMSH_ARG $COOKIE_ARG\" - -# Setup command to control the node -NODETOOL=\"$BINDIR/escript $ROOTDIR/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 - $BINDIR/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 $BINDIR/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|downgrade|install) - if [ -z \"$2\" ]; then - echo \"Missing package argument\" - echo \"Usage: $REL_NAME $1 {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}'` - - exec $BINDIR/escript $ROOTDIR/bin/install_upgrade.escript $REL_NAME $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) [ -f $REL_DIR/$REL_NAME.boot ] && BOOTFILE=$REL_DIR/$REL_NAME || BOOTFILE=$REL_DIR/start ;; - console_clean) BOOTFILE=$ROOTDIR/bin/start_clean ;; - console_boot) - shift - BOOTFILE=\"$1\" - shift - ;; - esac - # Setup beam-required vars - EMU=beam - PROGNAME=`echo $0 | sed 's/.*\\///'` - CMD=\"$BINDIR/erlexec -boot $BOOTFILE -env ERL_LIBS $REL_DIR/lib -config $CONFIG_PATH -args_file $VMARGS_PATH\" - export EMU - 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 - - [ -f $REL_DIR/$REL_NAME.boot ] && BOOTFILE=$REL_NAME || BOOTFILE=start - FOREGROUNDOPTIONS=\"-noinput +Bd\" - - # Setup beam-required vars - EMU=beam - PROGNAME=`echo $0 | sed 's/.*\\///'` - CMD=\"$BINDIR/erlexec $FOREGROUNDOPTIONS -boot $REL_DIR/$BOOTFILE -mode embedded -config $CONFIG_PATH -args_file $VMARGS_PATH\" - export EMU - 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 |foreground|stop|restart|reboot|ping|console|console_clean|console_boot |attach|remote_console|upgrade}\" - exit 1 - ;; -esac - -exit 0">>]. + render(extended_bin, [{rel_name, RelName}, {rel_vsn, RelVsn}, + {erts_vsn, ErtsVsn}, {erl_opts, ErlOpts}]). install_upgrade_escript_contents() -> - [<<"#!/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)). - -%% Upgrades, to a new tar.gz release -main([RelName, NodeName, Cookie, VersionArg]) -> - TargetNode = start_distribution(NodeName, Cookie), - WhichReleases = which_releases(TargetNode), - Version = parse_version(VersionArg), - case proplists:get_value(Version, WhichReleases) of - undefined -> - %% not installed, so unpack tarball: - ?INFO(\"Release ~s not found, attempting to unpack releases/~s/~s.tar.gz~n\",[Version,Version,RelName]), - ReleasePackage = Version ++ \"/\" ++ RelName, - case rpc:call(TargetNode, release_handler, unpack_release, - [ReleasePackage], ?TIMEOUT) of - {ok, Vsn} -> - ?INFO(\"Unpacked successfully: ~p~n\", [Vsn]), - install_and_permafy(TargetNode, RelName, Vsn); - {error, UnpackReason} -> - print_existing_versions(TargetNode), - ?INFO(\"Unpack failed: ~p~n\",[UnpackReason]), - erlang:halt(2) - end; - old -> - %% no need to unpack, has been installed previously - ?INFO(\"Release ~s is marked old, switching to it.~n\",[Version]), - install_and_permafy(TargetNode, RelName, Version); - unpacked -> - ?INFO(\"Release ~s is already unpacked, now installing.~n\",[Version]), - install_and_permafy(TargetNode, RelName, Version); - current -> %% installed and in-use, just needs to be permanent - ?INFO(\"Release ~s is already installed and current. Making permanent.~n\",[Version]), - permafy(TargetNode, RelName, Version); - permanent -> - ?INFO(\"Release ~s is already installed, and set permanent.~n\",[Version]) - end; -main(_) -> - erlang:halt(1). - -parse_version(V) when is_list(V) -> - hd(string:tokens(V,\"/\")). - -install_and_permafy(TargetNode, RelName, Vsn) -> - case rpc:call(TargetNode, release_handler, check_install_release, [Vsn], ?TIMEOUT) of - {ok, _OtherVsn, _Desc} -> - ok; - {error, Reason} -> - ?INFO(\"ERROR: release_handler:check_install_release failed: ~p~n\",[Reason]), - erlang:halt(3) - end, - case rpc:call(TargetNode, release_handler, install_release, [Vsn], ?TIMEOUT) of - {ok, _, _} -> - ?INFO(\"Installed Release: ~s~n\", [Vsn]), - permafy(TargetNode, RelName, Vsn), - ok; - {error, {no_such_release, Vsn}} -> - VerList = - iolist_to_binary( - [io_lib:format(\"* ~s\t~s~n\",[V,S]) || {V,S} <- which_releases(TargetNode)]), - ?INFO(\"Installed versions:~n~s\", [VerList]), - ?INFO(\"ERROR: Unable to revert to '~s' - not installed.~n\", [Vsn]), - erlang:halt(2) - end. - -permafy(TargetNode, RelName, Vsn) -> - ok = rpc:call(TargetNode, release_handler, make_permanent, [Vsn], ?TIMEOUT), - file:copy(filename:join([\"bin\", RelName++\"-\"++Vsn]), - filename:join([\"bin\", RelName])), - ?INFO(\"Made release permanent: ~p~n\", [Vsn]), - ok. - -which_releases(TargetNode) -> - R = rpc:call(TargetNode, release_handler, which_releases, [], ?TIMEOUT), - [ {V, S} || {_,V,_, S} <- R ]. - -print_existing_versions(TargetNode) -> - VerList = iolist_to_binary([ - io_lib:format(\"* ~s\t~s~n\",[V,S]) - || {V,S} <- which_releases(TargetNode) ]), - ?INFO(\"Installed versions:~n~s\", [VerList]). - -start_distribution(NodeName, Cookie) -> - MyNode = make_script_node(NodeName), - {ok, _Pid} = net_kernel:start([MyNode, longnames]), - erlang:set_cookie(node(), list_to_atom(Cookie)), - TargetNode = list_to_atom(NodeName), - case {net_kernel:connect_node(TargetNode), - net_adm:ping(TargetNode)} of - {true, pong} -> - ok; - {_, pang} -> - io:format(\"Node ~p not responding to pings.\n\", [TargetNode]), - erlang:halt(1) - end, - {ok, Cwd} = file:get_cwd(), - ok = rpc:call(TargetNode, file, set_cwd, [Cwd], ?TIMEOUT), - TargetNode. - -make_script_node(Node) -> - [Name, Host] = string:tokens(Node, \"@\"), - list_to_atom(lists:concat([Name, \"_upgrader_\", os:getpid(), \"@\", Host])). -">>]. + read_file("install_upgrade_escript"). 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.">>]. + read_file("nodetool"). sys_config_file() -> - [<<"%% Thanks to Ulf Wiger at Ericcson for these comments: -%% -%% This file is identified via the erl command line option -config File. -%% Note that File should have no extension, e.g. -%% erl -config .../sys (if this file is called sys.config) -%% -%% In this file, you can redefine application environment variables. -%% This way, you don't have to modify the .app files of e.g. OTP applications. -[].">>]. + read_file("sys_config"). vm_args_file(RelName) -> - [<<"## Name of the node --name ">>, RelName, <<"@127.0.0.1 - -## Cookie for distributed erlang --setcookie ">>, RelName, <<" + render(vm_args, [{rel_name, RelName}]). -## Heartbeat management; auto-restarts VM if it dies or becomes unresponsive -## (Disabled by default..use with caution!) -##-heart +render(TemplateName, Data) -> + TemplatePath = template_path(TemplateName), + {ok, Rendered} = case erlydtl:compile(TemplatePath, TemplateName) of + ok -> TemplateName:render(Data); + {ok, Template} -> Template:render(Data) + end, + Rendered. -## Enable kernel poll and a few async threads -##+K true -##+A 5 +template_path(TemplateName) -> + BaseName = string:join([erlang:atom_to_list(TemplateName), ".dtl"]), + filename:join([code:priv_dir(relx), "templates", BaseName]). -## Increase number of concurrent ports/sockets -##-env ERL_MAX_PORTS 4096 +read_file(FileName) -> + {ok, Contents} = file:read_file(file_path(FileName)), + Contents. -## Tweak GC to run more often -##-env ERL_FULLSWEEP_AFTER 10">>]. +file_path(FileName) -> + filename:join([code:priv_dir(relx), "files", FileName]). -- cgit v1.2.3 From 7703e99c8ef26988242907ef3253470317330c8c Mon Sep 17 00:00:00 2001 From: nuex Date: Fri, 21 Feb 2014 15:16:39 -0500 Subject: add templates --- priv/templates/bin.dtl | 44 ++++++ priv/templates/erl_script.dtl | 13 ++ priv/templates/extended_bin.dtl | 292 ++++++++++++++++++++++++++++++++++++++++ priv/templates/vm_args.dtl | 19 +++ 4 files changed, 368 insertions(+) create mode 100644 priv/templates/bin.dtl create mode 100644 priv/templates/erl_script.dtl create mode 100644 priv/templates/extended_bin.dtl create mode 100644 priv/templates/vm_args.dtl diff --git a/priv/templates/bin.dtl b/priv/templates/bin.dtl new file mode 100644 index 0000000..9e0dfb6 --- /dev/null +++ b/priv/templates/bin.dtl @@ -0,0 +1,44 @@ +#!/bin/sh + +set -e + +SCRIPT_DIR=`dirname $0` +RELEASE_ROOT_DIR=`cd $SCRIPT_DIR/.. && pwd` +REL_NAME={{ rel_name }} +REL_VSN={{ rel_vsn }} +ERTS_VSN={{ erts_vsn }} +REL_DIR=$RELEASE_ROOT_DIR/releases/$REL_VSN +ERL_OPTS={{ erl_opts }} + +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 +} + +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 + +$BINDIR/erlexec $ERL_OPTS $SYS_CONFIG -boot $REL_DIR/$REL_NAME $@ diff --git a/priv/templates/erl_script.dtl b/priv/templates/erl_script.dtl new file mode 100644 index 0000000..261eb6a --- /dev/null +++ b/priv/templates/erl_script.dtl @@ -0,0 +1,13 @@ +#!/bin/sh +set -e + +SCRIPT_DIR=`dirname $0` +ROOTDIR=`cd $SCRIPT_DIR/../../ && pwd` +BINDIR=$ROOTDIR/erts-{{ erts_vsn }}/bin +EMU=beam +PROGNAME=`echo $0 | sed 's/.*\\///'` +export EMU +export ROOTDIR +export BINDIR +export PROGNAME +exec \"$BINDIR/erlexec\" ${1+\"$@\"} diff --git a/priv/templates/extended_bin.dtl b/priv/templates/extended_bin.dtl new file mode 100644 index 0000000..13200ed --- /dev/null +++ b/priv/templates/extended_bin.dtl @@ -0,0 +1,292 @@ +#!/bin/sh + +set -e + +SCRIPT_DIR=`dirname $0` +RELEASE_ROOT_DIR=`cd $SCRIPT_DIR/.. && pwd` +REL_NAME={{ rel_nam }} +REL_VSN={{ rel_vsn }} +ERTS_VSN={{ erts_vsn }} +REL_DIR=$RELEASE_ROOT_DIR/releases/$REL_VSN +ERL_OPTS={{ erl_opts }} +PIPE_DIR=/tmp/erl_pipes/{{ rel_name }}/ + +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 \"$RELEASE_ROOT_DIR/vm.args\" ]; then + VMARGS_PATH=$RELEASE_ROOT_DIR/vm.args + USE_DIR=$RELEASE_ROOT_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=$RELEASE_ROOT_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 -boot start_clean\" + +# 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 + +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 remote shell command to control node +REMSH=\"$BINDIR/erl $REMSH_NAME_ARG $REMSH_REMSH_ARG $COOKIE_ARG\" + +# Setup command to control the node +NODETOOL=\"$BINDIR/escript $ROOTDIR/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 + $BINDIR/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 $BINDIR/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|downgrade|install) + if [ -z \"$2\" ]; then + echo \"Missing package argument\" + echo \"Usage: $REL_NAME $1 {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}'` + + exec $BINDIR/escript $ROOTDIR/bin/install_upgrade.escript $REL_NAME $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) [ -f $REL_DIR/$REL_NAME.boot ] && BOOTFILE=$REL_DIR/$REL_NAME || BOOTFILE=$REL_DIR/start ;; + console_clean) BOOTFILE=$ROOTDIR/bin/start_clean ;; + console_boot) + shift + BOOTFILE=\"$1\" + shift + ;; + esac + # Setup beam-required vars + EMU=beam + PROGNAME=`echo $0 | sed 's/.*\\///'` + CMD=\"$BINDIR/erlexec -boot $BOOTFILE -env ERL_LIBS $REL_DIR/lib -config $CONFIG_PATH -args_file $VMARGS_PATH\" + export EMU + 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 + + [ -f $REL_DIR/$REL_NAME.boot ] && BOOTFILE=$REL_NAME || BOOTFILE=start + FOREGROUNDOPTIONS=\"-noinput +Bd\" + + # Setup beam-required vars + EMU=beam + PROGNAME=`echo $0 | sed 's/.*\\///'` + CMD=\"$BINDIR/erlexec $FOREGROUNDOPTIONS -boot $REL_DIR/$BOOTFILE -mode embedded -config $CONFIG_PATH -args_file $VMARGS_PATH\" + export EMU + 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 |foreground|stop|restart|reboot|ping|console|console_clean|console_boot |attach|remote_console|upgrade}\" + exit 1 + ;; +esac + +exit 0 diff --git a/priv/templates/vm_args.dtl b/priv/templates/vm_args.dtl new file mode 100644 index 0000000..558ce39 --- /dev/null +++ b/priv/templates/vm_args.dtl @@ -0,0 +1,19 @@ +## Name of the node +-name {{ rel_name }}@127.0.0.1 + +## Cookie for distributed erlang +-setcookie {{ rel_name }} + +## 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 -- cgit v1.2.3 From a9e42ed88f85bab78a36c84061b043c0d2916bad Mon Sep 17 00:00:00 2001 From: nuex Date: Fri, 21 Feb 2014 15:16:46 -0500 Subject: add files --- priv/files/install_upgrade_escript | 106 ++++++++++++++++++++++++++++ priv/files/nodetool | 137 +++++++++++++++++++++++++++++++++++++ priv/files/sys_config | 9 +++ 3 files changed, 252 insertions(+) create mode 100644 priv/files/install_upgrade_escript create mode 100644 priv/files/nodetool create mode 100644 priv/files/sys_config diff --git a/priv/files/install_upgrade_escript b/priv/files/install_upgrade_escript new file mode 100644 index 0000000..6dcddb3 --- /dev/null +++ b/priv/files/install_upgrade_escript @@ -0,0 +1,106 @@ +#!/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)). + +%% Upgrades, to a new tar.gz release +main([RelName, NodeName, Cookie, VersionArg]) -> + TargetNode = start_distribution(NodeName, Cookie), + WhichReleases = which_releases(TargetNode), + Version = parse_version(VersionArg), + case proplists:get_value(Version, WhichReleases) of + undefined -> + %% not installed, so unpack tarball: + ?INFO(\"Release ~s not found, attempting to unpack releases/~s/~s.tar.gz~n\",[Version,Version,RelName]), + ReleasePackage = Version ++ \"/\" ++ RelName, + case rpc:call(TargetNode, release_handler, unpack_release, + [ReleasePackage], ?TIMEOUT) of + {ok, Vsn} -> + ?INFO(\"Unpacked successfully: ~p~n\", [Vsn]), + install_and_permafy(TargetNode, RelName, Vsn); + {error, UnpackReason} -> + print_existing_versions(TargetNode), + ?INFO(\"Unpack failed: ~p~n\",[UnpackReason]), + erlang:halt(2) + end; + old -> + %% no need to unpack, has been installed previously + ?INFO(\"Release ~s is marked old, switching to it.~n\",[Version]), + install_and_permafy(TargetNode, RelName, Version); + unpacked -> + ?INFO(\"Release ~s is already unpacked, now installing.~n\",[Version]), + install_and_permafy(TargetNode, RelName, Version); + current -> %% installed and in-use, just needs to be permanent + ?INFO(\"Release ~s is already installed and current. Making permanent.~n\",[Version]), + permafy(TargetNode, RelName, Version); + permanent -> + ?INFO(\"Release ~s is already installed, and set permanent.~n\",[Version]) + end; +main(_) -> + erlang:halt(1). + +parse_version(V) when is_list(V) -> + hd(string:tokens(V,\"/\")). + +install_and_permafy(TargetNode, RelName, Vsn) -> + case rpc:call(TargetNode, release_handler, check_install_release, [Vsn], ?TIMEOUT) of + {ok, _OtherVsn, _Desc} -> + ok; + {error, Reason} -> + ?INFO(\"ERROR: release_handler:check_install_release failed: ~p~n\",[Reason]), + erlang:halt(3) + end, + case rpc:call(TargetNode, release_handler, install_release, [Vsn], ?TIMEOUT) of + {ok, _, _} -> + ?INFO(\"Installed Release: ~s~n\", [Vsn]), + permafy(TargetNode, RelName, Vsn), + ok; + {error, {no_such_release, Vsn}} -> + VerList = + iolist_to_binary( + [io_lib:format(\"* ~s\t~s~n\",[V,S]) || {V,S} <- which_releases(TargetNode)]), + ?INFO(\"Installed versions:~n~s\", [VerList]), + ?INFO(\"ERROR: Unable to revert to '~s' - not installed.~n\", [Vsn]), + erlang:halt(2) + end. + +permafy(TargetNode, RelName, Vsn) -> + ok = rpc:call(TargetNode, release_handler, make_permanent, [Vsn], ?TIMEOUT), + file:copy(filename:join([\"bin\", RelName++\"-\"++Vsn]), + filename:join([\"bin\", RelName])), + ?INFO(\"Made release permanent: ~p~n\", [Vsn]), + ok. + +which_releases(TargetNode) -> + R = rpc:call(TargetNode, release_handler, which_releases, [], ?TIMEOUT), + [ {V, S} || {_,V,_, S} <- R ]. + +print_existing_versions(TargetNode) -> + VerList = iolist_to_binary([ + io_lib:format(\"* ~s\t~s~n\",[V,S]) + || {V,S} <- which_releases(TargetNode) ]), + ?INFO(\"Installed versions:~n~s\", [VerList]). + +start_distribution(NodeName, Cookie) -> + MyNode = make_script_node(NodeName), + {ok, _Pid} = net_kernel:start([MyNode, longnames]), + erlang:set_cookie(node(), list_to_atom(Cookie)), + TargetNode = list_to_atom(NodeName), + case {net_kernel:connect_node(TargetNode), + net_adm:ping(TargetNode)} of + {true, pong} -> + ok; + {_, pang} -> + io:format(\"Node ~p not responding to pings.\n\", [TargetNode]), + erlang:halt(1) + end, + {ok, Cwd} = file:get_cwd(), + ok = rpc:call(TargetNode, file, set_cwd, [Cwd], ?TIMEOUT), + TargetNode. + +make_script_node(Node) -> + [Name, Host] = string:tokens(Node, \"@\"), + list_to_atom(lists:concat([Name, \"_upgrader_\", os:getpid(), \"@\", Host])). diff --git a/priv/files/nodetool b/priv/files/nodetool new file mode 100644 index 0000000..c98e544 --- /dev/null +++ b/priv/files/nodetool @@ -0,0 +1,137 @@ +%% -*- 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/priv/files/sys_config b/priv/files/sys_config new file mode 100644 index 0000000..eda2e75 --- /dev/null +++ b/priv/files/sys_config @@ -0,0 +1,9 @@ +%% Thanks to Ulf Wiger at Ericcson for these comments: +%% +%% This file is identified via the erl command line option -config File. +%% Note that File should have no extension, e.g. +%% erl -config .../sys (if this file is called sys.config) +%% +%% In this file, you can redefine application environment variables. +%% This way, you don't have to modify the .app files of e.g. OTP applications. +[]. -- cgit v1.2.3 From 953ddba4b8484e29a97c78775dddf281174c5bb2 Mon Sep 17 00:00:00 2001 From: nuex Date: Fri, 21 Feb 2014 16:25:29 -0500 Subject: set doc_root on dtl templates --- rebar.config | 1 + 1 file changed, 1 insertion(+) diff --git a/rebar.config b/rebar.config index 858add2..3c26745 100644 --- a/rebar.config +++ b/rebar.config @@ -29,6 +29,7 @@ {cover_print_enabled, true}. %% Misc ======================================================================= +{erlydtl_opts, [{doc_root, "priv/templates"}]}. {escript_incl_apps, [getopt, erlware_commons, erlydtl]}. -- cgit v1.2.3 From adeccbc3c31a1e647c5bb42c6ccf4a7823265755 Mon Sep 17 00:00:00 2001 From: nuex Date: Fri, 21 Feb 2014 16:53:48 -0500 Subject: add rebar_vsn_plugin directory to rebar.config --- rebar.config | 1 + 1 file changed, 1 insertion(+) diff --git a/rebar.config b/rebar.config index 3c26745..4eef17e 100644 --- a/rebar.config +++ b/rebar.config @@ -16,6 +16,7 @@ {branch, "master"}}}]}. %% Rebar Plugins ============================================================== +{plugin_dir, "deps/rebar_vsn_plugin/src"}. {plugins, [rebar_vsn_plugin]}. %% Compiler Options ============================================================ -- cgit v1.2.3 From 50744eff1c429ecd019511090e5dfdec55bcc503 Mon Sep 17 00:00:00 2001 From: nuex Date: Sat, 22 Feb 2014 02:38:41 -0500 Subject: just compile regular files as template modules --- priv/files/install_upgrade_escript | 106 ---------------------- priv/files/nodetool | 137 ----------------------------- priv/files/sys_config | 9 -- priv/templates/install_upgrade_escript.dtl | 106 ++++++++++++++++++++++ priv/templates/nodetool.dtl | 137 +++++++++++++++++++++++++++++ priv/templates/sys_config.dtl | 9 ++ 6 files changed, 252 insertions(+), 252 deletions(-) delete mode 100644 priv/files/install_upgrade_escript delete mode 100644 priv/files/nodetool delete mode 100644 priv/files/sys_config create mode 100644 priv/templates/install_upgrade_escript.dtl create mode 100644 priv/templates/nodetool.dtl create mode 100644 priv/templates/sys_config.dtl diff --git a/priv/files/install_upgrade_escript b/priv/files/install_upgrade_escript deleted file mode 100644 index 6dcddb3..0000000 --- a/priv/files/install_upgrade_escript +++ /dev/null @@ -1,106 +0,0 @@ -#!/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)). - -%% Upgrades, to a new tar.gz release -main([RelName, NodeName, Cookie, VersionArg]) -> - TargetNode = start_distribution(NodeName, Cookie), - WhichReleases = which_releases(TargetNode), - Version = parse_version(VersionArg), - case proplists:get_value(Version, WhichReleases) of - undefined -> - %% not installed, so unpack tarball: - ?INFO(\"Release ~s not found, attempting to unpack releases/~s/~s.tar.gz~n\",[Version,Version,RelName]), - ReleasePackage = Version ++ \"/\" ++ RelName, - case rpc:call(TargetNode, release_handler, unpack_release, - [ReleasePackage], ?TIMEOUT) of - {ok, Vsn} -> - ?INFO(\"Unpacked successfully: ~p~n\", [Vsn]), - install_and_permafy(TargetNode, RelName, Vsn); - {error, UnpackReason} -> - print_existing_versions(TargetNode), - ?INFO(\"Unpack failed: ~p~n\",[UnpackReason]), - erlang:halt(2) - end; - old -> - %% no need to unpack, has been installed previously - ?INFO(\"Release ~s is marked old, switching to it.~n\",[Version]), - install_and_permafy(TargetNode, RelName, Version); - unpacked -> - ?INFO(\"Release ~s is already unpacked, now installing.~n\",[Version]), - install_and_permafy(TargetNode, RelName, Version); - current -> %% installed and in-use, just needs to be permanent - ?INFO(\"Release ~s is already installed and current. Making permanent.~n\",[Version]), - permafy(TargetNode, RelName, Version); - permanent -> - ?INFO(\"Release ~s is already installed, and set permanent.~n\",[Version]) - end; -main(_) -> - erlang:halt(1). - -parse_version(V) when is_list(V) -> - hd(string:tokens(V,\"/\")). - -install_and_permafy(TargetNode, RelName, Vsn) -> - case rpc:call(TargetNode, release_handler, check_install_release, [Vsn], ?TIMEOUT) of - {ok, _OtherVsn, _Desc} -> - ok; - {error, Reason} -> - ?INFO(\"ERROR: release_handler:check_install_release failed: ~p~n\",[Reason]), - erlang:halt(3) - end, - case rpc:call(TargetNode, release_handler, install_release, [Vsn], ?TIMEOUT) of - {ok, _, _} -> - ?INFO(\"Installed Release: ~s~n\", [Vsn]), - permafy(TargetNode, RelName, Vsn), - ok; - {error, {no_such_release, Vsn}} -> - VerList = - iolist_to_binary( - [io_lib:format(\"* ~s\t~s~n\",[V,S]) || {V,S} <- which_releases(TargetNode)]), - ?INFO(\"Installed versions:~n~s\", [VerList]), - ?INFO(\"ERROR: Unable to revert to '~s' - not installed.~n\", [Vsn]), - erlang:halt(2) - end. - -permafy(TargetNode, RelName, Vsn) -> - ok = rpc:call(TargetNode, release_handler, make_permanent, [Vsn], ?TIMEOUT), - file:copy(filename:join([\"bin\", RelName++\"-\"++Vsn]), - filename:join([\"bin\", RelName])), - ?INFO(\"Made release permanent: ~p~n\", [Vsn]), - ok. - -which_releases(TargetNode) -> - R = rpc:call(TargetNode, release_handler, which_releases, [], ?TIMEOUT), - [ {V, S} || {_,V,_, S} <- R ]. - -print_existing_versions(TargetNode) -> - VerList = iolist_to_binary([ - io_lib:format(\"* ~s\t~s~n\",[V,S]) - || {V,S} <- which_releases(TargetNode) ]), - ?INFO(\"Installed versions:~n~s\", [VerList]). - -start_distribution(NodeName, Cookie) -> - MyNode = make_script_node(NodeName), - {ok, _Pid} = net_kernel:start([MyNode, longnames]), - erlang:set_cookie(node(), list_to_atom(Cookie)), - TargetNode = list_to_atom(NodeName), - case {net_kernel:connect_node(TargetNode), - net_adm:ping(TargetNode)} of - {true, pong} -> - ok; - {_, pang} -> - io:format(\"Node ~p not responding to pings.\n\", [TargetNode]), - erlang:halt(1) - end, - {ok, Cwd} = file:get_cwd(), - ok = rpc:call(TargetNode, file, set_cwd, [Cwd], ?TIMEOUT), - TargetNode. - -make_script_node(Node) -> - [Name, Host] = string:tokens(Node, \"@\"), - list_to_atom(lists:concat([Name, \"_upgrader_\", os:getpid(), \"@\", Host])). diff --git a/priv/files/nodetool b/priv/files/nodetool deleted file mode 100644 index c98e544..0000000 --- a/priv/files/nodetool +++ /dev/null @@ -1,137 +0,0 @@ -%% -*- 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/priv/files/sys_config b/priv/files/sys_config deleted file mode 100644 index eda2e75..0000000 --- a/priv/files/sys_config +++ /dev/null @@ -1,9 +0,0 @@ -%% Thanks to Ulf Wiger at Ericcson for these comments: -%% -%% This file is identified via the erl command line option -config File. -%% Note that File should have no extension, e.g. -%% erl -config .../sys (if this file is called sys.config) -%% -%% In this file, you can redefine application environment variables. -%% This way, you don't have to modify the .app files of e.g. OTP applications. -[]. diff --git a/priv/templates/install_upgrade_escript.dtl b/priv/templates/install_upgrade_escript.dtl new file mode 100644 index 0000000..6dcddb3 --- /dev/null +++ b/priv/templates/install_upgrade_escript.dtl @@ -0,0 +1,106 @@ +#!/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)). + +%% Upgrades, to a new tar.gz release +main([RelName, NodeName, Cookie, VersionArg]) -> + TargetNode = start_distribution(NodeName, Cookie), + WhichReleases = which_releases(TargetNode), + Version = parse_version(VersionArg), + case proplists:get_value(Version, WhichReleases) of + undefined -> + %% not installed, so unpack tarball: + ?INFO(\"Release ~s not found, attempting to unpack releases/~s/~s.tar.gz~n\",[Version,Version,RelName]), + ReleasePackage = Version ++ \"/\" ++ RelName, + case rpc:call(TargetNode, release_handler, unpack_release, + [ReleasePackage], ?TIMEOUT) of + {ok, Vsn} -> + ?INFO(\"Unpacked successfully: ~p~n\", [Vsn]), + install_and_permafy(TargetNode, RelName, Vsn); + {error, UnpackReason} -> + print_existing_versions(TargetNode), + ?INFO(\"Unpack failed: ~p~n\",[UnpackReason]), + erlang:halt(2) + end; + old -> + %% no need to unpack, has been installed previously + ?INFO(\"Release ~s is marked old, switching to it.~n\",[Version]), + install_and_permafy(TargetNode, RelName, Version); + unpacked -> + ?INFO(\"Release ~s is already unpacked, now installing.~n\",[Version]), + install_and_permafy(TargetNode, RelName, Version); + current -> %% installed and in-use, just needs to be permanent + ?INFO(\"Release ~s is already installed and current. Making permanent.~n\",[Version]), + permafy(TargetNode, RelName, Version); + permanent -> + ?INFO(\"Release ~s is already installed, and set permanent.~n\",[Version]) + end; +main(_) -> + erlang:halt(1). + +parse_version(V) when is_list(V) -> + hd(string:tokens(V,\"/\")). + +install_and_permafy(TargetNode, RelName, Vsn) -> + case rpc:call(TargetNode, release_handler, check_install_release, [Vsn], ?TIMEOUT) of + {ok, _OtherVsn, _Desc} -> + ok; + {error, Reason} -> + ?INFO(\"ERROR: release_handler:check_install_release failed: ~p~n\",[Reason]), + erlang:halt(3) + end, + case rpc:call(TargetNode, release_handler, install_release, [Vsn], ?TIMEOUT) of + {ok, _, _} -> + ?INFO(\"Installed Release: ~s~n\", [Vsn]), + permafy(TargetNode, RelName, Vsn), + ok; + {error, {no_such_release, Vsn}} -> + VerList = + iolist_to_binary( + [io_lib:format(\"* ~s\t~s~n\",[V,S]) || {V,S} <- which_releases(TargetNode)]), + ?INFO(\"Installed versions:~n~s\", [VerList]), + ?INFO(\"ERROR: Unable to revert to '~s' - not installed.~n\", [Vsn]), + erlang:halt(2) + end. + +permafy(TargetNode, RelName, Vsn) -> + ok = rpc:call(TargetNode, release_handler, make_permanent, [Vsn], ?TIMEOUT), + file:copy(filename:join([\"bin\", RelName++\"-\"++Vsn]), + filename:join([\"bin\", RelName])), + ?INFO(\"Made release permanent: ~p~n\", [Vsn]), + ok. + +which_releases(TargetNode) -> + R = rpc:call(TargetNode, release_handler, which_releases, [], ?TIMEOUT), + [ {V, S} || {_,V,_, S} <- R ]. + +print_existing_versions(TargetNode) -> + VerList = iolist_to_binary([ + io_lib:format(\"* ~s\t~s~n\",[V,S]) + || {V,S} <- which_releases(TargetNode) ]), + ?INFO(\"Installed versions:~n~s\", [VerList]). + +start_distribution(NodeName, Cookie) -> + MyNode = make_script_node(NodeName), + {ok, _Pid} = net_kernel:start([MyNode, longnames]), + erlang:set_cookie(node(), list_to_atom(Cookie)), + TargetNode = list_to_atom(NodeName), + case {net_kernel:connect_node(TargetNode), + net_adm:ping(TargetNode)} of + {true, pong} -> + ok; + {_, pang} -> + io:format(\"Node ~p not responding to pings.\n\", [TargetNode]), + erlang:halt(1) + end, + {ok, Cwd} = file:get_cwd(), + ok = rpc:call(TargetNode, file, set_cwd, [Cwd], ?TIMEOUT), + TargetNode. + +make_script_node(Node) -> + [Name, Host] = string:tokens(Node, \"@\"), + list_to_atom(lists:concat([Name, \"_upgrader_\", os:getpid(), \"@\", Host])). diff --git a/priv/templates/nodetool.dtl b/priv/templates/nodetool.dtl new file mode 100644 index 0000000..c98e544 --- /dev/null +++ b/priv/templates/nodetool.dtl @@ -0,0 +1,137 @@ +%% -*- 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/priv/templates/sys_config.dtl b/priv/templates/sys_config.dtl new file mode 100644 index 0000000..eda2e75 --- /dev/null +++ b/priv/templates/sys_config.dtl @@ -0,0 +1,9 @@ +%% Thanks to Ulf Wiger at Ericcson for these comments: +%% +%% This file is identified via the erl command line option -config File. +%% Note that File should have no extension, e.g. +%% erl -config .../sys (if this file is called sys.config) +%% +%% In this file, you can redefine application environment variables. +%% This way, you don't have to modify the .app files of e.g. OTP applications. +[]. -- cgit v1.2.3 From 011af017567915c32721a4e4ef1bd58e61ad58d8 Mon Sep 17 00:00:00 2001 From: nuex Date: Sat, 22 Feb 2014 02:39:17 -0500 Subject: fix typo in rel_name tag --- priv/templates/extended_bin.dtl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/priv/templates/extended_bin.dtl b/priv/templates/extended_bin.dtl index 13200ed..39701c0 100644 --- a/priv/templates/extended_bin.dtl +++ b/priv/templates/extended_bin.dtl @@ -4,7 +4,7 @@ set -e SCRIPT_DIR=`dirname $0` RELEASE_ROOT_DIR=`cd $SCRIPT_DIR/.. && pwd` -REL_NAME={{ rel_nam }} +REL_NAME={{ rel_name }} REL_VSN={{ rel_vsn }} ERTS_VSN={{ erts_vsn }} REL_DIR=$RELEASE_ROOT_DIR/releases/$REL_VSN -- cgit v1.2.3 From dbede76ca6c76dd6be1cf22bacee1fe345631909 Mon Sep 17 00:00:00 2001 From: nuex Date: Sat, 22 Feb 2014 02:40:45 -0500 Subject: assume templates are already compiled, use dtl for files too --- src/rlx_prv_assembler.erl | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/src/rlx_prv_assembler.erl b/src/rlx_prv_assembler.erl index 255e0da..1a24ab6 100644 --- a/src/rlx_prv_assembler.erl +++ b/src/rlx_prv_assembler.erl @@ -646,43 +646,31 @@ ensure_not_exist(RelConfPath) -> end. erl_script(ErtsVsn) -> - render(erl_script, [{erts_vsn, ErtsVsn}]). + render(erl_script_dtl, [{erts_vsn, ErtsVsn}]). bin_file_contents(RelName, RelVsn, ErtsVsn, ErlOpts) -> - render(bin, [{rel_name, RelName}, {rel_vsn, RelVsn}, {erts_vsn, ErtsVsn}, - {erl_opts, ErlOpts}]). + render(bin_dtl, [{rel_name, RelName}, {rel_vsn, RelVsn}, + {erts_vsn, ErtsVsn}, {erl_opts, ErlOpts}]). extended_bin_file_contents(RelName, RelVsn, ErtsVsn, ErlOpts) -> - render(extended_bin, [{rel_name, RelName}, {rel_vsn, RelVsn}, - {erts_vsn, ErtsVsn}, {erl_opts, ErlOpts}]). + render(extended_bin_dtl, [{rel_name, RelName}, {rel_vsn, RelVsn}, + {erts_vsn, ErtsVsn}, {erl_opts, ErlOpts}]). install_upgrade_escript_contents() -> - read_file("install_upgrade_escript"). + render(install_upgrade_escript_dtl). nodetool_contents() -> - read_file("nodetool"). + render(nodetool_dtl). sys_config_file() -> - read_file("sys_config"). + render(sys_config_dtl). vm_args_file(RelName) -> - render(vm_args, [{rel_name, RelName}]). + render(vm_args_dtl, [{rel_name, RelName}]). -render(TemplateName, Data) -> - TemplatePath = template_path(TemplateName), - {ok, Rendered} = case erlydtl:compile(TemplatePath, TemplateName) of - ok -> TemplateName:render(Data); - {ok, Template} -> Template:render(Data) - end, - Rendered. - -template_path(TemplateName) -> - BaseName = string:join([erlang:atom_to_list(TemplateName), ".dtl"]), - filename:join([code:priv_dir(relx), "templates", BaseName]). +render(Template) -> + render(Template, []). -read_file(FileName) -> - {ok, Contents} = file:read_file(file_path(FileName)), - Contents. - -file_path(FileName) -> - filename:join([code:priv_dir(relx), "files", FileName]). +render(Template, Data) -> + {ok, Rendered} = Template:render(Data), + Rendered. -- cgit v1.2.3 From 4ef840816d7df2a47a7955d3fd339720ace0a2b8 Mon Sep 17 00:00:00 2001 From: nuex Date: Sat, 22 Feb 2014 04:20:40 -0500 Subject: unescape quotes --- priv/templates/bin.dtl | 8 +- priv/templates/erl_script.dtl | 2 +- priv/templates/extended_bin.dtl | 124 ++++++++++++++--------------- priv/templates/install_upgrade_escript.dtl | 44 +++++----- priv/templates/nodetool.dtl | 60 +++++++------- 5 files changed, 119 insertions(+), 119 deletions(-) diff --git a/priv/templates/bin.dtl b/priv/templates/bin.dtl index 9e0dfb6..1af2554 100644 --- a/priv/templates/bin.dtl +++ b/priv/templates/bin.dtl @@ -12,12 +12,12 @@ ERL_OPTS={{ erl_opts }} find_erts_dir() { local erts_dir=$RELEASE_ROOT_DIR/erts-$ERTS_VSN - if [ -d \"$erts_dir\" ]; then + 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` + 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 @@ -26,8 +26,8 @@ find_erts_dir() { find_sys_config() { local possible_sys=$REL_DIR/sys.config - if [ -f \"$possible_sys\" ]; then - SYS_CONFIG=\"-config $possible_sys\" + if [ -f "$possible_sys" ]; then + SYS_CONFIG="-config $possible_sys" fi } diff --git a/priv/templates/erl_script.dtl b/priv/templates/erl_script.dtl index 261eb6a..72e93dd 100644 --- a/priv/templates/erl_script.dtl +++ b/priv/templates/erl_script.dtl @@ -10,4 +10,4 @@ export EMU export ROOTDIR export BINDIR export PROGNAME -exec \"$BINDIR/erlexec\" ${1+\"$@\"} +exec "$BINDIR/erlexec" ${1+"$@"} diff --git a/priv/templates/extended_bin.dtl b/priv/templates/extended_bin.dtl index 39701c0..16b8708 100644 --- a/priv/templates/extended_bin.dtl +++ b/priv/templates/extended_bin.dtl @@ -13,12 +13,12 @@ PIPE_DIR=/tmp/erl_pipes/{{ rel_name }}/ find_erts_dir() { local erts_dir=$RELEASE_ROOT_DIR/erts-$ERTS_VSN - if [ -d \"$erts_dir\" ]; then + 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` + 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 @@ -27,21 +27,21 @@ find_erts_dir() { find_sys_config() { local possible_sys=$REL_DIR/sys.config - if [ -f \"$possible_sys\" ]; then - SYS_CONFIG=\"-config $possible_sys\" + 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 \"$RELEASE_ROOT_DIR/vm.args\" ]; then +if [ -e "$RELEASE_ROOT_DIR/vm.args" ]; then VMARGS_PATH=$RELEASE_ROOT_DIR/vm.args USE_DIR=$RELEASE_ROOT_DIR else USE_DIR=$REL_DIR - if [ -e \"$REL_DIR/vm.args\" ]; then - VMARGS_PATH=\"$REL_DIR/vm.args\" + if [ -e "$REL_DIR/vm.args" ]; then + VMARGS_PATH="$REL_DIR/vm.args" else - VMARGS_PATH=\"$REL_DIR/vm.args\" + VMARGS_PATH="$REL_DIR/vm.args" fi fi @@ -50,20 +50,20 @@ RUNNER_LOG_DIR=$RELEASE_ROOT_DIR/log 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\" +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\" + if [ -e "$REL_DIR/sys.config" ]; then + CONFIG_PATH="$REL_DIR/sys.config" else - CONFIG_PATH=\"$REL_DIR/app.config\" + 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.\" +if [ -z "$NAME_ARG" ]; then + echo "vm.args needs to have either -name or -sname parameter." exit 1 fi @@ -72,13 +72,13 @@ 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 -boot start_clean\" +REMSH_NAME_ARG="$REMSH_TYPE remsh`date +%s`@`echo $REMSH_NAME | awk -F@ '{print $2}'`" +REMSH_REMSH_ARG="-remsh $REMSH_NAME -boot start_clean" # Extract the target cookie COOKIE_ARG=`grep '^-setcookie' $VMARGS_PATH` -if [ -z \"$COOKIE_ARG\" ]; then - echo \"vm.args needs to have a -setcookie parameter.\" +if [ -z "$COOKIE_ARG" ]; then + echo "vm.args needs to have a -setcookie parameter." exit 1 fi @@ -93,38 +93,38 @@ export LD_LIBRARY_PATH=$ERTS_DIR/lib cd $ROOTDIR # Setup remote shell command to control node -REMSH=\"$BINDIR/erl $REMSH_NAME_ARG $REMSH_REMSH_ARG $COOKIE_ARG\" +REMSH="$BINDIR/erl $REMSH_NAME_ARG $REMSH_REMSH_ARG $COOKIE_ARG" # Setup command to control the node -NODETOOL=\"$BINDIR/escript $ROOTDIR/bin/nodetool $NAME_ARG $COOKIE_ARG\" +NODETOOL="$BINDIR/escript $ROOTDIR/bin/nodetool $NAME_ARG $COOKIE_ARG" # Check the first argument for instructions -case \"$1\" in +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!\" + #if [ "$RES" = "pong" ]; then + # echo "Node is already running!" # exit 1 #fi - case \"$1\" in + case "$1" in start) shift - START_OPTION=\"console\" - HEART_OPTION=\"start\" + START_OPTION="console" + HEART_OPTION="start" ;; start_boot) shift - START_OPTION=\"console_boot\" - HEART_OPTION=\"start_boot\" + START_OPTION="console_boot" + HEART_OPTION="start_boot" ;; esac - RUN_PARAM=$(printf \"'%s' \" \"$@\") - HEART_COMMAND=\"$SCRIPT_DIR/bin/$REL_NAME $HEART_OPTION $RUN_PARAM\" + RUN_PARAM=$(printf "'%s' " "$@") + HEART_COMMAND="$SCRIPT_DIR/bin/$REL_NAME $HEART_OPTION $RUN_PARAM" export HEART_COMMAND mkdir -p $PIPE_DIR - $BINDIR/run_erl -daemon $PIPE_DIR $RUNNER_LOG_DIR \"exec $RELEASE_ROOT_DIR/bin/$REL_NAME $START_OPTION $RUN_PARAM\" 2>&1 + $BINDIR/run_erl -daemon $PIPE_DIR $RUNNER_LOG_DIR "exec $RELEASE_ROOT_DIR/bin/$REL_NAME $START_OPTION $RUN_PARAM" 2>&1 ;; stop) @@ -133,21 +133,21 @@ case \"$1\" in Linux|Darwin|FreeBSD|DragonFly|NetBSD|OpenBSD) # PID COMMAND PID=`ps ax -o pid= -o command=| - grep \"$SCRIPT_DIR/.*/[b]eam\"|awk '{print $1}'` + 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}'` + 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}'` + PID=`ps -efW|grep "$SCRIPT_DIR/.*/[b]eam"|awk '{print $2}'` ;; esac $NODETOOL stop ES=$? - if [ \"$ES\" -ne 0 ]; then + if [ "$ES" -ne 0 ]; then exit $ES fi while `kill -0 $PID 2>/dev/null`; @@ -160,7 +160,7 @@ case \"$1\" in ## Restart the VM without exiting the process $NODETOOL restart ES=$? - if [ \"$ES\" -ne 0 ]; then + if [ "$ES" -ne 0 ]; then exit $ES fi ;; @@ -169,7 +169,7 @@ case \"$1\" in ## Restart the VM completely (uses heart to restart it) $NODETOOL reboot ES=$? - if [ \"$ES\" -ne 0 ]; then + if [ "$ES" -ne 0 ]; then exit $ES fi ;; @@ -178,7 +178,7 @@ case \"$1\" in ## See if the VM is alive $NODETOOL ping ES=$? - if [ \"$ES\" -ne 0 ]; then + if [ "$ES" -ne 0 ]; then exit $ES fi ;; @@ -187,8 +187,8 @@ case \"$1\" in # Make sure a node IS running RES=`$NODETOOL ping` ES=$? - if [ \"$ES\" -ne 0 ]; then - echo \"Node is not running!\" + if [ "$ES" -ne 0 ]; then + echo "Node is not running!" exit $ES fi @@ -200,8 +200,8 @@ case \"$1\" in # Make sure a node IS running RES=`$NODETOOL ping` ES=$? - if [ \"$ES\" -ne 0 ]; then - echo \"Node is not running!\" + if [ "$ES" -ne 0 ]; then + echo "Node is not running!" exit $ES fi @@ -210,18 +210,18 @@ case \"$1\" in ;; upgrade|downgrade|install) - if [ -z \"$2\" ]; then - echo \"Missing package argument\" - echo \"Usage: $REL_NAME $1 {package base name}\" - echo \"NOTE {package base name} MUST NOT include the .tar.gz suffix\" + if [ -z "$2" ]; then + echo "Missing package argument" + echo "Usage: $REL_NAME $1 {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!\" + if [ "$ES" -ne 0 ]; then + echo "Node is not running!" exit $ES fi @@ -235,31 +235,31 @@ case \"$1\" in # .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 + case "$1" in console) [ -f $REL_DIR/$REL_NAME.boot ] && BOOTFILE=$REL_DIR/$REL_NAME || BOOTFILE=$REL_DIR/start ;; console_clean) BOOTFILE=$ROOTDIR/bin/start_clean ;; console_boot) shift - BOOTFILE=\"$1\" + BOOTFILE="$1" shift ;; esac # Setup beam-required vars EMU=beam PROGNAME=`echo $0 | sed 's/.*\\///'` - CMD=\"$BINDIR/erlexec -boot $BOOTFILE -env ERL_LIBS $REL_DIR/lib -config $CONFIG_PATH -args_file $VMARGS_PATH\" + CMD="$BINDIR/erlexec -boot $BOOTFILE -env ERL_LIBS $REL_DIR/lib -config $CONFIG_PATH -args_file $VMARGS_PATH" export EMU export PROGNAME # Dump environment info for logging purposes - echo \"Exec: $CMD\" -- ${1+\"$@\"} - echo \"Root: $ROOTDIR\" + echo "Exec: $CMD" -- ${1+"$@"} + echo "Root: $ROOTDIR" # Log the startup - logger -t \"$REL_NAME[$$]\" \"Starting up\" + logger -t "$REL_NAME[$$]" "Starting up" # Start the VM - exec $CMD -- ${1+\"$@\"} + exec $CMD -- ${1+"$@"} ;; foreground) @@ -267,24 +267,24 @@ case \"$1\" in # or other supervision services [ -f $REL_DIR/$REL_NAME.boot ] && BOOTFILE=$REL_NAME || BOOTFILE=start - FOREGROUNDOPTIONS=\"-noinput +Bd\" + FOREGROUNDOPTIONS="-noinput +Bd" # Setup beam-required vars EMU=beam PROGNAME=`echo $0 | sed 's/.*\\///'` - CMD=\"$BINDIR/erlexec $FOREGROUNDOPTIONS -boot $REL_DIR/$BOOTFILE -mode embedded -config $CONFIG_PATH -args_file $VMARGS_PATH\" + CMD="$BINDIR/erlexec $FOREGROUNDOPTIONS -boot $REL_DIR/$BOOTFILE -mode embedded -config $CONFIG_PATH -args_file $VMARGS_PATH" export EMU export PROGNAME # Dump environment info for logging purposes - echo \"Exec: $CMD\" -- ${1+\"$@\"} - echo \"Root: $ROOTDIR\" + echo "Exec: $CMD" -- ${1+"$@"} + echo "Root: $ROOTDIR" # Start the VM - exec $CMD -- ${1+\"$@\"} + exec $CMD -- ${1+"$@"} ;; *) - echo \"Usage: $REL_NAME {start|start_boot |foreground|stop|restart|reboot|ping|console|console_clean|console_boot |attach|remote_console|upgrade}\" + echo "Usage: $REL_NAME {start|start_boot |foreground|stop|restart|reboot|ping|console|console_clean|console_boot |attach|remote_console|upgrade}" exit 1 ;; esac diff --git a/priv/templates/install_upgrade_escript.dtl b/priv/templates/install_upgrade_escript.dtl index 6dcddb3..7c331aa 100644 --- a/priv/templates/install_upgrade_escript.dtl +++ b/priv/templates/install_upgrade_escript.dtl @@ -14,64 +14,64 @@ main([RelName, NodeName, Cookie, VersionArg]) -> case proplists:get_value(Version, WhichReleases) of undefined -> %% not installed, so unpack tarball: - ?INFO(\"Release ~s not found, attempting to unpack releases/~s/~s.tar.gz~n\",[Version,Version,RelName]), - ReleasePackage = Version ++ \"/\" ++ RelName, + ?INFO("Release ~s not found, attempting to unpack releases/~s/~s.tar.gz~n",[Version,Version,RelName]), + ReleasePackage = Version ++ "/" ++ RelName, case rpc:call(TargetNode, release_handler, unpack_release, [ReleasePackage], ?TIMEOUT) of {ok, Vsn} -> - ?INFO(\"Unpacked successfully: ~p~n\", [Vsn]), + ?INFO("Unpacked successfully: ~p~n", [Vsn]), install_and_permafy(TargetNode, RelName, Vsn); {error, UnpackReason} -> print_existing_versions(TargetNode), - ?INFO(\"Unpack failed: ~p~n\",[UnpackReason]), + ?INFO("Unpack failed: ~p~n",[UnpackReason]), erlang:halt(2) end; old -> %% no need to unpack, has been installed previously - ?INFO(\"Release ~s is marked old, switching to it.~n\",[Version]), + ?INFO("Release ~s is marked old, switching to it.~n",[Version]), install_and_permafy(TargetNode, RelName, Version); unpacked -> - ?INFO(\"Release ~s is already unpacked, now installing.~n\",[Version]), + ?INFO("Release ~s is already unpacked, now installing.~n",[Version]), install_and_permafy(TargetNode, RelName, Version); current -> %% installed and in-use, just needs to be permanent - ?INFO(\"Release ~s is already installed and current. Making permanent.~n\",[Version]), + ?INFO("Release ~s is already installed and current. Making permanent.~n",[Version]), permafy(TargetNode, RelName, Version); permanent -> - ?INFO(\"Release ~s is already installed, and set permanent.~n\",[Version]) + ?INFO("Release ~s is already installed, and set permanent.~n",[Version]) end; main(_) -> erlang:halt(1). parse_version(V) when is_list(V) -> - hd(string:tokens(V,\"/\")). + hd(string:tokens(V,"/")). install_and_permafy(TargetNode, RelName, Vsn) -> case rpc:call(TargetNode, release_handler, check_install_release, [Vsn], ?TIMEOUT) of {ok, _OtherVsn, _Desc} -> ok; {error, Reason} -> - ?INFO(\"ERROR: release_handler:check_install_release failed: ~p~n\",[Reason]), + ?INFO("ERROR: release_handler:check_install_release failed: ~p~n",[Reason]), erlang:halt(3) end, case rpc:call(TargetNode, release_handler, install_release, [Vsn], ?TIMEOUT) of {ok, _, _} -> - ?INFO(\"Installed Release: ~s~n\", [Vsn]), + ?INFO("Installed Release: ~s~n", [Vsn]), permafy(TargetNode, RelName, Vsn), ok; {error, {no_such_release, Vsn}} -> VerList = iolist_to_binary( - [io_lib:format(\"* ~s\t~s~n\",[V,S]) || {V,S} <- which_releases(TargetNode)]), - ?INFO(\"Installed versions:~n~s\", [VerList]), - ?INFO(\"ERROR: Unable to revert to '~s' - not installed.~n\", [Vsn]), + [io_lib:format("* ~s\t~s~n",[V,S]) || {V,S} <- which_releases(TargetNode)]), + ?INFO("Installed versions:~n~s", [VerList]), + ?INFO("ERROR: Unable to revert to '~s' - not installed.~n", [Vsn]), erlang:halt(2) end. permafy(TargetNode, RelName, Vsn) -> ok = rpc:call(TargetNode, release_handler, make_permanent, [Vsn], ?TIMEOUT), - file:copy(filename:join([\"bin\", RelName++\"-\"++Vsn]), - filename:join([\"bin\", RelName])), - ?INFO(\"Made release permanent: ~p~n\", [Vsn]), + file:copy(filename:join(["bin", RelName++"-"++Vsn]), + filename:join(["bin", RelName])), + ?INFO("Made release permanent: ~p~n", [Vsn]), ok. which_releases(TargetNode) -> @@ -80,9 +80,9 @@ which_releases(TargetNode) -> print_existing_versions(TargetNode) -> VerList = iolist_to_binary([ - io_lib:format(\"* ~s\t~s~n\",[V,S]) + io_lib:format("* ~s\t~s~n",[V,S]) || {V,S} <- which_releases(TargetNode) ]), - ?INFO(\"Installed versions:~n~s\", [VerList]). + ?INFO("Installed versions:~n~s", [VerList]). start_distribution(NodeName, Cookie) -> MyNode = make_script_node(NodeName), @@ -94,7 +94,7 @@ start_distribution(NodeName, Cookie) -> {true, pong} -> ok; {_, pang} -> - io:format(\"Node ~p not responding to pings.\n\", [TargetNode]), + io:format("Node ~p not responding to pings.\n", [TargetNode]), erlang:halt(1) end, {ok, Cwd} = file:get_cwd(), @@ -102,5 +102,5 @@ start_distribution(NodeName, Cookie) -> TargetNode. make_script_node(Node) -> - [Name, Host] = string:tokens(Node, \"@\"), - list_to_atom(lists:concat([Name, \"_upgrader_\", os:getpid(), \"@\", Host])). + [Name, Host] = string:tokens(Node, "@"), + list_to_atom(lists:concat([Name, "_upgrader_", os:getpid(), "@", Host])). diff --git a/priv/templates/nodetool.dtl b/priv/templates/nodetool.dtl index c98e544..a6faad5 100644 --- a/priv/templates/nodetool.dtl +++ b/priv/templates/nodetool.dtl @@ -16,58 +16,58 @@ main(Args) -> {true, pong} -> ok; {_, pang} -> - io:format(\"Node ~p not responding to pings.\n\", [TargetNode]), + io:format("Node ~p not responding to pings.\n", [TargetNode]), halt(1) end, case RestArgs of - [\"ping\"] -> + ["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] -> + %% 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]), + io:format("RPC to ~p failed: ~p\n", [TargetNode, Reason]), halt(1); _ -> halt(1) end; - [\"rpcterms\", Module, Function, ArgsAsString] -> + ["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]), + io:format("RPC to ~p failed: ~p\n", [TargetNode, Reason]), halt(1); Other -> - io:format(\"~p\n\", [Other]) + io:format("~p\n", [Other]) end; Other -> - io:format(\"Other: ~p\n\", [Other]), - io:format(\"Usage: nodetool {ping|stop|restart|reboot}\n\") + 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) -> +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_\"), +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_\"), +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) -> @@ -75,17 +75,17 @@ process_args([Arg | Rest], Acc, Opts) -> start_epmd() -> - [] = os:cmd(epmd_path() ++ \" -daemon\"), + [] = os:cmd(epmd_path() ++ " -daemon"), ok. epmd_path() -> ErtsBinDir = filename:dirname(escript:script_name()), - Name = \"epmd\", + Name = "epmd", case os:find_executable(Name, ErtsBinDir) of false -> case os:find_executable(Name) of false -> - io:format(\"Could not find epmd.~n\"), + io:format("Could not find epmd.~n"), halt(1); GlobalEpmd -> GlobalEpmd @@ -96,18 +96,18 @@ epmd_path() -> nodename(Name) -> - case string:tokens(Name, \"@\") of + 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])) + [_, 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 + case string:tokens(Name, "@") of [Node, Host] -> - list_to_atom(lists:concat([Node, Suffix, os:getpid(), \"@\", Host])); + list_to_atom(lists:concat([Node, Suffix, os:getpid(), "@", Host])); [Node] -> list_to_atom(lists:concat([Node, Suffix, os:getpid()])) end. -- cgit v1.2.3 From baaa2400e3ea9b21f453a77b064033edf96fc685 Mon Sep 17 00:00:00 2001 From: nuex Date: Fri, 7 Mar 2014 16:16:29 -0500 Subject: try adding some verbosity to erlydtl compilation errors --- rebar.config | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rebar.config b/rebar.config index 4eef17e..cf03ea3 100644 --- a/rebar.config +++ b/rebar.config @@ -30,7 +30,8 @@ {cover_print_enabled, true}. %% Misc ======================================================================= -{erlydtl_opts, [{doc_root, "priv/templates"}]}. +{erlydtl_opts, [{doc_root, "priv/templates"}, + {compiler_options, [report, return, debug_info]}]}. {escript_incl_apps, [getopt, erlware_commons, erlydtl]}. -- cgit v1.2.3 From cf7186ba8584f5276638fa09e0ff3c50d4d80f2f Mon Sep 17 00:00:00 2001 From: nuex Date: Sat, 8 Mar 2014 19:20:08 -0500 Subject: exclude dtl scripts from cover --- cover.spec | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cover.spec b/cover.spec index 3930df1..d0efd51 100644 --- a/cover.spec +++ b/cover.spec @@ -1,2 +1,6 @@ %% -*- mode: Erlang; fill-column: 80; comment-column: 75; -*- -{incl_app, relx, details}. +{incl_app, relx}. +{level, details}. +{excl_mods, [bin_dtl, erl_script_dtl, extended_bin_dtl, + install_upgrade_escript_dtl, nodetool_dtl, + sys_config_dtl, vm_args_dtl]}. -- cgit v1.2.3 From fdf63ccb750c1c3a6de9a55cdec17980c0559e39 Mon Sep 17 00:00:00 2001 From: nuex Date: Sat, 8 Mar 2014 19:33:05 -0500 Subject: honor existing LD_LIBRARY_PATH in script templates --- priv/templates/bin.dtl | 2 +- priv/templates/extended_bin.dtl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/priv/templates/bin.dtl b/priv/templates/bin.dtl index 1af2554..067e9f7 100644 --- a/priv/templates/bin.dtl +++ b/priv/templates/bin.dtl @@ -37,7 +37,7 @@ export ROOTDIR=$RELEASE_ROOT_DIR export BINDIR=$ERTS_DIR/bin export EMU=beam export PROGNAME=erl -export LD_LIBRARY_PATH=$ERTS_DIR/lib +export LD_LIBRARY_PATH=$ERTS_DIR/lib:$LD_LIBRARY_PATH cd $ROOTDIR diff --git a/priv/templates/extended_bin.dtl b/priv/templates/extended_bin.dtl index 16b8708..a0e46a9 100644 --- a/priv/templates/extended_bin.dtl +++ b/priv/templates/extended_bin.dtl @@ -88,7 +88,7 @@ export ROOTDIR=$RELEASE_ROOT_DIR export BINDIR=$ERTS_DIR/bin export EMU=beam export PROGNAME=erl -export LD_LIBRARY_PATH=$ERTS_DIR/lib +export LD_LIBRARY_PATH=$ERTS_DIR/lib:$LD_LIBRARY_PATH cd $ROOTDIR -- cgit v1.2.3 From f32a0a44f2e0748122d90a708cbff0003762c2ba Mon Sep 17 00:00:00 2001 From: nuex Date: Sat, 8 Mar 2014 19:34:11 -0500 Subject: ignore compiled templates --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 666c837..79a42f3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ ebin/* .eunit .relx_plt relx +priv/templates/*.erl # This is a generated file that should be ignored src/rcl_goal.erl logs -- cgit v1.2.3