Skip to content

Commit 29088e0

Browse files
author
Rob Harrop
committed
Merge with default
2 parents dc32d1f + a4d0167 commit 29088e0

File tree

7 files changed

+94
-23
lines changed

7 files changed

+94
-23
lines changed

packaging/RPMS/Fedora/rabbitmq-server.spec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ make install TARGET_DIR=%{_maindir} \
5050

5151
mkdir -p %{buildroot}%{_localstatedir}/lib/rabbitmq/mnesia
5252
mkdir -p %{buildroot}%{_localstatedir}/log/rabbitmq
53+
mkdir -p %{buildroot}%{_localstatedir}/run/rabbitmq
5354

5455
#Copy all necessary lib files etc.
5556
install -p -D -m 0755 %{S:1} %{buildroot}%{_initrddir}/rabbitmq-server
@@ -111,6 +112,7 @@ done
111112
%defattr(-,root,root,-)
112113
%attr(0750, rabbitmq, rabbitmq) %dir %{_localstatedir}/lib/rabbitmq
113114
%attr(0750, rabbitmq, rabbitmq) %dir %{_localstatedir}/log/rabbitmq
115+
%attr(0750, rabbitmq, rabbitmq) %dir %{_localstatedir}/run/rabbitmq
114116
%dir %{_sysconfdir}/rabbitmq
115117
%{_initrddir}/rabbitmq-server
116118
%config(noreplace) %{_sysconfdir}/logrotate.d/rabbitmq-server

packaging/common/rabbitmq-server.init

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ DESC=rabbitmq-server
2424
USER=rabbitmq
2525
ROTATE_SUFFIX=
2626
INIT_LOG_DIR=/var/log/rabbitmq
27+
PID_FILE=/var/run/rabbitmq/pid
2728

2829
LOCK_FILE= # This is filled in when building packages
2930

