Skip to content

Commit

Permalink
Refactor creation of .erlang.cookie for use in start/console procedur…
Browse files Browse the repository at this point in the history
…es (#690)

* Randomize dummy node name

The static name used so far would result in name clashing when running
the script multiple times in short succession.

* Fix find_sys_config routine in win32 extended bin

* Remove escaping of double quotes

* Fix dummy command syntax

* Refactor creation of .erlang.cookie at startup

* Fix dummy node logic for win32
  • Loading branch information
tolbrino authored and tsloughter committed Apr 5, 2019
1 parent 33ac606 commit 2a4ff9a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 22 deletions.
27 changes: 20 additions & 7 deletions priv/templates/extended_bin
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fi

# OSX does not support readlink '-f' flag, work
# around that
case $OSTYPE in
case $OSTYPE in
darwin*)
SCRIPT=$(readlink $0 || true)
;;
Expand Down Expand Up @@ -127,17 +127,26 @@ find_erts_dir() {
if [ -d "$__erts_dir" ]; then
ERTS_DIR="$__erts_dir";
ROOTDIR="$RELEASE_ROOT_DIR"
# run a dummy distributed erlang node just to ensure that a cookie exists
$ERTS_DIR/bin/erl -sname dummy -boot no_dot_erlang -noshell -eval "halt()"
else
__erl="$(which erl)"
code="io:format(\"~s\", [code:root_dir()]), halt()."
__erl_root="$("$__erl" -sname dummy -boot no_dot_erlang -sasl errlog_type error -noshell -eval "$code")"
__erl_root="$("$__erl" -boot no_dot_erlang -sasl errlog_type error -noshell -eval "$code")"
ERTS_DIR="$__erl_root/erts-$ERTS_VSN"
ROOTDIR="$__erl_root"
fi
}

ensure_dot_erlang_cookie() {
# Run a dummy distributed erlang node just to ensure that $HOME/.erlang.cookie exists.
# This action is best-effort and could fail because of the way the system is set up.
# Failures are logged, but ignored.
DUMMY_NAME="dummy-$(relx_gen_id)"
if ! output="$($ERTS_DIR/bin/erl -sname ${DUMMY_NAME} -boot no_dot_erlang -noshell -eval "halt()")"
then
echo "Creating .erlang.cookie failed: ${output}"
fi
}

