From 2a4ff9a61f50adce4c86b5414bf81e29bc013160 Mon Sep 17 00:00:00 2001 From: Tino Breddin Date: Fri, 5 Apr 2019 22:30:22 +0200 Subject: Refactor creation of .erlang.cookie for use in start/console procedures (#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 --- priv/templates/extended_bin | 27 ++++++++++++----- priv/templates/extended_bin_windows | 58 +++++++++++++++++++++++++++---------- 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/priv/templates/extended_bin b/priv/templates/extended_bin index 059f5bf..fde6729 100755 --- a/priv/templates/extended_bin +++ b/priv/templates/extended_bin @@ -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) ;; @@ -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)" @@ -310,10 +319,10 @@ relx_is_extension() { EXTENSION=$1 case "$EXTENSION" in {{{ extensions }}}) - echo "1" + echo "1" ;; *) - echo "0" + echo "0" ;; esac } @@ -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"" @@ -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" @@ -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" diff --git a/priv/templates/extended_bin_windows b/priv/templates/extended_bin_windows index 6d8f084..0db46cc 100644 --- a/priv/templates/extended_bin_windows +++ b/priv/templates/extended_bin_windows @@ -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 ) @@ -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 ) @@ -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" ( @@ -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 @( + set "erl_root=%%i" ) @set "erts_dir=%erl_root%\erts-%erts_vsn%" @set "rootdir=%erl_root%" @@ -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 @@ -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 @@ -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%" @@ -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 -- cgit v1.2.3