fix(emqx_ctl): Start CLI before ekka

This commit is contained in:
ieQu1 2023-02-08 09:58:39 +01:00
parent 8a822c664c
commit 794bedef9f
19 changed files with 105 additions and 30 deletions

View File

@ -35,7 +35,6 @@ init([]) ->
child_spec(emqx_hooks, worker),
child_spec(emqx_stats, worker),
child_spec(emqx_metrics, worker),
child_spec(emqx_ctl, worker),
child_spec(emqx_authn_authz_metrics_sup, supervisor)
]
}}.

View File

@ -3,7 +3,7 @@
{vsn, "0.1.12"},
{registered, []},
{mod, {emqx_conf_app, []}},
{applications, [kernel, stdlib]},
{applications, [kernel, stdlib, emqx_ctl]},
{env, []},
{modules, []}
]}.

4
apps/emqx_ctl/README.md Normal file
View File

@ -0,0 +1,4 @@
emqx_ctl
=====
Backend module for `emqx_ctl` command.

View File

@ -0,0 +1,2 @@
{erl_opts, [debug_info]}.
{deps, []}.

View File

@ -0,0 +1,15 @@
{application, emqx_ctl, [
{description, "Backend for emqx_ctl script"},
{vsn, "0.1.0"},
{registered, []},
{mod, {emqx_ctl_app, []}},
{applications, [
kernel,
stdlib
]},
{env, []},
{modules, []},
{licenses, ["Apache-2.0"]},
{links, []}
]}.

View File

