Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit f3b63fb

Browse files
committed
rabbit_env: Add $RABBITMQ_{PRODUCT_NAME,PRODUCT_VERSION,MOTD_FILE}
They are used to override the product name and version, and expand the banners which are printed and logged on startup. [#170054940]
1 parent 1c194f4 commit f3b63fb

File tree

2 files changed

+169
-1
lines changed

2 files changed

+169
-1
lines changed

src/rabbit_env.erl

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@
5656
"RABBITMQ_LOGS",
5757
"RABBITMQ_MNESIA_BASE",
5858
"RABBITMQ_MNESIA_DIR",
59+
"RABBITMQ_MOTD_FILE",
5960
"RABBITMQ_NODE_IP_ADDRESS",
6061
"RABBITMQ_NODE_PORT",
6162
"RABBITMQ_NODENAME",
6263
"RABBITMQ_PID_FILE",
6364
"RABBITMQ_PLUGINS_DIR",
6465
"RABBITMQ_PLUGINS_EXPAND_DIR",
66+
"RABBITMQ_PRODUCT_NAME",
67+
"RABBITMQ_PRODUCT_VERSION",
6568
"RABBITMQ_QUORUM_DIR",
6669
"RABBITMQ_UPGRADE_LOG",
6770
"RABBITMQ_USE_LONGNAME",
@@ -137,7 +140,10 @@ get_context_after_reloading_env(Context) ->
137140
fun maybe_stop_dist_for_remote_query/1,
138141
fun amqp_ipaddr/1,
139142
fun amqp_tcp_port/1,
140-
fun erlang_dist_tcp_port/1
143+
fun erlang_dist_tcp_port/1,
144+
fun product_name/1,
145+
fun product_version/1,
146+
fun motd_file/1
141147
],
142148

