From c62f7e5baae2b0edcc2ad60f203330a09f05c08a Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Wed, 13 Apr 2022 13:05:57 +0200 Subject: [PATCH 01/10] chore: fix typos in bin/emqx --- bin/emqx | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/bin/emqx b/bin/emqx index 472095e45..210304296 100755 --- a/bin/emqx +++ b/bin/emqx @@ -102,14 +102,15 @@ usage() { echo "For example $REL_NAME escript /path/to/my/escript my_arg1 my_arg2" ;; attach) - echo "This command is applicable when EMQX is started in daemon" - echo "mode. it attaches the current shell to EMQX's control console" - echo "through a named pipe" + echo "This command is applicable when EMQX is started in daemon mode." + echo "It attaches the current shell to EMQX's control console" + echo "through a named pipe." echo "WARNING: try to use the safer alternative, remote_console command." ;; remote_console) - echo "Start a dummy Erlang or Elixir node and hidden-connect EMQX to" - echo "with an interactive Erlang or Elixir shell" + echo "Start an interactive shell running an Erlang or Elixir node which " + echo "hidden-connects to the running EMQX node". + echo "This command is mostly used for troubleshooting." ;; ertspath) echo "Print path to Erlang runtime dir" @@ -131,7 +132,7 @@ usage() { echo "Print EMQX installation root dir" ;; eval) - echo "Evaluate an Erlang or Elxir expression in the EMQX node" + echo "Evaluate an Erlang or Elixir expression in the EMQX node" ;; eval-erl) echo "Evaluate an Erlang expression in the EMQX node, even on Elixir node" @@ -194,7 +195,7 @@ usage() { echo "More:" echo " Shell attach: remote_console | attach" echo " Up/Down-grade: upgrade | downgrade | install | uninstall" - echo " Install info: ertspath | root_dir | versions | root_dir" + echo " Install info: ertspath | root_dir | versions" echo " Runtime info: pid | ping | versions" echo " Advanced: console_clean | escript | rpc | rpcterms | eval | eval-erl" echo '' @@ -206,8 +207,11 @@ usage() { COMMAND="${1:-}" if [ -z "$COMMAND" ]; then - usage 'nil' + usage 'help' exit 1 +elif [ "$COMMAND" = 'help' ]; then + usage 'help' + exit 0 fi if [ "${2:-}" = 'help' ]; then From c8242816b097f315b3d2a4bf5c7bbb195df030db Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Wed, 13 Apr 2022 13:14:58 +0200 Subject: [PATCH 02/10] refactor(bin/emqx): no need to template-render RUNNER_ROOT_DIR --- bin/emqx | 4 ++-- mix.exs | 2 -- rebar.config.erl | 2 -- rel/emqx_vars | 1 - 4 files changed, 2 insertions(+), 7 deletions(-) diff --git a/bin/emqx b/bin/emqx index 210304296..3a7e959c3 100755 --- a/bin/emqx +++ b/bin/emqx @@ -9,9 +9,9 @@ if [ "$DEBUG" -eq 1 ]; then set -x fi -ROOT_DIR="$(cd "$(dirname "$(readlink "$0" || echo "$0")")"/..; pwd -P)" +RUNNER_ROOT_DIR="$(cd "$(dirname "$(readlink "$0" || echo "$0")")"/..; pwd -P)" # shellcheck disable=SC1090,SC1091 -. "$ROOT_DIR"/releases/emqx_vars +. "$RUNNER_ROOT_DIR"/releases/emqx_vars # defined in emqx_vars export RUNNER_ROOT_DIR diff --git a/mix.exs b/mix.exs index 5ee200120..4fa629381 100644 --- a/mix.exs +++ b/mix.exs @@ -537,7 +537,6 @@ defmodule EMQXUmbrella.MixProject do platform_lib_dir: "lib", platform_log_dir: "log", platform_plugins_dir: "plugins", - runner_root_dir: "$(cd $(dirname $(readlink $0 || echo $0))/..; pwd -P)", runner_bin_dir: "$RUNNER_ROOT_DIR/bin", emqx_etc_dir: "$RUNNER_ROOT_DIR/etc", runner_lib_dir: "$RUNNER_ROOT_DIR/lib", @@ -563,7 +562,6 @@ defmodule EMQXUmbrella.MixProject do platform_lib_dir: "", platform_log_dir: "/var/log/emqx", platform_plugins_dir: "/var/lib/emqx/plugins", - runner_root_dir: "/usr/lib/emqx", runner_bin_dir: "/usr/bin", emqx_etc_dir: "/etc/emqx", runner_lib_dir: "$RUNNER_ROOT_DIR/lib", diff --git a/rebar.config.erl b/rebar.config.erl index fc8d6d33b..9daddc19a 100644 --- a/rebar.config.erl +++ b/rebar.config.erl @@ -273,7 +273,6 @@ overlay_vars_pkg(bin) -> , {platform_lib_dir, "lib"} , {platform_log_dir, "log"} , {platform_plugins_dir, "plugins"} - , {runner_root_dir, "$(cd $(dirname $(readlink $0 || echo $0))/..; pwd -P)"} , {runner_bin_dir, "$RUNNER_ROOT_DIR/bin"} , {emqx_etc_dir, "$RUNNER_ROOT_DIR/etc"} , {runner_lib_dir, "$RUNNER_ROOT_DIR/lib"} @@ -288,7 +287,6 @@ overlay_vars_pkg(pkg) -> , {platform_lib_dir, ""} , {platform_log_dir, "/var/log/emqx"} , {platform_plugins_dir, "/var/lib/emqx/plugins"} - , {runner_root_dir, "/usr/lib/emqx"} , {runner_bin_dir, "/usr/bin"} , {emqx_etc_dir, "/etc/emqx"} , {runner_lib_dir, "$RUNNER_ROOT_DIR/lib"} diff --git a/rel/emqx_vars b/rel/emqx_vars index fea0e3fb3..e5eb012b0 100644 --- a/rel/emqx_vars +++ b/rel/emqx_vars @@ -6,7 +6,6 @@ REL_VSN="{{ release_version }}" ERTS_VSN="{{ erts_vsn }}" ERL_OPTS="{{ erl_opts }}" -RUNNER_ROOT_DIR="{{ runner_root_dir }}" RUNNER_BIN_DIR="{{ runner_bin_dir }}" RUNNER_LOG_DIR="{{ runner_log_dir }}" RUNNER_LIB_DIR="{{ runner_lib_dir }}" From 1c50841dea72524ca261c5d1a8e81de960109ede Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Wed, 13 Apr 2022 13:15:54 +0200 Subject: [PATCH 03/10] chore(build): delete unused template var platform_bin_dir --- mix.exs | 2 -- rebar.config.erl | 6 ++---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/mix.exs b/mix.exs index 4fa629381..1e75eb0d4 100644 --- a/mix.exs +++ b/mix.exs @@ -531,7 +531,6 @@ defmodule EMQXUmbrella.MixProject do defp template_vars(release, release_type, :bin = _package_type, edition_type) do [ - platform_bin_dir: "bin", platform_data_dir: "data", platform_etc_dir: "etc", platform_lib_dir: "lib", @@ -556,7 +555,6 @@ defmodule EMQXUmbrella.MixProject do defp template_vars(release, release_type, :pkg = _package_type, edition_type) do [ - platform_bin_dir: "", platform_data_dir: "/var/lib/emqx", platform_etc_dir: "/etc/emqx", platform_lib_dir: "", diff --git a/rebar.config.erl b/rebar.config.erl index 9daddc19a..5e1ec63bb 100644 --- a/rebar.config.erl +++ b/rebar.config.erl @@ -267,8 +267,7 @@ overlay_vars_edition(ee) -> %% vars per packaging type, bin(zip/tar.gz/docker) or pkg(rpm/deb) overlay_vars_pkg(bin) -> - [ {platform_bin_dir, "bin"} - , {platform_data_dir, "data"} + [ {platform_data_dir, "data"} , {platform_etc_dir, "etc"} , {platform_lib_dir, "lib"} , {platform_log_dir, "log"} @@ -281,8 +280,7 @@ overlay_vars_pkg(bin) -> , {is_elixir, "no"} ]; overlay_vars_pkg(pkg) -> - [ {platform_bin_dir, ""} - , {platform_data_dir, "/var/lib/emqx"} + [ {platform_data_dir, "/var/lib/emqx"} , {platform_etc_dir, "/etc/emqx"} , {platform_lib_dir, ""} , {platform_log_dir, "/var/log/emqx"} From 438be98e152c12a7b855c6b9ae7907ff160f57bc Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Wed, 13 Apr 2022 13:17:01 +0200 Subject: [PATCH 04/10] chore(build): delete unused template-var platform_lib_dir --- mix.exs | 2 -- rebar.config.erl | 2 -- 2 files changed, 4 deletions(-) diff --git a/mix.exs b/mix.exs index 1e75eb0d4..2478a28c1 100644 --- a/mix.exs +++ b/mix.exs @@ -533,7 +533,6 @@ defmodule EMQXUmbrella.MixProject do [ platform_data_dir: "data", platform_etc_dir: "etc", - platform_lib_dir: "lib", platform_log_dir: "log", platform_plugins_dir: "plugins", runner_bin_dir: "$RUNNER_ROOT_DIR/bin", @@ -557,7 +556,6 @@ defmodule EMQXUmbrella.MixProject do [ platform_data_dir: "/var/lib/emqx", platform_etc_dir: "/etc/emqx", - platform_lib_dir: "", platform_log_dir: "/var/log/emqx", platform_plugins_dir: "/var/lib/emqx/plugins", runner_bin_dir: "/usr/bin", diff --git a/rebar.config.erl b/rebar.config.erl index 5e1ec63bb..1cc18b434 100644 --- a/rebar.config.erl +++ b/rebar.config.erl @@ -269,7 +269,6 @@ overlay_vars_edition(ee) -> overlay_vars_pkg(bin) -> [ {platform_data_dir, "data"} , {platform_etc_dir, "etc"} - , {platform_lib_dir, "lib"} , {platform_log_dir, "log"} , {platform_plugins_dir, "plugins"} , {runner_bin_dir, "$RUNNER_ROOT_DIR/bin"} @@ -282,7 +281,6 @@ overlay_vars_pkg(bin) -> overlay_vars_pkg(pkg) -> [ {platform_data_dir, "/var/lib/emqx"} , {platform_etc_dir, "/etc/emqx"} - , {platform_lib_dir, ""} , {platform_log_dir, "/var/log/emqx"} , {platform_plugins_dir, "/var/lib/emqx/plugins"} , {runner_bin_dir, "/usr/bin"} From 72405f977cf3d94039433586ed11f9d22ae1f30c Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Wed, 13 Apr 2022 13:49:11 +0200 Subject: [PATCH 05/10] refactor(bin/emqx): remove unnecessary intermediate env variables --- bin/emqx | 17 ++++++++--------- rel/emqx_vars | 1 - 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/bin/emqx b/bin/emqx index 3a7e959c3..f921c4187 100755 --- a/bin/emqx +++ b/bin/emqx @@ -31,8 +31,7 @@ mkdir -p "$RUNNER_LOG_DIR" # hocon try to read environment variables starting with "EMQX_" export HOCON_ENV_OVERRIDE_PREFIX='EMQX_' -export ROOTDIR="$RUNNER_ROOT_DIR" -export ERTS_DIR="$ROOTDIR/erts-$ERTS_VSN" +export ERTS_DIR="$RUNNER_ROOT_DIR/erts-$ERTS_VSN" export BINDIR="$ERTS_DIR/bin" export EMU="beam" export PROGNAME="erl" @@ -113,7 +112,7 @@ usage() { echo "This command is mostly used for troubleshooting." ;; ertspath) - echo "Print path to Erlang runtime dir" + echo "Print path to Erlang runtime bin dir" ;; rpc) echo "Usge $REL_NAME rpc MODULE FUNCTION [ARGS, ...]" @@ -341,7 +340,7 @@ relx_gen_id() { } call_nodetool() { - "$ERTS_DIR/bin/escript" "$ROOTDIR/bin/nodetool" "$@" + "$ERTS_DIR/bin/escript" "$RUNNER_ROOT_DIR/bin/nodetool" "$@" } # Control a node @@ -388,7 +387,7 @@ check_license() { # Run an escript in the node's environment relx_escript() { shift; scriptpath="$1"; shift - "$ERTS_DIR/bin/escript" "$ROOTDIR/$scriptpath" "$@" + "$ERTS_DIR/bin/escript" "$RUNNER_ROOT_DIR/$scriptpath" "$@" } # Output a start command for the last argument of run_erl @@ -598,7 +597,7 @@ case "${COMMAND}" in IS_BOOT_COMMAND='yes' ;; ertspath) - echo "$ERTS_PATH" + echo "$ERTS_DIR" exit 0 ;; *) @@ -676,7 +675,7 @@ if [[ "${EMQX_DB__BACKEND:-}" != "mnesia" fi fi -cd "$ROOTDIR" +cd "$RUNNER_ROOT_DIR" case "${COMMAND}" in start) @@ -798,7 +797,7 @@ case "${COMMAND}" in assert_node_alive ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARG" \ - exec "$BINDIR/escript" "$ROOTDIR/bin/install_upgrade.escript" \ + exec "$BINDIR/escript" "$RUNNER_ROOT_DIR/bin/install_upgrade.escript" \ "$COMMAND" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@" ;; @@ -808,7 +807,7 @@ case "${COMMAND}" in shift ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARG" \ - exec "$BINDIR/escript" "$ROOTDIR/bin/install_upgrade.escript" \ + exec "$BINDIR/escript" "$RUNNER_ROOT_DIR/bin/install_upgrade.escript" \ "versions" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@" ;; diff --git a/rel/emqx_vars b/rel/emqx_vars index e5eb012b0..f7bd2db3f 100644 --- a/rel/emqx_vars +++ b/rel/emqx_vars @@ -19,6 +19,5 @@ export EMQX_DESCRIPTION='{{ emqx_description }}' ## computed vars REL_NAME="emqx" -ERTS_PATH="$RUNNER_ROOT_DIR/erts-$ERTS_VSN/bin" ## updated vars here From 2dbc65cfe3068134e0ce1a68c31dfda62bd57499 Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Wed, 13 Apr 2022 17:44:49 +0200 Subject: [PATCH 06/10] fix(bin/emqx): make possible to start in path with space --- bin/emqx | 131 +++++++++++++++++++------------------------------------ 1 file changed, 45 insertions(+), 86 deletions(-) diff --git a/bin/emqx b/bin/emqx index f921c4187..849045231 100755 --- a/bin/emqx +++ b/bin/emqx @@ -35,7 +35,7 @@ export ERTS_DIR="$RUNNER_ROOT_DIR/erts-$ERTS_VSN" export BINDIR="$ERTS_DIR/bin" export EMU="beam" export PROGNAME="erl" -export ERTS_LIB_DIR="$ERTS_DIR/../lib" +export ERTS_LIB_DIR="$RUNNER_ROOT_DIR/lib" DYNLIBS_DIR="$RUNNER_ROOT_DIR/dynlibs" # Echo to stderr on errors @@ -272,9 +272,8 @@ if [ "$ES" -ne 0 ]; then exit $ES fi -# EPMD_ARG="-start_epmd true $PROTO_DIST_ARG" NO_EPMD="-start_epmd false -epmd_module ekka_epmd -proto_dist ekka" -EPMD_ARG="${EPMD_ARG:-${NO_EPMD}}" +EPMD_ARGS="${EPMD_ARGS:-${NO_EPMD}}" # Warn the user if ulimit -n is less than 1024 ULIMIT_F=$(ulimit -n) @@ -312,26 +311,27 @@ relx_rem_sh() { # Get the node's ticktime so that we use the same thing. TICKTIME="$(relx_nodetool rpcterms net_kernel get_net_ticktime)" - # shellcheck disable=SC2086 # $EPMD_ARG is supposed to be split by whitespace + # shellcheck disable=SC2086 # Setup remote shell command to control node if [ "$IS_ELIXIR" = "yes" ] then - exec "$REL_DIR/iex" \ + set -- "$REL_DIR/iex" \ --remsh "$NAME" \ --boot-var RELEASE_LIB "$ERTS_LIB_DIR" \ --cookie "$COOKIE" \ --hidden \ --erl "-kernel net_ticktime $TICKTIME" \ - --erl "$EPMD_ARG" \ + --erl "$EPMD_ARGS" \ --erl "$NAME_TYPE $id" \ --boot "$REL_DIR/start_clean" else - exec "$BINDIR/erl" "$NAME_TYPE" "$id" \ + set -- "$BINDIR/erl" "$NAME_TYPE" "$id" \ -remsh "$NAME" -boot "$REL_DIR/start_clean" \ -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \ -setcookie "$COOKIE" -hidden -kernel net_ticktime "$TICKTIME" \ - $EPMD_ARG + $EPMD_ARGS fi + exec "$@" } # Generate a random id @@ -346,7 +346,7 @@ call_nodetool() { # Control a node relx_nodetool() { command="$1"; shift - ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARG" \ + ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARGS" \ call_nodetool "$NAME_TYPE" "$NAME" \ -setcookie "$COOKIE" "$command" "$@" } @@ -406,6 +406,7 @@ CONFIGS_DIR="$DATA_DIR/configs" mkdir -p "$CONFIGS_DIR" # Function to generate app.config and vm.args +# sets two environment variables CONF_FILE and ARGS_FILE generate_config() { local name_type="$1" local node_name="$2" @@ -424,16 +425,14 @@ generate_config() { call_hocon -v -t "$NOW_TIME" -s "$SCHEMA_MOD" -c "$EMQX_ETC_DIR"/emqx.conf -d "$DATA_DIR"/configs generate ## filenames are per-hocon convention - local CONF_FILE="$CONFIGS_DIR/app.$NOW_TIME.config" - local HOCON_GEN_ARG_FILE="$CONFIGS_DIR/vm.$NOW_TIME.args" + CONF_FILE="$CONFIGS_DIR/app.$NOW_TIME.config" + ARGS_FILE="$CONFIGS_DIR/vm.$NOW_TIME.args" # This is needed by the Elixir scripts. # Do NOT append `.config`. RELEASE_SYS_CONFIG="$CONFIGS_DIR/app.$NOW_TIME" export RELEASE_SYS_CONFIG - CONFIG_ARGS="-config $CONF_FILE -args_file $HOCON_GEN_ARG_FILE" - ## Merge hocon generated *.args into the vm.args TMP_ARG_FILE="$CONFIGS_DIR/vm.args.tmp" cp "$EMQX_ETC_DIR/vm.args" "$TMP_ARG_FILE" @@ -442,7 +441,7 @@ generate_config() { ## read lines from generated vm.