chore(emqx_management): format code
This commit is contained in:
parent
baa9fd8255
commit
28653bb457
|
@ -23,8 +23,6 @@
|
||||||
|
|
||||||
-define(PRINT_CMD(Cmd, Descr), io:format("~-48s# ~s~n", [Cmd, Descr])).
|
-define(PRINT_CMD(Cmd, Descr), io:format("~-48s# ~s~n", [Cmd, Descr])).
|
||||||
|
|
||||||
-import(lists, [foreach/2]).
|
|
||||||
|
|
||||||
-export([load/0]).
|
-export([load/0]).
|
||||||
|
|
||||||
-export([ status/1
|
-export([ status/1
|
||||||
|
@ -58,7 +56,7 @@
|
||||||
-spec(load() -> ok).
|
-spec(load() -> ok).
|
||||||
load() ->
|
load() ->
|
||||||
Cmds = [Fun || {Fun, _} <- ?MODULE:module_info(exports), is_cmd(Fun)],
|
Cmds = [Fun || {Fun, _} <- ?MODULE:module_info(exports), is_cmd(Fun)],
|
||||||
foreach(fun(Cmd) -> emqx_ctl:register_command(Cmd, {?MODULE, Cmd}, []) end, Cmds).
|
lists:foreach(fun(Cmd) -> emqx_ctl:register_command(Cmd, {?MODULE, Cmd}, []) end, Cmds).
|
||||||
|
|
||||||
is_cmd(Fun) ->
|
is_cmd(Fun) ->
|
||||||
not lists:member(Fun, [init, load, module_info]).
|
not lists:member(Fun, [init, load, module_info]).
|
||||||
|
@ -100,7 +98,7 @@ mgmt(["delete", AppId]) ->
|
||||||
end;
|
end;
|
||||||
|
|
||||||
mgmt(["list"]) ->
|
mgmt(["list"]) ->
|
||||||
foreach(fun({AppId, AppSecret, Name, Desc, Status, Expired}) ->
|
lists:foreach(fun({AppId, AppSecret, Name, Desc, Status, Expired}) ->
|
||||||
emqx_ctl:print("app_id: ~s, secret: ~s, name: ~s, desc: ~s, status: ~s, expired: ~p~n",
|
emqx_ctl:print("app_id: ~s, secret: ~s, name: ~s, desc: ~s, status: ~s, expired: ~p~n",
|
||||||
[AppId, AppSecret, Name, Desc, Status, Expired])
|
[AppId, AppSecret, Name, Desc, Status, Expired])
|
||||||
end, emqx_mgmt_auth:list_apps());
|
end, emqx_mgmt_auth:list_apps());
|
||||||
|
@ -229,7 +227,7 @@ routes(_) ->
|
||||||
{"routes show <Topic>", "Show a route"}]).
|
{"routes show <Topic>", "Show a route"}]).
|
||||||
|
|
||||||
subscriptions(["list"]) ->
|
subscriptions(["list"]) ->
|
||||||
foreach(fun(Suboption) ->
|
lists:foreach(fun(Suboption) ->
|
||||||
print({emqx_suboption, Suboption})
|
print({emqx_suboption, Suboption})
|
||||||
end, ets:tab2list(emqx_suboption));
|
end, ets:tab2list(emqx_suboption));
|
||||||
|
|
||||||
|
@ -279,7 +277,7 @@ if_valid_qos(QoS, Fun) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
plugins(["list"]) ->
|
plugins(["list"]) ->
|
||||||
foreach(fun print/1, emqx_plugins:list());
|
lists:foreach(fun print/1, emqx_plugins:list());
|
||||||
|
|
||||||
plugins(["load", Name]) ->
|
plugins(["load", Name]) ->
|
||||||
case emqx_plugins:load(list_to_atom(Name)) of
|
case emqx_plugins:load(list_to_atom(Name)) of
|
||||||
|
@ -421,7 +419,7 @@ log(_) ->
|
||||||
%% @doc Trace Command
|
%% @doc Trace Command
|
||||||
|
|
||||||
trace(["list"]) ->
|
trace(["list"]) ->
|
||||||
foreach(fun({{Who, Name}, {Level, LogFile}}) ->
|
lists:foreach(fun({{Who, Name}, {Level, LogFile}}) ->
|
||||||
emqx_ctl:print("Trace(~s=~s, level=~s, destination=~p)~n", [Who, Name, Level, LogFile])
|
emqx_ctl:print("Trace(~s=~s, level=~s, destination=~p)~n", [Who, Name, Level, LogFile])
|
||||||
end, emqx_tracer:lookup_traces());
|
end, emqx_tracer:lookup_traces());
|
||||||
|
|
||||||
|
@ -470,7 +468,7 @@ trace_off(Who, Name) ->
|
||||||
%% @doc Listeners Command
|
%% @doc Listeners Command
|
||||||
|
|
||||||
listeners([]) ->
|
listeners([]) ->
|
||||||
foreach(fun({{Protocol, ListenOn}, _Pid}) ->
|
lists:foreach(fun({{Protocol, ListenOn}, _Pid}) ->
|
||||||
Info = [{listen_on, {string, emqx_listeners:format_listen_on(ListenOn)}},
|
Info = [{listen_on, {string, emqx_listeners:format_listen_on(ListenOn)}},
|
||||||
{acceptors, esockd:get_acceptors({Protocol, ListenOn})},
|
{acceptors, esockd:get_acceptors({Protocol, ListenOn})},
|
||||||
{max_conns, esockd:get_max_connections({Protocol, ListenOn})},
|
{max_conns, esockd:get_max_connections({Protocol, ListenOn})},
|
||||||
|
@ -478,9 +476,9 @@ listeners([]) ->
|
||||||
{shutdown_count, esockd:get_shutdown_count({Protocol, ListenOn})}
|
{shutdown_count, esockd:get_shutdown_count({Protocol, ListenOn})}
|
||||||
],
|
],
|
||||||
emqx_ctl:print("~s~n", [listener_identifier(Protocol, ListenOn)]),
|
emqx_ctl:print("~s~n", [listener_identifier(Protocol, ListenOn)]),
|
||||||
foreach(fun indent_print/1, Info)
|
lists:foreach(fun indent_print/1, Info)
|
||||||
end, esockd:listeners()),
|
end, esockd:listeners()),
|
||||||
foreach(fun({Protocol, Opts}) ->
|
lists:foreach(fun({Protocol, Opts}) ->
|
||||||
Port = proplists:get_value(port, Opts),
|
Port = proplists:get_value(port, Opts),
|
||||||
Info = [{listen_on, {string, emqx_listeners:format_listen_on(Port)}},
|
Info = [{listen_on, {string, emqx_listeners:format_listen_on(Port)}},
|
||||||
{acceptors, maps:get(num_acceptors, proplists:get_value(transport_options, Opts, #{}), 0)},
|
{acceptors, maps:get(num_acceptors, proplists:get_value(transport_options, Opts, #{}), 0)},
|
||||||
|
@ -488,7 +486,7 @@ listeners([]) ->
|
||||||
{current_conn, proplists:get_value(all_connections, Opts)},
|
{current_conn, proplists:get_value(all_connections, Opts)},
|
||||||
{shutdown_count, []}],
|
{shutdown_count, []}],
|
||||||
emqx_ctl:print("~s~n", [listener_identifier(Protocol, Port)]),
|
emqx_ctl:print("~s~n", [listener_identifier(Protocol, Port)]),
|
||||||
foreach(fun indent_print/1, Info)
|
lists:foreach(fun indent_print/1, Info)
|
||||||
end, ranch:info());
|
end, ranch:info());
|
||||||
|
|
||||||
listeners(["stop", Name = "http" ++ _N | _MaybePort]) ->
|
listeners(["stop", Name = "http" ++ _N | _MaybePort]) ->
|
||||||
|
@ -555,9 +553,9 @@ data(["import", Filename]) ->
|
||||||
ok ->
|
ok ->
|
||||||
emqx_ctl:print("The emqx data has been imported successfully.~n");
|
emqx_ctl:print("The emqx data has been imported successfully.~n");
|
||||||
{error, import_failed} ->
|
{error, import_failed} ->
|
||||||
emqx_ctl:print("The emqx data import failed. ~n");
|
emqx_ctl:print("The emqx data import failed.~n");
|
||||||
{error, unsupported_version} ->
|
{error, unsupported_version} ->
|
||||||
emqx_ctl:print("The emqx data import failed: Unsupported version. ~n");
|
emqx_ctl:print("The emqx data import failed: Unsupported version.~n");
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
emqx_ctl:print("The emqx data import failed: ~0p while reading ~s.~n", [Reason, Filename])
|
emqx_ctl:print("The emqx data import failed: ~0p while reading ~s.~n", [Reason, Filename])
|
||||||
end;
|
end;
|
||||||
|
|
|
@ -447,96 +447,51 @@ to_version(Version) when is_binary(Version) ->
|
||||||
to_version(Version) when is_list(Version) ->
|
to_version(Version) when is_list(Version) ->
|
||||||
Version.
|
Version.
|
||||||
|
|
||||||
-ifdef(EMQX_ENTERPRISE).
|
|
||||||
export() ->
|
export() ->
|
||||||
Modules = export_modules(),
|
|
||||||
Rules = export_rules(),
|
|
||||||
Resources = export_resources(),
|
|
||||||
Blacklist = export_blacklist(),
|
|
||||||
Apps = export_applications(),
|
|
||||||
Users = export_users(),
|
|
||||||
AuthMnesia = export_auth_mnesia(),
|
|
||||||
AclMnesia = export_acl_mnesia(),
|
|
||||||
Schemas = export_schemas(),
|
|
||||||
{Configs, State} = export_confs(),
|
|
||||||
Seconds = erlang:system_time(second),
|
Seconds = erlang:system_time(second),
|
||||||
|
Data = do_export_data() ++ [{date, erlang:list_to_binary(emqx_mgmt_util:strftime(Seconds))}],
|
||||||
{{Y, M, D}, {H, MM, S}} = emqx_mgmt_util:datetime(Seconds),
|
{{Y, M, D}, {H, MM, S}} = emqx_mgmt_util:datetime(Seconds),
|
||||||
Filename = io_lib:format("emqx-export-~p-~p-~p-~p-~p-~p.json", [Y, M, D, H, MM, S]),
|
Filename = io_lib:format("emqx-export-~p-~p-~p-~p-~p-~p.json", [Y, M, D, H, MM, S]),
|
||||||
NFilename = filename:join([emqx:get_env(data_dir), Filename]),
|
NFilename = filename:join([emqx:get_env(data_dir), Filename]),
|
||||||
Version = string:sub_string(emqx_sys:version(), 1, 3),
|
ok = filelib:ensure_dir(NFilename),
|
||||||
Data = [{version, erlang:list_to_binary(Version)},
|
case file:write_file(NFilename, emqx_json:encode(Data)) of
|
||||||
{date, erlang:list_to_binary(emqx_mgmt_util:strftime(Seconds))},
|
ok ->
|
||||||
{modules, Modules},
|
case file:read_file_info(NFilename) of
|
||||||
{rules, Rules},
|
{ok, #file_info{size = Size, ctime = {{Y, M, D}, {H, MM, S}}}} ->
|
||||||
{resources, Resources},
|
CreatedAt = io_lib:format("~p-~p-~p ~p:~p:~p", [Y, M, D, H, MM, S]),
|
||||||
{blacklist, Blacklist},
|
{ok, #{filename => list_to_binary(NFilename),
|
||||||
{apps, Apps},
|
size => Size,
|
||||||
{users, Users},
|
created_at => list_to_binary(CreatedAt),
|
||||||
{auth_mnesia, AuthMnesia},
|
node => node()
|
||||||
{acl_mnesia, AclMnesia},
|
}};
|
||||||
{schemas, Schemas},
|
Error -> Error
|
||||||
{configs, Configs},
|
|
||||||
{listeners_state, State}],
|
|
||||||
write_file(NFilename, Data).
|
|
||||||
|
|
||||||
import(Filename) ->
|
|
||||||
case file:read_file(FullFilename) of
|
|
||||||
{ok, Json} ->
|
|
||||||
Data = emqx_json:decode(Json, [return_maps]),
|
|
||||||
Version = to_version(maps:get(<<"version">>, Data)),
|
|
||||||
case lists:member(Version, ?VERSIONS) of
|
|
||||||
true ->
|
|
||||||
try
|
|
||||||
import_confs(maps:get(<<"configs">>, Data, []), maps:get(<<"listeners_state">>, Data, [])),
|
|
||||||
import_resources(maps:get(<<"resources">>, Data, [])),
|
|
||||||
import_rules(maps:get(<<"rules">>, Data, [])),
|
|
||||||
import_blacklist(maps:get(<<"blacklist">>, Data, [])),
|
|
||||||
import_applications(maps:get(<<"apps">>, Data, [])),
|
|
||||||
import_users(maps:get(<<"users">>, Data, [])),
|
|
||||||
import_modules(maps:get(<<"modules">>, Data, [])),
|
|
||||||
import_auth_clientid(maps:get(<<"auth_clientid">>, Data, [])),
|
|
||||||
import_auth_username(maps:get(<<"auth_username">>, Data, [])),
|
|
||||||
import_auth_mnesia(maps:get(<<"auth_mnesia">>, Data, []), Version),
|
|
||||||
import_acl_mnesia(maps:get(<<"acl_mnesia">>, Data, []), Version),
|
|
||||||
import_schemas(maps:get(<<"schemas">>, Data, [])),
|
|
||||||
logger:debug("The emqx data has been imported successfully"),
|
|
||||||
ok
|
|
||||||
catch Class:Reason:Stack ->
|
|
||||||
logger:error("The emqx data import failed: ~0p", [{Class,Reason,Stack}]),
|
|
||||||
{error, import_failed}
|
|
||||||
end;
|
|
||||||
false ->
|
|
||||||
logger:error("Unsupported version: ~p", [Version]),
|
|
||||||
{error, unsupported_version}
|
|
||||||
end;
|
end;
|
||||||
{error, Reason} ->
|
Error -> Error
|
||||||
{error, Reason}
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-else.
|
do_export_data() ->
|
||||||
export() ->
|
|
||||||
Rules = export_rules(),
|
|
||||||
Resources = export_resources(),
|
|
||||||
Blacklist = export_blacklist(),
|
|
||||||
Apps = export_applications(),
|
|
||||||
Users = export_users(),
|
|
||||||
AuthMnesia = export_auth_mnesia(),
|
|
||||||
AclMnesia = export_acl_mnesia(),
|
|
||||||
Seconds = erlang:system_time(second),
|
|
||||||
{{Y, M, D}, {H, MM, S}} = emqx_mgmt_util:datetime(Seconds),
|
|
||||||
Filename = io_lib:format("emqx-export-~p-~p-~p-~p-~p-~p.json", [Y, M, D, H, MM, S]),
|
|
||||||
NFilename = filename:join([emqx:get_env(data_dir), Filename]),
|
|
||||||
Version = string:sub_string(emqx_sys:version(), 1, 3),
|
Version = string:sub_string(emqx_sys:version(), 1, 3),
|
||||||
Data = [{version, erlang:list_to_binary(Version)},
|
[{version, erlang:list_to_binary(Version)},
|
||||||
{date, erlang:list_to_binary(emqx_mgmt_util:strftime(Seconds))},
|
{rules, export_rules()},
|
||||||
{rules, Rules},
|
{resources, export_resources()},
|
||||||
{resources, Resources},
|
{blacklist, export_blacklist()},
|
||||||
{blacklist, Blacklist},
|
{apps, export_applications()},
|
||||||
{apps, Apps},
|
{users, export_users()},
|
||||||
{users, Users},
|
{auth_mnesia, export_auth_mnesia()},
|
||||||
{auth_mnesia, AuthMnesia},
|
{acl_mnesia, export_acl_mnesia()}
|
||||||
{acl_mnesia, AclMnesia}],
|
] ++ do_export_extra_data().
|
||||||
write_file(NFilename, Data).
|
|
||||||
|
-ifdef(EMQX_ENTERPRISE).
|
||||||
|
do_export_extra_data() ->
|
||||||
|
{Configs, State} = export_confs(),
|
||||||
|
[{modules, export_modules()},
|
||||||
|
{schemas, export_schemas()},
|
||||||
|
{configs, Configs},
|
||||||
|
{listeners_state, State}
|
||||||
|
].
|
||||||
|
-else.
|
||||||
|
do_export_extra_data() -> [].
|
||||||
|
-endif.
|
||||||
|
|
||||||
import(Filename) ->
|
import(Filename) ->
|
||||||
case file:read_file(Filename) of
|
case file:read_file(Filename) of
|
||||||
|
@ -546,14 +501,7 @@ import(Filename) ->
|
||||||
case lists:member(Version, ?VERSIONS) of
|
case lists:member(Version, ?VERSIONS) of
|
||||||
true ->
|
true ->
|
||||||
try
|
try
|
||||||
import_resources_and_rules(maps:get(<<"resources">>, Data, []), maps:get(<<"rules">>, Data, []), Version),
|
do_import_data(Data, Version),
|
||||||
import_blacklist(maps:get(<<"blacklist">>, Data, [])),
|
|
||||||
import_applications(maps:get(<<"apps">>, Data, [])),
|
|
||||||
import_users(maps:get(<<"users">>, Data, [])),
|
|
||||||
import_auth_clientid(maps:get(<<"auth_clientid">>, Data, [])),
|
|
||||||
import_auth_username(maps:get(<<"auth_username">>, Data, [])),
|
|
||||||
import_auth_mnesia(maps:get(<<"auth_mnesia">>, Data, []), Version),
|
|
||||||
import_acl_mnesia(maps:get(<<"acl_mnesia">>, Data, []), Version),
|
|
||||||
logger:debug("The emqx data has been imported successfully"),
|
logger:debug("The emqx data has been imported successfully"),
|
||||||
ok
|
ok
|
||||||
catch Class:Reason:Stack ->
|
catch Class:Reason:Stack ->
|
||||||
|
@ -566,21 +514,25 @@ import(Filename) ->
|
||||||
end;
|
end;
|
||||||
Error -> Error
|
Error -> Error
|
||||||
end.
|
end.
|
||||||
-endif.
|
|
||||||
|
|
||||||
write_file(Filename, Data) ->
|
do_import_data(Data, Version) ->
|
||||||
ok = filelib:ensure_dir(Filename),
|
import_blacklist(maps:get(<<"blacklist">>, Data, [])),
|
||||||
case file:write_file(Filename, emqx_json:encode(Data)) of
|
import_applications(maps:get(<<"apps">>, Data, [])),
|
||||||
ok ->
|
import_users(maps:get(<<"users">>, Data, [])),
|
||||||
case file:read_file_info(Filename) of
|
import_auth_clientid(maps:get(<<"auth_clientid">>, Data, [])),
|
||||||
{ok, #file_info{size = Size, ctime = {{Y, M, D}, {H, MM, S}}}} ->
|
import_auth_username(maps:get(<<"auth_username">>, Data, [])),
|
||||||
CreatedAt = io_lib:format("~p-~p-~p ~p:~p:~p", [Y, M, D, H, MM, S]),
|
import_auth_mnesia(maps:get(<<"auth_mnesia">>, Data, []), Version),
|
||||||
{ok, #{filename => list_to_binary(Filename),
|
import_acl_mnesia(maps:get(<<"acl_mnesia">>, Data, []), Version),
|
||||||
size => Size,
|
do_import_extra_data(Data, Version).
|
||||||
created_at => list_to_binary(CreatedAt),
|
|
||||||
node => node()
|
-ifdef(EMQX_ENTERPRISE).
|
||||||
}};
|
do_import_extra_data(Data, Version) ->
|
||||||
Error -> Error
|
import_confs(maps:get(<<"configs">>, Data, []), maps:get(<<"listeners_state">>, Data, [])),
|
||||||
end;
|
import_resources(maps:get(<<"resources">>, Data, [])),
|
||||||
Error -> Error
|
import_rules(maps:get(<<"rules">>, Data, [])),
|
||||||
end.
|
import_modules(maps:get(<<"modules">>, Data, [])),
|
||||||
|
import_schemas(maps:get(<<"schemas">>, Data, [])).
|
||||||
|
-else.
|
||||||
|
do_import_extra_data(Data, Version) ->
|
||||||
|
import_resources_and_rules(maps:get(<<"resources">>, Data, []), maps:get(<<"rules">>, Data, []), Version).
|
||||||
|
-endif.
|
||||||
|
|
Loading…
Reference in New Issue