-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemqttd_plugins.erl
271 lines (238 loc) · 8.36 KB
/
emqttd_plugins.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
%%--------------------------------------------------------------------
%% Copyright (c) 2012-2016 Feng Lee <[email protected]>.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(emqttd_plugins).
-include("emqttd.hrl").
-export([load/0, unload/0]).
-export([load/1, unload/1]).
-export([list/0]).
%% @doc Load all plugins when the broker started.
-spec(load() -> list() | {error, any()}).
load() ->
case env(loaded_file) of
{ok, File} ->
with_loaded_file(File, fun(Names) -> load_plugins(Names, false) end);
undefined ->
%% No plugins available
ignore
end.
with_loaded_file(File, SuccFun) ->
case read_loaded(File) of
{ok, Names} ->
SuccFun(Names);
{error, Error} ->
lager:error("Failed to read: ~p, error: ~p", [File, Error]),
{error, Error}
end.
load_plugins(Names, Persistent) ->
Plugins = list(), NotFound = Names -- names(Plugins),
case NotFound of
[] -> ok;
NotFound -> lager:error("Cannot find plugins: ~p", [NotFound])
end,
NeedToLoad = Names -- NotFound -- names(started_app),
[load_plugin(find_plugin(Name, Plugins), Persistent) || Name <- NeedToLoad].
%% @doc Unload all plugins before broker stopped.
-spec(unload() -> list() | {error, any()}).
unload() ->
case env(loaded_file) of
{ok, File} ->
with_loaded_file(File, fun stop_plugins/1);
undefined ->
ignore
end.
%% stop plugins
stop_plugins(Names) ->
[stop_app(App) || App <- Names].
%% @doc List all available plugins
-spec(list() -> [mqtt_plugin()]).
list() ->
case env(plugins_dir) of
{ok, PluginsDir} ->
AppFiles = filelib:wildcard("*/ebin/*.app", PluginsDir),
Plugins = [plugin(PluginsDir, AppFile) || AppFile <- AppFiles],
StartedApps = names(started_app),
lists:map(fun(Plugin = #mqtt_plugin{name = Name}) ->
case lists:member(Name, StartedApps) of
true -> Plugin#mqtt_plugin{active = true};
false -> Plugin
end
end, Plugins);
undefined ->
[]
end.
plugin(PluginsDir, AppFile0) ->
AppFile = filename:join(PluginsDir, AppFile0),
{ok, [{application, Name, Attrs}]} = file:consult(AppFile),
CfgFile = filename:join([PluginsDir, Name, "etc/plugin.config"]),
AppsEnv1 =
case filelib:is_file(CfgFile) of
true ->
{ok, [AppsEnv]} = file:consult(CfgFile),
AppsEnv;
false ->
[]
end,
Ver = proplists:get_value(vsn, Attrs, "0"),
Descr = proplists:get_value(description, Attrs, ""),
#mqtt_plugin{name = Name, version = Ver, config = AppsEnv1, descr = Descr}.
%% @doc Load a Plugin
-spec(load(atom()) -> ok | {error, any()}).
load(PluginName) when is_atom(PluginName) ->
case lists:member(PluginName, names(started_app)) of
true ->
lager:error("Plugin ~p is already started", [PluginName]),
{error, already_started};
false ->
case find_plugin(PluginName) of
false ->
lager:error("Plugin ~s not found", [PluginName]),
{error, not_found};
Plugin ->
load_plugin(Plugin, true)
end
end.
load_plugin(#mqtt_plugin{name = Name, config = Config}, Persistent) ->
case load_app(Name, Config) of
ok ->
start_app(Name, fun(App) -> plugin_loaded(App, Persistent) end);
{error, Error} ->
{error, Error}
end.
load_app(App, Config) ->
case application:load(App) of
ok ->
set_config(Config);
{error, {already_loaded, App}} ->
set_config(Config);
{error, Error} ->
{error, Error}
end.
%% This trick is awesome:)
set_config([]) ->
ok;
set_config([{AppName, Envs} | Config]) ->
[application:set_env(AppName, Par, Val) || {Par, Val} <- Envs],
set_config(Config).
start_app(App, SuccFun) ->
case application:ensure_all_started(App) of
{ok, Started} ->
lager:info("started Apps: ~p", [Started]),
lager:info("load plugin ~p successfully", [App]),
SuccFun(App),
{ok, Started};
{error, {ErrApp, Reason}} ->
lager:error("load plugin ~p error, cannot start app ~s for ~p", [App, ErrApp, Reason]),
{error, {ErrApp, Reason}}
end.
find_plugin(Name) ->
find_plugin(Name, list()).
find_plugin(Name, Plugins) ->
lists:keyfind(Name, 2, Plugins).
%% @doc UnLoad a Plugin
-spec(unload(atom()) -> ok | {error, any()}).
unload(PluginName) when is_atom(PluginName) ->
case {lists:member(PluginName, names(started_app)), lists:member(PluginName, names(plugin))} of
{true, true} ->
unload_plugin(PluginName, true);
{false, _} ->
lager:error("Plugin ~p is not started", [PluginName]),
{error, not_started};
{true, false} ->
lager:error("~s is not a plugin, cannot unload it", [PluginName]),
{error, not_found}
end.
unload_plugin(App, Persistent) ->
case stop_app(App) of
ok ->
plugin_unloaded(App, Persistent), ok;
{error, Reason} ->
{error, Reason}
end.
stop_app(App) ->
case application:stop(App) of
ok ->
lager:info("stop plugin ~p successfully~n", [App]), ok;
{error, {not_started, App}} ->
lager:error("plugin ~p is not started~n", [App]), ok;
{error, Reason} ->
lager:error("stop plugin ~p error: ~p", [App]), {error, Reason}
end.
%%--------------------------------------------------------------------
%% Internal functions
%%--------------------------------------------------------------------
names(plugin) ->
names(list());
names(started_app) ->
[Name || {Name, _Descr, _Ver} <- application:which_applications()];
names(Plugins) ->
[Name || #mqtt_plugin{name = Name} <- Plugins].
plugin_loaded(_Name, false) ->
ok;
plugin_loaded(Name, true) ->
case read_loaded() of
{ok, Names} ->
case lists:member(Name, Names) of
false ->
%% write file if plugin is loaded
write_loaded(lists:append(Names, [Name]));
true ->
ignore
end;
{error, Error} ->
lager:error("Cannot read loaded plugins: ~p", [Error])
end.
plugin_unloaded(_Name, false) ->
ok;
plugin_unloaded(Name, true) ->
case read_loaded() of
{ok, Names} ->
case lists:member(Name, Names) of
true ->
write_loaded(lists:delete(Name, Names));
false ->
lager:error("Cannot find ~s in loaded_file", [Name])
end;
{error, Error} ->
lager:error("Cannot read loaded_plugins: ~p", [Error])
end.
read_loaded() ->
{ok, File} = env(loaded_file),
read_loaded(File).
read_loaded(File) ->
file:consult(File).
write_loaded(AppNames) ->
{ok, File} = env(loaded_file),
case file:open(File, [binary, write]) of
{ok, Fd} ->
lists:foreach(fun(Name) ->
file:write(Fd, iolist_to_binary(io_lib:format("~s.~n", [Name])))
end, AppNames);
{error, Error} ->
lager:error("Open File ~p Error: ~p", [File, Error]),
{error, Error}
end.
env(Name) ->
case application:get_env(emqttd, plugins) of
{ok, PluginsEnv} ->
case proplists:get_value(Name, PluginsEnv) of
undefined ->
undefined;
Val ->
{ok, Val}
end;
undefined ->
undefined
end.