From c6b02bc13f8def3e0c9a53cdcd9a1ec70d804eca Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Thu, 30 May 2024 16:47:33 +0800 Subject: [PATCH] feat: support starting emqx from relup dir We put all of the unpacked files into `relup` dir, and warn the user if boot from it --- bin/emqx | 109 +++++++++++--------- rebar.config.erl | 4 +- relup/examples/5.6.1-to-5.6.1+patch.A.relup | 3 +- 3 files changed, 63 insertions(+), 53 deletions(-) diff --git a/bin/emqx b/bin/emqx index a2f9e2691..d455b97c9 100755 --- a/bin/emqx +++ b/bin/emqx @@ -13,53 +13,6 @@ if [ "$DEBUG" -eq 2 ]; then export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }' fi -# We need to find real directory with emqx files on all platforms -# even when bin/emqx is symlinked on several levels -# - readlink -f works perfectly, but `-f` flag has completely different meaning in BSD version, -# so we can't use it universally. -# - `stat -f%R` on MacOS does exactly what `readlink -f` does on Linux, but we can't use it -# as a universal solution either because GNU stat has different syntax and this argument is invalid. -# Also, version of stat which supports this syntax is only available since MacOS 12 -if [ "$(uname -s)" == 'Darwin' ]; then - product_version="$(sw_vers -productVersion | cut -d '.' -f 1)" - if [ "$product_version" -ge 12 ]; then - # if homebrew coreutils package is installed, GNU version of stat can take precedence, - # so we use absolute path to ensure we are calling MacOS default - RUNNER_ROOT_DIR="$(cd "$(dirname "$(/usr/bin/stat -f%R "$0" || echo "$0")")"/..; pwd -P)" - else - # try our best to resolve link on MacOS <= 11 - RUNNER_ROOT_DIR="$(cd "$(dirname "$(readlink "$0" || echo "$0")")"/..; pwd -P)" - fi -else - RUNNER_ROOT_DIR="$(cd "$(dirname "$(realpath "$0" || echo "$0")")"/..; pwd -P)" -fi - -# shellcheck disable=SC1090,SC1091 -. "$RUNNER_ROOT_DIR"/releases/emqx_vars - -# defined in emqx_vars -export RUNNER_ROOT_DIR -export EMQX_ETC_DIR -export REL_VSN -export SCHEMA_MOD -export IS_ENTERPRISE - -RUNNER_SCRIPT="$RUNNER_BIN_DIR/$REL_NAME" -CODE_LOADING_MODE="${CODE_LOADING_MODE:-embedded}" -REL_DIR="$RUNNER_ROOT_DIR/releases/$REL_VSN" - -WHOAMI=$(whoami 2>/dev/null || id -u) - -# hocon try to read environment variables starting with "EMQX_" -export HOCON_ENV_OVERRIDE_PREFIX='EMQX_' - -export ERTS_DIR="$RUNNER_ROOT_DIR/erts-$ERTS_VSN" -export BINDIR="$ERTS_DIR/bin" -export EMU="beam" -export PROGNAME="erl" -export ERTS_LIB_DIR="$RUNNER_ROOT_DIR/lib" -DYNLIBS_DIR="$RUNNER_ROOT_DIR/dynlibs" - logerr() { if [ "${TERM:-dumb}" = dumb ]; then echo -e "ERROR: $*" 1>&2 @@ -89,6 +42,64 @@ die() { exit "$errno" } +# We need to find real directory with emqx files on all platforms +# even when bin/emqx is symlinked on several levels +# - readlink -f works perfectly, but `-f` flag has completely different meaning in BSD version, +# so we can't use it universally. +# - `stat -f%R` on MacOS does exactly what `readlink -f` does on Linux, but we can't use it +# as a universal solution either because GNU stat has different syntax and this argument is invalid. +# Also, version of stat which supports this syntax is only available since MacOS 12 +if [ "$(uname -s)" == 'Darwin' ]; then + product_version="$(sw_vers -productVersion | cut -d '.' -f 1)" + if [ "$product_version" -ge 12 ]; then + # if homebrew coreutils package is installed, GNU version of stat can take precedence, + # so we use absolute path to ensure we are calling MacOS default + RUNNER_ROOT_DIR="$(cd "$(dirname "$(/usr/bin/stat -f%R "$0" || echo "$0")")"/..; pwd -P)" + else + # try our best to resolve link on MacOS <= 11 + RUNNER_ROOT_DIR="$(cd "$(dirname "$(readlink "$0" || echo "$0")")"/..; pwd -P)" + fi +else + RUNNER_ROOT_DIR="$(cd "$(dirname "$(realpath "$0" || echo "$0")")"/..; pwd -P)" +fi + +BASE_RUNNER_ROOT_DIR="${BASE_RUNNER_ROOT_DIR:-$RUNNER_ROOT_DIR}" + +if [ -f "$RUNNER_ROOT_DIR/relup/version" ]; then + TARGET_VSN=$(cat "$RUNNER_ROOT_DIR/relup/version") + export BASE_RUNNER_ROOT_DIR + logwarn "Loading emqx from hot upgrade dir: $RUNNER_ROOT_DIR/relup" + exec $RUNNER_ROOT_DIR/relup/$TARGET_VSN/bin/emqx "$@" +else + logdebug "Loading emqx from $RUNNER_ROOT_DIR" +fi + +# shellcheck disable=SC1090,SC1091 +. "$RUNNER_ROOT_DIR"/releases/emqx_vars + +# defined in emqx_vars +export RUNNER_ROOT_DIR +export EMQX_ETC_DIR +export REL_VSN +export SCHEMA_MOD +export IS_ENTERPRISE + +RUNNER_SCRIPT="$RUNNER_BIN_DIR/$REL_NAME" +CODE_LOADING_MODE="${CODE_LOADING_MODE:-embedded}" +REL_DIR="$RUNNER_ROOT_DIR/releases/$REL_VSN" + +WHOAMI=$(whoami 2>/dev/null || id -u) + +# hocon try to read environment variables starting with "EMQX_" +export HOCON_ENV_OVERRIDE_PREFIX='EMQX_' + +export ERTS_DIR="$RUNNER_ROOT_DIR/erts-$ERTS_VSN" +export BINDIR="$ERTS_DIR/bin" +export EMU="beam" +export PROGNAME="erl" +export ERTS_LIB_DIR="$RUNNER_ROOT_DIR/lib" +DYNLIBS_DIR="$RUNNER_ROOT_DIR/dynlibs" + assert_node_alive() { if ! relx_nodetool "ping" > /dev/null; then exit 1 @@ -598,7 +609,7 @@ DATA_DIR="$(get_boot_config 'node.data_dir')" DATA_DIR="${DATA_DIR%/}" if [[ $DATA_DIR != /* ]]; then # relative path - DATA_DIR="${RUNNER_ROOT_DIR}/${DATA_DIR}" + DATA_DIR="${BASE_RUNNER_ROOT_DIR}/${DATA_DIR}" fi CONFIGS_DIR="$DATA_DIR/configs" mkdir -p "$CONFIGS_DIR" @@ -1060,7 +1071,7 @@ nodetool_shutdown() { logger -t "${REL_NAME}[${PID}]" "STOP: OK" } -cd "$RUNNER_ROOT_DIR" +cd "$BASE_RUNNER_ROOT_DIR" case "${COMMAND}" in start) diff --git a/rebar.config.erl b/rebar.config.erl index d01990f33..d96b8165d 100644 --- a/rebar.config.erl +++ b/rebar.config.erl @@ -392,9 +392,9 @@ overlay_vars_pkg(bin) -> {platform_etc_dir, "etc"}, {platform_plugins_dir, "plugins"}, {runner_bin_dir, "$RUNNER_ROOT_DIR/bin"}, - {emqx_etc_dir, "$RUNNER_ROOT_DIR/etc"}, + {emqx_etc_dir, "$BASE_RUNNER_ROOT_DIR/etc"}, {runner_lib_dir, "$RUNNER_ROOT_DIR/lib"}, - {runner_log_dir, "$RUNNER_ROOT_DIR/log"}, + {runner_log_dir, "$BASE_RUNNER_ROOT_DIR/log"}, {runner_user, ""}, {is_elixir, "no"} ]; diff --git a/relup/examples/5.6.1-to-5.6.1+patch.A.relup b/relup/examples/5.6.1-to-5.6.1+patch.A.relup index 3f0d7676e..1c833c579 100644 --- a/relup/examples/5.6.1-to-5.6.1+patch.A.relup +++ b/relup/examples/5.6.1-to-5.6.1+patch.A.relup @@ -14,8 +14,7 @@ target_version => "5.6.1+patch.A", from_version => "5.6.1", code_changes => - [ {load_module, emqx_post_upgrade} - , {load_module, emqx_broker} + [ {load_module, emqx_broker} , {load_module, emqx_metrics} , {load_module, emqx_persistent_message} , {load_module, emqx_dashboard_monitor}