@@ -40,9 +41,9 @@ start_rabbitmq () {
4041
else
4142
RETVAL=0
4243
set +e
43-
setsid sh -c "$DAEMON > ${INIT_LOG_DIR}/startup_log \
44-
2> ${INIT_LOG_DIR}/startup_err" &
45-
$CONTROL wait >/dev/null 2>&1
44+
setsid sh -c "RABBITMQ_PID_FILE=$PID_FILE $DAEMON > \
45+
${INIT_LOG_DIR}/startup_log 2> ${INIT_LOG_DIR}/startup_err" &
46+
$CONTROL wait $PID_FILE >/dev/null 2>&1
4647
RETVAL=$?
4748
set -e
4849
case "$RETVAL" in
@@ -53,6 +54,7 @@ start_rabbitmq () {
5354
fi
5455
;;
5556
*)
57+
rm -f $PID_FILE
5658
echo FAILED - check ${INIT_LOG_DIR}/startup_\{log, _err\}
5759
RETVAL=1
5860
;;
@@ -68,6 +70,7 @@ stop_rabbitmq () {
6870
RETVAL=$?
6971
set -e
7072
if [ $RETVAL = 0 ] ; then
73+
rm -f $PID_FILE
7174
if [ -n "$LOCK_FILE" ] ; then
7275
rm -f $LOCK_FILE
7376
fi

packaging/common/rabbitmq-server.ocf

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
## OCF_RESKEY_log_base
3030
## OCF_RESKEY_mnesia_base
3131
## OCF_RESKEY_server_start_args
32+
## OCF_RESKEY_pid_file
3233

3334
#######################################################################
3435
# Initialization:
@@ -42,10 +43,12 @@ OCF_RESKEY_server_default="/usr/sbin/rabbitmq-server"
4243
OCF_RESKEY_ctl_default="/usr/sbin/rabbitmqctl"
4344
OCF_RESKEY_nodename_default="rabbit@localhost"
4445
OCF_RESKEY_log_base_default="/var/log/rabbitmq"
46+
OCF_RESKEY_pid_file_default="/var/lib/rabbitmq/pid"
4547
: ${OCF_RESKEY_server=${OCF_RESKEY_server_default}}
4648
: ${OCF_RESKEY_ctl=${OCF_RESKEY_ctl_default}}
4749
: ${OCF_RESKEY_nodename=${OCF_RESKEY_nodename_default}}
4850
: ${OCF_RESKEY_log_base=${OCF_RESKEY_log_base_default}}
51+
: ${OCF_RESKEY_pid_file=${OCF_RESKEY_pid_file_default}}
4952

5053
meta_data() {
5154
cat <<END
@@ -133,6 +136,14 @@ Additional arguments provided to the server on startup
133136
<content type="string" default="" />
134137
</parameter>
135138
139+
<parameter name="pid_file" unique="0" required="0">
140+
<longdesc lang="en">
141+
Location of the file in which the pid will be stored
142+
</longdesc>
143+
<shortdesc lang="en">Pid file path</shortdesc>
144+
<content type="string" default="${OCF_RESKEY_pid_file_default}" />
145+
</parameter>
146+
136147
</parameters>
137148
138149
<actions>
@@ -164,6 +175,7 @@ RABBITMQ_CONFIG_FILE=$OCF_RESKEY_config_file
164175
RABBITMQ_LOG_BASE=$OCF_RESKEY_log_base
165176
RABBITMQ_MNESIA_BASE=$OCF_RESKEY_mnesia_base
166177
RABBITMQ_SERVER_START_ARGS=$OCF_RESKEY_server_start_args
178+
RABBITMQ_PID_FILE=$OCF_RESKEY_pid_file
167179
[ ! -z $RABBITMQ_NODENAME ] && NODENAME_ARG="-n $RABBITMQ_NODENAME"
168180
[ ! -z $RABBITMQ_NODENAME ] && export RABBITMQ_NODENAME
169181

@@ -174,6 +186,7 @@ export_vars() {
174186
[ ! -z $RABBITMQ_LOG_BASE ] && export RABBITMQ_LOG_BASE
175187
[ ! -z $RABBITMQ_MNESIA_BASE ] && export RABBITMQ_MNESIA_BASE
176188
[ ! -z $RABBITMQ_SERVER_START_ARGS ] && export RABBITMQ_SERVER_START_ARGS
189+
[ ! -z $RABBITMQ_PID_FILE ] && export RABBITMQ_PID_FILE
177190
}
178191

179192
rabbit_validate_partial() {
@@ -214,13 +227,13 @@ rabbit_status() {
214227
}
215228

216229
rabbit_wait() {
217-
rabbitmqctl_action "wait"
230+
rabbitmqctl_action "wait" $1
218231
}
219232

220233
rabbitmqctl_action() {
221234
local rc
222235
local action
223-
action=$1
236+
action=$@
224237
$RABBITMQ_CTL $NODENAME_ARG $action > /dev/null 2> /dev/null
225238
rc=$?
226239
case "$rc" in
@@ -252,9 +265,10 @@ rabbit_start() {
252265

253266
# Wait for the server to come up.
254267
# Let the CRM/LRM time us out if required
255-
rabbit_wait
268+
rabbit_wait $RABBITMQ_PID_FILE
256269
rc=$?
257270
if [ "$rc" != $OCF_SUCCESS ]; then
271+
rm -f $RABBITMQ_PID_FILE
258272
ocf_log info "rabbitmq-server start failed: $rc"
259273
exit $OCF_ERR_GENERIC
260274
fi
@@ -285,6 +299,7 @@ rabbit_stop() {
285299
rabbit_status
286300
rc=$?
287301
if [ "$rc" = $OCF_NOT_RUNNING ]; then
302+
rm -f $RABBITMQ_PID_FILE
288303
stop_wait=0
289304
break
290305
elif [ "$rc" != $OCF_SUCCESS ]; then

packaging/debs/Debian/debian/dirs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ usr/sbin
44
usr/share/man
55
var/lib/rabbitmq/mnesia
66
var/log/rabbitmq
7+
var/run/rabbitmq
78
etc/logrotate.d
89
etc/rabbitmq
910

packaging/debs/Debian/debian/postinst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ fi
3232

3333
chown -R rabbitmq:rabbitmq /var/lib/rabbitmq
3434
chown -R rabbitmq:rabbitmq /var/log/rabbitmq
35+
chown -R rabbitmq:rabbitmq /var/run/rabbitmq
3536

3637
case "$1" in
3738
configure)

scripts/rabbitmq-server

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ fi
4747

4848
[ "x" = "x$RABBITMQ_MNESIA_DIR" ] && RABBITMQ_MNESIA_DIR=${MNESIA_DIR}
4949
[ "x" = "x$RABBITMQ_MNESIA_DIR" ] && RABBITMQ_MNESIA_DIR=${RABBITMQ_MNESIA_BASE}/${RABBITMQ_NODENAME}
50+
[ "x" = "x$RABBITMQ_PID_FILE" ] && RABBITMQ_PID_FILE=${RABBITMQ_MNESIA_DIR}/pid
5051

5152
[ "x" = "x$RABBITMQ_PLUGINS_EXPAND_DIR" ] && RABBITMQ_PLUGINS_EXPAND_DIR=${PLUGINS_EXPAND_DIR}
5253
[ "x" = "x$RABBITMQ_PLUGINS_EXPAND_DIR" ] && RABBITMQ_PLUGINS_EXPAND_DIR=${RABBITMQ_MNESIA_BASE}/${RABBITMQ_NODENAME}-plugins-expand
@@ -67,6 +68,9 @@ fi
6768
RABBITMQ_START_RABBIT=
6869
[ "x" = "x$RABBITMQ_ALLOW_INPUT" ] && RABBITMQ_START_RABBIT='-noinput'
6970

71+
mkdir -p $(dirname ${RABBITMQ_PID_FILE})
72+
echo $$ > ${RABBITMQ_PID_FILE}
73+
7074
RABBITMQ_EBIN_ROOT="${RABBITMQ_HOME}/ebin"
7175
if [ "x" = "x$RABBITMQ_NODE_ONLY" ]; then
7276
if erl \

src/rabbit_control.erl

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
-export([start/0, stop/0, action/5, diagnostics/1, log_action/3]).
2121

2222
-define(RPC_TIMEOUT, infinity).
23-
-define(WAIT_FOR_VM_ATTEMPTS, 5).
2423

2524
-define(QUIET_OPT, "-q").
2625
-define(NODE_OPT, "-n").
@@ -193,9 +192,9 @@ action(force_cluster, Node, ClusterNodeSs, _Opts, Inform) ->
193192
[Node, ClusterNodes]),
194193
rpc_call(Node, rabbit_mnesia, force_cluster, [ClusterNodes]);
195194

196-
action(wait, Node, [], _Opts, Inform) ->
195+
action(wait, Node, [PidFile], _Opts, Inform) ->
197196
Inform("Waiting for ~p", [Node]),
198-
wait_for_application(Node, ?WAIT_FOR_VM_ATTEMPTS);
197+
wait_for_application(Node, PidFile, Inform);
199198

200199
action(status, Node, [], _Opts, Inform) ->
201200
Inform("Status of node ~p", [Node]),
@@ -356,23 +355,69 @@ action(report, Node, _Args, _Opts, Inform) ->
356355

357356
%%----------------------------------------------------------------------------
358357

359-
wait_for_application(Node, Attempts) ->
358+
wait_for_application(Node, PidFile, Inform) ->
359+
Pid = wait_and_read_pid_file(PidFile),
360+
Inform("pid is ~s", [Pid]),
361+
wait_for_application(Node, Pid).
362+
363+
wait_for_application(Node, Pid) ->
364+
case process_up(Pid) of
365+
true -> case node_up(Node) of
366+
true -> ok;
367+
false -> timer:sleep(1000),
368+
wait_for_application(Node, Pid)
369+
end;
370+
false -> {error, process_not_running}
371+
end.
372+
373+
wait_and_read_pid_file(PidFile) ->
374+
case file:read_file(PidFile) of
375+
{ok, Bin} -> string:strip(binary_to_list(Bin), right, $\n);
376+
{error, enoent} -> timer:sleep(500),
377+
wait_and_read_pid_file(PidFile);
378+
{error, _} = E -> exit({error, {could_not_read_pid, E}})
379+
end.
380+
381+
node_up(Node) ->
360382
case rpc_call(Node, application, which_applications, [infinity]) of
361-
{badrpc, _} = E -> case Attempts of
362-
0 -> E;
363-
_ -> wait_for_application0(Node, Attempts - 1)
364-
end;
365-
Apps -> case proplists:is_defined(rabbit, Apps) of
366-
%% We've seen the node up; if it goes down
367-
%% die immediately.
368-
true -> ok;
369-
false -> wait_for_application0(Node, 0)
370-
end
383+
{badrpc, _} -> false;
384+
Apps -> proplists:is_defined(rabbit, Apps)
371385
end.
372386

373-
wait_for_application0(Node, Attempts) ->
374-
timer:sleep(1000),
375-
wait_for_application(Node, Attempts).
387+
% Test using some OS clunkiness since we shouldn't trust
388+
% rpc:call(os, getpid, []) at this point
389+
process_up(Pid) ->
390+
with_os([{unix, fun () ->
391+
system("ps -p " ++ Pid
392+
++ " >/dev/null 2>&1") =:= 0
393+
end},
394+
{win32, fun () ->
395+
Res = os:cmd("tasklist /nh /fi \"pid eq " ++
396+
Pid ++ "\" 2>&1"),
397+
case re:run(Res, "erl\\.exe", [{capture, none}]) of
398+
match -> true;
399+
_ -> false
400+
end
401+
end}]).
402+
403+
with_os(Handlers) ->
404+
{OsFamily, _} = os:type(),
405+
case proplists:get_value(OsFamily, Handlers) of
406+
undefined -> throw({unsupported_os, OsFamily});
407+
Handler -> Handler()
408+
end.
409+
410+
% Like system(3)
411+
system(Cmd) ->
412+
ShCmd = "sh -c '" ++ escape_quotes(Cmd) ++ "'",
413+
Port = erlang:open_port({spawn, ShCmd}, [exit_status,nouse_stdio]),
414+
receive {Port, {exit_status, Status}} -> Status end.
415+
416+
% Escape the quotes in a shell command so that it can be used in "sh -c 'cmd'"
417+
escape_quotes(Cmd) ->
418+
lists:flatten(lists:map(fun ($') -> "'\\''"; (Ch) -> Ch end, Cmd)).
419+
420+
%%----------------------------------------------------------------------------
376421

377422
default_if_empty(List, Default) when is_list(List) ->
378423
if List == [] -> Default;

0 commit comments

Comments
 (0)