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

Commit 54b89f3

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 54b89f3

File tree

2 files changed

+180
-1
lines changed

2 files changed

+180
-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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@
4444
check_RABBITMQ_LOGS/1,
4545
check_RABBITMQ_MNESIA_BASE/1,
4646
check_RABBITMQ_MNESIA_DIR/1,
47+
check_RABBITMQ_MOTD_FILE/1,
4748
check_RABBITMQ_NODE_IP_ADDRESS/1,
4849
check_RABBITMQ_NODE_PORT/1,
4950
check_RABBITMQ_NODENAME/1,
5051
check_RABBITMQ_PID_FILE/1,
5152
check_RABBITMQ_PLUGINS_DIR/1,
5253
check_RABBITMQ_PLUGINS_EXPAND_DIR/1,
54+
check_RABBITMQ_PRODUCT_NAME/1,
55+
check_RABBITMQ_PRODUCT_VERSION/1,
5356
check_RABBITMQ_QUORUM_DIR/1,
5457
check_RABBITMQ_UPGRADE_LOG/1,
5558
check_RABBITMQ_USE_LOGNAME/1,
@@ -78,12 +81,15 @@ all() ->
7881
check_RABBITMQ_LOGS,
7982
check_RABBITMQ_MNESIA_BASE,
8083
check_RABBITMQ_MNESIA_DIR,
84+
check_RABBITMQ_MOTD_FILE,
8185
check_RABBITMQ_NODE_IP_ADDRESS,
8286
check_RABBITMQ_NODE_PORT,
8387
check_RABBITMQ_NODENAME,
8488
check_RABBITMQ_PID_FILE,
8589
check_RABBITMQ_PLUGINS_DIR,
8690
check_RABBITMQ_PLUGINS_EXPAND_DIR,
91+
check_RABBITMQ_PRODUCT_NAME,
92+
check_RABBITMQ_PRODUCT_VERSION,
8793
check_RABBITMQ_QUORUM_DIR,
8894
check_RABBITMQ_UPGRADE_LOG,
8995
check_RABBITMQ_USE_LOGNAME,
@@ -168,13 +174,16 @@ check_default_values(_) ->
168174
main_log_file => default,
169175
mnesia_base_dir => default,
170176
mnesia_dir => default,
177+
motd_file => default,
171178
nodename => default,
172179
nodename_type => default,
173180
os_type => environment,
174181
output_supports_colors => default,
175182
pid_file => default,
176183
plugins_expand_dir => default,
177184
plugins_path => default,
185+
product_name => default,
186+
product_version => default,
178187
quorum_queue_dir => default,
179188
rabbitmq_home => default,
180189
upgrade_log_file => default
@@ -204,6 +213,7 @@ check_default_values(_) ->
204213
main_log_file => "/var/log/rabbitmq/" ++ NodeS ++ ".log",
205214
mnesia_base_dir => "/var/lib/rabbitmq/mnesia",
206215
mnesia_dir => "/var/lib/rabbitmq/mnesia/" ++ NodeS,
216+
motd_file => "/etc/rabbitmq/motd",
207217
nodename => Node,
208218
nodename_type => shortnames,
209219
os_type => {unix, undefined},
@@ -212,6 +222,8 @@ check_default_values(_) ->
212222
plugins_expand_dir =>
213223
"/var/lib/rabbitmq/mnesia/" ++ NodeS ++ "-plugins-expand",
214224
plugins_path => maps:get(plugins_path, UnixContext),
225+
product_name => undefined,
226+
product_version => undefined,
215227
quorum_queue_dir =>
216228
"/var/lib/rabbitmq/mnesia/" ++ NodeS ++ "/quorum",
217229
rabbitmq_home => maps:get(rabbitmq_home, UnixContext),
@@ -247,6 +259,7 @@ check_default_values(_) ->
247259
main_log_file => "%APPDATA%/RabbitMQ/log/" ++ NodeS ++ ".log",
248260
mnesia_base_dir => "%APPDATA%/RabbitMQ/db",
249261
mnesia_dir => "%APPDATA%/RabbitMQ/db/" ++ NodeS ++ "-mnesia",
262+
motd_file => "%APPDATA%/RabbitMQ/motd.txt",
250263
nodename => Node,
251264
nodename_type => shortnames,
252265
os_type => {win32, undefined},
@@ -255,6 +268,8 @@ check_default_values(_) ->
255268
plugins_expand_dir =>
256269
"%APPDATA%/RabbitMQ/db/" ++ NodeS ++ "-plugins-expand",
257270
plugins_path => maps:get(plugins_path, Win32Context),
271+
product_name => undefined,
272+
product_version => undefined,
258273
quorum_queue_dir =>
259274
"%APPDATA%/RabbitMQ/db/" ++ NodeS ++ "-mnesia/quorum",
260275
rabbitmq_base => "%APPDATA%/RabbitMQ",
@@ -365,13 +380,16 @@ check_values_from_reachable_remote_node(Config) ->
365380
main_log_file => default,
366381
mnesia_base_dir => default,
367382
mnesia_dir => remote_node,
383+
motd_file => default,
368384
nodename => environment,
369385
nodename_type => default,
370386
os_type => environment,
371387
output_supports_colors => default,
372388
pid_file => default,
373389
plugins_expand_dir => default,
374390
plugins_path => remote_node,
391+
product_name => default,
392+
product_version => default,
375393
quorum_queue_dir => default,
376394
rabbitmq_home => default,
377395
upgrade_log_file => default
@@ -401,13 +419,16 @@ check_values_from_reachable_remote_node(Config) ->
401419
main_log_file => "/var/log/rabbitmq/" ++ NodeS ++ ".log",
402420
mnesia_base_dir => undefined,
403421
mnesia_dir => MnesiaDir,
422+
motd_file => undefined,
404423
nodename => Node,
405424
nodename_type => shortnames,
406425
os_type => {unix, undefined},
407426
output_supports_colors => true,
408427
pid_file => undefined,
409428
plugins_expand_dir => undefined,
410429
plugins_path => PluginsDir,
430+
product_name => undefined,
431+
product_version => undefined,
411432
quorum_queue_dir => MnesiaDir ++ "/quorum",
412433
rabbitmq_home => maps:get(rabbitmq_home, UnixContext),
413434
split_nodename => rabbit_nodes_common:parts(Node),
@@ -474,13 +495,16 @@ check_values_from_offline_remote_node(_) ->
474495
main_log_file => default,
475496
mnesia_base_dir => default,
476497
mnesia_dir => default,
498+
motd_file => default,
477499
nodename => environment,
478500
nodename_type => default,
479501
os_type => environment,
480502
output_supports_colors => default,
481503
pid_file => default,
482504
plugins_expand_dir => default,
483505
plugins_path => default,
506+
product_name => default,
507+
product_version => default,
484508
quorum_queue_dir => default,
485509
rabbitmq_home => default,
486510
upgrade_log_file => default
@@ -510,13 +534,16 @@ check_values_from_offline_remote_node(_) ->
510534
main_log_file => "/var/log/rabbitmq/" ++ NodeS ++ ".log",
511535
mnesia_base_dir => undefined,
512536
mnesia_dir => undefined,
537+
motd_file => undefined,
513538
nodename => Node,
514539
nodename_type => shortnames,
515540
os_type => {unix, undefined},
516541
output_supports_colors => true,
517542
pid_file => undefined,
518543
plugins_expand_dir => undefined,
519544
plugins_path => undefined,
545+
product_name => undefined,
546+
product_version => undefined,
520547
quorum_queue_dir => undefined,
521548
rabbitmq_home => maps:get(rabbitmq_home, UnixContext),
522549
split_nodename => rabbit_nodes_common:parts(Node),
@@ -849,6 +876,33 @@ check_RABBITMQ_PLUGINS_EXPAND_DIR(_) ->
849876
Value1, Value1,
850877
Value2, Value2).
851878

879+
check_RABBITMQ_PRODUCT_NAME(_) ->
880+
Value1 = random_string(),
881+
Value2 = random_string(),
882+
check_prefixed_variable("RABBITMQ_PRODUCT_NAME",
883+
product_name,
884+
'_',
885+
Value1, Value1,
886+
Value2, Value2).
887+
888+
check_RABBITMQ_PRODUCT_VERSION(_) ->
889+
Value1 = random_string(),
890+
Value2 = random_string(),
891+
check_prefixed_variable("RABBITMQ_PRODUCT_VERSION",
892+
product_version,
893+
'_',
894+
Value1, Value1,
895+
Value2, Value2).
896+
897+
check_RABBITMQ_MOTD_FILE(_) ->
898+
Value1 = random_string(),
899+
Value2 = random_string(),
900+
check_prefixed_variable("RABBITMQ_MOTD_FILE",
901+
motd_file,
902+
'_',
903+
Value1, Value1,
904+
Value2, Value2).
905+
852906
check_RABBITMQ_QUORUM_DIR(_) ->
853907
Value1 = random_string(),
854908
Value2 = random_string(),

0 commit comments

Comments
 (0)