fix(bin/emqx): make possible to start in path with space

This commit is contained in:
Zaiming (Stone) Shi 2022-04-13 17:44:49 +02:00
parent 72405f977c
commit 2dbc65cfe3
1 changed files with 45 additions and 86 deletions

131
bin/emqx
View File

@ -35,7 +35,7 @@ export ERTS_DIR="$RUNNER_ROOT_DIR/erts-$ERTS_VSN"
export BINDIR="$ERTS_DIR/bin" export BINDIR="$ERTS_DIR/bin"
export EMU="beam" export EMU="beam"
export PROGNAME="erl" export PROGNAME="erl"
export ERTS_LIB_DIR="$ERTS_DIR/../lib" export ERTS_LIB_DIR="$RUNNER_ROOT_DIR/lib"
DYNLIBS_DIR="$RUNNER_ROOT_DIR/dynlibs" DYNLIBS_DIR="$RUNNER_ROOT_DIR/dynlibs"
# Echo to stderr on errors # Echo to stderr on errors
@ -272,9 +272,8 @@ if [ "$ES" -ne 0 ]; then
exit $ES exit $ES
fi fi
# EPMD_ARG="-start_epmd true $PROTO_DIST_ARG"
NO_EPMD="-start_epmd false -epmd_module ekka_epmd -proto_dist ekka" 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 # Warn the user if ulimit -n is less than 1024
ULIMIT_F=$(ulimit -n) ULIMIT_F=$(ulimit -n)
@ -312,26 +311,27 @@ relx_rem_sh() {
# Get the node's ticktime so that we use the same thing. # Get the node's ticktime so that we use the same thing.
TICKTIME="$(relx_nodetool rpcterms net_kernel get_net_ticktime)" 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 # Setup remote shell command to control node
if [ "$IS_ELIXIR" = "yes" ] if [ "$IS_ELIXIR" = "yes" ]
then then
exec "$REL_DIR/iex" \ set -- "$REL_DIR/iex" \
--remsh "$NAME" \ --remsh "$NAME" \
--boot-var RELEASE_LIB "$ERTS_LIB_DIR" \ --boot-var RELEASE_LIB "$ERTS_LIB_DIR" \
--cookie "$COOKIE" \ --cookie "$COOKIE" \
--hidden \ --hidden \
--erl "-kernel net_ticktime $TICKTIME" \ --erl "-kernel net_ticktime $TICKTIME" \
--erl "$EPMD_ARG" \ --erl "$EPMD_ARGS" \
--erl "$NAME_TYPE $id" \ --erl "$NAME_TYPE $id" \
--boot "$REL_DIR/start_clean" --boot "$REL_DIR/start_clean"
else else
exec "$BINDIR/erl" "$NAME_TYPE" "$id" \ set -- "$BINDIR/erl" "$NAME_TYPE" "$id" \
-remsh "$NAME" -boot "$REL_DIR/start_clean" \ -remsh "$NAME" -boot "$REL_DIR/start_clean" \
-boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \ -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
-setcookie "$COOKIE" -hidden -kernel net_ticktime "$TICKTIME" \ -setcookie "$COOKIE" -hidden -kernel net_ticktime "$TICKTIME" \
$EPMD_ARG $EPMD_ARGS
fi fi
exec "$@"
} }
# Generate a random id # Generate a random id
@ -346,7 +346,7 @@ call_nodetool() {
# Control a node # Control a node
relx_nodetool() { relx_nodetool() {
command="$1"; shift command="$1"; shift
ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARG" \ ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARGS" \
call_nodetool "$NAME_TYPE" "$NAME" \ call_nodetool "$NAME_TYPE" "$NAME" \
-setcookie "$COOKIE" "$command" "$@" -setcookie "$COOKIE" "$command" "$@"
} }
@ -406,6 +406,7 @@ CONFIGS_DIR="$DATA_DIR/configs"
mkdir -p "$CONFIGS_DIR" mkdir -p "$CONFIGS_DIR"
# Function to generate app.config and vm.args # Function to generate app.config and vm.args
# sets two environment variables CONF_FILE and ARGS_FILE
generate_config() { generate_config() {
local name_type="$1" local name_type="$1"
local node_name="$2" 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 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 ## filenames are per-hocon convention
local CONF_FILE="$CONFIGS_DIR/app.$NOW_TIME.config" CONF_FILE="$CONFIGS_DIR/app.$NOW_TIME.config"
local HOCON_GEN_ARG_FILE="$CONFIGS_DIR/vm.$NOW_TIME.args" ARGS_FILE="$CONFIGS_DIR/vm.$NOW_TIME.args"
# This is needed by the Elixir scripts. # This is needed by the Elixir scripts.
# Do NOT append `.config`. # Do NOT append `.config`.
RELEASE_SYS_CONFIG="$CONFIGS_DIR/app.$NOW_TIME" RELEASE_SYS_CONFIG="$CONFIGS_DIR/app.$NOW_TIME"
export RELEASE_SYS_CONFIG export RELEASE_SYS_CONFIG
CONFIG_ARGS="-config $CONF_FILE -args_file $HOCON_GEN_ARG_FILE"
## Merge hocon generated *.args into the vm.args ## Merge hocon generated *.args into the vm.args
TMP_ARG_FILE="$CONFIGS_DIR/vm.args.tmp" TMP_ARG_FILE="$CONFIGS_DIR/vm.args.tmp"
cp "$EMQX_ETC_DIR/vm.args" "$TMP_ARG_FILE" cp "$EMQX_ETC_DIR/vm.args" "$TMP_ARG_FILE"
@ -442,7 +441,7 @@ generate_config() {
## read lines from generated vm.<time>.args file ## read lines from generated vm.<time>.args file
## drop comment lines, and empty lines using sed ## drop comment lines, and empty lines using sed
## pipe the lines to a while loop ## pipe the lines to a while loop
sed '/^#/d' "$HOCON_GEN_ARG_FILE" | sed '/^$/d' | while IFS='' read -r ARG_LINE || [ -n "$ARG_LINE" ]; do sed '/^#/d' "$ARGS_FILE" | sed '/^$/d' | while IFS='' read -r ARG_LINE || [ -n "$ARG_LINE" ]; do
## in the loop, split the 'key[:space:]value' pair ## in the loop, split the 'key[:space:]value' pair
ARG_KEY=$(echo "$ARG_LINE" | awk '{$NF="";print}') ARG_KEY=$(echo "$ARG_LINE" | awk '{$NF="";print}')
ARG_VALUE=$(echo "$ARG_LINE" | awk '{print $NF}') ARG_VALUE=$(echo "$ARG_LINE" | awk '{print $NF}')
@ -453,7 +452,7 @@ generate_config() {
## if they are different ## if they are different
if [ -n "$TMP_ARG_VALUE" ]; then if [ -n "$TMP_ARG_VALUE" ]; then
## if the old value is present, replace it with generated value ## if the old value is present, replace it with generated value
sh -c "$SED_REPLACE 's|^$ARG_KEY.*$|$ARG_LINE|' $TMP_ARG_FILE" sh -c "$SED_REPLACE 's|^$ARG_KEY.*$|$ARG_LINE|' \"$TMP_ARG_FILE\""
else else
## otherwise append generated value to the end ## otherwise append generated value to the end
echo "$ARG_LINE" >> "$TMP_ARG_FILE" echo "$ARG_LINE" >> "$TMP_ARG_FILE"
@ -462,12 +461,7 @@ generate_config() {
done done
echo "$name_type $node_name" >> "$TMP_ARG_FILE" echo "$name_type $node_name" >> "$TMP_ARG_FILE"
## rename the generated vm.<time>.args file ## rename the generated vm.<time>.args file
mv -f "$TMP_ARG_FILE" "$HOCON_GEN_ARG_FILE" mv -f "$TMP_ARG_FILE" "$ARGS_FILE"
# shellcheck disable=SC2086
if ! relx_nodetool chkconfig $CONFIG_ARGS; then
die "failed_to_check_config $CONFIG_ARGS"
fi
} }
# check if a PID is down # check if a PID is down
@ -588,7 +582,11 @@ maybe_log_to_console() {
# check if using an OTP version that has the mnesia_hook patch for use # check if using an OTP version that has the mnesia_hook patch for use
# in mria. # in mria.
is_otp_compatible() { is_otp_compatible() {
"$ERTS_DIR"/bin/erl -noshell -eval 'try mnesia_hook:module_info() of _ -> init:stop() catch _:_ -> init:stop(1) end.' 1>/dev/null 2>/dev/null "$BINDIR/$PROGNAME" \
-noshell \
-boot_var RELEASE_LIB "$ERTS_LIB_DIR/lib" \
-boot "$REL_DIR/start_clean" \
-eval 'try mnesia_hook:module_info() of _ -> init:stop() catch _:_ -> halt(1) end.' >/dev/null 2>&1
} }
## IS_BOOT_COMMAND is set for later to inspect node name and cookie from hocon config (or env variable) ## IS_BOOT_COMMAND is set for later to inspect node name and cookie from hocon config (or env variable)
@ -725,7 +723,7 @@ case "${COMMAND}" in
else else
echo "$EMQX_DESCRIPTION $REL_VSN failed to start in ${WAIT_TIME} seconds." echo "$EMQX_DESCRIPTION $REL_VSN failed to start in ${WAIT_TIME} seconds."
echo "Please find more information in erlang.log.N" echo "Please find more information in erlang.log.N"
echo "Or run 'DEBUG=1 $0 console' to have logs printed to console." echo "Or run 'env DEBUG=1 $0 console' to have logs printed to console."
exit 1 exit 1
fi fi
;; ;;
@ -796,7 +794,7 @@ case "${COMMAND}" in
assert_node_alive assert_node_alive
ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARG" \ ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARGS" \
exec "$BINDIR/escript" "$RUNNER_ROOT_DIR/bin/install_upgrade.escript" \ exec "$BINDIR/escript" "$RUNNER_ROOT_DIR/bin/install_upgrade.escript" \
"$COMMAND" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@" "$COMMAND" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
;; ;;
@ -806,17 +804,17 @@ case "${COMMAND}" in
shift shift
ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARG" \ ERL_FLAGS="${ERL_FLAGS:-} $EPMD_ARGS" \
exec "$BINDIR/escript" "$RUNNER_ROOT_DIR/bin/install_upgrade.escript" \ exec "$BINDIR/escript" "$RUNNER_ROOT_DIR/bin/install_upgrade.escript" \
"versions" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@" "versions" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
;; ;;
console|console_clean) console|console_clean|foreground)
# .boot file typically just $REL_NAME (ie, the app name) # .boot file typically just $REL_NAME (ie, the app name)
# however, for debugging, sometimes start_clean.boot is useful. # however, for debugging, sometimes start_clean.boot is useful.
# For e.g. 'setup', one may even want to name another boot script. # For e.g. 'setup', one may even want to name another boot script.
case "$COMMAND" in case "$COMMAND" in
console) console|foreground)
if [ -f "$REL_DIR/$REL_NAME.boot" ]; then if [ -f "$REL_DIR/$REL_NAME.boot" ]; then
BOOTFILE="$REL_DIR/$REL_NAME" BOOTFILE="$REL_DIR/$REL_NAME"
else else
@ -827,6 +825,14 @@ case "${COMMAND}" in
BOOTFILE="$REL_DIR/start_clean" BOOTFILE="$REL_DIR/start_clean"
;; ;;
esac esac
case "$COMMAND" in
foreground)
FOREGROUNDOPTIONS="-noshell -noinput +Bd"
;;
*)
FOREGROUNDOPTIONS=''
;;
esac
# set before generate_config # set before generate_config
if [ "${_EMQX_START_DAEMON_MODE:-}" = 1 ]; then if [ "${_EMQX_START_DAEMON_MODE:-}" = 1 ]; then
@ -850,25 +856,31 @@ case "${COMMAND}" in
# Store passed arguments since they will be erased by `set` # Store passed arguments since they will be erased by `set`
ARGS="$*" ARGS="$*"
# shellcheck disable=SC2086 # $CONFIG_ARGS $EPMD_ARG are supposed to be split by whitespace # shellcheck disable=SC2086
# Build an array of arguments to pass to exec later on # Build an array of arguments to pass to exec later on
# Build it here because this command will be used for logging. # Build it here because this command will be used for logging.
if [ "$IS_ELIXIR" = yes ] if [ "$IS_ELIXIR" = yes ]
then then
set -- "$REL_DIR/iex" \ set -- "$REL_DIR/iex" \
--boot "$BOOTFILE" \ --boot "$BOOTFILE" \
--erl "-mode $CODE_LOADING_MODE" \
--boot-var RELEASE_LIB "$ERTS_LIB_DIR" \ --boot-var RELEASE_LIB "$ERTS_LIB_DIR" \
--erl-config "${CONF_FILE}" \
--vm-args "${ARGS_FILE}" \
--erl "$FOREGROUNDOPTIONS" \
--erl "-mode $CODE_LOADING_MODE" \
--erl "-mnesia dir \"${MNESIA_DATA_DIR}\"" \ --erl "-mnesia dir \"${MNESIA_DATA_DIR}\"" \
--erl "$CONFIG_ARGS" \ --erl "$EPMD_ARGS" \
--erl "$EPMD_ARG" \
--werl --werl
else else
set -- "$BINDIR/erlexec" \ set -- "$BINDIR/erlexec" \
-boot "$BOOTFILE" -mode "$CODE_LOADING_MODE" \ $FOREGROUNDOPTIONS \
-boot "$BOOTFILE" \
-boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \ -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
-mode "$CODE_LOADING_MODE" \
-mnesia dir "\"${MNESIA_DATA_DIR}\"" \ -mnesia dir "\"${MNESIA_DATA_DIR}\"" \
$CONFIG_ARGS $EPMD_ARG -config "$CONF_FILE" \
-args_file "$ARGS_FILE" \
$EPMD_ARGS
fi fi
# Log the startup # Log the startup
@ -878,59 +890,6 @@ case "${COMMAND}" in
exec "$@" -- ${1+$ARGS} exec "$@" -- ${1+$ARGS}
;; ;;
foreground)
# start up the release in the foreground for use by runit
# or other supervision services
maybe_log_to_console
#generate app.config and vm.args
generate_config "$NAME_TYPE" "$NAME"
check_license
[ -f "$REL_DIR/$REL_NAME.boot" ] && BOOTFILE="$REL_NAME" || BOOTFILE=start
FOREGROUNDOPTIONS="-noshell -noinput +Bd"
# Setup beam-required vars
EMU=beam
PROGNAME="${0#*/}"
export EMU
export PROGNAME
# Store passed arguments since they will be erased by `set`
ARGS="$*"
# shellcheck disable=SC2086 # $CONFIG_ARGS $EPMD_ARG are supposed to be split by whitespace
# Build an array of arguments to pass to exec later on
# Build it here because this command will be used for logging.
if [ "$IS_ELIXIR" = yes ]
then
set -- "$REL_DIR/elixir" \
--boot "$REL_DIR/start" \
--erl "$FOREGROUNDOPTIONS" \
--erl "-mode $CODE_LOADING_MODE" \
--boot-var RELEASE_LIB "$ERTS_LIB_DIR" \
--boot-var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
--erl "-mnesia dir \"${MNESIA_DATA_DIR}\"" \
--erl "$CONFIG_ARGS" \
--erl "$EPMD_ARG" \
--no-halt
else
set -- "$BINDIR/erlexec" $FOREGROUNDOPTIONS \
-boot "$REL_DIR/$BOOTFILE" -mode "$CODE_LOADING_MODE" \
-boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
-mnesia dir "\"${MNESIA_DATA_DIR}\"" \
$CONFIG_ARGS $EPMD_ARG
fi
# Log the startup
logger -t "${REL_NAME}[$$]" "EXEC: $* -- ${1+$ARGS}"
# Start the VM
exec "$@" -- ${1+$ARGS}
;;
ctl) ctl)
assert_node_alive assert_node_alive