fix: ensure plugin configured states after join cluster

This commit is contained in:
JimMoen 2024-05-24 12:04:03 +08:00
parent 874bcd9a8b
commit 43506495ae
No known key found for this signature in database
1 changed files with 37 additions and 15 deletions

View File

@ -1123,9 +1123,31 @@ for_plugins(ActionFun) ->
ok ok
end. end.
maybe_post_op_after_installed(NameVsn) -> maybe_post_op_after_installed(NameVsn0) ->
NameVsn = wrap_to_list(NameVsn0),
_ = maybe_load_config_schema(NameVsn), _ = maybe_load_config_schema(NameVsn),
_ = ensure_state(NameVsn, no_move, false, global), ok = maybe_ensure_state(NameVsn).
maybe_ensure_state(NameVsn) ->
EnsureStateFun = fun(#{name_vsn := NV, enable := Bool}, AccIn) ->
case NV of
NameVsn ->
%% Configured, using existed cluster config
ensure_state(NV, no_move, Bool, global),
AccIn#{ensured => true};
_ ->
AccIn
end
end,
case lists:foldl(EnsureStateFun, #{ensured => false}, configured()) of
#{ensured := true} ->
ok;
#{ensured := false} ->
?SLOG(info, #{msg => "plugin_not_configured", name_vsn => NameVsn}),
%% Clean installation, no config, ensure with `Enable = false`
_ = ensure_state(NameVsn, no_move, false, global),
ok
end,
ok. ok.
maybe_load_config_schema(NameVsn) -> maybe_load_config_schema(NameVsn) ->
@ -1137,7 +1159,7 @@ maybe_load_config_schema(NameVsn) ->
_ = maybe_create_config_dir(NameVsn). _ = maybe_create_config_dir(NameVsn).
do_load_config_schema(NameVsn, AvscPath) -> do_load_config_schema(NameVsn, AvscPath) ->
case emqx_plugins_serde:add_schema(NameVsn, AvscPath) of case emqx_plugins_serde:add_schema(bin(NameVsn), AvscPath) of
ok -> ok; ok -> ok;
{error, already_exists} -> ok; {error, already_exists} -> ok;
{error, _Reason} -> ok {error, _Reason} -> ok
@ -1345,23 +1367,23 @@ read_file_fun(Path, ErrMsg, #{read_mode := ?JSON_MAP}) ->
%% Directorys %% Directorys
-spec plugin_dir(name_vsn()) -> string(). -spec plugin_dir(name_vsn()) -> string().
plugin_dir(NameVsn) -> plugin_dir(NameVsn) ->
wrap_list_path(filename:join([install_dir(), NameVsn])). wrap_to_list(filename:join([install_dir(), NameVsn])).
-spec plugin_priv_dir(name_vsn()) -> string(). -spec plugin_priv_dir(name_vsn()) -> string().
plugin_priv_dir(NameVsn) -> plugin_priv_dir(NameVsn) ->
case read_plugin_info(NameVsn, #{fill_readme => false}) of case read_plugin_info(NameVsn, #{fill_readme => false}) of
{ok, #{<<"name">> := Name, <<"metadata_vsn">> := Vsn}} -> {ok, #{<<"name">> := Name, <<"metadata_vsn">> := Vsn}} ->
AppDir = make_name_vsn_string(Name, Vsn), AppDir = make_name_vsn_string(Name, Vsn),
wrap_list_path(filename:join([plugin_dir(NameVsn), AppDir, "priv"])); wrap_to_list(filename:join([plugin_dir(NameVsn), AppDir, "priv"]));
_ -> _ ->
wrap_list_path(filename:join([install_dir(), NameVsn, "priv"])) wrap_to_list(filename:join([install_dir(), NameVsn, "priv"]))
end. end.
-spec plugin_config_dir(name_vsn()) -> string() | {error, Reason :: string()}. -spec plugin_config_dir(name_vsn()) -> string() | {error, Reason :: string()}.
plugin_config_dir(NameVsn) -> plugin_config_dir(NameVsn) ->
case parse_name_vsn(NameVsn) of case parse_name_vsn(NameVsn) of
{ok, NameAtom, _Vsn} -> {ok, NameAtom, _Vsn} ->
wrap_list_path(filename:join([emqx:data_dir(), "plugins", atom_to_list(NameAtom)])); wrap_to_list(filename:join([emqx:data_dir(), "plugins", atom_to_list(NameAtom)]));
{error, Reason} -> {error, Reason} ->
?SLOG(warning, #{ ?SLOG(warning, #{
msg => "failed_to_generate_plugin_config_dir_for_plugin", msg => "failed_to_generate_plugin_config_dir_for_plugin",
@ -1374,32 +1396,32 @@ plugin_config_dir(NameVsn) ->
%% Files %% Files
-spec pkg_file_path(name_vsn()) -> string(). -spec pkg_file_path(name_vsn()) -> string().
pkg_file_path(NameVsn) -> pkg_file_path(NameVsn) ->
wrap_list_path(filename:join([install_dir(), bin([NameVsn, ".tar.gz"])])). wrap_to_list(filename:join([install_dir(), bin([NameVsn, ".tar.gz"])])).
-spec info_file_path(name_vsn()) -> string(). -spec info_file_path(name_vsn()) -> string().
info_file_path(NameVsn) -> info_file_path(NameVsn) ->
wrap_list_path(filename:join([plugin_dir(NameVsn), "release.json"])). wrap_to_list(filename:join([plugin_dir(NameVsn), "release.json"])).
-spec avsc_file_path(name_vsn()) -> string(). -spec avsc_file_path(name_vsn()) -> string().
avsc_file_path(NameVsn) -> avsc_file_path(NameVsn) ->
wrap_list_path(filename:join([plugin_priv_dir(NameVsn), "config_schema.avsc"])). wrap_to_list(filename:join([plugin_priv_dir(NameVsn), "config_schema.avsc"])).
-spec plugin_config_file(name_vsn()) -> string(). -spec plugin_config_file(name_vsn()) -> string().
plugin_config_file(NameVsn) -> plugin_config_file(NameVsn) ->
wrap_list_path(filename:join([plugin_config_dir(NameVsn), "config.hocon"])). wrap_to_list(filename:join([plugin_config_dir(NameVsn), "config.hocon"])).
%% should only used when plugin installing %% should only used when plugin installing
-spec default_plugin_config_file(name_vsn()) -> string(). -spec default_plugin_config_file(name_vsn()) -> string().
default_plugin_config_file(NameVsn) -> default_plugin_config_file(NameVsn) ->
wrap_list_path(filename:join([plugin_priv_dir(NameVsn), "config.hocon"])). wrap_to_list(filename:join([plugin_priv_dir(NameVsn), "config.hocon"])).
-spec i18n_file_path(name_vsn()) -> string(). -spec i18n_file_path(name_vsn()) -> string().
i18n_file_path(NameVsn) -> i18n_file_path(NameVsn) ->
wrap_list_path(filename:join([plugin_priv_dir(NameVsn), "config_i18n.json"])). wrap_to_list(filename:join([plugin_priv_dir(NameVsn), "config_i18n.json"])).
-spec readme_file(name_vsn()) -> string(). -spec readme_file(name_vsn()) -> string().
readme_file(NameVsn) -> readme_file(NameVsn) ->
wrap_list_path(filename:join([plugin_dir(NameVsn), "README.md"])). wrap_to_list(filename:join([plugin_dir(NameVsn), "README.md"])).
running_apps() -> running_apps() ->
lists:map( lists:map(
@ -1431,5 +1453,5 @@ bin(A) when is_atom(A) -> atom_to_binary(A, utf8);
bin(L) when is_list(L) -> unicode:characters_to_binary(L, utf8); bin(L) when is_list(L) -> unicode:characters_to_binary(L, utf8);
bin(B) when is_binary(B) -> B. bin(B) when is_binary(B) -> B.
wrap_list_path(Path) -> wrap_to_list(Path) ->
binary_to_list(iolist_to_binary(Path)). binary_to_list(iolist_to_binary(Path)).