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
2015-03-06 15:42:41 +01:00

98 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# Remove the current ejabberd node in a cluster
# copyright (c) 2010-2015 ProcessOne
#
# This script is proprietary software and cannot be published or redistribute.
# Return Code:
# 0 : groovy baby
# 11 : erl not found
# 12 : erlc not found
# 20 : database dir doesn't exist
# 21 : database dir not writable
# 21 : database dir variable not set
function error
{
echo "Error: $1" >&2
exit $2
}
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 ""
HERE=`which "$0"`
BASE=`dirname $HERE`/..
ROOTDIR=`cd $BASE; pwd`
. $ROOTDIR/bin/ejabberdctl stop 2>/dev/null >/dev/null
$ROOTDIR/bin/ejabberdctl stopped
PA=/tmp/clustersetup_$$
CLUSTERSETUP=clustersetup
CLUSTERSETUP_ERL=$PA/$CLUSTERSETUP.erl
set -o errexit
set -o nounset
echo "Using commands:"
which erl || error "can't find erl" 11
which erlc || error "can't find erlc" 12
echo ""
cd $ROOTDIR
mkdir -p $PA
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
erlc -o $PA $CLUSTERSETUP_ERL
sh -c "erl $NAME $ERLANG_NODE -pa $PA $KERNEL_OPTS -mnesia dir "\"$SPOOL_DIR\"" -s mnesia -s $CLUSTERSETUP start"
rm -Rf $PA
echo "End."
echo "Check that there is no error in the above messages."