2021-05-18 14:54:48 +08:00
|
|
|
#!/bin/bash
|
2021-03-19 00:12:57 +08:00
|
|
|
# -*- tab-width:4;indent-tabs-mode:nil -*-
|
|
|
|
# ex: ts=4 sw=4 et
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
ROOT_DIR="$(cd "$(dirname "$(readlink "$0" || echo "$0")")"/..; pwd -P)"
|
|
|
|
# shellcheck disable=SC1090
|
|
|
|
. "$ROOT_DIR"/releases/emqx_vars
|
2021-03-19 00:12:57 +08:00
|
|
|
|
|
|
|
RUNNER_SCRIPT="$RUNNER_BIN_DIR/$REL_NAME"
|
|
|
|
CODE_LOADING_MODE="${CODE_LOADING_MODE:-embedded}"
|
|
|
|
REL_DIR="$RUNNER_ROOT_DIR/releases/$REL_VSN"
|
|
|
|
|
|
|
|
WHOAMI=$(whoami)
|
|
|
|
|
2021-05-14 07:28:48 +08:00
|
|
|
# Make sure log directory exists
|
|
|
|
mkdir -p "$RUNNER_LOG_DIR"
|
|
|
|
|
|
|
|
# Make sure data directory exists
|
|
|
|
mkdir -p "$RUNNER_DATA_DIR"
|
|
|
|
|
2022-01-07 16:30:56 +08:00
|
|
|
export ROOTDIR="$RUNNER_ROOT_DIR"
|
|
|
|
export ERTS_DIR="$ROOTDIR/erts-$ERTS_VSN"
|
|
|
|
export BINDIR="$ERTS_DIR/bin"
|
|
|
|
export EMU="beam"
|
|
|
|
export PROGNAME="erl"
|
|
|
|
DYNLIBS_DIR="$RUNNER_ROOT_DIR/dynlibs"
|
|
|
|
ERTS_LIB_DIR="$ERTS_DIR/../lib"
|
|
|
|
|
|
|
|
# Echo to stderr on errors
|
|
|
|
echoerr() { echo "$*" 1>&2; }
|
|
|
|
|
|
|
|
check_eralng_start() {
|
|
|
|
"$BINDIR/$PROGNAME" -noshell -boot "$REL_DIR/start_clean" -s crypto start -s init stop
|
|
|
|
}
|
|
|
|
|
|
|
|
if ! check_eralng_start >/dev/null 2>&1; then
|
|
|
|
BUILT_ON="$(head -1 "${REL_DIR}/BUILT_ON")"
|
|
|
|
## failed to start, might be due to missing libs, try to be portable
|
|
|
|
export LD_LIBRARY_PATH="$DYNLIBS_DIR:$LD_LIBRARY_PATH"
|
|
|
|
if ! check_eralng_start; then
|
|
|
|
## it's hopeless
|
|
|
|
echoerr "FATAL: Unable to start Erlang (with libcrypto)."
|
|
|
|
echoerr "Please make sure it's running on the correct platform with all required dependencies."
|
|
|
|
echoerr "This EMQ X release is built for $BUILT_ON"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echoerr "WARNING: There seem to be missing dynamic libs from the OS. Using libs from ${DYNLIBS_DIR}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
## backward compatible
|
|
|
|
if [ -d "$ERTS_DIR/lib" ]; then
|
|
|
|
export LD_LIBRARY_PATH="$ERTS_DIR/lib:$LD_LIBRARY_PATH"
|
|
|
|
fi
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
# cuttlefish try to read environment variables starting with "EMQX_"
|
|
|
|
export CUTTLEFISH_ENV_OVERRIDE_PREFIX='EMQX_'
|
|
|
|
|
|
|
|
relx_usage() {
|
|
|
|
command="$1"
|
|
|
|
|
|
|
|
case "$command" in
|
|
|
|
unpack)
|
|
|
|
echo "Usage: $REL_NAME unpack [VERSION]"
|
|
|
|
echo "Unpacks a release package VERSION, it assumes that this"
|
|
|
|
echo "release package tarball has already been deployed at one"
|
|
|
|
echo "of the following locations:"
|
|
|
|
echo " releases/<relname>-<version>.tar.gz"
|
|
|
|
echo " releases/<relname>-<version>.zip"
|
|
|
|
;;
|
|
|
|
install)
|
|
|
|
echo "Usage: $REL_NAME install [VERSION]"
|
|
|
|
echo "Installs a release package VERSION, it assumes that this"
|
|
|
|
echo "release package tarball has already been deployed at one"
|
|
|
|
echo "of the following locations:"
|
|
|
|
echo " releases/<relname>-<version>.tar.gz"
|
|
|
|
echo " releases/<relname>-<version>.zip"
|
|
|
|
echo ""
|
|
|
|
echo " --no-permanent Install release package VERSION but"
|
|
|
|
echo " don't make it permanent"
|
|
|
|
;;
|
|
|
|
uninstall)
|
|
|
|
echo "Usage: $REL_NAME uninstall [VERSION]"
|
|
|
|
echo "Uninstalls a release VERSION, it will only accept"
|
|
|
|
echo "versions that are not currently in use"
|
|
|
|
;;
|
|
|
|
upgrade)
|
|
|
|
echo "Usage: $REL_NAME upgrade [VERSION]"
|
|
|
|
echo "Upgrades the currently running release to VERSION, it assumes"
|
|
|
|
echo "that a release package tarball has already been deployed at one"
|
|
|
|
echo "of the following locations:"
|
|
|
|
echo " releases/<relname>-<version>.tar.gz"
|
|
|
|
echo " releases/<relname>-<version>.zip"
|
|
|
|
echo ""
|
|
|
|
echo " --no-permanent Install release package VERSION but"
|
|
|
|
echo " don't make it permanent"
|
|
|
|
;;
|
|
|
|
downgrade)
|
|
|
|
echo "Usage: $REL_NAME downgrade [VERSION]"
|
|
|
|
echo "Downgrades the currently running release to VERSION, it assumes"
|
|
|
|
echo "that a release package tarball has already been deployed at one"
|
|
|
|
echo "of the following locations:"
|
|
|
|
echo " releases/<relname>-<version>.tar.gz"
|
|
|
|
echo " releases/<relname>-<version>.zip"
|
|
|
|
echo ""
|
|
|
|
echo " --no-permanent Install release package VERSION but"
|
|
|
|
echo " don't make it permanent"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Usage: $REL_NAME {start|start_boot <file>|ertspath|foreground|stop|restart|reboot|pid|ping|console|console_clean|console_boot <file>|attach|remote_console|upgrade|downgrade|install|uninstall|versions|escript|rpc|rpcterms|eval|root_dir}"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2021-05-14 07:28:48 +08:00
|
|
|
# Simple way to check the correct user and fail early
|
|
|
|
check_user() {
|
|
|
|
# Validate that the user running the script is the owner of the
|
|
|
|
# RUN_DIR.
|
2021-05-18 14:54:48 +08:00
|
|
|
if [ "$RUNNER_USER" ] && [ "x$WHOAMI" != "x$RUNNER_USER" ]; then
|
2021-05-14 07:28:48 +08:00
|
|
|
if [ "x$WHOAMI" != "xroot" ]; then
|
|
|
|
echo "You need to be root or use sudo to run this command"
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-05-18 14:54:48 +08:00
|
|
|
CMD="\"$RUNNER_SCRIPT\" "
|
|
|
|
for ARG in "$@"; do
|
|
|
|
CMD="${CMD} \"$ARG\""
|
|
|
|
done
|
2021-05-14 07:28:48 +08:00
|
|
|
# This will drop priviledges into the runner user
|
|
|
|
# It exec's in a new shell and the current shell will exit
|
2021-05-18 14:54:48 +08:00
|
|
|
exec su - "$RUNNER_USER" -c "$CMD"
|
2021-05-14 07:28:48 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Make sure the user running this script is the owner and/or su to that user
|
2021-05-18 14:54:48 +08:00
|
|
|
check_user "$@"
|
2021-05-14 07:28:48 +08:00
|
|
|
ES=$?
|
|
|
|
if [ "$ES" -ne 0 ]; then
|
|
|
|
exit $ES
|
|
|
|
fi
|
|
|
|
|
2021-03-19 00:12:57 +08:00
|
|
|
if [ -z "$WITH_EPMD" ]; then
|
|
|
|
EPMD_ARG="-start_epmd false -epmd_module ekka_epmd -proto_dist ekka"
|
|
|
|
else
|
|
|
|
EPMD_ARG="-start_epmd true $PROTO_DIST_ARG"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Warn the user if ulimit -n is less than 1024
|
2021-05-18 14:54:48 +08:00
|
|
|
ULIMIT_F=$(ulimit -n)
|
2021-03-19 00:12:57 +08:00
|
|
|
if [ "$ULIMIT_F" -lt 1024 ]; then
|
|
|
|
echo "!!!!"
|
|
|
|
echo "!!!! WARNING: ulimit -n is ${ULIMIT_F}; 1024 is the recommended minimum."
|
|
|
|
echo "!!!!"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# By default, use cuttlefish to generate app.config and vm.args
|
|
|
|
CUTTLEFISH="${USE_CUTTLEFISH:-yes}"
|
|
|
|
|
|
|
|
SED_REPLACE="sed -i "
|
|
|
|
case $(sed --help 2>&1) in
|
|
|
|
*GNU*) SED_REPLACE="sed -i ";;
|
|
|
|
*BusyBox*) SED_REPLACE="sed -i ";;
|
2021-05-14 07:28:48 +08:00
|
|
|
*) SED_REPLACE="sed -i '' ";;
|
2021-03-19 00:12:57 +08:00
|
|
|
esac
|
|
|
|
|
|
|
|
# Get node pid
|
|
|
|
relx_get_pid() {
|
|
|
|
if output="$(relx_nodetool rpcterms os getpid)"
|
|
|
|
then
|
2021-05-18 14:54:48 +08:00
|
|
|
# shellcheck disable=SC2001 # Escaped quote taken as closing quote in editor
|
2021-03-19 00:12:57 +08:00
|
|
|
echo "$output" | sed -e 's/"//g'
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
echo "$output"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
relx_get_nodename() {
|
|
|
|
id="longname$(relx_gen_id)-${NAME}"
|
2021-05-18 14:54:48 +08:00
|
|
|
"$BINDIR/erl" -boot "$REL_DIR/start_clean" -eval '[Host] = tl(string:tokens(atom_to_list(node()),"@")), io:format("~s~n", [Host]), halt()' -noshell "${NAME_TYPE}" "$id"
|
2021-03-19 00:12:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Connect to a remote node
|
|
|
|
relx_rem_sh() {
|
|
|
|
# Generate a unique id used to allow multiple remsh to the same node
|
|
|
|
# transparently
|
|
|
|
id="remsh$(relx_gen_id)-${NAME}"
|
|
|
|
# Get the node's ticktime so that we use the same thing.
|
|
|
|
TICKTIME="$(relx_nodetool rpcterms net_kernel get_net_ticktime)"
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
# shellcheck disable=SC2086 # $EPMD_ARG is supposed to be split by whitespace
|
2021-03-19 00:12:57 +08:00
|
|
|
# Setup remote shell command to control node
|
2021-05-18 14:54:48 +08:00
|
|
|
exec "$BINDIR/erl" "$NAME_TYPE" "$id" -remsh "$NAME" -boot "$REL_DIR/start_clean" \
|
2021-03-19 00:12:57 +08:00
|
|
|
-boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
|
2021-05-18 14:54:48 +08:00
|
|
|
-setcookie "$COOKIE" -hidden -kernel net_ticktime "$TICKTIME" $EPMD_ARG
|
2021-03-19 00:12:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Generate a random id
|
|
|
|
relx_gen_id() {
|
|
|
|
od -t x -N 4 /dev/urandom | head -n1 | awk '{print $2}'
|
|
|
|
}
|
|
|
|
|
|
|
|
# Control a node
|
|
|
|
relx_nodetool() {
|
|
|
|
command="$1"; shift
|
2022-01-07 16:30:56 +08:00
|
|
|
export RUNNER_ROOT_DIR
|
|
|
|
export REL_VSN
|
2021-03-19 00:12:57 +08:00
|
|
|
|
|
|
|
ERL_FLAGS="$ERL_FLAGS $EPMD_ARG" \
|
|
|
|
"$ERTS_DIR/bin/escript" "$ROOTDIR/bin/nodetool" "$NAME_TYPE" "$NAME" \
|
2021-05-18 14:54:48 +08:00
|
|
|
-setcookie "$COOKIE" "$command" "$@"
|
2021-03-19 00:12:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Run an escript in the node's environment
|
|
|
|
relx_escript() {
|
|
|
|
shift; scriptpath="$1"; shift
|
|
|
|
export RUNNER_ROOT_DIR
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
"$ERTS_DIR/bin/escript" "$ROOTDIR/$scriptpath" "$@"
|
2021-03-19 00:12:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Output a start command for the last argument of run_erl
|
|
|
|
relx_start_command() {
|
|
|
|
printf "exec \"%s\" \"%s\"" "$RUNNER_SCRIPT" \
|
|
|
|
"$START_OPTION"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function to generate app.config and vm.args
|
|
|
|
generate_config() {
|
|
|
|
## Delete the *.siz files first or it cann't start after
|
|
|
|
## changing the config 'log.rotation.size'
|
|
|
|
rm -rf "${RUNNER_LOG_DIR}"/*.siz
|
|
|
|
|
|
|
|
if [ "$CUTTLEFISH" != "yes" ]; then
|
|
|
|
# Note: we have added a parameter '-vm_args' to this. It
|
|
|
|
# appears redundant but it is not! the erlang vm allows us to
|
|
|
|
# access all arguments to the erl command EXCEPT '-args_file',
|
|
|
|
# so in order to get access to this file location from within
|
|
|
|
# the vm, we need to pass it in twice.
|
|
|
|
CONFIG_ARGS=" -config $RUNNER_ETC_DIR/app.config -args_file $RUNNER_ETC_DIR/vm.args -vm_args $RUNNER_ETC_DIR/vm.args "
|
|
|
|
else
|
2021-05-18 14:54:48 +08:00
|
|
|
EMQX_LICENSE_CONF_OPTION=""
|
|
|
|
if [ "${EMQX_LICENSE_CONF:-}" != "" ]; then
|
|
|
|
EMQX_LICENSE_CONF_OPTION="-i ${EMQX_LICENSE_CONF}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
set +e
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
CUTTLEFISH_OUTPUT="$("$ERTS_PATH"/escript "$RUNNER_ROOT_DIR"/bin/cuttlefish -v -i "$REL_DIR"/emqx.schema $EMQX_LICENSE_CONF_OPTION -c "$RUNNER_ETC_DIR"/emqx.conf -d "$RUNNER_DATA_DIR"/configs generate)"
|
|
|
|
# shellcheck disable=SC2181
|
|
|
|
RESULT=$?
|
|
|
|
set -e
|
|
|
|
if [ $RESULT -gt 0 ]; then
|
|
|
|
echo "$CUTTLEFISH_OUTPUT"
|
|
|
|
exit $RESULT
|
|
|
|
fi
|
|
|
|
# print override from environment variables (EMQX_*)
|
|
|
|
echo "$CUTTLEFISH_OUTPUT" | sed -e '$d'
|
|
|
|
CONFIG_ARGS=$(echo "$CUTTLEFISH_OUTPUT" | tail -n 1)
|
2021-03-19 00:12:57 +08:00
|
|
|
|
|
|
|
## Merge cuttlefish generated *.args into the vm.args
|
2021-05-18 14:54:48 +08:00
|
|
|
CUTTLE_GEN_ARG_FILE=$(echo "$CONFIG_ARGS" | sed -n 's/^.*\(vm_args[[:space:]]\)//p' | awk '{print $1}')
|
2021-03-19 00:12:57 +08:00
|
|
|
TMP_ARG_FILE="$RUNNER_DATA_DIR/configs/vm.args.tmp"
|
|
|
|
cp "$RUNNER_ETC_DIR/vm.args" "$TMP_ARG_FILE"
|
|
|
|
echo "" >> "$TMP_ARG_FILE"
|
2021-05-18 14:54:48 +08:00
|
|
|
echo "-pa ${REL_DIR}/consolidated" >> "$TMP_ARG_FILE"
|
|
|
|
sed '/^#/d' "$CUTTLE_GEN_ARG_FILE" | sed '/^$/d' | while IFS='' read -r ARG_LINE || [ -n "$ARG_LINE" ]; do
|
|
|
|
ARG_KEY=$(echo "$ARG_LINE" | awk '{$NF="";print}')
|
|
|
|
ARG_VALUE=$(echo "$ARG_LINE" | awk '{print $NF}')
|
2022-01-07 16:30:56 +08:00
|
|
|
if [ "$ARG_KEY" = '' ]; then
|
|
|
|
## for the flags, e.g. -heart -emu_args etc
|
|
|
|
ARG_KEY=$(echo "$ARG_LINE" | awk '{print $1}')
|
|
|
|
ARG_VALUE=''
|
|
|
|
TMP_ARG_KEY=$(grep "^$ARG_KEY" "$TMP_ARG_FILE" | awk '{print $1}')
|
|
|
|
if [ "$TMP_ARG_KEY" = '' ]; then
|
|
|
|
echo "$ARG_KEY" >> "$TMP_ARG_FILE"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
TMP_ARG_VALUE=$(grep "^$ARG_KEY" "$TMP_ARG_FILE" | awk '{print $NF}')
|
|
|
|
if [ "$ARG_VALUE" != "$TMP_ARG_VALUE" ] ; then
|
|
|
|
if [ -n "$TMP_ARG_VALUE" ]; then
|
|
|
|
sh -c "$SED_REPLACE 's/^$ARG_KEY.*$/$ARG_LINE/' $TMP_ARG_FILE"
|
|
|
|
else
|
|
|
|
echo "$ARG_LINE" >> "$TMP_ARG_FILE"
|
|
|
|
fi
|
2021-05-14 07:28:48 +08:00
|
|
|
fi
|
2021-03-19 00:12:57 +08:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
mv -f "$TMP_ARG_FILE" "$CUTTLE_GEN_ARG_FILE"
|
|
|
|
fi
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
# shellcheck disable=SC2086
|
2021-03-19 00:12:57 +08:00
|
|
|
if ! relx_nodetool chkconfig $CONFIG_ARGS; then
|
|
|
|
echoerr "Error reading $CONFIG_ARGS"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Call bootstrapd for daemon commands like start/stop/console
|
|
|
|
bootstrapd() {
|
|
|
|
if [ -e "$RUNNER_DATA_DIR/.erlang.cookie" ]; then
|
2021-05-18 14:54:48 +08:00
|
|
|
chown "$RUNNER_USER" "$RUNNER_DATA_DIR"/.erlang.cookie
|
2021-03-19 00:12:57 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Use $CWD/etc/sys.config if exists
|
|
|
|
if [ -z "$RELX_CONFIG_PATH" ]; then
|
|
|
|
if [ -f "$RUNNER_ETC_DIR/sys.config" ]; then
|
|
|
|
RELX_CONFIG_PATH="-config $RUNNER_ETC_DIR/sys.config"
|
|
|
|
else
|
|
|
|
RELX_CONFIG_PATH=""
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
IS_BOOT_COMMAND='no'
|
|
|
|
case "$1" in
|
|
|
|
start|start_boot)
|
|
|
|
IS_BOOT_COMMAND='yes'
|
|
|
|
;;
|
|
|
|
console|console_clean|console_boot)
|
|
|
|
IS_BOOT_COMMAND='yes'
|
|
|
|
;;
|
|
|
|
foreground)
|
|
|
|
IS_BOOT_COMMAND='yes'
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
2021-03-19 00:12:57 +08:00
|
|
|
if [ -z "$NAME_ARG" ]; then
|
2021-05-18 14:54:48 +08:00
|
|
|
NODENAME="${EMQX_NODE_NAME:-}"
|
|
|
|
# compatible with docker entrypoint
|
|
|
|
[ -z "$NODENAME" ] && [ -n "$EMQX_NAME" ] && [ -n "$EMQX_HOST" ] && NODENAME="${EMQX_NAME}@${EMQX_HOST}"
|
|
|
|
if [ -z "$NODENAME" ]; then
|
|
|
|
if [ "$IS_BOOT_COMMAND" = 'no' ]; then
|
|
|
|
# for non-boot commands, inspect vm.<time>.args for node name
|
|
|
|
# shellcheck disable=SC2012,SC2086
|
|
|
|
LATEST_VM_ARGS="$(ls -t $RUNNER_DATA_DIR/configs/vm.*.args | head -1)"
|
|
|
|
if [ -z "$LATEST_VM_ARGS" ]; then
|
|
|
|
echo "For command $1, there is no vm.*.args config file found in $RUNNER_DATA_DIR/configs/"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
NODENAME="$(grep -E '^-name' "$LATEST_VM_ARGS" | awk '{print $2}')"
|
|
|
|
else
|
|
|
|
# for boot commands, inspect emqx.conf for node name
|
|
|
|
NODENAME=$("$ERTS_PATH"/escript "$RUNNER_ROOT_DIR"/bin/cuttlefish -i "$REL_DIR"/emqx.schema -c "$RUNNER_ETC_DIR"/emqx.conf get node.name)
|
|
|
|
fi
|
2021-05-14 07:28:48 +08:00
|
|
|
fi
|
2021-03-19 00:12:57 +08:00
|
|
|
if [ -z "$NODENAME" ]; then
|
2021-05-18 14:54:48 +08:00
|
|
|
echoerr "Failed to resolve emqx node name"
|
|
|
|
if [ "$IS_BOOT_COMMAND" = 'yes' ]; then
|
|
|
|
echoerr "Make sure runner has read permission on '$RUNNER_ETC_DIR/emqx.conf'"
|
|
|
|
fi
|
|
|
|
echoerr "Maybe override node name with environment variable ENQX_NODE_NAME='name@host.name'"
|
|
|
|
echoerr "or, EMQX_NAME='name' and EMQX_HOST='host.name'"
|
2021-03-19 00:12:57 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
2021-05-18 14:54:48 +08:00
|
|
|
NAME_ARG="-name ${NODENAME# *}"
|
2021-03-19 00:12:57 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Extract the name type and name from the NAME_ARG for REMSH
|
|
|
|
NAME_TYPE="$(echo "$NAME_ARG" | awk '{print $1}')"
|
|
|
|
NAME="$(echo "$NAME_ARG" | awk '{print $2}')"
|
2021-05-18 14:54:48 +08:00
|
|
|
NODENAME="$(echo "$NAME" | awk -F'@' '{print $1}')"
|
|
|
|
export ESCRIPT_NAME="$NODENAME"
|
2021-03-19 00:12:57 +08:00
|
|
|
|
|
|
|
PIPE_DIR="${PIPE_DIR:-/$RUNNER_DATA_DIR/${WHOAMI}_erl_pipes/$NAME/}"
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
COOKIE="${EMQX_NODE_COOKIE:-}"
|
|
|
|
if [ -z "$COOKIE" ]; then
|
|
|
|
if [ "$IS_BOOT_COMMAND" = 'yes' ]; then
|
|
|
|
COOKIE=$("$ERTS_PATH"/escript "$RUNNER_ROOT_DIR"/bin/cuttlefish -i "$REL_DIR"/emqx.schema -c "$RUNNER_ETC_DIR"/emqx.conf get node.cookie)
|
2021-05-14 07:28:48 +08:00
|
|
|
else
|
2021-05-18 14:54:48 +08:00
|
|
|
# shellcheck disable=SC2012,SC2086
|
|
|
|
LATEST_VM_ARGS="$(ls -t $RUNNER_DATA_DIR/configs/vm.*.args | head -1)"
|
|
|
|
if [ -z "$LATEST_VM_ARGS" ]; then
|
|
|
|
echo "For command $1, there is no vm.*.args config file found in $RUNNER_DATA_DIR/configs/"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
COOKIE="$(grep -E '^-setcookie' "$LATEST_VM_ARGS" | awk '{print $2}')"
|
2021-03-19 00:12:57 +08:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
if [ -z "$COOKIE" ]; then
|
|
|
|
echoerr "Please set node.cookie in $RUNNER_ETC_DIR/emqx.conf or override from environment variable EMQX_NODE_COOKIE"
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-03-19 00:12:57 +08:00
|
|
|
|
2021-05-22 02:06:00 +08:00
|
|
|
# Support for IPv6 Dist. See: https://github.com/emqtt/emqttd/issues/1460
|
2021-05-18 14:54:48 +08:00
|
|
|
PROTO_DIST=$(grep -E '^[ \t]*cluster.proto_dist[ \t]*=[ \t]*' "$RUNNER_ETC_DIR/emqx.conf" 2> /dev/null | tail -1 | awk -F"= " '{print $NF}')
|
2021-03-19 00:12:57 +08:00
|
|
|
if [ -z "$PROTO_DIST" ]; then
|
|
|
|
PROTO_DIST_ARG=""
|
|
|
|
else
|
|
|
|
PROTO_DIST_ARG="-proto_dist $PROTO_DIST"
|
|
|
|
fi
|
|
|
|
|
|
|
|
cd "$ROOTDIR"
|
|
|
|
|
|
|
|
# User can specify an sname without @hostname
|
|
|
|
# This will fail when creating remote shell
|
|
|
|
# So here we check for @ and add @hostname if missing
|
|
|
|
case $NAME in
|
|
|
|
*@*)
|
|
|
|
# Nothing to do
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
NAME=$NAME@$(relx_get_nodename)
|
|
|
|
;;
|
|
|
|
esac
|
2022-01-07 16:30:56 +08:00
|
|
|
MNESIA_DATA_DIR="$RUNNER_DATA_DIR/mnesia/$NAME"
|
2021-03-19 00:12:57 +08:00
|
|
|
|
|
|
|
# Check the first argument for instructions
|
|
|
|
case "$1" in
|
|
|
|
start|start_boot)
|
|
|
|
# Make sure a node IS not running
|
|
|
|
if relx_nodetool "ping" >/dev/null 2>&1; then
|
|
|
|
echo "Node is already running!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
# Bootstrap daemon command (check perms & drop to $RUNNER_USER)
|
2021-05-18 14:54:48 +08:00
|
|
|
bootstrapd
|
|
|
|
|
|
|
|
# this flag passes down to console mode
|
|
|
|
# so we know it's intended to be run in daemon mode
|
|
|
|
export _EMQX_START_MODE="$1"
|
2021-03-19 00:12:57 +08:00
|
|
|
|
|
|
|
# Save this for later.
|
|
|
|
CMD=$1
|
|
|
|
case "$1" in
|
|
|
|
start)
|
|
|
|
shift
|
|
|
|
START_OPTION="console"
|
|
|
|
HEART_OPTION="start"
|
|
|
|
;;
|
|
|
|
start_boot)
|
|
|
|
shift
|
|
|
|
START_OPTION="console_boot"
|
|
|
|
HEART_OPTION="start_boot"
|
|
|
|
;;
|
|
|
|
esac
|
2021-05-18 14:54:48 +08:00
|
|
|
RUN_PARAM="$*"
|
2021-03-19 00:12:57 +08:00
|
|
|
|
|
|
|
# Set arguments for the heart command
|
|
|
|
set -- "$RUNNER_SCRIPT" "$HEART_OPTION"
|
|
|
|
[ "$RUN_PARAM" ] && set -- "$@" "$RUN_PARAM"
|
|
|
|
|
|
|
|
# Export the HEART_COMMAND
|
|
|
|
HEART_COMMAND="$RUNNER_SCRIPT $CMD"
|
|
|
|
export HEART_COMMAND
|
|
|
|
|
|
|
|
## See: http://erlang.org/doc/man/run_erl.html
|
|
|
|
# Export the RUN_ERL_LOG_GENERATIONS
|
2021-05-18 14:54:48 +08:00
|
|
|
export RUN_ERL_LOG_GENERATIONS=${RUN_ERL_LOG_GENERATIONS:-"5"}
|
2021-03-19 00:12:57 +08:00
|
|
|
|
|
|
|
# Export the RUN_ERL_LOG_MAXSIZE
|
2021-05-18 14:54:48 +08:00
|
|
|
export RUN_ERL_LOG_MAXSIZE=${RUN_ERL_LOG_MAXSIZE:-"10485760"}
|
2021-03-19 00:12:57 +08:00
|
|
|
|
|
|
|
mkdir -p "$PIPE_DIR"
|
|
|
|
|
|
|
|
"$BINDIR/run_erl" -daemon "$PIPE_DIR" "$RUNNER_LOG_DIR" \
|
|
|
|
"$(relx_start_command)"
|
|
|
|
|
2021-05-14 07:28:48 +08:00
|
|
|
WAIT_TIME=${WAIT_FOR_ERLANG:-15}
|
2021-05-18 14:54:48 +08:00
|
|
|
while [ "$WAIT_TIME" -gt 0 ]; do
|
2021-03-19 00:12:57 +08:00
|
|
|
if ! relx_nodetool "ping" >/dev/null 2>&1; then
|
2021-05-18 14:54:48 +08:00
|
|
|
WAIT_TIME=$((WAIT_TIME - 1))
|
2021-05-14 07:28:48 +08:00
|
|
|
sleep 1
|
2021-03-19 00:12:57 +08:00
|
|
|
continue
|
|
|
|
fi
|
2021-05-14 07:28:48 +08:00
|
|
|
sleep 1
|
|
|
|
if relx_nodetool "ping" >/dev/null 2>&1; then
|
2021-05-18 14:54:48 +08:00
|
|
|
echo "$EMQX_DESCRIPTION $REL_VSN is started successfully!"
|
2021-05-14 07:28:48 +08:00
|
|
|
exit 0
|
|
|
|
fi
|
2021-05-18 14:54:48 +08:00
|
|
|
done && echo "$EMQX_DESCRIPTION $REL_VSN failed to start within ${WAIT_FOR_ERLANG:-15} seconds,"
|
2021-05-14 07:28:48 +08:00
|
|
|
echo "see the output of '$0 console' for more information."
|
2021-03-19 00:12:57 +08:00
|
|
|
echo "If you want to wait longer, set the environment variable"
|
|
|
|
echo "WAIT_FOR_ERLANG to the number of seconds to wait."
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
|
|
|
|
stop)
|
|
|
|
# Wait for the node to completely stop...
|
|
|
|
PID="$(relx_get_pid)"
|
|
|
|
if ! relx_nodetool "stop"; then
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-05-18 14:54:48 +08:00
|
|
|
while kill -s 0 "$PID" 2>/dev/null; do
|
2021-03-19 00:12:57 +08:00
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
|
2021-05-14 07:28:48 +08:00
|
|
|
restart|reboot)
|
2021-05-18 14:54:48 +08:00
|
|
|
echo "$EMQX_DESCRIPTION $REL_VSN is stopped: $("$RUNNER_BIN_DIR"/emqx stop)"
|
|
|
|
"$RUNNER_BIN_DIR"/emqx start
|
2021-03-19 00:12:57 +08:00
|
|
|
;;
|
|
|
|
|
|
|
|
pid)
|
|
|
|
## Get the VM's pid
|
|
|
|
if ! relx_get_pid; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
|
|
|
ping)
|
|
|
|
## See if the VM is alive
|
|
|
|
if ! relx_nodetool "ping"; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
|
|
|
escript)
|
|
|
|
## Run an escript under the node's environment
|
2021-05-18 14:54:48 +08:00
|
|
|
if ! relx_escript "$@"; then
|
2021-03-19 00:12:57 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
|
|
|
attach)
|
|
|
|
# Make sure a node IS running
|
|
|
|
if ! relx_nodetool "ping" > /dev/null; then
|
|
|
|
echo "Node is not running!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Bootstrap daemon command (check perms & drop to $RUNNER_USER)
|
2021-05-18 14:54:48 +08:00
|
|
|
bootstrapd
|
2021-03-19 00:12:57 +08:00
|
|
|
|
|
|
|
shift
|
|
|
|
exec "$BINDIR/to_erl" "$PIPE_DIR"
|
|
|
|
;;
|
|
|
|
|
|
|
|
remote_console)
|
|
|
|
# Make sure a node IS running
|
|
|
|
if ! relx_nodetool "ping" > /dev/null; then
|
|
|
|
echo "Node is not running!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Bootstrap daemon command (check perms & drop to $RUNNER_USER)
|
2021-05-18 14:54:48 +08:00
|
|
|
bootstrapd
|
2021-03-19 00:12:57 +08:00
|
|
|
|
|
|
|
shift
|
|
|
|
relx_rem_sh
|
|
|
|
;;
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
upgrade|downgrade|install|unpack|uninstall)
|
2021-03-19 00:12:57 +08:00
|
|
|
if [ -z "$2" ]; then
|
2021-05-18 14:54:48 +08:00
|
|
|
echo "Missing version argument"
|
|
|
|
echo "Usage: $REL_NAME $1 {version}"
|
2021-03-19 00:12:57 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
COMMAND="$1"; shift
|
|
|
|
|
2021-03-19 00:12:57 +08:00
|
|
|
# Make sure a node IS running
|
|
|
|
if ! relx_nodetool "ping" > /dev/null; then
|
|
|
|
echo "Node is not running!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
ERL_FLAGS="$ERL_FLAGS $EPMD_ARG" \
|
2021-03-19 00:12:57 +08:00
|
|
|
exec "$BINDIR/escript" "$ROOTDIR/bin/install_upgrade.escript" \
|
2021-05-18 14:54:48 +08:00
|
|
|
"$COMMAND" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
|
2021-03-19 00:12:57 +08:00
|
|
|
;;
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
versions)
|
2021-03-19 00:12:57 +08:00
|
|
|
# Make sure a node IS running
|
|
|
|
if ! relx_nodetool "ping" > /dev/null; then
|
|
|
|
echo "Node is not running!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
COMMAND="$1"; shift
|
|
|
|
|
|
|
|
ERL_FLAGS="$ERL_FLAGS $EPMD_ARG" \
|
2021-03-19 00:12:57 +08:00
|
|
|
exec "$BINDIR/escript" "$ROOTDIR/bin/install_upgrade.escript" \
|
2021-05-18 14:54:48 +08:00
|
|
|
"versions" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
|
2021-03-19 00:12:57 +08:00
|
|
|
;;
|
|
|
|
|
|
|
|
console|console_clean|console_boot)
|
|
|
|
# Bootstrap daemon command (check perms & drop to $RUNNER_USER)
|
2021-05-18 14:54:48 +08:00
|
|
|
bootstrapd
|
2021-03-19 00:12:57 +08:00
|
|
|
|
|
|
|
# .boot file typically just $REL_NAME (ie, the app name)
|
|
|
|
# however, for debugging, sometimes start_clean.boot is useful.
|
|
|
|
# For e.g. 'setup', one may even want to name another boot script.
|
|
|
|
case "$1" in
|
|
|
|
console)
|
|
|
|
if [ -f "$REL_DIR/$REL_NAME.boot" ]; then
|
|
|
|
BOOTFILE="$REL_DIR/$REL_NAME"
|
|
|
|
else
|
|
|
|
BOOTFILE="$REL_DIR/start"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
console_clean)
|
2021-05-18 14:54:48 +08:00
|
|
|
BOOTFILE="$REL_DIR/start_clean"
|
2021-03-19 00:12:57 +08:00
|
|
|
;;
|
|
|
|
console_boot)
|
|
|
|
shift
|
|
|
|
BOOTFILE="$1"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
# set before generate_config
|
|
|
|
if [ "${_EMQX_START_MODE:-}" = '' ]; then
|
|
|
|
export EMQX_LOG__TO="${EMQX_LOG__TO:-console}"
|
|
|
|
fi
|
|
|
|
|
2021-03-19 00:12:57 +08:00
|
|
|
#generate app.config and vm.args
|
|
|
|
generate_config
|
|
|
|
|
|
|
|
# Setup beam-required vars
|
|
|
|
EMU="beam"
|
|
|
|
PROGNAME="${0#*/}"
|
|
|
|
|
|
|
|
export EMU
|
|
|
|
export PROGNAME
|
|
|
|
|
|
|
|
# Store passed arguments since they will be erased by `set`
|
2021-05-18 14:54:48 +08:00
|
|
|
ARGS="$*"
|
2021-03-19 00:12:57 +08:00
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
# shellcheck disable=SC2086 # $RELX_CONFIG_PATH $CONFIG_ARGS $EPMD_ARG are supposed to be split by whitespace
|
2021-03-19 00:12:57 +08:00
|
|
|
# Build an array of arguments to pass to exec later on
|
|
|
|
# Build it here because this command will be used for logging.
|
2021-05-18 14:54:48 +08:00
|
|
|
set -- "$BINDIR/erlexec" \
|
|
|
|
-boot "$BOOTFILE" -mode "$CODE_LOADING_MODE" \
|
2021-03-19 00:12:57 +08:00
|
|
|
-boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \
|
|
|
|
-mnesia dir "\"${MNESIA_DATA_DIR}\"" \
|
|
|
|
$RELX_CONFIG_PATH $CONFIG_ARGS $EPMD_ARG
|
|
|
|
|
|
|
|
# Log the startup
|
2022-01-07 16:30:56 +08:00
|
|
|
logger -t "${REL_NAME}[$$]" "$* -- ${1+$ARGS}"
|
2021-03-19 00:12:57 +08:00
|
|
|
|
|
|
|
# Start the VM
|
|
|
|
exec "$@" -- ${1+$ARGS}
|
|
|
|
;;
|
|
|
|
|
|
|
|
foreground)
|
|
|
|
# Bootstrap daemon command (check perms & drop to $RUNNER_USER)
|
2021-05-18 14:54:48 +08:00
|
|
|
bootstrapd
|
2021-03-19 00:12:57 +08:00
|
|
|
# start up the release in the foreground for use by runit
|
|
|
|
# or other supervision services
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
# set before generate_config
|
|
|
|
export EMQX_LOG__TO="${EMQX_LOG__TO:-console}"
|
|
|
|
|
2021-03-19 00:12:57 +08:00
|
|
|
#generate app.config and vm.args
|
|
|
|
generate_config
|
|
|
|
|
|
|
|
[ -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`
|
2021-05-18 14:54:48 +08:00
|
|
|
ARGS="$*"
|
2021-03-19 00:12:57 +08:00
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
# shellcheck disable=SC2086 # $RELX_CONFIG_PATH $CONFIG_ARGS $EPMD_ARG are supposed to be split by whitespace
|
2021-03-19 00:12:57 +08:00
|
|
|
# Build an array of arguments to pass to exec later on
|
|
|
|
# Build it here because this command will be used for logging.
|
|
|
|
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}\"" \
|
|
|
|
$RELX_CONFIG_PATH $CONFIG_ARGS $EPMD_ARG
|
|
|
|
|
2022-01-07 16:30:56 +08:00
|
|
|
# Log the startup
|
|
|
|
logger -t "${REL_NAME}[$$]" "$* -- ${1+$ARGS}"
|
2021-03-19 00:12:57 +08:00
|
|
|
|
|
|
|
# Start the VM
|
|
|
|
exec "$@" -- ${1+$ARGS}
|
|
|
|
;;
|
|
|
|
ertspath)
|
2021-05-18 14:54:48 +08:00
|
|
|
echo "$ERTS_PATH"
|
2021-03-19 00:12:57 +08:00
|
|
|
;;
|
|
|
|
rpc)
|
|
|
|
# Make sure a node IS running
|
|
|
|
if ! relx_nodetool "ping" > /dev/null; then
|
|
|
|
echo "Node is not running!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
shift
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
relx_nodetool rpc "$@"
|
2021-03-19 00:12:57 +08:00
|
|
|
;;
|
|
|
|
rpcterms)
|
|
|
|
# Make sure a node IS running
|
|
|
|
if ! relx_nodetool "ping" > /dev/null; then
|
|
|
|
echo "Node is not running!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
shift
|
|
|
|
|
2021-05-18 14:54:48 +08:00
|
|
|
relx_nodetool rpcterms "$@"
|
|
|
|
;;
|
|
|
|
root_dir)
|
|
|
|
# Make sure a node IS running
|
|
|
|
if ! relx_nodetool "ping" > /dev/null; then
|
|
|
|
echo "Node is not running!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
shift
|
|
|
|
relx_nodetool "eval" 'code:root_dir()'
|
2021-03-19 00:12:57 +08:00
|
|
|
;;
|
|
|
|
eval)
|
|
|
|
# Make sure a node IS running
|
|
|
|
if ! relx_nodetool "ping" > /dev/null; then
|
|
|
|
echo "Node is not running!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
shift
|
2021-05-18 14:54:48 +08:00
|
|
|
relx_nodetool "eval" "$@"
|
2021-03-19 00:12:57 +08:00
|
|
|
;;
|
|
|
|
*)
|
2021-05-18 14:54:48 +08:00
|
|
|
relx_usage "$1"
|
2021-03-19 00:12:57 +08:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
exit 0
|