Skip to content

Commit

Permalink
don't require a cookie in the start script (#708)
Browse files Browse the repository at this point in the history
* don't require a cookie in the start script

* remove ensure cookie function from windows script

* Add updated cookie handling for win32
  • Loading branch information
tsloughter authored Apr 12, 2019
1 parent 2a4ff9a commit a6107ec
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 62 deletions.
62 changes: 28 additions & 34 deletions priv/templates/extended_bin
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
39 changes: 11 additions & 28 deletions priv/templates/extended_bin_windows
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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%"
Expand Down Expand Up @@ -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

0 comments on commit a6107ec

Please sign in to comment.