aboutsummaryrefslogtreecommitdiff
path: root/tools/leavecluster
blob: ec21621bc8975db6bc1c57b34c238336014adbc3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/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."