fix: use hocon format as plugin config

This commit is contained in:
JimMoen 2024-05-22 06:37:58 +08:00
parent 14f2a68799
commit 33aa61daea
No known key found for this signature in database
2 changed files with 30 additions and 20 deletions

View File

@ -342,8 +342,8 @@ get_config_bin(NameVsn) ->
put_config(NameVsn, AvroJsonMap, DecodedPluginConfig) when not is_binary(NameVsn) -> put_config(NameVsn, AvroJsonMap, DecodedPluginConfig) when not is_binary(NameVsn) ->
put_config(bin(NameVsn), AvroJsonMap, DecodedPluginConfig); put_config(bin(NameVsn), AvroJsonMap, DecodedPluginConfig);
put_config(NameVsn, AvroJsonMap, _DecodedPluginConfig) -> put_config(NameVsn, AvroJsonMap, _DecodedPluginConfig) ->
AvroJsonBin = emqx_utils_json:encode(AvroJsonMap), HoconBin = hocon_pp:do(AvroJsonMap, #{}),
ok = backup_and_write_avro_bin(NameVsn, AvroJsonBin), ok = backup_and_write_avro_bin(NameVsn, HoconBin),
%% TODO: callback in plugin's on_config_changed (config update by mgmt API) %% TODO: callback in plugin's on_config_changed (config update by mgmt API)
%% TODO: callback in plugin's on_config_upgraded (config vsn upgrade v1 -> v2) %% TODO: callback in plugin's on_config_upgraded (config vsn upgrade v1 -> v2)
%% {ok, AppName, AppVsn} = parse_name_vsn(AppNameVsn), %% {ok, AppName, AppVsn} = parse_name_vsn(AppNameVsn),
@ -717,7 +717,7 @@ read_plugin_avro(NameVsn) ->
read_plugin_avro(NameVsn, Options) -> read_plugin_avro(NameVsn, Options) ->
tryit( tryit(
atom_to_list(?FUNCTION_NAME), atom_to_list(?FUNCTION_NAME),
read_file_fun(avro_config_file(NameVsn), "bad_avro_file", Options) read_file_fun(plugin_config_file(NameVsn), "bad_avro_file", Options)
). ).
ensure_exists_and_installed(NameVsn) -> ensure_exists_and_installed(NameVsn) ->
@ -1170,21 +1170,21 @@ do_ensure_plugin_config(NameVsn) ->
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) ->
AvroJsonBin = emqx_utils_json:encode(AvroJsonMap), AvroJsonBin = emqx_utils_json:encode(AvroJsonMap),
ok = file:write_file(avro_config_file(NameVsn), AvroJsonBin), ok = file:write_file(plugin_config_file(NameVsn), AvroJsonBin),
ensure_avro_config(NameVsn); ensure_avro_config(NameVsn);
_ -> _ ->
?SLOG(warning, #{msg => "config_not_found_from_cluster"}), ?SLOG(info, #{msg => "config_not_found_from_cluster"}),
%% otherwise cp default avro file %% otherwise cp default avro file
%% i.e. Clean installation %% i.e. Clean installation
cp_default_avro_file(NameVsn), cp_default_config_file(NameVsn),
ensure_avro_config(NameVsn) ensure_avro_config(NameVsn)
end. end.
cp_default_avro_file(NameVsn) -> cp_default_config_file(NameVsn) ->
%% always copy default avro file into config dir %% always copy default avro file into config dir
%% when can not get config from other nodes %% when can not get config from other nodes
Source = default_avro_config_file(NameVsn), Source = default_plugin_config_file(NameVsn),
Destination = avro_config_file(NameVsn), Destination = plugin_config_file(NameVsn),
maybe maybe
true ?= filelib:is_regular(Source), true ?= filelib:is_regular(Source),
%% destination path not existed (not configured) %% destination path not existed (not configured)
@ -1224,7 +1224,7 @@ do_ensure_avro_config(NameVsn) ->
backup_and_write_avro_bin(NameVsn, AvroBin) -> backup_and_write_avro_bin(NameVsn, AvroBin) ->
%% this may fail, but we don't care %% this may fail, but we don't care
%% e.g. read-only file system %% e.g. read-only file system
Path = avro_config_file(NameVsn), Path = plugin_config_file(NameVsn),
_ = filelib:ensure_dir(Path), _ = filelib:ensure_dir(Path),
TmpFile = Path ++ ".tmp", TmpFile = Path ++ ".tmp",
case file:write_file(TmpFile, AvroBin) of case file:write_file(TmpFile, AvroBin) of
@ -1309,6 +1309,16 @@ read_file_fun(Path, ErrMsg, #{read_mode := ?JSON_MAP}) ->
plugin_dir(NameVsn) -> plugin_dir(NameVsn) ->
wrap_list_path(filename:join([install_dir(), NameVsn])). wrap_list_path(filename:join([install_dir(), NameVsn])).
-spec plugin_priv_dir(name_vsn()) -> string().
plugin_priv_dir(NameVsn) ->
case read_plugin_info(NameVsn, #{fill_readme => false}) of
{ok, #{<<"name">> := Name, <<"metadata_vsn">> := Vsn}} ->
AppDir = <<Name/binary, "-", Vsn/binary>>,
wrap_list_path(filename:join([plugin_dir(NameVsn), AppDir, "priv"]));
_ ->
wrap_list_path(filename:join([install_dir(), NameVsn, "priv"]))
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
@ -1334,20 +1344,20 @@ info_file_path(NameVsn) ->
-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_dir(NameVsn), "config", "config_schema.avsc"])). wrap_list_path(filename:join([plugin_priv_dir(NameVsn), "config_schema.avsc"])).
-spec avro_config_file(name_vsn()) -> string(). -spec plugin_config_file(name_vsn()) -> string().
avro_config_file(NameVsn) -> plugin_config_file(NameVsn) ->
wrap_list_path(filename:join([plugin_config_dir(NameVsn), "config.avro"])). wrap_list_path(filename:join([plugin_config_dir(NameVsn), "config.hocon"])).
%% should only used when plugin installing %% should only used when plugin installing
-spec default_avro_config_file(name_vsn()) -> string(). -spec default_plugin_config_file(name_vsn()) -> string().
default_avro_config_file(NameVsn) -> default_plugin_config_file(NameVsn) ->
wrap_list_path(filename:join([plugin_dir(NameVsn), "config", "config.avro"])). wrap_list_path(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_dir(NameVsn), "config", "config_i18n.json"])). wrap_list_path(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) ->

View File

@ -151,10 +151,10 @@ terminate(_Reason, _State) ->
-spec get_plugin_avscs() -> [{string(), string()}]. -spec get_plugin_avscs() -> [{string(), string()}].
get_plugin_avscs() -> get_plugin_avscs() ->
Pattern = filename:join([emqx_plugins:install_dir(), "*", "config", "config_schema.avsc"]), Pattern = filename:join([emqx_plugins:install_dir(), "*", "*", "priv", "config_schema.avsc"]),
lists:foldl( lists:foldl(
fun(AvscPath, AccIn) -> fun(AvscPath, AccIn) ->
[_, _, NameVsn | _] = lists:reverse(filename:split(AvscPath)), [_, _, _, NameVsn | _] = lists:reverse(filename:split(AvscPath)),
[{to_bin(NameVsn), AvscPath} | AccIn] [{to_bin(NameVsn), AvscPath} | AccIn]
end, end,
_Acc0 = [], _Acc0 = [],