diff options
| author | Michael Klishin <michael@novemberain.com> | 2018-08-25 00:39:46 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-08-25 00:39:46 +0300 |
| commit | 526ec377a74008633e10c9e71e20e103a9fd93e7 (patch) | |
| tree | b1250f1117483c759429cfb169cf1bd0cc141c8d /src | |
| parent | 79f943a7fedd7617f52a19f1734d1d3246a86ecf (diff) | |
| parent | 34bba9bcece3070ab0c92c259e3ee37b1d7e7224 (diff) | |
| download | rabbitmq-server-git-526ec377a74008633e10c9e71e20e103a9fd93e7.tar.gz | |
Merge pull request #1671 from rabbitmq/config_files_check
Config files check
Diffstat (limited to 'src')
| -rw-r--r-- | src/rabbit.erl | 5 | ||||
| -rw-r--r-- | src/rabbit_config.erl | 102 | ||||
| -rw-r--r-- | src/rabbit_prelaunch.erl | 21 |
3 files changed, 105 insertions, 23 deletions
diff --git a/src/rabbit.erl b/src/rabbit.erl index 57aa825a1b..6f36de0aa5 100644 --- a/src/rabbit.erl +++ b/src/rabbit.erl @@ -313,6 +313,11 @@ boot() -> end). ensure_config() -> + case rabbit_config:validate_config_files() of + ok -> ok; + {error, {ErrFmt, ErrArgs}} -> + log_boot_error_and_exit(check_config_file, ErrFmt, ErrArgs) + end, case rabbit_config:prepare_and_use_config() of {error, Reason} -> {Format, Arg} = case Reason of diff --git a/src/rabbit_config.erl b/src/rabbit_config.erl index de22d298be..79f4a754bf 100644 --- a/src/rabbit_config.erl +++ b/src/rabbit_config.erl @@ -7,7 +7,8 @@ update_app_config/1, schema_dir/0, config_files/0, - get_advanced_config/0 + get_advanced_config/0, + validate_config_files/0 ]). prepare_and_use_config() -> @@ -42,16 +43,16 @@ legacy_erlang_term_config_used() -> get_confs() -> case init:get_argument(conf) of - {ok, Configs} -> Configs; - _ -> [] + {ok, Confs} -> [ filename:rootname(Conf, ".conf") ++ ".conf" + || Conf <- Confs ]; + _ -> [] end. -prepare_config(Configs) -> +prepare_config(Confs) -> case {init:get_argument(conf_dir), init:get_argument(conf_script_dir)} of {{ok, ConfDir}, {ok, ScriptDir}} -> - ConfFiles = [Config ++ ".conf" || [Config] <- Configs, - rabbit_file:is_file(Config ++ - ".conf")], + ConfFiles = [Conf || Conf <- Confs, + rabbit_file:is_file(Conf)], case ConfFiles of [] -> ok; _ -> @@ -178,9 +179,8 @@ get_advanced_config() -> case init:get_argument(conf_advanced) of %% There can be only one advanced.config {ok, [FileName | _]} -> - ConfigName = FileName ++ ".config", - case rabbit_file:is_file(ConfigName) of - true -> ConfigName; + case rabbit_file:is_file(FileName) of + true -> FileName; false -> none end; _ -> none @@ -193,25 +193,25 @@ prepare_plugin_schemas(SchemaDir) -> false -> ok end. - config_files() -> - Abs = fun (F, Ex) -> filename:absname(filename:rootname(F, Ex) ++ Ex) end, case legacy_erlang_term_config_used() of true -> case init:get_argument(config) of - {ok, Files} -> [Abs(File, ".config") || [File] <- Files]; + {ok, Files} -> [ filename:absname(filename:rootname(File) ++ ".config") + || [File] <- Files]; error -> case config_setting() of none -> []; - File -> [Abs(File, ".config") + File -> [filename:absname(filename:rootname(File, ".config") ++ ".config") ++ " (not found)"] end end; false -> - ConfFiles = [Abs(File, ".conf") || File <- get_confs()], + ConfFiles = [filename:absname(File) || File <- get_confs(), + filelib:is_regular(File)], AdvancedFiles = case get_advanced_config() of none -> []; - FileName -> [Abs(FileName, ".config")] + FileName -> [filename:absname(FileName)] end, AdvancedFiles ++ ConfFiles @@ -234,3 +234,73 @@ config_setting() -> end end. +-spec validate_config_files() -> ok | {error, {Fmt :: string(), Args :: list()}}. +validate_config_files() -> + ConfigFile = os:getenv("RABBITMQ_CONFIG_FILE"), + AdvancedConfigFile = get_advanced_config(), + AssertConfig = case filename:extension(ConfigFile) of + ".config" -> assert_config(ConfigFile, "RABBITMQ_CONFIG_FILE"); + ".conf" -> assert_conf(ConfigFile, "RABBITMQ_CONFIG_FILE"); + _ -> ok + end, + case AssertConfig of + ok -> + assert_config(AdvancedConfigFile, "RABBITMQ_ADVANCED_CONFIG_FILE"); + {error, Err} -> + {error, Err} + end. + +assert_config("", _) -> ok; +assert_config(none, _) -> ok; +assert_config(Filename, Env) -> + ".config" = filename:extension(Filename), + case filelib:is_regular(Filename) of + true -> + case file:consult(Filename) of + {ok, []} -> {error, + {"ERROR: Config file ~s should not be empty: ~s", + [Env, Filename]}}; + {ok, [_]} -> ok; + {ok, [_|_]} -> {error, + {"ERROR: Config file ~s must contain ONE list ended by <dot>: ~s", + [Env, Filename]}}; + {error, {1, erl_parse, Err}} -> + {error, {"ERROR: Unable to parse erlang terms from ~s file: ~s~n" + "ERROR: Reason: ~p~n" + "ERROR: Check that the file is in the erlang terms format. " ++ + case Env of + "RABBITMQ_CONFIG_FILE" -> + "If you are using the new ini-style format, the file " + "extension should be '.conf'~n"; + _ -> "" + end, + [Env, Filename, Err]}}; + {error, Err} -> + {error, {"ERROR Unable to parse erlang terms from ~s file: ~s~n" + "Error: ~p~n", + [Env, Filename, Err]}} + end; + false -> + ok + end. + +assert_conf("", _) -> ok; +assert_conf(Filename, Env) -> + ".conf" = filename:extension(Filename), + case filelib:is_regular(Filename) of + true -> + case file:consult(Filename) of + {ok, []} -> ok; + {ok, _} -> + {error, {"ERROR: Wrong format of the config file ~s: ~s~n" + "ERROR: Check that the file is in the new ini-style config format " + "If you are using the old format the file extension should " + "be .config~n", + [Env, Filename]}}; + _ -> + ok + end; + false -> + ok + end. + diff --git a/src/rabbit_prelaunch.erl b/src/rabbit_prelaunch.erl index f5905151cc..1231f0090e 100644 --- a/src/rabbit_prelaunch.erl +++ b/src/rabbit_prelaunch.erl @@ -17,6 +17,7 @@ -module(rabbit_prelaunch). -export([start/0, stop/0]). +-export([config_file_check/0]). -import(rabbit_misc, [pget/2, pget/3]). @@ -43,7 +44,8 @@ start() -> ok = duplicate_node_check(NodeName, NodeHost), ok = dist_port_set_check(), ok = dist_port_range_check(), - ok = dist_port_use_check(NodeHost); + ok = dist_port_use_check(NodeHost), + ok = config_file_check(); [] -> %% Ignore running node while installing windows service ok = dist_port_set_check(), @@ -57,6 +59,14 @@ stop() -> %%---------------------------------------------------------------------------- +config_file_check() -> + case rabbit_config:validate_config_files() of + ok -> ok; + {error, {Err, Args}} -> + io:format(Err, Args), + rabbit_misc:quit(?ERROR_CODE) + end. + %% Check whether a node with the same name is already running duplicate_node_check(NodeName, NodeHost) -> case rabbit_nodes:names(NodeHost) of @@ -75,7 +85,7 @@ duplicate_node_check(NodeName, NodeHost) -> end. dist_port_set_check() -> - case get_config(os:getenv("RABBITMQ_CONFIG_FILE")) of + case get_config(os:getenv("RABBITMQ_CONFIG_ARG_FILE")) of {ok, [Config]} -> Kernel = pget(kernel, Config, []), case {pget(inet_dist_listen_min, Kernel, none), @@ -89,14 +99,11 @@ dist_port_set_check() -> ok end. +get_config("") -> {error, nofile}; get_config(File) -> case consult_file(File) of {ok, Contents} -> {ok, Contents}; - {error, _} -> - case rabbit_config:get_advanced_config() of - none -> {error, enoent}; - FileName -> file:consult(FileName) - end + {error, _} = E -> E end. consult_file(false) -> {error, nofile}; |
