Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get the plugins list through -emqx_plugin module attributes #2339

Merged
merged 1 commit into from Mar 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 17 additions & 46 deletions src/emqx_plugins.erl
Expand Up @@ -60,14 +60,15 @@ load() ->
load_expand_plugins() ->
case emqx_config:get_env(expand_plugins_dir) of
undefined -> ok;
Dir ->
PluginsDir = filelib:wildcard("*", Dir),
lists:foreach(fun(PluginDir) ->
case filelib:is_dir(Dir ++ PluginDir) of
true -> load_expand_plugin(Dir ++ PluginDir);
ExpandPluginsDir ->
Plugins = filelib:wildcard("*", ExpandPluginsDir),
lists:foreach(fun(Plugin) ->
PluginDir = filename:join(ExpandPluginsDir, Plugin),
case filelib:is_dir(PluginDir) of
true -> load_expand_plugin(PluginDir);
false -> ok
end
end, PluginsDir)
end, Plugins)
end.

load_expand_plugin(PluginDir) ->
Expand Down Expand Up @@ -98,25 +99,6 @@ init_expand_plugin_config(PluginDir) ->
[application:set_env(AppName, Par, Val) || {Par, Val} <- Envs]
end, AppsEnv).

get_expand_plugin_config() ->
case emqx_config:get_env(expand_plugins_dir) of
undefined -> ok;
Dir ->
PluginsDir = filelib:wildcard("*", Dir),
lists:foldl(fun(PluginDir, Acc) ->
case filelib:is_dir(Dir ++ PluginDir) of
true ->
Etc = Dir ++ PluginDir ++ "/etc",
case filelib:wildcard("*.{conf,config}", Etc) of
[] -> Acc;
[Conf] -> [Conf | Acc]
end;
false ->
Acc
end
end, [], PluginsDir)
end.

ensure_file(File) ->
case filelib:is_file(File) of false -> write_loaded([]); true -> ok end.

Expand Down Expand Up @@ -155,23 +137,16 @@ stop_plugins(Names) ->
%% @doc List all available plugins
-spec(list() -> [emqx_types:plugin()]).
list() ->
case emqx_config:get_env(plugins_etc_dir) of
undefined ->
[];
PluginsEtc ->
CfgFiles = filelib:wildcard("*.{conf,config}", PluginsEtc) ++ get_expand_plugin_config(),
Plugins = [plugin(CfgFile) || CfgFile <- CfgFiles],
StartedApps = names(started_app),
lists:map(fun(Plugin = #plugin{name = Name}) ->
case lists:member(Name, StartedApps) of
true -> Plugin#plugin{active = true};
false -> Plugin
end
end, Plugins)
end.

plugin(CfgFile) ->
AppName = app_name(CfgFile),
StartedApps = names(started_app),
lists:map(fun({Name, _, _}) ->
Plugin = plugin(Name),
case lists:member(Name, StartedApps) of
true -> Plugin#plugin{active = true};
false -> Plugin
end
end, ekka_boot:all_module_attributes(emqx_plugin)).

plugin(AppName) ->
case application:get_all_key(AppName) of
{ok, Attrs} ->
Ver = proplists:get_value(vsn, Attrs, "0"),
Expand Down Expand Up @@ -268,10 +243,6 @@ stop_app(App) ->
%%--------------------------------------------------------------------
%% Internal functions
%%--------------------------------------------------------------------

app_name(File) ->
[AppName | _] = string:tokens(File, "."), list_to_atom(AppName).

names(plugin) ->
names(list());

Expand Down