From 67cca5d3a15c81add5e2ea2eafe8c921848cbe5e Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Thu, 23 Dec 2021 15:41:35 -0300 Subject: [PATCH] chore(mix): use the same script in mix release as in rebar release Surprisingly enough, by doing small cirurgical changes in the existing EMQX control scripts, we are able to get it running with Elixir and with existing functionalities (`console`, `remote_console`, `start`, `stop`, `ctl`, `foreground`, `eval`). --- .github/workflows/elixir_release.yml | 7 +++- bin/common_functions.sh | 28 +++++++++++-- bin/emqx | 63 +++++++++++++++++++++------- bin/nodetool | 17 ++++++++ data/emqx_vars | 1 + mix.exs | 12 ++++-- rebar.config.erl | 2 + rel/overlays/bin/emqx | 48 --------------------- 8 files changed, 105 insertions(+), 73 deletions(-) delete mode 100755 rel/overlays/bin/emqx diff --git a/.github/workflows/elixir_release.yml b/.github/workflows/elixir_release.yml index 033e9c010..dc887efbc 100644 --- a/.github/workflows/elixir_release.yml +++ b/.github/workflows/elixir_release.yml @@ -27,9 +27,12 @@ jobs: run: mix release --overwrite - name: start release run: | - cd _build/dev/rel/emqx_base - bin/emqx daemon_iex + cd _build/dev/rel/emqx + bin/emqx start - name: check if started run: | sleep 10 nc -zv localhost 1883 + cd _build/dev/rel/emqx + bin/emqx ping + bin/emqx ctl status diff --git a/bin/common_functions.sh b/bin/common_functions.sh index 5354fc58e..3ec099f49 100644 --- a/bin/common_functions.sh +++ b/bin/common_functions.sh @@ -16,7 +16,12 @@ assert_node_alive() { } check_erlang_start() { - "$BINDIR/$PROGNAME" -noshell -boot "$REL_DIR/start_clean" -s crypto start -s erlang halt + "$BINDIR/$PROGNAME" \ + -noshell \ + -boot_var RELEASE_LIB "$ERTS_LIB_DIR/lib" \ + -boot "$REL_DIR/start_clean" \ + -s crypto start \ + -s erlang halt } # Simple way to check the correct user and fail early @@ -62,9 +67,24 @@ relx_rem_sh() { # shellcheck disable=SC2086 # $EPMD_ARG is supposed to be split by whitespace # shellcheck disable=SC2153 # $NAME_TYPE is defined by `common_defs2.sh`, which runs before this is called # Setup remote shell command to control node - exec "$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 + if [ "$IS_ELIXIR" = "yes" ] + then + exec "$REL_DIR/iex" \ + --remsh "$NAME" \ + --boot-var RELEASE_LIB "$ERTS_LIB_DIR" \ + --cookie "$COOKIE" \ + --hidden \ + --erl "-kernel net_ticktime $TICKTIME" \ + --erl "$EPMD_ARG" \ + --erl "$NAME_TYPE $id" \ + --boot "$REL_DIR/start_clean" + else + exec "$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 + fi } # Generate a random id diff --git a/bin/emqx b/bin/emqx index 21458cb53..bc1c8d148 100755 --- a/bin/emqx +++ b/bin/emqx @@ -69,14 +69,14 @@ usage() { echo "Print path to Erlang runtime dir" ;; rpc) - echo "Usge $REL_NAME rpc MODULE FUNCTION [ARGS, ...]" + echo "Usage $REL_NAME rpc MODULE FUNCTION [ARGS, ...]" echo "Connect to the $EMQX_DESCRIPTION node and make an Erlang RPC" echo "The result of the RPC call must be 'ok'" echo "This command blocks for at most 60 seconds in case the node" echo "does not reply the call in time" ;; rpcterms) - echo "Usge $REL_NAME rpcterms MODULE FUNCTION [ARGS, ...]" + echo "Usage $REL_NAME rpcterms MODULE FUNCTION [ARGS, ...]" echo "Connect to the $EMQX_DESCRIPTION node and make an Erlang RPC" echo "The result of the RPC call is pretty-printed as an Erlang term" ;; @@ -173,9 +173,9 @@ if [ "$ES" -ne 0 ]; then exit $ES fi -## IS_BOOT_COMMAND is set for later to inspect node name and cookie -## from hocon config (or env variable), which resides in -## `common_defs2.sh`. +## IS_BOOT_COMMAND is set to be later used by `common_defs2.sh` to +## inspect node name and cookie from hocon config (or env variable), +## which also resides in `common_defs2.sh`. case "${COMMAND}" in start|console|console_clean|foreground) IS_BOOT_COMMAND='yes' @@ -368,11 +368,23 @@ case "${COMMAND}" in # 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. - set -- "$BINDIR/erlexec" \ - -boot "$BOOTFILE" -mode "$CODE_LOADING_MODE" \ - -boot_var ERTS_LIB_DIR "$ERTS_LIB_DIR" \ - -mnesia dir "\"${MNESIA_DATA_DIR}\"" \ - $CONFIG_ARGS $EPMD_ARG + if [ "$IS_ELIXIR" = yes ] + then + set -- "$REL_DIR/iex" \ + --boot "$BOOTFILE" \ + --erl "-mode $CODE_LOADING_MODE" \ + --boot-var RELEASE_LIB "$ERTS_LIB_DIR" \ + --erl "-mnesia dir \"${MNESIA_DATA_DIR}\"" \ + --erl "$CONFIG_ARGS" \ + --erl "$EPMD_ARG" \ + --werl + else + set -- "$BINDIR/erlexec" \ + -boot "$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}" @@ -407,11 +419,25 @@ case "${COMMAND}" in # 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. - 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 + 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}" @@ -453,7 +479,12 @@ case "${COMMAND}" in assert_node_alive shift - relx_nodetool "eval" "$@" + if [ "$IS_ELIXIR" = "yes" ] + then + relx_nodetool "eval-elixir" "$@" + else + relx_nodetool "eval" "$@" + fi ;; *) usage "$COMMAND" diff --git a/bin/nodetool b/bin/nodetool index 0fef516cd..9d5b659de 100755 --- a/bin/nodetool +++ b/bin/nodetool @@ -126,6 +126,23 @@ do(Args) -> io:format("RPC to ~p failed: ~p~n", [TargetNode, Reason]), halt(1) end; + ["eval-elixir" | ListOfArgs] -> + JoinedArgs = string:join(ListOfArgs, " "), + case rpc:call(TargetNode, 'Elixir.Code', eval_string, [JoinedArgs]) of + {Value, []} -> + %% Since we are in Erlang land, we ask Elixir to + %% format the result as well: + case rpc:call(TargetNode, 'Elixir.Kernel', inspect, [Value, [{pretty, true}]]) of + {badrpc, Reason} -> + io:format("RPC to ~p failed: ~p~n", [TargetNode, Reason]), + halt(1); + PrettyStr -> + io:format("~s~n", [PrettyStr]) + end; + {badrpc, Reason} -> + io:format("RPC to ~p failed: ~p~n", [TargetNode, Reason]), + halt(1) + end; Other -> io:format("Other: ~p~n", [Other]), io:format("Usage: nodetool chkconfig|getpid|ping|stop|rpc|rpc_infinity|rpcterms|eval|cold_eval [Terms] [RPC]\n") diff --git a/data/emqx_vars b/data/emqx_vars index 453bd618f..6687ca881 100644 --- a/data/emqx_vars +++ b/data/emqx_vars @@ -13,6 +13,7 @@ RUNNER_LIB_DIR="{{ runner_lib_dir }}" RUNNER_ETC_DIR="{{ runner_etc_dir }}" RUNNER_DATA_DIR="{{ runner_data_dir }}" RUNNER_USER="{{ runner_user }}" +IS_ELIXIR="{{ is_elixir }}" EMQX_LICENSE_CONF='' export EMQX_DESCRIPTION='{{ emqx_description }}' diff --git a/mix.exs b/mix.exs index 16dd150cd..86a7a3c35 100644 --- a/mix.exs +++ b/mix.exs @@ -93,7 +93,7 @@ defmodule EMQXUmbrella.MixProject do defp releases() do [ - emqx_base: [ + emqx: [ applications: [ logger: :permanent, esasl: :load, @@ -106,7 +106,6 @@ defmodule EMQXUmbrella.MixProject do inets: :permanent, compiler: :permanent, runtime_tools: :permanent, - hocon: :load, emqx: :load, emqx_conf: :load, emqx_machine: :permanent, @@ -222,7 +221,8 @@ defmodule EMQXUmbrella.MixProject do # FIXME: this is empty in `make emqx` ??? erl_opts: "", # FIXME: varies with edge/community/enterprise - emqx_description: "EMQ X Community Edition" + emqx_description: "EMQ X Community Edition", + is_elixir: "yes" ] # This is generated by `scripts/merge-config.escript` or `make @@ -261,6 +261,12 @@ defmodule EMQXUmbrella.MixProject do ) ) + Mix.Generator.copy_file( + "bin/emqx", + Path.join(bin, "emqx"), + force: overwrite? + ) + release end diff --git a/rebar.config.erl b/rebar.config.erl index 6e0774424..adfb2deb8 100644 --- a/rebar.config.erl +++ b/rebar.config.erl @@ -219,6 +219,7 @@ overlay_vars_pkg(bin) -> , {runner_log_dir, "$RUNNER_ROOT_DIR/log"} , {runner_data_dir, "$RUNNER_ROOT_DIR/data"} , {runner_user, ""} + , {is_elixir, "no"} ]; overlay_vars_pkg(pkg) -> [ {platform_bin_dir, ""} @@ -234,6 +235,7 @@ overlay_vars_pkg(pkg) -> , {runner_log_dir, "/var/log/emqx"} , {runner_data_dir, "/var/lib/emqx"} , {runner_user, "emqx"} + , {is_elixir, "no"} ]. relx_apps(ReleaseType, Edition) -> diff --git a/rel/overlays/bin/emqx b/rel/overlays/bin/emqx deleted file mode 100755 index 41e921abc..000000000 --- a/rel/overlays/bin/emqx +++ /dev/null @@ -1,48 +0,0 @@ -#!/bin/bash -# -*- tab-width:4;indent-tabs-mode:nil -*- -# ex: ts=4 sw=4 et - -set -euo pipefail - -BASE="$(cd "$(dirname "$(readlink "$0" || echo "$0")")"/..; pwd -P)" -# shellcheck disable=SC1090,SC1091 -source "$BASE/bin/common_defs.sh" -# shellcheck disable=SC1090,SC1091 -source "$BASE/bin/common_functions.sh" - -# Make sure log directory exists -mkdir -p "$RUNNER_LOG_DIR" - -# Make sure data directory exists -mkdir -p "$RUNNER_DATA_DIR" - -# Make sure data/configs exists -mkdir -p "$CONFIGS_DIR" - -COMMAND="${1:-}" - -## IS_BOOT_COMMAND is set for later to inspect node name and cookie -## from hocon config (or env variable), which resides in -## `common_defs2.sh`. -case "${COMMAND}" in - daemon|daemon_iex|start|start_iex) - IS_BOOT_COMMAND='yes' - ;; - *) - IS_BOOT_COMMAND='no' - ;; -esac -export IS_BOOT_COMMAND - -# shellcheck disable=SC1090,SC1091 -source "$BASE/bin/common_defs2.sh" - -cd "$ROOTDIR" - -# FIXME!!! Create case for commands. -generate_config "$NAME_TYPE" "$NAME" -# Must be explicitly exported here in order to be picked up correctly -export RELEASE_SYS_CONFIG -export RELEASE_TMP="$RUNNER_ROOT_DIR" - -exec bin/emqx_base "$@"