25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00
xmpp.chapril.org-ejabberd/tools/leavecluster

112 lines
3.0 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
2015-03-06 15:40:09 +01:00
# Remove the current ejabberd node in a cluster
# copyright (c) 2010-2015 ProcessOne
# Return Code:
# 0 : groovy baby
2015-04-21 13:32:14 +02:00
# 10 : ejabberdctl not found
2015-03-06 15:40:09 +01:00
# 11 : erl not found
# 12 : erlc not found
2015-04-21 13:32:14 +02:00
# 22 : temporary dir can not be created
2015-03-06 15:40:09 +01:00
function error
{
echo "Error: $1" >&2
exit $2
}
[ -z $NO_WARNINGS ] && {
echo "--------------------------------------------------------------------"
echo ""
echo "ejabberd cluster configuration"
echo ""
echo "This ejabberd node will be removed from the cluster."
echo "IMPORTANT: this node will be stopped. At least one other clustered"
echo "node must be running."
echo ""
echo "--------------------------------------------------------------------"
echo "Press any key to continue, or Ctrl+C to stop now"
read foo
echo ""
}
echo ok
exit
2015-03-06 15:40:09 +01:00
PA=/tmp/clustersetup_$$
2015-04-21 13:32:14 +02:00
CTL=$(which ejabberdctl)
[ -x "$CTL" ] || {
2015-04-21 13:32:14 +02:00
HERE=`which "$0"`
BASE=`dirname $HERE`/..
ROOTDIR=`cd $BASE; pwd`
PATH=$ROOTDIR/bin:$PATH
PA=$ROOTDIR/clustersetup_$$
CTL=$(which ejabberdctl)
}
echo "Using commands:"
[ -x "$CTL" ] && echo $CTL || error "can't find ejabberdctl" 10
2015-04-21 13:32:14 +02:00
. $CTL stop 2>/dev/null >/dev/null
ERLC=${ERL}c
[ -x $ERL ] && echo $ERL || error "can't find erl" 11
[ -x $ERLC ] && echo $ERLC || error "can't find erlc" 12
echo ""
$CTL stopped
2015-03-06 15:40:09 +01:00
CLUSTERSETUP=clustersetup
CLUSTERSETUP_ERL=$PA/$CLUSTERSETUP.erl
set -o errexit
set -o nounset
2015-04-21 13:32:14 +02:00
mkdir -p $PA || error "$PA cannot be created" 22
cd $PA
2015-03-06 15:40:09 +01:00
cat <<EOF > $CLUSTERSETUP_ERL
-module($CLUSTERSETUP).
-export([start/0]).
del_table_copy(Table, Node) ->
case mnesia:del_table_copy(Table, Node) of
{aborted, Reason} -> io:format("Error: can not remove ~p table: ~p~n", [Table, Reason]);
_ -> io:format("table ~p removed from cluster~n", [Table])
end.
del_tables([],_) ->
ok;
del_tables([schema | Tables], Node) ->
del_tables(Tables, Node);
del_tables([Table | Tables], Node) ->
del_table_copy(Table, Node),
del_tables(Tables, Node).
start() ->
io:format("~n",[]),
Removed = node(),
case mnesia:system_info(running_db_nodes)--[Removed] of
[] -> io:format("Error: no other node running in the cluster~n");
Nodes ->
del_tables(mnesia:system_info(local_tables), Removed),
mnesia:stop(),
case rpc:call(hd(Nodes), mnesia, del_table_copy, [schema, Removed]) of
{badrpc,Reason} -> io:format("Error: can not unregister node ~p from cluster: ~p~n", [Removed, Reason]);
{aborted,Reason} -> io:format("Error: can not unregister node ~p from cluster: ~p~n", [Removed, Reason]);
{atomic, ok} ->
mnesia:delete_schema([Removed]),
io:format("node ~p removed from cluster~n", [Removed])
end
end,
halt(0).
EOF
2015-04-21 13:32:14 +02:00
$ERLC -o $PA $CLUSTERSETUP_ERL
sh -c "$ERL $NAME $ERLANG_NODE -pa $PA $KERNEL_OPTS -mnesia dir \"\\\"$SPOOL_DIR\\\"\" -s mnesia -s $CLUSTERSETUP start"
cd -
2015-03-06 15:40:09 +01:00
rm -Rf $PA
echo "End."
echo "Check that there is no error in the above messages."