diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | .travis.yml | 9 | ||||
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | cover.spec | 6 | ||||
-rwxr-xr-x | priv/templates/bin.dtl | 52 | ||||
-rw-r--r-- | priv/templates/erl_script.dtl | 13 | ||||
-rw-r--r-- | priv/templates/extended_bin.dtl | 292 | ||||
-rw-r--r-- | priv/templates/install_upgrade_escript.dtl | 106 | ||||
-rw-r--r-- | priv/templates/nodetool.dtl | 137 | ||||
-rw-r--r-- | priv/templates/sys_config.dtl | 9 | ||||
-rw-r--r-- | priv/templates/vm_args.dtl | 19 | ||||
-rw-r--r-- | rebar.config | 9 | ||||
-rw-r--r-- | rebar.config.script | 2 | ||||
-rw-r--r-- | src/relx.erl | 1 | ||||
-rw-r--r-- | src/rlx_cmd_args.erl | 8 | ||||
-rw-r--r-- | src/rlx_dscv_util.erl | 29 | ||||
-rw-r--r-- | src/rlx_goal.erl | 91 | ||||
-rw-r--r-- | src/rlx_prv_assembler.erl | 723 | ||||
-rw-r--r-- | src/rlx_prv_config.erl | 9 | ||||
-rw-r--r-- | src/rlx_prv_overlay.erl | 37 | ||||
-rw-r--r-- | src/rlx_rel_discovery.erl | 18 | ||||
-rw-r--r-- | src/rlx_state.erl | 6 | ||||
-rw-r--r-- | test/rlx_discover_SUITE.erl | 6 | ||||
-rw-r--r-- | test/rlx_release_SUITE.erl | 8 |
25 files changed, 892 insertions, 706 deletions
@@ -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 diff --git a/.travis.yml b/.travis.yml index adf0a04..764fe9c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,10 @@ language: erlang otp_release: + - 17.0-rc1 + - R16B03-1 + - R16B03 + - R16B02 + - R16B01 - R16B - R15B03 - R15B02 @@ -8,7 +13,7 @@ otp_release: - R14B04 - R14B03 - R14B02 -script: "make rebuild" +script: "make get-rebar && make rebuild" branches: only: - master @@ -19,4 +24,4 @@ notifications: channels: - "irc.freenode.org#erlware" use_notice: true - skip_join: true
\ No newline at end of file + skip_join: true @@ -31,6 +31,11 @@ endif REBAR=$(shell which rebar) +# If building on travis, use the rebar in the current directory +ifeq ($(TRAVIS),true) +REBAR=$(CURDIR)/rebar +endif + ifeq ($(REBAR),) REBAR=$(CURDIR)/rebar endif @@ -95,6 +100,7 @@ ct: compile clean-common-test-data -pa $(CURDIR)/deps/*/ebin \ -logdir $(CURDIR)/logs \ -dir $(CURDIR)/test/ \ + -cover cover.spec \ -suite rlx_command_SUITE rlx_discover_SUITE -suite rlx_release_SUITE test: compile dialyzer eunit ct @@ -62,6 +62,7 @@ Options | -a | --override_app | string | | An app name and a directory to override in the form appname:dir | | -c | --config | string | ./relx.config | Config file path | | | --overlay_vars | string | | Path to a file of overlay variables | +| | --sys_config | string | | Path to a file to use for sys.config | | -d | --dev-mode | boolean | false | Symlink all applications and configuration into the release instead of copying| Wiki ---- diff --git a/cover.spec b/cover.spec new file mode 100644 index 0000000..d0efd51 --- /dev/null +++ b/cover.spec @@ -0,0 +1,6 @@ +%% -*- mode: Erlang; fill-column: 80; comment-column: 75; -*- +{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]}. diff --git a/priv/templates/bin.dtl b/priv/templates/bin.dtl new file mode 100755 index 0000000..8f8b048 --- /dev/null +++ b/priv/templates/bin.dtl @@ -0,0 +1,52 @@ +#!/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_vm_args() { + local possible_vm_args=$REL_DIR/vm.args + if [ -f "$possible_vm_args" ]; then + VM_ARGS="-args_file $possible_vm_args" + fi +} + +find_erts_dir +find_sys_config +find_vm_args +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 $VM_ARGS -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..72e93dd --- /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..a0e46a9 --- /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_name }} +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:$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 <file>|foreground|stop|restart|reboot|ping|console|console_clean|console_boot <file>|attach|remote_console|upgrade}" + exit 1 + ;; +esac + +exit 0 diff --git a/priv/templates/install_upgrade_escript.dtl b/priv/templates/install_upgrade_escript.dtl new file mode 100644 index 0000000..7c331aa --- /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..a6faad5 --- /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. +[]. 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 diff --git a/rebar.config b/rebar.config index 63ec977..cf03ea3 100644 --- a/rebar.config +++ b/rebar.config @@ -2,20 +2,21 @@ %% Dependencies ================================================================ {deps, [{rebar_vsn_plugin, ".*", {git, "https://github.com/erlware/rebar_vsn_plugin.git", - {tag, "master"}}}, + {branch, "master"}}}, {neotoma, ".*", {git, "https://github.com/seancribbs/neotoma.git", {branch, "master"}}}, {erlware_commons, ".*", {git, "https://github.com/erlware/erlware_commons.git", {branch, "master"}}}, {erlydtl, ".*", - {git, "https://github.com/evanmiller/erlydtl.git", - {branch, "master"}}}, + {git, "https://github.com/erlydtl/erlydtl.git", + {tag, "0.9.0"}}}, {getopt, "", {git, "https://github.com/jcomellas/getopt.git", {branch, "master"}}}]}. %% Rebar Plugins ============================================================== +{plugin_dir, "deps/rebar_vsn_plugin/src"}. {plugins, [rebar_vsn_plugin]}. %% Compiler Options ============================================================ @@ -29,6 +30,8 @@ {cover_print_enabled, true}. %% Misc ======================================================================= +{erlydtl_opts, [{doc_root, "priv/templates"}, + {compiler_options, [report, return, debug_info]}]}. {escript_incl_apps, [getopt, erlware_commons, erlydtl]}. diff --git a/rebar.config.script b/rebar.config.script index c19a751..819fc7b 100644 --- a/rebar.config.script +++ b/rebar.config.script @@ -1,4 +1,4 @@ -{match, [ErtsNumber]} = re:run(erlang:system_info(otp_release), "R(\\d+).+", [{capture, [1], list}]), +{match, [ErtsNumber]} = re:run(erlang:system_info(otp_release), "(\\d+)", [{capture, [0], list}]), ErtsVsn = erlang:list_to_integer(ErtsNumber), Opts1 = case lists:keysearch(erl_opts, 1, CONFIG) of {value, {erl_opts, Opts0}} -> diff --git a/src/relx.erl b/src/relx.erl index 359d47d..79eeae2 100644 --- a/src/relx.erl +++ b/src/relx.erl @@ -203,6 +203,7 @@ opt_spec_list() -> "Provide an app name and a directory to override in the form <appname>:<app directory>"}, {config, $c, "config", {string, ""}, "The path to a config file"}, {overlay_vars, undefined, "overlay_vars", string, "Path to a file of overlay variables"}, + {sys_config, undefined, "sys_config", string, "Path to a file to use for sys.config"}, {system_libs, undefined, "system_libs", string, "Path to dir of Erlang system libs"}, {version, undefined, "version", undefined, "Print relx version"}, {root_dir, $r, "root", string, "The project root directory"}]. diff --git a/src/rlx_cmd_args.erl b/src/rlx_cmd_args.erl index d2402ce..da8f3a0 100644 --- a/src/rlx_cmd_args.erl +++ b/src/rlx_cmd_args.erl @@ -253,7 +253,13 @@ create_disable_default_libs(Opts, Acc) -> {ok, rlx_state:cmd_args()} | relx:error(). create_overlay_vars(Opts, Acc) -> OverlayVars = proplists:get_all_values(overlay_vars, Opts), - create_system_libs(Opts, [{overlay_vars, OverlayVars} | Acc]). + create_sys_config(Opts, [{overlay_vars, OverlayVars} | Acc]). + +-spec create_sys_config([getopt:option()], rlx_state:cmd_args()) -> + {ok, rlx_state:cmd_args()} | relx:error(). +create_sys_config(Opts, Acc) -> + SysConfig = proplists:get_value(sys_config, Opts, undefined), + create_system_libs(Opts, [{sys_config, SysConfig} | Acc]). -spec create_system_libs([getopt:option()], rlx_state:cmd_args()) -> {ok, rlx_state:cmd_args()} | relx:error(). diff --git a/src/rlx_dscv_util.erl b/src/rlx_dscv_util.erl index 6cfea81..a4a265c 100644 --- a/src/rlx_dscv_util.erl +++ b/src/rlx_dscv_util.erl @@ -115,19 +115,24 @@ discover_real_symlink_dir(ProcessDir, File) -> ok = file:set_cwd(File), {ok, ActualRealDir} = file:get_cwd(), ok = file:set_cwd(CurCwd), - lists:prefix(iolist_to_list(filename:absname(ActualRealDir)), - iolist_to_list(filename:absname(File))), - case ProcessDir(File, directory) of - {ok, Result, true} -> - [{ok, Result} | recurse(ProcessDir, File)]; - {noresult, true} -> - recurse(ProcessDir, File); - {ok, Result, _} -> - [{ok, Result}]; - {noresult, _} -> + case lists:prefix(iolist_to_list(filename:absname(ActualRealDir)), + iolist_to_list(filename:absname(File))) of + true -> + %% Ignore cycles []; - Err = {error, _} -> - [Err] + false -> + case ProcessDir(File, directory) of + {ok, Result, true} -> + [{ok, Result} | recurse(ProcessDir, File)]; + {noresult, true} -> + recurse(ProcessDir, File); + {ok, Result, _} -> + [{ok, Result}]; + {noresult, _} -> + []; + Err = {error, _} -> + [Err] + end end. recurse(ProcessDir, File) -> diff --git a/src/rlx_goal.erl b/src/rlx_goal.erl index 138a197..e588a0e 100644 --- a/src/rlx_goal.erl +++ b/src/rlx_goal.erl @@ -1,6 +1,14 @@ -module(rlx_goal). -export([parse/1,file/1]). --compile({nowarn_unused_function,[p/4, p/5, p_eof/0, p_optional/1, p_not/1, p_assert/1, p_seq/1, p_and/1, p_choose/1, p_zero_or_more/1, p_one_or_more/1, p_label/2, p_string/1, p_anything/0, p_charclass/1, p_regexp/1, p_attempt/4, line/1, column/1]}). +-define(p_anything,true). +-define(p_charclass,true). +-define(p_choose,true). +-define(p_not,true). +-define(p_one_or_more,true). +-define(p_optional,true). +-define(p_scan,true). +-define(p_seq,true). +-define(p_string,true). -compile(export_all). @@ -17,6 +25,7 @@ parse(Input) when is_binary(Input) -> end, release_memo(), Result. +-spec 'constraint'(input(), index()) -> parse_result(). 'constraint'(Input, Index) -> p(Input, Index, 'constraint', fun(I,D) -> (p_choose([p_seq([p_optional(fun 'ws'/2), fun 'app_name'/2, p_optional(fun 'ws'/2), fun 'between_op'/2, p_optional(fun 'ws'/2), fun 'version'/2, p_optional(fun 'ws'/2), p_string(<<",">>), p_optional(fun 'ws'/2), fun 'version'/2, p_optional(fun 'ws'/2), p_not(p_anything())]), p_seq([p_optional(fun 'ws'/2), fun 'app_name'/2, p_optional(fun 'ws'/2), fun 'constraint_op'/2, p_optional(fun 'ws'/2), fun 'version'/2, p_optional(fun 'ws'/2), p_not(p_anything())]), p_seq([p_optional(fun 'ws'/2), fun 'app_name'/2, p_optional(fun 'ws'/2), p_not(p_anything())])]))(I,D) end, fun(Node, _Idx) -> case Node of @@ -38,12 +47,15 @@ parse(Input) when is_binary(Input) -> end end). +-spec 'ws'(input(), index()) -> parse_result(). 'ws'(Input, Index) -> p(Input, Index, 'ws', fun(I,D) -> (p_charclass(<<"[\s\t\n\s\r]">>))(I,D) end, fun(Node, Idx) ->transform('ws', Node, Idx) end). +-spec 'app_name'(input(), index()) -> parse_result(). 'app_name'(Input, Index) -> p(Input, Index, 'app_name', fun(I,D) -> (p_one_or_more(p_charclass(<<"[a-zA-Z0-9_]">>)))(I,D) end, fun(Node, _Idx) -> erlang:list_to_atom(erlang:binary_to_list(erlang:iolist_to_binary(Node))) end). +-spec 'between_op'(input(), index()) -> parse_result(). 'between_op'(Input, Index) -> p(Input, Index, 'between_op', fun(I,D) -> (p_seq([p_string(<<":">>), p_optional(fun 'ws'/2), p_choose([p_string(<<"btwn">>), p_string(<<"between">>)]), p_optional(fun 'ws'/2), p_string(<<":">>)]))(I,D) end, fun(Node, _Idx) -> case Node of [C,_,Op,_,C] -> erlang:iolist_to_binary([C,Op,C]); @@ -51,9 +63,11 @@ parse(Input) when is_binary(Input) -> end end). +-spec 'constraint_op'(input(), index()) -> parse_result(). 'constraint_op'(Input, Index) -> p(Input, Index, 'constraint_op', fun(I,D) -> (p_choose([p_string(<<"=">>), p_string(<<"-">>), p_string(<<"<=">>), p_string(<<"<">>), p_string(<<"~>">>), p_string(<<">=">>), p_string(<<">">>), fun 'word_constraint_op'/2, p_string(<<":">>)]))(I,D) end, fun(Node, Idx) ->transform('constraint_op', Node, Idx) end). +-spec 'word_constraint_op'(input(), index()) -> parse_result(). 'word_constraint_op'(Input, Index) -> p(Input, Index, 'word_constraint_op', fun(I,D) -> (p_seq([p_string(<<":">>), p_optional(fun 'ws'/2), p_choose([p_string(<<"gte">>), p_string(<<"lte">>), p_string(<<"gt">>), p_string(<<"lt">>), p_string(<<"pes">>)]), p_optional(fun 'ws'/2), p_string(<<":">>)]))(I,D) end, fun(Node, _Idx) -> case Node of [C,_,Op,_,C] -> erlang:iolist_to_binary([C,Op,C]); @@ -61,15 +75,22 @@ parse(Input) when is_binary(Input) -> end end). +-spec 'version'(input(), index()) -> parse_result(). 'version'(Input, Index) -> p(Input, Index, 'version', fun(I,D) -> (p_one_or_more(p_charclass(<<"[0-9a-zA-Z-+.]">>)))(I,D) end, fun(Node, Idx) ->transform('version', Node, Idx) end). transform(_,Node,_Index) -> Node. - -p(Inp, Index, Name, ParseFun) -> - p(Inp, Index, Name, ParseFun, fun(N, _Idx) -> N end). - +-file("peg_includes.hrl", 1). +-type index() :: {{line, pos_integer()}, {column, pos_integer()}}. +-type input() :: binary(). +-type parse_failure() :: {fail, term()}. +-type parse_success() :: {term(), input(), index()}. +-type parse_result() :: parse_failure() | parse_success(). +-type parse_fun() :: fun((input(), index()) -> parse_result()). +-type xform_fun() :: fun((input(), index()) -> term()). + +-spec p(input(), index(), atom(), parse_fun(), xform_fun()) -> parse_result(). p(Inp, StartIndex, Name, ParseFun, TransformFun) -> case get_memo(StartIndex, Name) of % See if the current reduction is memoized {ok, Memo} -> %Memo; % If it is, return the stored result @@ -86,12 +107,15 @@ p(Inp, StartIndex, Name, ParseFun, TransformFun) -> Result end. +-spec setup_memo() -> ets:tid(). setup_memo() -> put({parse_memo_table, ?MODULE}, ets:new(?MODULE, [set])). +-spec release_memo() -> true. release_memo() -> ets:delete(memo_table_name()). +-spec memoize(index(), atom(), term()) -> true. memoize(Index, Name, Result) -> Memo = case ets:lookup(memo_table_name(), Index) of [] -> []; @@ -99,6 +123,7 @@ memoize(Index, Name, Result) -> end, ets:insert(memo_table_name(), {Index, [{Name, Result}|Memo]}). +-spec get_memo(index(), atom()) -> {ok, term()} | {error, not_found}. get_memo(Index, Name) -> case ets:lookup(memo_table_name(), Index) of [] -> {error, not_found}; @@ -109,13 +134,19 @@ get_memo(Index, Name) -> end end. +-spec memo_table_name() -> ets:tid(). memo_table_name() -> get({parse_memo_table, ?MODULE}). +-ifdef(p_eof). +-spec p_eof() -> parse_fun(). p_eof() -> fun(<<>>, Index) -> {eof, [], Index}; (_, Index) -> {fail, {expected, eof, Index}} end. +-endif. +-ifdef(p_optional). +-spec p_optional(parse_fun()) -> parse_fun(). p_optional(P) -> fun(Input, Index) -> case P(Input, Index) of @@ -123,7 +154,10 @@ p_optional(P) -> {_, _, _} = Success -> Success end end. +-endif. +-ifdef(p_not). +-spec p_not(parse_fun()) -> parse_fun(). p_not(P) -> fun(Input, Index)-> case P(Input,Index) of @@ -132,7 +166,10 @@ p_not(P) -> {Result, _, _} -> {fail, {expected, {no_match, Result},Index}} end end. +-endif. +-ifdef(p_assert). +-spec p_assert(parse_fun()) -> parse_fun(). p_assert(P) -> fun(Input,Index) -> case P(Input,Index) of @@ -140,27 +177,32 @@ p_assert(P) -> _ -> {[], Input, Index} end end. +-endif. -p_and(P) -> - p_seq(P). - +-ifdef(p_seq). +-spec p_seq([parse_fun()]) -> parse_fun(). p_seq(P) -> fun(Input, Index) -> p_all(P, Input, Index, []) end. +-spec p_all([parse_fun()], input(), index(), [term()]) -> parse_result(). p_all([], Inp, Index, Accum ) -> {lists:reverse( Accum ), Inp, Index}; p_all([P|Parsers], Inp, Index, Accum) -> case P(Inp, Index) of {fail, _} = Failure -> Failure; {Result, InpRem, NewIndex} -> p_all(Parsers, InpRem, NewIndex, [Result|Accum]) end. +-endif. +-ifdef(p_choose). +-spec p_choose([parse_fun()]) -> parse_fun(). p_choose(Parsers) -> fun(Input, Index) -> p_attempt(Parsers, Input, Index, none) end. +-spec p_attempt([parse_fun()], input(), index(), none | parse_failure()) -> parse_result(). p_attempt([], _Input, _Index, Failure) -> Failure; p_attempt([P|Parsers], Input, Index, FirstFailure)-> case P(Input, Index) of @@ -171,12 +213,18 @@ p_attempt([P|Parsers], Input, Index, FirstFailure)-> end; Result -> Result end. +-endif. +-ifdef(p_zero_or_more). +-spec p_zero_or_more(parse_fun()) -> parse_fun(). p_zero_or_more(P) -> fun(Input, Index) -> p_scan(P, Input, Index, []) end. +-endif. +-ifdef(p_one_or_more). +-spec p_one_or_more(parse_fun()) -> parse_fun(). p_one_or_more(P) -> fun(Input, Index)-> Result = p_scan(P, Input, Index, []), @@ -188,7 +236,10 @@ p_one_or_more(P) -> {fail, {expected, {at_least_one, Failure}, Index}} end end. +-endif. +-ifdef(p_label). +-spec p_label(atom(), parse_fun()) -> parse_fun(). p_label(Tag, P) -> fun(Input, Index) -> case P(Input, Index) of @@ -198,15 +249,20 @@ p_label(Tag, P) -> {{Tag, Result}, InpRem, NewIndex} end end. +-endif. +-ifdef(p_scan). +-spec p_scan(parse_fun(), input(), index(), [term()]) -> parse_result(). p_scan(_, [], Index, Accum) -> {lists:reverse( Accum ), [], Index}; p_scan(P, Inp, Index, Accum) -> case P(Inp, Index) of {fail,_} -> {lists:reverse(Accum), Inp, Index}; {Result, InpRem, NewIndex} -> p_scan(P, InpRem, NewIndex, [Result | Accum]) end. +-endif. -p_string(S) when is_list(S) -> p_string(list_to_binary(S)); +-ifdef(p_string). +-spec p_string(binary()) -> parse_fun(). p_string(S) -> Length = erlang:byte_size(S), fun(Input, Index) -> @@ -217,14 +273,20 @@ p_string(S) -> error:{badmatch,_} -> {fail, {expected, {string, S}, Index}} end end. +-endif. +-ifdef(p_anything). +-spec p_anything() -> parse_fun(). p_anything() -> fun(<<>>, Index) -> {fail, {expected, any_character, Index}}; (Input, Index) when is_binary(Input) -> <<C/utf8, Rest/binary>> = Input, {<<C/utf8>>, Rest, p_advance_index(<<C/utf8>>, Index)} end. +-endif. +-ifdef(p_charclass). +-spec p_charclass(string() | binary()) -> parse_fun(). p_charclass(Class) -> {ok, RE} = re:compile(Class, [unicode, dotall]), fun(Inp, Index) -> @@ -235,7 +297,10 @@ p_charclass(Class) -> _ -> {fail, {expected, {character_class, binary_to_list(Class)}, Index}} end end. +-endif. +-ifdef(p_regexp). +-spec p_regexp(binary()) -> parse_fun(). p_regexp(Regexp) -> {ok, RE} = re:compile(Regexp, [unicode, dotall, anchored]), fun(Inp, Index) -> @@ -246,13 +311,21 @@ p_regexp(Regexp) -> _ -> {fail, {expected, {regexp, binary_to_list(Regexp)}, Index}} end end. +-endif. +-ifdef(line). +-spec line(index() | term()) -> pos_integer() | undefined. line({{line,L},_}) -> L; line(_) -> undefined. +-endif. +-ifdef(column). +-spec column(index() | term()) -> pos_integer() | undefined. column({_,{column,C}}) -> C; column(_) -> undefined. +-endif. +-spec p_advance_index(input() | unicode:charlist() | pos_integer(), index()) -> index(). p_advance_index(MatchedInput, Index) when is_list(MatchedInput) orelse is_binary(MatchedInput)-> % strings lists:foldl(fun p_advance_index/2, Index, unicode:characters_to_list(MatchedInput)); p_advance_index(MatchedInput, Index) when is_integer(MatchedInput) -> % single characters diff --git a/src/rlx_prv_assembler.erl b/src/rlx_prv_assembler.erl index fba21ca..1a24ab6 100644 --- a/src/rlx_prv_assembler.erl +++ b/src/rlx_prv_assembler.erl @@ -302,6 +302,25 @@ write_bin_file(State, Release, OutputDir, RelDir) -> rlx_release:erts(Release), ErlOpts); true -> + case rlx_state:get(State, extended_start_script, false) of + true -> + Prefix = code:root_dir(), + DstFile = filename:join([BinDir, "start_clean.boot"]), + %% Explicitly remove before cp, since it is 0444 mode + ec_file:remove(DstFile), + ok = ec_file:copy(filename:join([Prefix, "bin", "start_clean.boot"]), + DstFile), + NodeToolFile = nodetool_contents(), + InstallUpgradeFile = install_upgrade_escript_contents(), + NodeTool = filename:join([BinDir, "nodetool"]), + InstallUpgrade = filename:join([BinDir, "install_upgrade.escript"]), + ok = file:write_file(NodeTool, NodeToolFile), + ok = file:write_file(InstallUpgrade, InstallUpgradeFile), + ok = file:change_mode(NodeTool, 8#755), + ok = file:change_mode(InstallUpgrade, 8#755); + false -> + ok + end, extended_bin_file_contents(RelName, RelVsn, rlx_release:erts(Release), ErlOpts) end, %% We generate the start script by default, unless the user @@ -315,61 +334,57 @@ write_bin_file(State, Release, OutputDir, RelDir) -> ok = file:write_file(BareRel, StartFile), ok = file:change_mode(BareRel, 8#777) end, - copy_or_generate_vmargs_file(State, RelName, RelDir), - copy_or_generate_sys_config_file(State, Release, OutputDir, RelDir). + copy_or_generate_vmargs_file(State, Release, RelDir), + copy_or_generate_sys_config_file(State, RelDir), + include_erts(State, Release, OutputDir, RelDir). %% @doc copy vm.args or generate one to releases/VSN/vm.args --spec copy_or_generate_vmargs_file(rlx_state:t(), string(), file:name()) -> +-spec copy_or_generate_vmargs_file(rlx_state:t(), rlx_release:t(), file:name()) -> {ok, rlx_state:t()} | relx:error(). -copy_or_generate_vmargs_file(State, RelName, RelDir) -> +copy_or_generate_vmargs_file(State, Release, RelDir) -> RelVmargsPath = filename:join([RelDir, "vm.args"]), case rlx_state:vm_args(State) of undefined -> + RelName = erlang:atom_to_list(rlx_release:name(Release)), unless_exists_write_default(RelVmargsPath, vm_args_file(RelName)); ArgsPath -> case filelib:is_regular(ArgsPath) of false -> ?RLX_ERROR({vmargs_does_not_exist, ArgsPath}); true -> - ec_file:copy(ArgsPath, RelVmargsPath) + copy_or_symlink_config_file(State, ArgsPath, RelVmargsPath) end end. %% @doc copy config/sys.config or generate one to releases/VSN/sys.config --spec copy_or_generate_sys_config_file(rlx_state:t(), rlx_release:t(), - file:name(), file:name()) -> +-spec copy_or_generate_sys_config_file(rlx_state:t(), file:name()) -> {ok, rlx_state:t()} | relx:error(). -copy_or_generate_sys_config_file(State, Release, OutputDir, RelDir) -> +copy_or_generate_sys_config_file(State, RelDir) -> RelSysConfPath = filename:join([RelDir, "sys.config"]), case rlx_state:sys_config(State) of undefined -> - unless_exists_write_default(RelSysConfPath, sys_config_file()), - include_erts(State, Release, OutputDir, RelDir); + unless_exists_write_default(RelSysConfPath, sys_config_file()); ConfigPath -> case filelib:is_regular(ConfigPath) of false -> ?RLX_ERROR({config_does_not_exist, ConfigPath}); true -> - copy_or_symlink_sys_config_file(State, Release, OutputDir, RelDir, - ConfigPath, RelSysConfPath) + copy_or_symlink_config_file(State, ConfigPath, RelSysConfPath) end end. %% @doc copy config/sys.config or generate one to releases/VSN/sys.config --spec copy_or_symlink_sys_config_file(rlx_state:t(), rlx_release:t(), - file:name(), file:name(), - file:name(), file:name()) -> - {ok, rlx_state:t()} | relx:error(). -copy_or_symlink_sys_config_file(State, Release, OutputDir, RelDir, - ConfigPath, RelSysConfPath) -> +-spec copy_or_symlink_config_file(rlx_state:t(), file:name(), file:name()) -> + ok. +copy_or_symlink_config_file(State, ConfigPath, RelConfPath) -> + ensure_not_exist(RelConfPath), case rlx_state:dev_mode(State) of true -> - ok = file:make_symlink(ConfigPath, RelSysConfPath); + ok = file:make_symlink(ConfigPath, RelConfPath); _ -> - ok = ec_file:copy(ConfigPath, RelSysConfPath) - end, - include_erts(State, Release, OutputDir, RelDir). + ok = ec_file:copy(ConfigPath, RelConfPath) + end. %% @doc Optionally add erts directory to release, if defined. -spec include_erts(rlx_state:t(), rlx_release:t(), file:name(), file:name()) -> @@ -391,21 +406,6 @@ include_erts(State, Release, OutputDir, RelDir) -> ok = ec_file:remove(Erl), ok = file:write_file(Erl, erl_script(ErtsVersion)), ok = file:change_mode(Erl, 8#755), - case rlx_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"])), - NodeToolFile = nodetool_contents(), - InstallUpgradeFile = install_upgrade_escript_contents(), - NodeTool = filename:join([LocalErts, "bin", "nodetool"]), - InstallUpgrade = filename:join([LocalErts, "bin", "install_upgrade.escript"]), - ok = file:write_file(NodeTool, NodeToolFile), - ok = file:write_file(InstallUpgrade, InstallUpgradeFile), - ok = file:change_mode(NodeTool, 8#755), - ok = file:change_mode(InstallUpgrade, 8#755); - false -> - ok - end, make_boot_script(State, Release, OutputDir, RelDir) end; _ -> @@ -426,14 +426,14 @@ make_boot_script(State, Release, OutputDir, RelDir) -> systools:make_script(Name, CorrectedOptions) end) of ok -> - ec_cmd_log:error(rlx_state:log(State), + ec_cmd_log:info(rlx_state:log(State), "release successfully created!"), create_RELEASES(OutputDir, ReleaseFile), {ok, State}; error -> ?RLX_ERROR({release_script_generation_error, ReleaseFile}); {ok, _, []} -> - ec_cmd_log:error(rlx_state:log(State), + ec_cmd_log:info(rlx_state:log(State), "release successfully created!"), create_RELEASES(OutputDir, ReleaseFile), {ok, State}; @@ -636,638 +636,41 @@ unless_exists_write_default(Path, File) -> ok = file:write_file(Path, File) 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+\"$@\"} -">>]. +-spec ensure_not_exist(file:name()) -> ok. +ensure_not_exist(RelConfPath) -> + case ec_file:exists(RelConfPath) of + false -> + ok; + _ -> + ec_file:remove(RelConfPath) + end. +erl_script(ErtsVsn) -> + render(erl_script_dtl, [{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 - -cd $ROOTDIR - -$BINDIR/erlexec $ERL_OPTS $SYS_CONFIG -boot $REL_DIR/$REL_NAME $@">>]. + render(bin_dtl, [{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 - -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 $BINDIR/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 $BINDIR/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_NAME || BOOTFILE=start ;; - console_clean) BOOTFILE=start_clean ;; - console_boot) - shift - BOOTFILE=\"$1\" - shift - ;; - esac - # Setup beam-required vars - 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 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 -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 <file>|foreground|stop|restart|reboot|ping|console|console_clean|console_boot <file>|attach|remote_console|upgrade}\" - exit 1 - ;; -esac - -exit 0">>]. + render(extended_bin_dtl, [{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])). -">>]. + render(install_upgrade_escript_dtl). 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.">>]. + render(nodetool_dtl). 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. -[].">>]. + render(sys_config_dtl). 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 + render(vm_args_dtl, [{rel_name, RelName}]). -## Increase number of concurrent ports/sockets -##-env ERL_MAX_PORTS 4096 +render(Template) -> + render(Template, []). -## Tweak GC to run more often -##-env ERL_FULLSWEEP_AFTER 10">>]. +render(Template, Data) -> + {ok, Rendered} = Template:render(Data), + Rendered. diff --git a/src/rlx_prv_config.erl b/src/rlx_prv_config.erl index aa2e79c..da620be 100644 --- a/src/rlx_prv_config.erl +++ b/src/rlx_prv_config.erl @@ -188,7 +188,14 @@ load_terms({release, {RelName, Vsn}, {erts, ErtsVsn}, load_terms({vm_args, VmArgs}, {ok, State}) -> {ok, rlx_state:vm_args(State, filename:absname(VmArgs))}; load_terms({sys_config, SysConfig}, {ok, State}) -> - {ok, rlx_state:sys_config(State, filename:absname(SysConfig))}; + case rlx_state:sys_config(State) of + undefined -> + {ok, rlx_state:sys_config(State, filename:absname(SysConfig))}; + _ -> + {ok, State} + end; +load_terms({output_dir, OutputDir}, {ok, State}) -> + {ok, rlx_state:output_dir(State, filename:absname(OutputDir))}; load_terms({overlay_vars, OverlayVars}, {ok, State}) -> CurrentOverlayVars = rlx_state:get(State, overlay_vars), NewOverlayVars0 = list_of_overlay_vars_files(OverlayVars), diff --git a/src/rlx_prv_overlay.erl b/src/rlx_prv_overlay.erl index e8d2547..882c67e 100644 --- a/src/rlx_prv_overlay.erl +++ b/src/rlx_prv_overlay.erl @@ -30,6 +30,8 @@ -define(DIRECTORY_RE, ".*(\/|\\\\)$"). +-define(ERLYDTL_COMPILE_OPTS, [report_warnings, return_errors, {auto_escape, false}]). + -include("relx.hrl"). %%============================================================================ @@ -83,8 +85,8 @@ format_error({unable_to_render_template, FromFile, Reason}) -> io_lib:format("Unable to render template ~s because ~p", [FromFile, Reason]); format_error({unable_to_compile_template, FromFile, Reason}) -> - io_lib:format("Unable to compile template ~s because ~p", - [FromFile, Reason]); + io_lib:format("Unable to compile template ~s because \n~s", + [FromFile, [format_errors(F, Es) || {F, Es} <- Reason]]); format_error({unable_to_make_dir, Absolute, Error}) -> io_lib:format("Unable to make directory ~s because ~p", [Absolute, Error]). @@ -92,6 +94,25 @@ format_error({unable_to_make_dir, Absolute, Error}) -> %%%=================================================================== %%% Internal Functions %%%=================================================================== + +format_errors(File, [{none, Mod, E}|Es]) -> + [io_lib:format("~s~s: ~ts~n", + [rlx_util:indent(2), File, + Mod:format_error(E)]) + |format_errors(File, Es)]; +format_errors(File, [{{Line, Col}, Mod, E}|Es]) -> + [io_lib:format("~s~s:~w:~w: ~ts~n", + [rlx_util:indent(2), File, Line, Col, + Mod:format_error(E)]) + |format_errors(File, Es)]; +format_errors(File, [{Line, Mod, E}|Es]) -> + [io_lib:format("~s~s:~w: ~ts~n", + [rlx_util:indent(2), File, Line, + Mod:format_error(E)]) + |format_errors(File, Es)]; +format_errors(_, []) -> []. + + -spec generate_overlay_vars(rlx_state:t(), rlx_release:t()) -> {ok, rlx_state:t()} | relx:error(). generate_overlay_vars(State, Release) -> @@ -262,7 +283,7 @@ handle_errors(State, Result) -> {ok, rlx_state:t()} | relx:error(). do_individual_overlay(State, OverlayVars, {mkdir, Dir}) -> ModuleName = make_template_name("rlx_mkdir_template", Dir), - case erlydtl:compile(erlang:iolist_to_binary(Dir), ModuleName) of + case erlydtl:compile(erlang:iolist_to_binary(Dir), ModuleName, ?ERLYDTL_COMPILE_OPTS) of {ok, ModuleName} -> case render(ModuleName, OverlayVars) of {ok, IoList} -> @@ -278,7 +299,7 @@ do_individual_overlay(State, OverlayVars, {mkdir, Dir}) -> {error, Error} -> ?RLX_ERROR({dir_render_failed, Dir, Error}) end; - {error, Reason} -> + {error, Reason, _Warnings} -> ?RLX_ERROR({unable_to_compile_template, Dir, Reason}) end; do_individual_overlay(State, OverlayVars, {copy, From, To}) -> @@ -361,7 +382,7 @@ is_directory(ToFile0, ToFile1) -> ok | relx:error(). render_template(OverlayVars, Data) -> TemplateName = make_template_name("rlx_template_renderer", Data), - case erlydtl:compile(Data, TemplateName) of + case erlydtl:compile(Data, TemplateName, ?ERLYDTL_COMPILE_OPTS) of Good when Good =:= ok; Good =:= {ok, TemplateName} -> case render(TemplateName, OverlayVars) of {ok, IoData} -> @@ -369,7 +390,7 @@ render_template(OverlayVars, Data) -> {error, Reason} -> ?RLX_ERROR({unable_to_render_template, Data, Reason}) end; - {error, Reason} -> + {error, Reason, _Warnings} -> ?RLX_ERROR({unable_to_compile_template, Data, Reason}) end. @@ -397,7 +418,7 @@ write_template(OverlayVars, FromFile, ToFile) -> fun((term()) -> {ok, rlx_state:t()} | relx:error())) -> {ok, rlx_state:t()} | relx:error(). file_render_do(OverlayVars, Data, TemplateName, NextAction) -> - case erlydtl:compile(erlang:iolist_to_binary(Data), TemplateName) of + case erlydtl:compile(erlang:iolist_to_binary(Data), TemplateName, ?ERLYDTL_COMPILE_OPTS) of {ok, TemplateName} -> case render(TemplateName, OverlayVars) of {ok, IoList} -> @@ -405,7 +426,7 @@ file_render_do(OverlayVars, Data, TemplateName, NextAction) -> {error, Error} -> ?RLX_ERROR({render_failed, Data, Error}) end; - {error, Reason} -> + {error, Reason, _Warnings} -> ?RLX_ERROR({unable_to_compile_template, Data, Reason}) end. diff --git a/src/rlx_rel_discovery.erl b/src/rlx_rel_discovery.erl index 837ebe6..23c0ab9 100644 --- a/src/rlx_rel_discovery.erl +++ b/src/rlx_rel_discovery.erl @@ -38,12 +38,18 @@ -spec do(rlx_state:t(), [file:name()], [rlx_app_info:t()]) -> {ok, [rlx_release:t()]} | relx:error(). do(State, LibDirs, AppMeta) -> - ec_cmd_log:info(rlx_state:log(State), - fun() -> - ["Resolving available OTP Releases from directories:\n", - string:join([[rlx_util:indent(2), LibDir] || LibDir <- LibDirs], "\n")] - end), - resolve_rel_metadata(State, LibDirs, AppMeta). + case rlx_state:get(State, disable_rel_discovery, false) of + true -> + ec_cmd_log:debug(rlx_state:log(State), "Disbaled resolving of OTP releases"), + {ok, []}; + false -> + ec_cmd_log:info(rlx_state:log(State), + fun() -> + ["Resolving available OTP Releases from directories:\n", + string:join([[rlx_util:indent(2), LibDir] || LibDir <- LibDirs], "\n")] + end), + resolve_rel_metadata(State, LibDirs, AppMeta) + end. -spec format_error([ErrorDetail::term()]) -> iolist(). format_error(ErrorDetails) diff --git a/src/rlx_state.erl b/src/rlx_state.erl index bc4af5b..0d4051e 100644 --- a/src/rlx_state.erl +++ b/src/rlx_state.erl @@ -27,6 +27,7 @@ log/1, actions/1, output_dir/1, + output_dir/2, lib_dirs/1, add_lib_dirs/2, overrides/1, @@ -128,6 +129,7 @@ new(PropList, Targets) output_dir=proplists:get_value(output_dir, PropList, ""), lib_dirs=[to_binary(Dir) || Dir <- proplists:get_value(lib_dirs, PropList, [])], config_file=proplists:get_value(config, PropList, undefined), + sys_config=proplists:get_value(sys_config, PropList, undefined), dev_mode = proplists:get_value(dev_mode, PropList), actions = Targets, caller = Caller, @@ -186,6 +188,10 @@ log(#state_t{log=LogState}) -> output_dir(#state_t{output_dir=OutDir}) -> OutDir. +-spec output_dir(t(), Directory::file:filename()) -> t(). +output_dir(State, Directory) -> + State#state_t{output_dir=Directory}. + -spec lib_dirs(t()) -> [file:name()]. lib_dirs(#state_t{lib_dirs=LibDir}) -> LibDir. diff --git a/test/rlx_discover_SUITE.erl b/test/rlx_discover_SUITE.erl index 846c62b..8ea95c4 100644 --- a/test/rlx_discover_SUITE.erl +++ b/test/rlx_discover_SUITE.erl @@ -111,10 +111,12 @@ no_beam_case(Config) -> AppDir = filename:join([LibDir2, BadName]), write_app_file(AppDir, BadName, BadVsn), State0 = proplists:get_value(state, Config), - {DiscoverProvider, {ok, State1}} = rlx_provider:new(rlx_prv_discover, State0), + %% Deliberately disable release discovery when running `rlx_prv_discover` + State1 = rlx_state:put(State0, disable_rel_discovery, true), + {DiscoverProvider, {ok, State2}} = rlx_provider:new(rlx_prv_discover, State1), ?assertMatch({ok, _}, - rlx_provider:do(DiscoverProvider, State1)). + rlx_provider:do(DiscoverProvider, State2)). bad_ebin_case(Config) -> LibDir1 = proplists:get_value(lib1, Config), diff --git a/test/rlx_release_SUITE.erl b/test/rlx_release_SUITE.erl index c2ed984..8f27050 100644 --- a/test/rlx_release_SUITE.erl +++ b/test/rlx_release_SUITE.erl @@ -900,10 +900,14 @@ make_dev_mode_release(Config) -> SysConfig = filename:join([LibDir1, "config", "sys.config"]), write_config(SysConfig, [{this_is_a_test, "yup it is"}]), + VmArgs = filename:join([LibDir1, "config", "vm.args"]), + ec_file:write(VmArgs, ""), + ConfigFile = filename:join([LibDir1, "relx.config"]), write_config(ConfigFile, [{dev_mode, true}, {sys_config, SysConfig}, + {vm_args, VmArgs}, {release, {foo, "0.0.1"}, [goal_app_1, goal_app_2]}]), @@ -919,7 +923,9 @@ make_dev_mode_release(Config) -> ?assert(ec_file:is_symlink(filename:join([OutputDir, "lib", "goal_app_2-0.0.1"]))), ?assert(ec_file:is_symlink(filename:join([OutputDir, "lib", "lib_dep_1-0.0.1"]))), ?assert(ec_file:is_symlink(filename:join([OutputDir, "releases", "0.0.1", - "sys.config"]))). + "sys.config"]))), + ?assert(ec_file:is_symlink(filename:join([OutputDir, "releases", "0.0.1", + "vm.args"]))). %%%=================================================================== |