From 3d82071245e1c658eef0e70bae297a574c07e6d9 Mon Sep 17 00:00:00 2001 From: Luis Rascao Date: Thu, 9 Aug 2018 00:25:59 +0100 Subject: Capture internal start script arguments --- priv/templates/extended_bin | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/priv/templates/extended_bin b/priv/templates/extended_bin index 78f25e0..57f4cac 100755 --- a/priv/templates/extended_bin +++ b/priv/templates/extended_bin @@ -312,6 +312,16 @@ relx_run_hooks() { done } +relx_disable_hooks() { + PRE_START_HOOKS="" + POST_START_HOOKS="" + PRE_STOP_HOOKS="" + POST_STOP_HOOKS="" + PRE_INSTALL_UPGRADE_HOOKS="" + POST_INSTALL_UPGRADE_HOOKS="" + STATUS_HOOK="" +} + relx_is_extension() { EXTENSION=$1 case "$EXTENSION" in @@ -346,6 +356,26 @@ relx_run_extension() { [ "$SCRIPT_DIR/$EXTENSION_SCRIPT" ] && . "$SCRIPT_DIR/$EXTENSION_SCRIPT" $@ } +# given a list of arguments, identify the internal ones +# --relx-disable-hooks +# and process them accordingly +process_internal_args() { + for arg in $@ + do + shift + case "$arg" in + --relx-disable-hooks) + relx_disable_hooks + ;; + *) + ;; + esac + done +} + +# process internal arguments +process_internal_args $@ + find_erts_dir export ROOTDIR="$RELEASE_ROOT_DIR" export BINDIR="$ERTS_DIR/bin" -- cgit v1.2.3 From e9a8b13668ca3dd1470b8b65320e1491c213c521 Mon Sep 17 00:00:00 2001 From: Luis Rascao Date: Thu, 9 Aug 2018 00:26:31 +0100 Subject: Prevent double hook invocation on 'start' command --- priv/templates/extended_bin | 6 +++++- test/rlx_extended_bin_SUITE.erl | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/priv/templates/extended_bin b/priv/templates/extended_bin index 57f4cac..a06c2c3 100755 --- a/priv/templates/extended_bin +++ b/priv/templates/extended_bin @@ -530,7 +530,7 @@ case "$1" in 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" + "exec \"$RELEASE_ROOT_DIR/bin/$REL_NAME\" \"$START_OPTION\" --relx-disable-hooks $ARGS" relx_run_hooks "$POST_START_HOOKS" ;; @@ -705,6 +705,7 @@ case "$1" in echo "$RELEASE_ROOT_DIR" logger -t "$REL_NAME[$$]" "Starting up" + relx_run_hooks "$PRE_START_HOOKS" # Start the VM exec "$BINDIR/erlexec" $FOREGROUNDOPTIONS \ -boot "$BOOTFILE" -mode "$CODE_LOADING_MODE" \ @@ -712,6 +713,9 @@ case "$1" in -config "$RELX_CONFIG_PATH" \ -args_file "$VMARGS_PATH" \ -pa ${__code_paths} -- "$@" + # exec will replace the current image and nothing else gets + # executed from this point on, this explains the absence + # of the pre start hook ;; rpc) # Make sure a node IS running diff --git a/test/rlx_extended_bin_SUITE.erl b/test/rlx_extended_bin_SUITE.erl index 86a9d23..3824156 100644 --- a/test/rlx_extended_bin_SUITE.erl +++ b/test/rlx_extended_bin_SUITE.erl @@ -49,6 +49,7 @@ replace_os_vars_dev_mode/1, replace_os_vars_twice/1, custom_start_script_hooks/1, + custom_start_script_hooks_console/1, builtin_wait_for_vm_start_script_hook/1, builtin_pid_start_script_hook/1, builtin_wait_for_process_start_script_hook/1, @@ -88,7 +89,8 @@ all() -> ping, shortname_ping, longname_ping, attach, pid, restart, reboot, escript, remote_console, shortname_remote_console, replace_os_vars, replace_os_vars_sys_config_vm_args_src, replace_os_vars_multi_node, replace_os_vars_included_config, - replace_os_vars_custom_location, replace_os_vars_dev_mode, replace_os_vars_twice, custom_start_script_hooks, + replace_os_vars_custom_location, replace_os_vars_dev_mode, replace_os_vars_twice, + custom_start_script_hooks, custom_start_script_hooks_console, builtin_wait_for_vm_start_script_hook, builtin_pid_start_script_hook, builtin_wait_for_process_start_script_hook, mixed_custom_and_builtin_start_script_hooks, builtin_status_script, custom_status_script, @@ -1359,6 +1361,42 @@ builtin_pid_start_script_hook(Config) -> os:cmd(filename:join([OutputDir, "foo", "bin", "foo stop"])), ok. +custom_start_script_hooks_console(Config) -> + LibDir1 = proplists:get_value(lib1, Config), + + rlx_test_utils:create_app(LibDir1, "goal_app", "0.0.1", [stdlib,kernel], []), + + ConfigFile = filename:join([LibDir1, "relx.config"]), + rlx_test_utils:write_config(ConfigFile, + [{release, {foo, "0.0.1"}, + [goal_app]}, + {lib_dirs, [filename:join(LibDir1, "*")]}, + {generate_start_script, true}, + {extended_start_script, true}, + {extended_start_script_hooks, [ + {pre_start, [ + {custom, "hooks/pre_start"} + ]} + ]}, + {mkdir, "scripts"}, + {overlay, [{copy, "./pre_start", "bin/hooks/pre_start"}]} + ]), + + %% write the hook scripts, each of them will write an erlang term to a file + %% that will later be consulted + ok = file:write_file(filename:join([LibDir1, "./pre_start"]), + "#!/bin/bash\n# $*\necho \\{pre_start, $REL_NAME, \\'$NAME\\', $COOKIE\\}. >> test"), + + OutputDir = filename:join([proplists:get_value(priv_dir, Config), + rlx_test_utils:create_random_name("relx-output")]), + {ok, _State} = relx:do(foo, undefined, [], [LibDir1], 3, + OutputDir, ConfigFile), + %% now start/stop the release to make sure the script hooks are really getting + %% executed + {ok, _} = sh(filename:join([OutputDir, "foo", "bin", "foo console &"])), + %% now check that the output file contains the expected format + {ok,[{pre_start, foo, _, foo}]} = file:consult(filename:join([OutputDir, "foo", "test"])). + builtin_wait_for_vm_start_script_hook(Config) -> LibDir1 = proplists:get_value(lib1, Config), -- cgit v1.2.3