|
|
@@ -21,6 +21,7 @@
|
|
|
-export([ init_load/1
|
|
|
, init_load/2
|
|
|
, read_override_conf/1
|
|
|
+ , delete_override_conf_files/0
|
|
|
, check_config/2
|
|
|
, fill_defaults/1
|
|
|
, fill_defaults/2
|
|
|
@@ -252,23 +253,7 @@ init_load(SchemaMod) ->
|
|
|
%% in the rear of the list overrides prior values.
|
|
|
-spec init_load(module(), [string()] | binary() | hocon:config()) -> ok.
|
|
|
init_load(SchemaMod, Conf) when is_list(Conf) orelse is_binary(Conf) ->
|
|
|
- IncDir = include_dirs(),
|
|
|
- ParseOptions = #{format => map, include_dirs => IncDir},
|
|
|
- Parser = case is_binary(Conf) of
|
|
|
- true -> fun hocon:binary/2;
|
|
|
- false -> fun hocon:files/2
|
|
|
- end,
|
|
|
- case Parser(Conf, ParseOptions) of
|
|
|
- {ok, RawRichConf} ->
|
|
|
- init_load(SchemaMod, RawRichConf);
|
|
|
- {error, Reason} ->
|
|
|
- ?SLOG(error, #{msg => "failed_to_load_hocon_conf",
|
|
|
- reason => Reason,
|
|
|
- pwd => file:get_cwd(),
|
|
|
- include_dirs => IncDir
|
|
|
- }),
|
|
|
- error(failed_to_load_hocon_conf)
|
|
|
- end;
|
|
|
+ init_load(SchemaMod, parse_hocon(Conf));
|
|
|
init_load(SchemaMod, RawConf) when is_map(RawConf) ->
|
|
|
ok = save_schema_mod_and_names(SchemaMod),
|
|
|
%% Merge environment varialbe overrides on top
|
|
|
@@ -276,7 +261,6 @@ init_load(SchemaMod, RawConf) when is_map(RawConf) ->
|
|
|
ClusterOverrides = read_override_conf(#{override_to => cluster}),
|
|
|
LocalOverrides = read_override_conf(#{override_to => local}),
|
|
|
Overrides = hocon:deep_merge(ClusterOverrides, LocalOverrides),
|
|
|
- %% TODO: log overrides
|
|
|
RawConfWithOverrides = hocon:deep_merge(RawConfWithEnvs, Overrides),
|
|
|
%% check configs against the schema
|
|
|
{_AppEnvs, CheckedConf} =
|
|
|
@@ -285,6 +269,28 @@ init_load(SchemaMod, RawConf) when is_map(RawConf) ->
|
|
|
ok = save_to_config_map(maps:with(get_atom_root_names(), CheckedConf),
|
|
|
maps:with(RootNames, RawConfWithEnvs)).
|
|
|
|
|
|
+parse_hocon(Conf) ->
|
|
|
+ IncDirs = include_dirs(),
|
|
|
+ case do_parse_hocon(Conf, IncDirs) of
|
|
|
+ {ok, HoconMap} ->
|
|
|
+ HoconMap;
|
|
|
+ {error, Reason} ->
|
|
|
+ ?SLOG(error, #{msg => "failed_to_load_hocon_conf",
|
|
|
+ reason => Reason,
|
|
|
+ pwd => file:get_cwd(),
|
|
|
+ include_dirs => IncDirs,
|
|
|
+ config_file => Conf
|
|
|
+ }),
|
|
|
+ error(failed_to_load_hocon_conf)
|
|
|
+ end.
|
|
|
+
|
|
|
+do_parse_hocon(Conf, IncDirs) ->
|
|
|
+ Opts = #{format => map, include_dirs => IncDirs},
|
|
|
+ case is_binary(Conf) of
|
|
|
+ true -> hocon:binary(Conf, Opts);
|
|
|
+ false -> hocon:files(Conf, Opts)
|
|
|
+ end.
|
|
|
+
|
|
|
include_dirs() ->
|
|
|
[filename:join(emqx:data_dir(), "configs")].
|
|
|
|
|
|
@@ -328,6 +334,23 @@ fill_defaults(SchemaMod, RawConf) ->
|
|
|
#{nullable => true, only_fill_defaults => true},
|
|
|
root_names_from_conf(RawConf)).
|
|
|
|
|
|
+
|
|
|
+%% @doc Only for test cleanups.
|
|
|
+%% Delete override config files.
|
|
|
+-spec delete_override_conf_files() -> ok.
|
|
|
+delete_override_conf_files() ->
|
|
|
+ F1 = override_conf_file(#{override_to => local}),
|
|
|
+ F2 = override_conf_file(#{override_to => cluster}),
|
|
|
+ ok = ensure_file_deleted(F1),
|
|
|
+ ok = ensure_file_deleted(F2).
|
|
|
+
|
|
|
+ensure_file_deleted(F) ->
|
|
|
+ case file:delete(F) of
|
|
|
+ ok -> ok;
|
|
|
+ {error, enoent} -> ok;
|
|
|
+ {error, Reason} -> error({F, Reason})
|
|
|
+ end.
|
|
|
+
|
|
|
-spec read_override_conf(map()) -> raw_config().
|
|
|
read_override_conf(#{} = Opts) ->
|
|
|
File = override_conf_file(Opts),
|