fix: ensure plugin's config on boot and join cluster

This commit is contained in:
JimMoen 2024-05-20 16:04:29 +08:00
parent 677352e498
commit 1abc8cf502
No known key found for this signature in database
1 changed files with 29 additions and 9 deletions

View File

@ -16,6 +16,8 @@
-module(emqx_plugins). -module(emqx_plugins).
-feature(maybe_expr, enable).
-include_lib("emqx/include/logger.hrl"). -include_lib("emqx/include/logger.hrl").
-include("emqx_plugins.hrl"). -include("emqx_plugins.hrl").
@ -160,11 +162,16 @@ ensure_installed() ->
ensure_installed(NameVsn) -> ensure_installed(NameVsn) ->
case read_plugin_info(NameVsn, #{}) of case read_plugin_info(NameVsn, #{}) of
{ok, _} -> {ok, _} ->
ok; ok,
_ = maybe_ensure_plugin_config(NameVsn);
{error, _} -> {error, _} ->
ok = purge(NameVsn), ok = purge(NameVsn),
do_ensure_installed(NameVsn), case do_ensure_installed(NameVsn) of
ok = maybe_post_op_after_install(NameVsn) ok ->
maybe_post_op_after_installed(NameVsn);
_ ->
ok
end
end. end.
%% @doc Ensure files and directories for the given plugin are being deleted. %% @doc Ensure files and directories for the given plugin are being deleted.
@ -1080,9 +1087,9 @@ for_plugins(ActionFun) ->
ok ok
end. end.
maybe_post_op_after_install(NameVsn) -> maybe_post_op_after_installed(NameVsn) ->
_ = ensure_state(NameVsn, _Position = no_move, _Enabled = false, _ConfLocation = global),
_ = maybe_load_config_schema(NameVsn), _ = maybe_load_config_schema(NameVsn),
_ = ensure_state(NameVsn, no_move, false, global),
ok. ok.
maybe_load_config_schema(NameVsn) -> maybe_load_config_schema(NameVsn) ->
@ -1125,6 +1132,14 @@ do_create_config_dir(NameVsn) ->
end. end.
maybe_ensure_plugin_config(NameVsn) -> maybe_ensure_plugin_config(NameVsn) ->
maybe
true ?= filelib:is_regular(avro_config_file(NameVsn)),
ensure_avro_config(NameVsn)
else
_ -> do_ensure_plugin_config(NameVsn)
end.
do_ensure_plugin_config(NameVsn) ->
Nodes = [N || N <- mria:running_nodes(), N /= node()], Nodes = [N || N <- mria:running_nodes(), N /= node()],
case get_avro_config_from_any_node(Nodes, NameVsn, []) of case get_avro_config_from_any_node(Nodes, NameVsn, []) of
{ok, AvroJsonMap} when is_map(AvroJsonMap) -> {ok, AvroJsonMap} when is_map(AvroJsonMap) ->
@ -1142,11 +1157,13 @@ cp_default_avro_file(NameVsn) ->
%% when can not get config from other nodes %% when can not get config from other nodes
Source = default_avro_config_file(NameVsn), Source = default_avro_config_file(NameVsn),
Destination = avro_config_file(NameVsn), Destination = avro_config_file(NameVsn),
filelib:is_regular(Source) andalso maybe
true ?= filelib:is_regular(Source),
%% destination path not existed (not configured)
true ?= (not filelib:is_regular(Destination)),
case file:copy(Source, Destination) of case file:copy(Source, Destination) of
{ok, _} -> {ok, _} ->
ok, ok;
ensure_avro_config(NameVsn);
{error, Reason} -> {error, Reason} ->
?SLOG(warning, #{ ?SLOG(warning, #{
msg => "failed_to_copy_plugin_default_avro_config", msg => "failed_to_copy_plugin_default_avro_config",
@ -1154,7 +1171,10 @@ cp_default_avro_file(NameVsn) ->
destination => Destination, destination => Destination,
reason => Reason reason => Reason
}) })
end. end
else
_ -> ensure_avro_config(NameVsn)
end.
ensure_avro_config(NameVsn) -> ensure_avro_config(NameVsn) ->
with_plugin_avsc(NameVsn) andalso with_plugin_avsc(NameVsn) andalso