143149
run_context_steps(Context, Steps).
@@ -1220,6 +1226,125 @@ output_supports_colors(#{os_type := {unix, _}} = Context) ->
12201226
output_supports_colors(#{os_type := {win32, _}} = Context) ->
12211227
update_context(Context, output_supports_colors, false, default).
12221228

1229+
%% -------------------------------------------------------------------
1230+
%%
1231+
%% RABBITMQ_PRODUCT_NAME
1232+
%% Override the product name
1233+
%% Default: unset (i.e. "RabbitMQ")
1234+
%%
1235+
%% RABBITMQ_PRODUCT_VERSION
1236+
%% Override the product version
1237+
%% Default: unset (i.e. `rabbit` application version).
1238+
%%
1239+
%% RABBITMQ_MOTD_FILE
1240+
%% Indicate a filename containing a "message of the day" to add to
1241+
%% the banners, both the logged and the printed ones.
1242+
%% Default: (Unix) ${SYS_PREFIX}/etc/rabbitmq/motd
1243+
%% (Windows) ${RABBITMQ_BASE}\motd.txt
1244+
1245+
product_name(#{from_remote_node := Remote} = Context) ->
1246+
case get_prefixed_env_var("RABBITMQ_PRODUCT_NAME") of
1247+
false when Remote =:= offline ->
1248+
update_context(Context, product_name, undefined, default);
1249+
false ->
1250+
product_name_from_node(Context);
1251+
Value ->
1252+
update_context(Context, product_name, Value, environment)
1253+
end;
1254+
product_name(Context) ->
1255+
product_name_from_env(Context).
1256+
1257+
product_name_from_env(Context) ->
1258+
case get_prefixed_env_var("RABBITMQ_PRODUCT_NAME") of
1259+
false ->
1260+
update_context(Context, product_name, undefined, default);
1261+
Value ->
1262+
update_context(Context, product_name, Value, environment)
1263+
end.
1264+
1265+
product_name_from_node(#{from_remote_node := Remote} = Context) ->
1266+
Ret = (catch query_remote(Remote, rabbit, product_name, [])),
1267+
case Ret of
1268+
{badrpc, nodedown} ->
1269+
update_context(Context, product_name, undefined, default);
1270+
{query, _, _} ->
1271+
update_context(Context, product_name, undefined, default);
1272+
Value ->
1273+
update_context(Context, product_name, Value, remote_node)
1274+
end.
1275+
1276+
product_version(#{from_remote_node := Remote} = Context) ->
1277+
case get_prefixed_env_var("RABBITMQ_PRODUCT_VERSION") of
1278+
false when Remote =:= offline ->
1279+
update_context(Context, product_version, undefined, default);
1280+
false ->
1281+
product_version_from_node(Context);
1282+
Value ->
1283+
update_context(Context, product_version, Value, environment)
1284+
end;
1285+
product_version(Context) ->
1286+
product_version_from_env(Context).
1287+
1288+
product_version_from_env(Context) ->
1289+
case get_prefixed_env_var("RABBITMQ_PRODUCT_VERSION") of
1290+
false ->
1291+
update_context(Context, product_version, undefined, default);
1292+
Value ->
1293+
update_context(Context, product_version, Value, environment)
1294+
end.
1295+
1296+
product_version_from_node(#{from_remote_node := Remote} = Context) ->
1297+
Ret = (catch query_remote(Remote, rabbit, product_version, [])),
1298+
case Ret of
1299+
{badrpc, _} ->
1300+
update_context(Context, product_version, undefined, default);
1301+
{query, _, _} ->
1302+
update_context(Context, product_version, undefined, default);
1303+
Value ->
1304+
update_context(Context, product_version, Value, remote_node)
1305+
end.
1306+
1307+
motd_file(#{from_remote_node := Remote} = Context) ->
1308+
case get_prefixed_env_var("RABBITMQ_MOTD_FILE") of
1309+
false when Remote =:= offline ->
1310+
update_context(Context, motd_file, undefined, default);
1311+
false ->
1312+
motd_file_from_node(Context);
1313+
Value ->
1314+
File = normalize_path(Value),
1315+
update_context(Context, motd_file, File, environment)
1316+
end;
1317+
motd_file(Context) ->
1318+
motd_file_from_env(Context).
1319+
1320+
motd_file_from_env(Context) ->
1321+
case get_prefixed_env_var("RABBITMQ_MOTD_FILE") of
1322+
false ->
1323+
File = get_default_motd_file(Context),
1324+
update_context(Context, motd_file, File, default);
1325+
Value ->
1326+
File = normalize_path(Value),
1327+
update_context(Context, motd_file, File, environment)
1328+
end.
1329+
1330+
get_default_motd_file(#{os_type := {unix, _},
1331+
config_base_dir := ConfigBaseDir}) ->
1332+
filename:join(ConfigBaseDir, "motd");
1333+
get_default_motd_file(#{os_type := {win32, _},
1334+
config_base_dir := ConfigBaseDir}) ->
1335+
filename:join(ConfigBaseDir, "motd.txt").
1336+
1337+
motd_file_from_node(#{from_remote_node := Remote} = Context) ->
1338+
Ret = (catch query_remote(Remote, rabbit, motd_file, [])),
1339+
case Ret of
1340+
{badrpc, _} ->
1341+
update_context(Context, motd_file, undefined, default);
1342+
{query, _, _} ->
1343+
update_context(Context, motd_file, undefined, default);
1344+
File ->
1345+
update_context(Context, motd_file, File, remote_node)
1346+
end.
1347+
12231348
%% -------------------------------------------------------------------
12241349
%% Loading of rabbitmq-env.conf.
12251350
%% -------------------------------------------------------------------

test/rabbit_env_SUITE.erl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
check_RABBITMQ_PID_FILE/1,
5151
check_RABBITMQ_PLUGINS_DIR/1,
5252
check_RABBITMQ_PLUGINS_EXPAND_DIR/1,
53+
check_RABBITMQ_PRODUCT_NAME/1,
54+
check_RABBITMQ_PRODUCT_VERSION/1,
5355
check_RABBITMQ_QUORUM_DIR/1,
5456
check_RABBITMQ_UPGRADE_LOG/1,
5557
check_RABBITMQ_USE_LOGNAME/1,
@@ -84,6 +86,8 @@ all() ->
8486
check_RABBITMQ_PID_FILE,
8587
check_RABBITMQ_PLUGINS_DIR,
8688
check_RABBITMQ_PLUGINS_EXPAND_DIR,
89+
check_RABBITMQ_PRODUCT_NAME,
90+
check_RABBITMQ_PRODUCT_VERSION,
8791
check_RABBITMQ_QUORUM_DIR,
8892
check_RABBITMQ_UPGRADE_LOG,
8993
check_RABBITMQ_USE_LOGNAME,
@@ -168,13 +172,16 @@ check_default_values(_) ->
168172
main_log_file => default,
169173
mnesia_base_dir => default,
170174
mnesia_dir => default,
175+
motd_file => default,
171176
nodename => default,
172177
nodename_type => default,
173178
os_type => environment,
174179
output_supports_colors => default,
175180
pid_file => default,
176181
plugins_expand_dir => default,
177182
plugins_path => default,
183+
product_name => default,
184+
product_version => default,
178185
quorum_queue_dir => default,
179186
rabbitmq_home => default,
180187
upgrade_log_file => default
@@ -204,6 +211,7 @@ check_default_values(_) ->
204211
main_log_file => "/var/log/rabbitmq/" ++ NodeS ++ ".log",
205212
mnesia_base_dir => "/var/lib/rabbitmq/mnesia",
206213
mnesia_dir => "/var/lib/rabbitmq/mnesia/" ++ NodeS,
214+
motd_file => "/etc/rabbitmq/motd",
207215
nodename => Node,
208216
nodename_type => shortnames,
209217
os_type => {unix, undefined},
@@ -212,6 +220,8 @@ check_default_values(_) ->
212220
plugins_expand_dir =>
213221
"/var/lib/rabbitmq/mnesia/" ++ NodeS ++ "-plugins-expand",
214222
plugins_path => maps:get(plugins_path, UnixContext),
223+
product_name => undefined,
224+
product_version => undefined,
215225
quorum_queue_dir =>
216226
"/var/lib/rabbitmq/mnesia/" ++ NodeS ++ "/quorum",
217227
rabbitmq_home => maps:get(rabbitmq_home, UnixContext),
@@ -247,6 +257,7 @@ check_default_values(_) ->
247257
main_log_file => "%APPDATA%/RabbitMQ/log/" ++ NodeS ++ ".log",
248258
mnesia_base_dir => "%APPDATA%/RabbitMQ/db",
249259
mnesia_dir => "%APPDATA%/RabbitMQ/db/" ++ NodeS ++ "-mnesia",
260+
motd_file => "%APPDATA%/RabbitMQ/motd.txt",
250261
nodename => Node,
251262
nodename_type => shortnames,
252263
os_type => {win32, undefined},
@@ -255,6 +266,8 @@ check_default_values(_) ->
255266
plugins_expand_dir =>
256267
"%APPDATA%/RabbitMQ/db/" ++ NodeS ++ "-plugins-expand",
257268
plugins_path => maps:get(plugins_path, Win32Context),
269+
product_name => undefined,
270+
product_version => undefined,
258271
quorum_queue_dir =>
259272
"%APPDATA%/RabbitMQ/db/" ++ NodeS ++ "-mnesia/quorum",
260273
rabbitmq_base => "%APPDATA%/RabbitMQ",
@@ -365,13 +378,16 @@ check_values_from_reachable_remote_node(Config) ->
365378
main_log_file => default,
366379
mnesia_base_dir => default,
367380
mnesia_dir => remote_node,
381+
motd_file => default,
368382
nodename => environment,
369383
nodename_type => default,
370384
os_type => environment,
371385
output_supports_colors => default,
372386
pid_file => default,
373387
plugins_expand_dir => default,
374388
plugins_path => remote_node,
389+
product_name => default,
390+
product_version => default,
375391
quorum_queue_dir => default,
376392
rabbitmq_home => default,
377393
upgrade_log_file => default
@@ -401,13 +417,16 @@ check_values_from_reachable_remote_node(Config) ->
401417
main_log_file => "/var/log/rabbitmq/" ++ NodeS ++ ".log",
402418
mnesia_base_dir => undefined,
403419
mnesia_dir => MnesiaDir,
420+
motd_file => undefined,
404421
nodename => Node,
405422
nodename_type => shortnames,
406423
os_type => {unix, undefined},
407424
output_supports_colors => true,
408425
pid_file => undefined,
409426
plugins_expand_dir => undefined,
410427
plugins_path => PluginsDir,
428+
product_name => undefined,
429+
product_version => undefined,
411430
quorum_queue_dir => MnesiaDir ++ "/quorum",
412431
rabbitmq_home => maps:get(rabbitmq_home, UnixContext),
413432
split_nodename => rabbit_nodes_common:parts(Node),
@@ -474,13 +493,16 @@ check_values_from_offline_remote_node(_) ->
474493
main_log_file => default,
475494
mnesia_base_dir => default,
476495
mnesia_dir => default,
496+
motd_file => default,
477497
nodename => environment,
478498
nodename_type => default,
479499
os_type => environment,
480500
output_supports_colors => default,
481501
pid_file => default,
482502
plugins_expand_dir => default,
483503
plugins_path => default,
504+
product_name => default,
505+
product_version => default,
484506
quorum_queue_dir => default,
485507
rabbitmq_home => default,
486508
upgrade_log_file => default
@@ -510,13 +532,16 @@ check_values_from_offline_remote_node(_) ->
510532
main_log_file => "/var/log/rabbitmq/" ++ NodeS ++ ".log",
511533
mnesia_base_dir => undefined,
512534
mnesia_dir => undefined,
535+
motd_file => undefined,
513536
nodename => Node,
514537
nodename_type => shortnames,
515538
os_type => {unix, undefined},
516539
output_supports_colors => true,
517540
pid_file => undefined,
518541
plugins_expand_dir => undefined,
519542
plugins_path => undefined,
543+
product_name => undefined,
544+
product_version => undefined,
520545
quorum_queue_dir => undefined,
521546
rabbitmq_home => maps:get(rabbitmq_home, UnixContext),
522547
split_nodename => rabbit_nodes_common:parts(Node),
@@ -849,6 +874,24 @@ check_RABBITMQ_PLUGINS_EXPAND_DIR(_) ->
849874
Value1, Value1,
850875
Value2, Value2).
851876

877+
check_RABBITMQ_PRODUCT_NAME(_) ->
878+
Value1 = random_string(),
879+
Value2 = random_string(),
880+
check_prefixed_variable("RABBITMQ_PRODUCT_NAME",
881+
product_name,
882+
'_',
883+
Value1, Value1,
884+
Value2, Value2).
885+
886+
check_RABBITMQ_PRODUCT_VERSION(_) ->
887+
Value1 = random_string(),
888+
Value2 = random_string(),
889+
check_prefixed_variable("RABBITMQ_PRODUCT_VERSION",
890+
product_version,
891+
'_',
892+
Value1, Value1,
893+
Value2, Value2).
894+
852895
check_RABBITMQ_QUORUM_DIR(_) ->
853896
Value1 = random_string(),
854897
Value2 = random_string(),

0 commit comments

Comments
 (0)