feat(config): load and merge emqx_override.conf at bootup

This commit is contained in:
Shawn 2021-09-02 15:02:38 +08:00
parent d7b198b292
commit 304874f0ff
2 changed files with 13 additions and 15 deletions

View File

@ -235,7 +235,7 @@ put_raw(KeyPath, Config) -> do_put(?RAW_CONF, KeyPath, Config).
%% in the rear of the list overrides prior values. %% in the rear of the list overrides prior values.
-spec init_load(module(), [string()] | binary() | hocon:config()) -> ok. -spec init_load(module(), [string()] | binary() | hocon:config()) -> ok.
init_load(SchemaMod, Conf) when is_list(Conf) orelse is_binary(Conf) -> init_load(SchemaMod, Conf) when is_list(Conf) orelse is_binary(Conf) ->
ParseOptions = #{format => richmap}, ParseOptions = #{format => map},
Parser = case is_binary(Conf) of Parser = case is_binary(Conf) of
true -> fun hocon:binary/2; true -> fun hocon:binary/2;
false -> fun hocon:files/2 false -> fun hocon:files/2
@ -249,19 +249,14 @@ init_load(SchemaMod, Conf) when is_list(Conf) orelse is_binary(Conf) ->
}), }),
error(failed_to_load_hocon_conf) error(failed_to_load_hocon_conf)
end; end;
init_load(SchemaMod, RawRichConf) when is_map(RawRichConf) -> init_load(SchemaMod, RawConf0) when is_map(RawConf0) ->
%% check with richmap for line numbers in error reports (future enhancement)
Opts = #{return_plain => true,
nullable => true
},
%% this call throws exception in case of check failure
{_AppEnvs, CheckedConf} = hocon_schema:map_translate(SchemaMod, RawRichConf, Opts),
ok = save_schema_mod_and_names(SchemaMod), ok = save_schema_mod_and_names(SchemaMod),
ok = save_to_config_map(emqx_map_lib:unsafe_atom_key_map(normalize_conf(CheckedConf)), %% override part of the input conf using emqx_override.conf
normalize_conf(hocon_schema:richmap_to_map(RawRichConf))). RawConf = maps:merge(RawConf0, maps:with(maps:keys(RawConf0), read_override_conf())),
%% check and save configs
normalize_conf(Conf) -> {_AppEnvs, CheckedConf} = check_config(SchemaMod, RawConf),
maps:with(get_root_names(), Conf). ok = save_to_config_map(maps:with(get_atom_root_names(), CheckedConf),
maps:with(get_root_names(), RawConf)).
-spec check_config(module(), raw_config()) -> {AppEnvs, CheckedConf} -spec check_config(module(), raw_config()) -> {AppEnvs, CheckedConf}
when AppEnvs :: app_envs(), CheckedConf :: config(). when AppEnvs :: app_envs(), CheckedConf :: config().
@ -320,6 +315,9 @@ get_schema_mod(RootName) ->
get_root_names() -> get_root_names() ->
maps:get(names, persistent_term:get(?PERSIS_SCHEMA_MODS, #{names => []})). maps:get(names, persistent_term:get(?PERSIS_SCHEMA_MODS, #{names => []})).
get_atom_root_names() ->
[atom(N) || N <- get_root_names()].
-spec save_configs(app_envs(), config(), raw_config(), raw_config()) -> ok | {error, term()}. -spec save_configs(app_envs(), config(), raw_config(), raw_config()) -> ok | {error, term()}.
save_configs(_AppEnvs, Conf, RawConf, OverrideConf) -> save_configs(_AppEnvs, Conf, RawConf, OverrideConf) ->
%% We may need also support hot config update for the apps that use application envs. %% We may need also support hot config update for the apps that use application envs.

View File

@ -50,7 +50,7 @@
%% @doc List configured listeners. %% @doc List configured listeners.
-spec(list() -> [{ListenerId :: atom(), ListenerConf :: map()}]). -spec(list() -> [{ListenerId :: atom(), ListenerConf :: map()}]).
list() -> list() ->
[{listener_id(ZoneName, LName), LConf} || {ZoneName, LName, LConf} <- do_list()]. [{listener_id(Type, LName), LConf} || {Type, LName, LConf} <- do_list()].
do_list() -> do_list() ->
Listeners = maps:to_list(emqx:get_config([listeners], #{})), Listeners = maps:to_list(emqx:get_config([listeners], #{})),
@ -64,7 +64,7 @@ list(Type, Conf) ->
-spec is_running(ListenerId :: atom()) -> boolean() | {error, no_found}. -spec is_running(ListenerId :: atom()) -> boolean() | {error, no_found}.
is_running(ListenerId) -> is_running(ListenerId) ->
case lists:filtermap(fun({_Zone, Id, #{running := IsRunning}}) -> case lists:filtermap(fun({_Type, Id, #{running := IsRunning}}) ->
Id =:= ListenerId andalso {true, IsRunning} Id =:= ListenerId andalso {true, IsRunning}
end, do_list()) of end, do_list()) of
[IsRunning] -> IsRunning; [IsRunning] -> IsRunning;