chore: refine boot script with more readable help info

This commit is contained in:
Zaiming (Stone) Shi 2022-04-11 23:48:00 +02:00
parent 5e652217c8
commit 2da27392f7
1 changed files with 166 additions and 80 deletions

158
bin/emqx
View File

@ -31,6 +31,12 @@ ERTS_LIB_DIR="$ERTS_DIR/../lib"
# Echo to stderr on errors # Echo to stderr on errors
echoerr() { echo "$*" 1>&2; } echoerr() { echo "$*" 1>&2; }
assert_node_alive() {
if ! relx_nodetool "ping" > /dev/null; then
die "node_is_not_running!" 1
fi
}
check_eralng_start() { check_eralng_start() {
"$BINDIR/$PROGNAME" -noshell -boot "$REL_DIR/start_clean" -s crypto start -s init stop "$BINDIR/$PROGNAME" -noshell -boot "$REL_DIR/start_clean" -s crypto start -s init stop
} }
@ -60,16 +66,80 @@ fi
# cuttlefish try to read environment variables starting with "EMQX_" # cuttlefish try to read environment variables starting with "EMQX_"
export CUTTLEFISH_ENV_OVERRIDE_PREFIX='EMQX_' export CUTTLEFISH_ENV_OVERRIDE_PREFIX='EMQX_'
relx_usage() { usage() {
command="$1" local command="$1"
case "$command" in case "$command" in
start)
echo "Start EMQX service in daemon mode"
;;
stop)
echo "Stop the running EMQX program"
;;
console)
echo "Boot up EMQX service in an interactive Erlang shell"
echo "This command needs a tty"
;;
console_clean)
echo "This command does NOT boot up the EMQX service"
echo "It only starts an interactive Erlang shell with all the"
echo "EMQX code available"
;;
foreground)
echo "Start EMQX in foreground mode without an interactive shell"
;;
pid)
echo "Print out EMQX process identifier"
;;
ping)
echo "Check if the EMQX node is up and running"
echo "This command exit with 0 silently if node is running"
;;
escript)
echo "Execute a escript using the Erlang runtime from EMQX package installation"
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 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 an interactive shell running an Erlang 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"
;;
rpc)
echo "Usge $REL_NAME rpc MODULE FUNCTION [ARGS, ...]"
echo "Connect to the EMQX node and make an Erlang RPC"
echo "This command blocks for at most 60 seconds."
echo "It exits with non-zero code in case of any RPC failure"
echo "including connection error and runtime exception"
;;
rpcterms)
echo "Usge $REL_NAME rpcterms MODULE FUNCTION [ARGS, ...]"
echo "Connect to the EMQX node and make an Erlang RPC"
echo "The result of the RPC call is pretty-printed as an "
echo "Erlang term"
;;
root_dir)
echo "Print EMQX installation root dir"
;;
eval)
echo "Evaluate an Erlang expression in the EMQX node"
;;
versions)
echo "List installed EMQX versions and their status"
;;
unpack) unpack)
echo "Usage: $REL_NAME unpack [VERSION]" echo "Usage: $REL_NAME unpack [VERSION]"
echo "Unpacks a release package VERSION, it assumes that this" echo "Unpacks a release package VERSION, it assumes that this"
echo "release package tarball has already been deployed at one" echo "release package tarball has already been deployed at one"
echo "of the following locations:" echo "of the following locations:"
echo " releases/<relname>-<version>.tar.gz"
echo " releases/<relname>-<version>.zip" echo " releases/<relname>-<version>.zip"
;; ;;
install) install)
@ -77,7 +147,6 @@ relx_usage() {
echo "Installs a release package VERSION, it assumes that this" echo "Installs a release package VERSION, it assumes that this"
echo "release package tarball has already been deployed at one" echo "release package tarball has already been deployed at one"
echo "of the following locations:" echo "of the following locations:"
echo " releases/<relname>-<version>.tar.gz"
echo " releases/<relname>-<version>.zip" echo " releases/<relname>-<version>.zip"
echo "" echo ""
echo " --no-permanent Install release package VERSION but" echo " --no-permanent Install release package VERSION but"
@ -93,7 +162,6 @@ relx_usage() {
echo "Upgrades the currently running release to VERSION, it assumes" echo "Upgrades the currently running release to VERSION, it assumes"
echo "that a release package tarball has already been deployed at one" echo "that a release package tarball has already been deployed at one"
echo "of the following locations:" echo "of the following locations:"
echo " releases/<relname>-<version>.tar.gz"
echo " releases/<relname>-<version>.zip" echo " releases/<relname>-<version>.zip"
echo "" echo ""
echo " --no-permanent Install release package VERSION but" echo " --no-permanent Install release package VERSION but"
@ -104,18 +172,51 @@ relx_usage() {
echo "Downgrades the currently running release to VERSION, it assumes" echo "Downgrades the currently running release to VERSION, it assumes"
echo "that a release package tarball has already been deployed at one" echo "that a release package tarball has already been deployed at one"
echo "of the following locations:" echo "of the following locations:"
echo " releases/<relname>-<version>.tar.gz"
echo " releases/<relname>-<version>.zip" echo " releases/<relname>-<version>.zip"
echo "" echo ""
echo " --no-permanent Install release package VERSION but" echo " --no-permanent Install release package VERSION but"
echo " don't make it permanent" 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}" echo "Usage: $REL_NAME COMMAND [help]"
echo ''
echo "Commonly used COMMANDs:"
echo " start: Start EMQX in daemon mode"
echo " console: Start EMQX in an interactive Erlang shell"
echo " foreground: Start EMQX in foreground mode without an interactive shell"
echo " stop: Stop the running EMQX node"
echo " ctl: Administration commands, execute '$REL_NAME ctl help' for more details"
echo ''
echo "More:"
echo " Shell attach: remote_console | attach"
echo " Up/Down-grade: upgrade | downgrade | install | uninstall"
echo " Install info: ertspath | root_dir | versions"
echo " Runtime info: pid | ping | versions"
echo " Advanced: console_clean | escript | rpc | rpcterms | eval"
echo ''
echo "Execute '$REL_NAME COMMAND help' for more information"
;; ;;
esac esac
} }
COMMAND="${1:-}"
if [ -z "$COMMAND" ]; then
usage 'help'
exit 1
elif [ "$COMMAND" = 'help' ]; then
usage 'help'
exit 0
fi
if [ "${2:-}" = 'help' ]; then
## 'ctl' command has its own usage info
if [ "$COMMAND" != 'ctl' ]; then
usage "$COMMAND"
exit 0
fi
fi
# Simple way to check the correct user and fail early # Simple way to check the correct user and fail early
check_user() { check_user() {
# Validate that the user running the script is the owner of the # Validate that the user running the script is the owner of the
@ -135,7 +236,6 @@ check_user() {
fi fi
} }
# Make sure the user running this script is the owner and/or su to that user # Make sure the user running this script is the owner and/or su to that user
check_user "$@" check_user "$@"
ES=$? ES=$?
@ -357,9 +457,10 @@ if [ -z "$NAME_ARG" ]; then
if [ "$IS_BOOT_COMMAND" = 'no' ]; then if [ "$IS_BOOT_COMMAND" = 'no' ]; then
# for non-boot commands, inspect vm.<time>.args for node name # for non-boot commands, inspect vm.<time>.args for node name
# shellcheck disable=SC2012,SC2086 # shellcheck disable=SC2012,SC2086
LATEST_VM_ARGS="$(ls -t $RUNNER_DATA_DIR/configs/vm.*.args | head -1)" LATEST_VM_ARGS="$(ls -t $RUNNER_DATA_DIR/configs/vm.*.args 2>/dev/null | head -1)"
if [ -z "$LATEST_VM_ARGS" ]; then if [ -z "$LATEST_VM_ARGS" ]; then
echo "For command $1, there is no vm.*.args config file found in $RUNNER_DATA_DIR/configs/" echoerr "For command $1, there is no vm.*.args config file found in $RUNNER_DATA_DIR/configs/"
echoerr "Please make sure the node is initialized (started for at least once)"
exit 1 exit 1
fi fi
NODENAME="$(grep -E '^-name' "$LATEST_VM_ARGS" | awk '{print $2}')" NODENAME="$(grep -E '^-name' "$LATEST_VM_ARGS" | awk '{print $2}')"
@ -709,50 +810,35 @@ case "$1" in
ertspath) ertspath)
echo "$ERTS_PATH" echo "$ERTS_PATH"
;; ;;
rpc)
# Make sure a node IS running
if ! relx_nodetool "ping" > /dev/null; then
echo "Node is not running!"
exit 1
fi
ctl)
assert_node_alive
shift shift
relx_nodetool rpc_infinity emqx_ctl run_command "$@"
;;
rpc)
assert_node_alive
shift
relx_nodetool rpc "$@" relx_nodetool rpc "$@"
;; ;;
rpcterms) rpcterms)
# Make sure a node IS running assert_node_alive
if ! relx_nodetool "ping" > /dev/null; then
echo "Node is not running!"
exit 1
fi
shift shift
relx_nodetool rpcterms "$@" relx_nodetool rpcterms "$@"
;; ;;
root_dir) root_dir)
# Make sure a node IS running assert_node_alive
if ! relx_nodetool "ping" > /dev/null; then
echo "Node is not running!"
exit 1
fi
shift shift
relx_nodetool "eval" 'code:root_dir()' relx_nodetool "eval" 'code:root_dir()'
;; ;;
eval) eval)
# Make sure a node IS running assert_node_alive
if ! relx_nodetool "ping" > /dev/null; then
echo "Node is not running!"
exit 1
fi
shift shift
relx_nodetool "eval" "$@" relx_nodetool "eval" "$@"
;; ;;
*) *)
relx_usage "$1" usage "$COMMAND"
exit 1 exit 1
;; ;;
esac esac