# Get node pid
relx_get_pid() {
if output="$(relx_nodetool rpcterms os getpid)"
Expand Down Expand Up @@ -310,10 +319,10 @@ relx_is_extension() {
EXTENSION=$1
case "$EXTENSION" in
{{{ extensions }}})
echo "1"
echo "1"
;;
*)
echo "0"
echo "0"
;;
esac
}
Expand All @@ -324,7 +333,7 @@ relx_get_extension_script() {
# of the form:
# foo_extension="path/to/foo_script";bar_extension="path/to/bar_script"
{{{extension_declarations}}}
# get the command extension (eg. foo) and
# get the command extension (eg. foo) and
# obtain the actual script filename that it
# refers to (eg. "path/to/foo_script"
eval echo "$"${EXTENSION}_extension""
Expand Down Expand Up @@ -512,6 +521,8 @@ case "$1" in
# Make sure log directory exists
mkdir -p "$RUNNER_LOG_DIR"

ensure_dot_erlang_cookie

relx_run_hooks "$PRE_START_HOOKS"
"$BINDIR/run_erl" -daemon "$PIPE_DIR" "$RUNNER_LOG_DIR" \
"exec \"$RELEASE_ROOT_DIR/bin/$REL_NAME\" \"$START_OPTION\" $ARGS --relx-disable-hooks"
Expand Down Expand Up @@ -682,6 +693,8 @@ case "$1" in
-pa ${__code_paths} -- "$@"
echo "Root: $ROOTDIR"

ensure_dot_erlang_cookie

# Log the startup
echo "$RELEASE_ROOT_DIR"
logger -t "$REL_NAME[$$]" "Starting up"
Expand Down
58 changes: 43 additions & 15 deletions priv/templates/extended_bin_windows
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@
@set vm_args=%rel_dir%\vm.args
@set progname=erl.exe
@set clean_boot_script=%release_root_dir%\bin\start_clean
@set erlsrv="%bindir%\erlsrv.exe"
@set epmd="%bindir%\epmd.exe"
@set escript="%bindir%\escript.exe"
@set werl="%bindir%\werl.exe"
@set nodetool="%release_root_dir%\bin\nodetool"
@set "erlsrv=%bindir%\erlsrv.exe"
@set "epmd=%bindir%\epmd.exe"
@set "escript=%bindir%\escript.exe"
@set "werl=%bindir%\werl.exe"
@set "erl=%bindir%\erl.exe"
@set "nodetool=%release_root_dir%\bin\nodetool"
@set "extensions0={{ extensions }}"
@set "extensions1=%extensions0:|= %"
@set "extensions=%extensions1: undefined=%"

:: Extract node type and name from vm.args
@for /f "usebackq tokens=1-2" %%I in (`findstr /b "\-name \-sname" "%vm_args%"`) do @(
@for /f "usebackq tokens=1-2" %%I in ('findstr /b "\-name \-sname" "%vm_args%"') do @(
set node_type=%%I
set node_name=%%J
)
Expand Down Expand Up @@ -77,7 +78,7 @@
)

:: Extract cookie from vm.args
@for /f "usebackq tokens=1-2" %%I in (`findstr /b \-setcookie "%vm_args%"`) do @(
@for /f "usebackq tokens=1-2" %%I in ('findstr /b \-setcookie "%vm_args%"') do @(
set cookie=%%J
)

Expand All @@ -86,7 +87,7 @@

:: Collect any additional VM args into erl_opts
@setlocal EnableDelayedExpansion
@for /f "usebackq tokens=1-2" %%I in (`findstr /r "^[^#]" "%vm_args%"`) do @(
@for /f "usebackq tokens=1-2" %%I in ('findstr /r "^[^#]" "%vm_args%"') do @(
if not "%%I" == "-name" (
if not "%%I" == "-sname" (
if not "%%I" == "-setcookie" (
Expand Down Expand Up @@ -143,11 +144,10 @@
:: Set the ERTS dir from erl
:set_erts_dir_from_erl
@for /f "delims=" %%i in ('where erl') do @(
set erl=%%i
set "erl=%%i"
)
@set dir_cmd="%erl%" -sname dummy -boot no_dot_erlang -noshell -eval "io:format(\"~s\", [filename:nativename(code:root_dir())])." -s init stop
@for /f "delims=" %%i in ('%%dir_cmd%%') do @(
set erl_root=%%i
@for /f "delims=" %%i in ('"%erl%" -boot no_dot_erlang -noshell -eval "io:format(\"~s\", [filename:nativename(code:root_dir())])." -s init stop') do @(

This comment has been minimized.

Copy link
@jlstarnes

jlstarnes Jan 4, 2020

This update takes data from the dir_cmd from line #148 and moves the contents inline on line #149; however, it is missing the outer %...% that surrounded the %dir_cmd% command that was on line #149. I believe may be causing an issue I noticed when running the generated {{name}}.cmd file, which complains about the “C:\Program “ path when trying to run with console option.

I recommend using dir_cmd that was removed on this update

set "erl_root=%%i"
)
@set "erts_dir=%erl_root%\erts-%erts_vsn%"
@set "rootdir=%erl_root%"
Expand All @@ -164,12 +164,13 @@
set sys_config=-config "%possible_sys%"
)
)
@goto :eof

:: Find the vm.args file
:find_vm_args
@if not exist "%m_args%" (
@if exist "%m_args%.orig" (
ren "%m_args%.orig" vm.args
@if not exist "%vm_args%" (
@if exist "%vm_args%.orig" (
ren "%vm_args%.orig" vm.args
)
)
@goto :eof
Expand Down Expand Up @@ -278,6 +279,7 @@ set description=Erlang node %node_name%%hostname% in %rootdir%

:: Start the Windows service
:start
@call :ensure_dot_erlang_cookie
@%erlsrv% start %service_name%
@goto :eof

Expand All @@ -299,6 +301,7 @@ set description=Erlang node %node_name%%hostname% in %rootdir%

:: Start a console
:console
@call :ensure_dot_erlang_cookie
@set boot=-boot "%boot_script%" -boot_var RELEASE_DIR "%release_root_dir%"
@start "%rel_name% console" %werl% %boot% %sys_config% ^
-args_file "%vm_args%"
Expand Down Expand Up @@ -351,3 +354,28 @@ set description=Erlang node %node_name%%hostname% in %rootdir%
@if not "%ext_test_3%"=="x%extensions%" exit /b 0

@exit /b 1

:ensure_dot_erlang_cookie

:: Run a dummy distributed erlang node just to ensure that %HOMEPATH%\.erlang.cookie exists.
:: This action is best-effort and could fail because of the way the system is set up.
:: Failures are logged, but ignored.

:: Create random dummy node name
@set /a "_rand_nr=%RANDOM%+100000"
@set "dummy_name=dummy-%_rand_nr%"

:: Run node and capture output in local var
@for /f "delims=" %%i in ('%erl% -sname %dummy_name% -boot no_dot_erlang -noshell -eval "halt()."') do @(
set "dummy_node_output=%%i"
)

:: Check for execution error
@if "%dummy_node_output%" neq "" @(
echo Creating .erlang.cookie failed: "%dummy_node_output%"
)

:: Clear all local variables
@set "dummy_node_output="

@exit /b 0

0 comments on commit 2a4ff9a

Please sign in to comment.