diff --git a/bin/emqx b/bin/emqx index 1722e96e0..ac459b6f7 100755 --- a/bin/emqx +++ b/bin/emqx @@ -31,6 +31,12 @@ ERTS_LIB_DIR="$ERTS_DIR/../lib" # Echo to stderr on errors echoerr() { echo "$*" 1>&2; } +assert_node_alive() { + if ! relx_nodetool "ping" > /dev/null; then + die "node_is_not_running!" 1 + fi +} + check_eralng_start() { "$BINDIR/$PROGNAME" -noshell -boot "$REL_DIR/start_clean" -s crypto start -s init stop } @@ -60,62 +66,157 @@ fi # cuttlefish try to read environment variables starting with "EMQX_" export CUTTLEFISH_ENV_OVERRIDE_PREFIX='EMQX_' -relx_usage() { - command="$1" +usage() { + local 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/-.tar.gz" - echo " releases/-.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/-.tar.gz" - echo " releases/-.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/-.tar.gz" - echo " releases/-.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/-.tar.gz" - echo " releases/-.zip" - echo "" - echo " --no-permanent Install release package VERSION but" - echo " don't make it permanent" - ;; - *) - echo "Usage: $REL_NAME {start|start_boot |ertspath|foreground|stop|restart|reboot|pid|ping|console|console_clean|console_boot |attach|remote_console|upgrade|downgrade|install|uninstall|versions|escript|rpc|rpcterms|eval|root_dir}" - ;; + 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) + 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/-.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/-.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/-.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/-.zip" + echo "" + echo " --no-permanent Install release package VERSION but" + echo " don't make it permanent" + ;; + *) + 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 } +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 check_user() { # Validate that the user running the script is the owner of the @@ -135,7 +236,6 @@ check_user() { fi } - # Make sure the user running this script is the owner and/or su to that user check_user "$@" ES=$? @@ -357,9 +457,10 @@ if [ -z "$NAME_ARG" ]; then if [ "$IS_BOOT_COMMAND" = 'no' ]; then # for non-boot commands, inspect vm.