@ -18,8 +18,7 @@
-behaviour(gen_server).
-include("types.hrl").
-include("logger.hrl").
-include_lib("kernel/include/logger.hrl").
-export([start_link/0, stop/0]).
@ -70,7 +69,7 @@
-define(SERVER, ?MODULE).
-define(CMD_TAB, emqx_command).
-spec start_link() -> startlink_ret().
-spec start_link() -> {ok, pid()}.
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
@ -103,7 +102,7 @@ cast(Msg) -> gen_server:cast(?SERVER, Msg).
run_command([]) ->
run_command(help, []);
run_command([Cmd | Args]) ->
case emqx_misc:safe_to_existing_atom(Cmd) of
case safe_to_existing_atom(Cmd) of
{ok, Cmd1} ->
run_command(Cmd1, Args);
_ ->
@ -122,7 +121,7 @@ run_command(Cmd, Args) when is_atom(Cmd) ->
ok
catch
_:Reason:Stacktrace ->
?SLOG(error, #{
?LOG_ERROR(#{
msg => "ctl_command_crashed",
stacktrace => Stacktrace,
reason => Reason
@ -220,7 +219,7 @@ format_usage(CmdParams, Desc, Width) ->
%%--------------------------------------------------------------------
init([]) ->
ok = emqx_tables:new(?CMD_TAB, [protected, ordered_set]),
_ = ets:new(?CMD_TAB, [named_table, protected, ordered_set]),
{ok, #state{seq = 0}}.
handle_call({register_command, Cmd, MF, Opts}, _From, State = #state{seq = Seq}) ->
@ -229,23 +228,23 @@ handle_call({register_command, Cmd, MF, Opts}, _From, State = #state{seq = Seq})
ets:insert(?CMD_TAB, {{Seq, Cmd}, MF, Opts}),
{reply, ok, next_seq(State)};
[[OriginSeq] | _] ->
?SLOG(warning, #{msg => "CMD_overidden", cmd => Cmd, mf => MF}),
?LOG_WARNING(#{msg => "CMD_overidden", cmd => Cmd, mf => MF}),
true = ets:insert(?CMD_TAB, {{OriginSeq, Cmd}, MF, Opts}),
{reply, ok, State}
end;
handle_call(Req, _From, State) ->
?SLOG(error, #{msg => "unexpected_call", call => Req}),
?LOG_ERROR(#{msg => "unexpected_call", call => Req}),
{reply, ignored, State}.
handle_cast({unregister_command, Cmd}, State) ->
ets:match_delete(?CMD_TAB, {{'_', Cmd}, '_', '_'}),
noreply(State);
handle_cast(Msg, State) ->
?SLOG(error, #{msg => "unexpected_cast", cast => Msg}),
?LOG_ERROR(#{msg => "unexpected_cast", cast => Msg}),
noreply(State).
handle_info(Info, State) ->
?SLOG(error, #{msg => "unexpected_info", info => Info}),
?LOG_ERROR(#{msg => "unexpected_info", info => Info}),
noreply(State).
terminate(_Reason, _State) ->
@ -272,3 +271,11 @@ zip_cmd([X | Xs], [Y | Ys]) -> [{X, Y} | zip_cmd(Xs, Ys)];
zip_cmd([X | Xs], []) -> [{X, ""} | zip_cmd(Xs, [])];
zip_cmd([], [Y | Ys]) -> [{"", Y} | zip_cmd([], Ys)];
zip_cmd([], []) -> [].
safe_to_existing_atom(Str) ->
try
{ok, list_to_existing_atom(Str)}
catch
_:badarg ->
undefined
end.

View File

@ -0,0 +1,18 @@
%%%-------------------------------------------------------------------
%% @doc emqx_ctl public API
%% @end
%%%-------------------------------------------------------------------
-module(emqx_ctl_app).
-behaviour(application).
-export([start/2, stop/1]).
start(_StartType, _StartArgs) ->
emqx_ctl_sup:start_link().
stop(_State) ->
ok.
%% internal functions

View File

@ -0,0 +1,33 @@
%%%-------------------------------------------------------------------
%% @doc emqx_ctl top level supervisor.
%% @end
%%%-------------------------------------------------------------------
-module(emqx_ctl_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
-define(SERVER, ?MODULE).
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
init([]) ->
SupFlags = #{
strategy => one_for_all,
intensity => 0,
period => 1
},
ChildSpecs = [
#{
id => emqx_ctl,
start => {emqx_ctl, start_link, []},
type => worker,
restart => permanent
}
],
{ok, {SupFlags, ChildSpecs}}.

View File

@ -22,12 +22,10 @@
-include_lib("eunit/include/eunit.hrl").
-include_lib("common_test/include/ct.hrl").
all() -> emqx_common_test_helpers:all(?MODULE).
all() -> [t_reg_unreg_command, t_run_commands, t_print, t_usage, t_unexpected].
init_per_suite(Config) ->
%% ensure stopped, this suite tests emqx_ctl process independently
application:stop(emqx),
ok = emqx_logger:set_log_level(emergency),
application:stop(emqx_ctl),
Config.
end_per_suite(_Config) ->

View File

@ -5,7 +5,7 @@
{vsn, "5.0.13"},
{modules, []},
{registered, [emqx_dashboard_sup]},
{applications, [kernel, stdlib, mnesia, minirest, emqx]},
{applications, [kernel, stdlib, mnesia, minirest, emqx, emqx_ctl]},
{mod, {emqx_dashboard_app, []}},
{env, []},
{licenses, ["Apache-2.0"]},

View File

@ -1,10 +1,10 @@
%% -*- mode: erlang -*-
{application, emqx_gateway, [
{description, "The Gateway management application"},
{vsn, "0.1.11"},
{vsn, "0.1.12"},
{registered, []},
{mod, {emqx_gateway_app, []}},
{applications, [kernel, stdlib, grpc, emqx, emqx_authn]},
{applications, [kernel, stdlib, grpc, emqx, emqx_authn, emqx_ctl]},
{env, []},
{modules, []},
{licenses, ["Apache 2.0"]},

View File

@ -3,10 +3,10 @@
{id, "emqx_machine"},
{description, "The EMQX Machine"},
% strict semver, bump manually!
{vsn, "0.1.3"},
{vsn, "0.1.4"},
{modules, []},
{registered, []},
{applications, [kernel, stdlib]},
{applications, [kernel, stdlib, emqx_ctl]},
{mod, {emqx_machine_app, []}},
{env, []},
{licenses, ["Apache-2.0"]},

View File

@ -29,6 +29,7 @@
%% @doc EMQX boot entrypoint.
start() ->
emqx_mgmt_cli:load(),
case os:type() of
{win32, nt} ->
ok;

View File

@ -5,7 +5,7 @@
{vsn, "5.0.15"},
{modules, []},
{registered, [emqx_management_sup]},
{applications, [kernel, stdlib, emqx_plugins, minirest, emqx]},
{applications, [kernel, stdlib, emqx_plugins, minirest, emqx, emqx_ctl]},
{mod, {emqx_mgmt_app, []}},
{env, []},
{licenses, ["Apache-2.0"]},

View File

@ -31,9 +31,7 @@ start(_Type, _Args) ->
ok = mria_rlog:wait_for_shards([?MANAGEMENT_SHARD], infinity),
case emqx_mgmt_auth:init_bootstrap_file() of
ok ->
{ok, Sup} = emqx_mgmt_sup:start_link(),
ok = emqx_mgmt_cli:load(),
{ok, Sup};
emqx_mgmt_sup:start_link();
{error, Reason} ->
{error, Reason}
end.

View File

@ -1,9 +1,9 @@
%% -*- mode: erlang -*-
{application, emqx_modules, [
{description, "EMQX Modules"},
{vsn, "5.0.9"},
{vsn, "5.0.10"},
{modules, []},
{applications, [kernel, stdlib, emqx]},
{applications, [kernel, stdlib, emqx, emqx_ctl]},
{mod, {emqx_modules_app, []}},
{registered, [emqx_modules_sup]},
{env, []}

View File

@ -5,7 +5,7 @@
{vsn, "5.0.9"},
{modules, []},
{registered, [emqx_retainer_sup]},
{applications, [kernel, stdlib, emqx]},
{applications, [kernel, stdlib, emqx, emqx_ctl]},
{mod, {emqx_retainer_app, []}},
{env, []},
{licenses, ["Apache-2.0"]},

View File

@ -5,7 +5,7 @@
{vsn, "5.0.8"},
{modules, []},
{registered, [emqx_rule_engine_sup, emqx_rule_engine]},
{applications, [kernel, stdlib, rulesql, getopt]},
{applications, [kernel, stdlib, rulesql, getopt, emqx_ctl]},
{mod, {emqx_rule_engine_app, []}},
{env, []},
{licenses, ["Apache-2.0"]},

View File

@ -3,6 +3,6 @@
{vsn, "5.0.5"},
{modules, []},
{registered, [emqx_license_sup]},
{applications, [kernel, stdlib]},
{applications, [kernel, stdlib, emqx_ctl]},
{mod, {emqx_license_app, []}}
]}.