From a6107ec667820de9f6b6d6ae835b0f3b7e613a58 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Fri, 12 Apr 2019 08:11:14 -0600 Subject: don't require a cookie in the start script (#708) * don't require a cookie in the start script * remove ensure cookie function from windows script * Add updated cookie handling for win32 --- priv/templates/extended_bin | 62 +++++++++++++++++-------------------- priv/templates/extended_bin_windows | 39 +++++++---------------- 2 files changed, 39 insertions(+), 62 deletions(-) diff --git a/priv/templates/extended_bin b/priv/templates/extended_bin index fde6729..e224c62 100755 --- a/priv/templates/extended_bin +++ b/priv/templates/extended_bin @@ -136,17 +136,6 @@ find_erts_dir() { 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)" @@ -161,10 +150,19 @@ relx_get_pid() { relx_get_nodename() { id="longname$(relx_gen_id)-${NAME}" - "$BINDIR/erl" -boot start_clean \ - -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \ - -eval '[_,H]=re:split(atom_to_list(node()),"@",[unicode,{return,list}]), io:format("~s~n",[H]), halt()' \ - -noshell ${NAME_TYPE} $id + if [ -z "$COOKIE" ]; then + "$BINDIR/erl" -boot start_clean \ + -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \ + -eval '[_,H]=re:split(atom_to_list(node()),"@",[unicode,{return,list}]), io:format("~s~n",[H]), halt()' \ + -noshell ${NAME_TYPE} $id + else + # running with setcookie prevents a ~/.erlang.cookie from being created + "$BINDIR/erl" -boot start_clean \ + -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \ + -eval '[_,H]=re:split(atom_to_list(node()),"@",[unicode,{return,list}]), io:format("~s~n",[H]), halt()' \ + -setcookie ${COOKIE} \ + -noshell ${NAME_TYPE} $id + fi } # Connect to a remote node @@ -456,6 +454,21 @@ NAME_ARG=$(eval echo "${NAME_ARG}") NAME_TYPE="$(echo "$NAME_ARG" | awk '{print $1}')" NAME="$(echo "$NAME_ARG" | awk '{print $2}')" +# Extract the target cookie +# Do this before relx_get_nodename so we can use it and not create a ~/.erlang.cookie +COOKIE_ARG="$(grep '^-setcookie' "$VMARGS_PATH" || true)" +DEFAULT_COOKIE_FILE="$HOME/.erlang.cookie" +if [ -z "$COOKIE_ARG" ]; then + if [ -f "$DEFAULT_COOKIE_FILE" ]; then + COOKIE="$(cat $DEFAULT_COOKIE_FILE)" + else + echo "No cookie is set or found. This limits the scripts functionality, installing, upgrading, rpc and getting a list of versions will not work." + fi +else + # Extract cookie name from COOKIE_ARG + COOKIE="$(echo "$COOKIE_ARG" | awk '{print $2}')" +fi + # User can specify an sname without @hostname # This will fail when creating remote shell # So here we check for @ and add @hostname if missing @@ -470,21 +483,6 @@ export NAME test -z "$PIPE_DIR" && PIPE_BASE_DIR='/tmp/erl_pipes/' PIPE_DIR="${PIPE_DIR:-/tmp/erl_pipes/$NAME/}" -# Extract the target cookie -COOKIE_ARG="$(grep '^-setcookie' "$VMARGS_PATH" || true)" -DEFAULT_COOKIE_FILE="$HOME/.erlang.cookie" -if [ -z "$COOKIE_ARG" ]; then - if [ -f "$DEFAULT_COOKIE_FILE" ]; then - COOKIE="$(cat $DEFAULT_COOKIE_FILE)" - else - echo "vm.args needs to have a -setcookie, or $DEFAULT_COOKIE_FILE (its permission must be 400) is required." - exit 1 - fi -else - # Extract cookie name from COOKIE_ARG - COOKIE="$(echo "$COOKIE_ARG" | awk '{print $2}')" -fi - VM_ARGS="$(grep -v -E '^#|^-name|^-sname|^-setcookie|^-heart|^-args_file' "$VMARGS_PATH" | xargs | sed -e 's/ / /g')" cd "$ROOTDIR" @@ -521,8 +519,6 @@ 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" @@ -693,8 +689,6 @@ 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 0db46cc..16e3d96 100644 --- a/priv/templates/extended_bin_windows +++ b/priv/templates/extended_bin_windows @@ -77,9 +77,18 @@ set "hostname=@%hostname%" ) -:: Extract cookie from vm.args +:: Extract the target cookie +:: Do this before relx_get_nodename so we can use it and not create a ~/.erlang.cookie @for /f "usebackq tokens=1-2" %%I in ('findstr /b \-setcookie "%vm_args%"') do @( - set cookie=%%J + set "cookie=%%J" +) +@set "default_cookie_file=%USERPROFILE%\.erlang.cookie" +@if "%cookie%" == "" @( + if exist "%default_cookie_file%" ( + set /p cookie=<%default_cookie_file% + ) else ( + echo No cookie is set or found. This limits the scripts functionality, installing, upgrading, rpc and getting a list of versions will not work. + ) ) :: Write the erl.ini file to set up paths relative to this script @@ -279,7 +288,6 @@ 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 @@ -301,7 +309,6 @@ 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%" @@ -355,27 +362,3 @@ set description=Erlang node %node_name%%hostname% in %rootdir% @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