aboutsummaryrefslogtreecommitdiff
path: root/ejabberdctl.template
blob: 75e3d2032472fff019b092dd375858d8ed89ef7a (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/bin/sh

# define default configuration
POLL=true
SMP=auto
ERL_MAX_PORTS=32000
ERL_PROCESSES=250000
ERL_MAX_ETS_TABLES=1400
FIREWALL_WINDOW=""
INET_DIST_INTERFACE=""
ERLANG_NODE=ejabberd@localhost

# define default environment variables
ERL="{{erl}}"
IEX="{{bindir}}/iex"
EPMD="{{epmd}}"
INSTALLUSER="{{installuser}}"

# check the proper system user is used
case $(id -un) in
    "$INSTALLUSER")
        EXEC_CMD="as_current_user"
        ;;
    root)
        if [ -n "$INSTALLUSER" ] ; then
            EXEC_CMD="as_install_user"
        else
            EXEC_CMD="as_current_user"
            echo "WARNING: This is not recommended to run ejabberd as root" >&2
        fi
        ;;
    *)
        if [ -n "$INSTALLUSER" ] ; then
            echo "ERROR: This command can only be run by root or the user $INSTALLUSER" >&2
            exit 7
        else
            EXEC_CMD="as_current_user"
        fi
        ;;
esac

# parse command line parameters
for arg; do
    case $arg in
        -n|--node) ERLANG_NODE_ARG=$2; shift;;
        -s|--spool) SPOOL_DIR=$2; shift;;
        -l|--logs) LOGS_DIR=$2; shift;;
        -f|--config) EJABBERD_CONFIG_PATH=$2; shift;;
        -c|--ctl-config) EJABBERDCTL_CONFIG_PATH=$2; shift;;
        -d|--config-dir) ETC_DIR=$2; shift;;
        -t|--no-timeout) NO_TIMEOUT="--no-timeout";;
        --) :;;
        *) break;;
    esac
    shift
done

# define ejabberd variables if not already defined from the command line
: "${ETC_DIR:="{{sysconfdir}}/ejabberd"}"
: "${LOGS_DIR:="{{localstatedir}}/log/ejabberd"}"
: "${SPOOL_DIR:="{{localstatedir}}/lib/ejabberd"}"
: "${EJABBERD_CONFIG_PATH:="$ETC_DIR/ejabberd.yml"}"
: "${EJABBERDCTL_CONFIG_PATH:="$ETC_DIR/ejabberdctl.cfg"}"
[ -f "$EJABBERDCTL_CONFIG_PATH" ] && . "$EJABBERDCTL_CONFIG_PATH"
[ -n "$ERLANG_NODE_ARG" ] && ERLANG_NODE="$ERLANG_NODE_ARG"
[ "$ERLANG_NODE" = "${ERLANG_NODE%.*}" ] && S="-s"
: "${EJABBERD_DOC_PATH:="{{docdir}}"}"
: "${EJABBERD_LOG_PATH:="$LOGS_DIR/ejabberd.log"}"

# define erl parameters
ERLANG_OPTS="+K $POLL -smp $SMP +P $ERL_PROCESSES $ERL_OPTIONS"
if [ -n "$FIREWALL_WINDOW" ] ; then
    ERLANG_OPTS="$ERLANG_OPTS -kernel inet_dist_listen_min ${FIREWALL_WINDOW%-*} inet_dist_listen_max ${FIREWALL_WINDOW#*-}"
fi
if [ -n "$INET_DIST_INTERFACE" ] ; then
    INET_DIST_INTERFACE2=$("$ERL" -noshell -eval 'case inet:parse_address("'$INET_DIST_INTERFACE'") of {ok,IP} -> io:format("~p",[IP]); _ -> ok end.' -s erlang halt)
    if [ -n "$INET_DIST_INTERFACE2" ] ; then
        ERLANG_OPTS="$ERLANG_OPTS -kernel inet_dist_use_interface $INET_DIST_INTERFACE2"
    fi
fi
ERL_LIBS={{libdir}}
ERL_CRASH_DUMP="$LOGS_DIR"/erl_crash_$(date "+%Y%m%d-%H%M%S").dump
ERL_INETRC="$ETC_DIR"/inetrc

# define ejabberd parameters
EJABBERD_OPTS="$EJABBERD_OPTS\
$(sed '/^log_rate_limit/!d;s/:[ \t]*\([0-9]*\).*/ \1/;s/^/ /' "$EJABBERD_CONFIG_PATH")\
$(sed '/^log_rotate_size/!d;s/:[ \t]*\([0-9]*\).*/ \1/;s/^/ /' "$EJABBERD_CONFIG_PATH")\
$(sed '/^log_rotate_count/!d;s/:[ \t]*\([0-9]*\).*/ \1/;s/^/ /' "$EJABBERD_CONFIG_PATH")\
$(sed '/^log_rotate_date/!d;s/:[ \t]*\(.[^ ]*\).*/ \1/;s/^/ /' "$EJABBERD_CONFIG_PATH")"
[ -n "$EJABBERD_OPTS" ] && EJABBERD_OPTS="-ejabberd $EJABBERD_OPTS"
EJABBERD_OPTS="-mnesia dir \"$SPOOL_DIR\" $MNESIA_OPTIONS $EJABBERD_OPTS -s ejabberd"

# export global variables
export EJABBERD_CONFIG_PATH
export EJABBERD_LOG_PATH
export EJABBERD_DOC_PATH
export EJABBERD_PID_PATH
export ERL_CRASH_DUMP
export ERL_EPMD_ADDRESS
export ERL_INETRC
export ERL_MAX_PORTS
export ERL_MAX_ETS_TABLES
export CONTRIB_MODULES_PATH
export CONTRIB_MODULES_CONF_DIR
export ERL_LIBS

# run command either directly or via su $INSTALLUSER
exec_cmd()
{
    case $EXEC_CMD in
        as_install_user) su -c '"$0" "$@"' "$INSTALLUSER" -- "$@" ;;
        as_current_user) "$@" ;;
    esac
}
exec_erl()
{
    NODE=$1; shift
    exec_cmd "$ERL" ${S:--}name "$NODE" $ERLANG_OPTS "$@"
}
exec_iex()
{
    NODE=$1; shift
    exec_cmd "$IEX" -${S:--}name "$NODE" --erl "$ERLANG_OPTS" "$@"
}

# usage
debugwarning()
{
    if [ "$EJABBERD_BYPASS_WARNINGS" != "true" ] ; then
        echo "--------------------------------------------------------------------"
        echo ""
        echo "IMPORTANT: we will attempt to attach an INTERACTIVE shell"
        echo "to an already running ejabberd node."
        echo "If an ERROR is printed, it means the connection was not successful."
        echo "You can interact with the ejabberd node if you know how to use it."
        echo "Please be extremely cautious with your actions,"
        echo "and exit immediately if you are not completely sure."
        echo ""
        echo "To detach this shell from ejabberd, press:"
        echo "  control+c, control+c"
        echo ""
        echo "--------------------------------------------------------------------"
        echo "To bypass permanently this warning, add to ejabberdctl.cfg the line:"
        echo "  EJABBERD_BYPASS_WARNINGS=true"
        echo "Press return to continue"
        read -r
        echo ""
    fi
}

livewarning()
{
    if [ "$EJABBERD_BYPASS_WARNINGS" != "true" ] ; then
        echo "--------------------------------------------------------------------"
        echo ""
        echo "IMPORTANT: ejabberd is going to start in LIVE (interactive) mode."
        echo "All log messages will be shown in the command shell."
        echo "You can interact with the ejabberd node if you know how to use it."
        echo "Please be extremely cautious with your actions,"
        echo "and exit immediately if you are not completely sure."
        echo ""
        echo "To exit this LIVE mode and stop ejabberd, press:"
        echo "  q().  and press the Enter key"
        echo ""
        echo "--------------------------------------------------------------------"
        echo "To bypass permanently this warning, add to ejabberdctl.cfg the line:"
        echo "  EJABBERD_BYPASS_WARNINGS=true"
        echo "Press return to continue"
        read -r
        echo ""
    fi
}

help()
{
    echo ""
    echo "Commands to start an ejabberd node:"
    echo "  start      Start an ejabberd node in server mode"
    echo "  debug      Attach an interactive Erlang shell to a running ejabberd node"
    echo "  iexdebug   Attach an interactive Elixir shell to a running ejabberd node"
    echo "  live       Start an ejabberd node in live (interactive) mode"
    echo "  iexlive    Start an ejabberd node in live (interactive) mode, within an Elixir shell"
    echo "  foreground Start an ejabberd node in server mode (attached)"
    echo ""
    echo "Optional parameters when starting an ejabberd node:"
    echo "  --config-dir dir   Config ejabberd:    $ETC_DIR"
    echo "  --config file      Config ejabberd:    $EJABBERD_CONFIG_PATH"
    echo "  --ctl-config file  Config ejabberdctl: $EJABBERDCTL_CONFIG_PATH"
    echo "  --logs dir         Directory for logs: $LOGS_DIR"
    echo "  --spool dir        Database spool dir: $SPOOL_DIR"
    echo "  --node nodename    ejabberd node name: $ERLANG_NODE"
    echo ""
}

# dynamic node name helper
uid()
{
    uuid=$(uuidgen 2>/dev/null)
    [ -z "$uuid" ] && [ -f /proc/sys/kernel/random/uuid ] && uuid=$(cat /proc/sys/kernel/random/uuid)
    [ -z "$uuid" ] && uuid=$(printf "%X" "${RANDOM:-$$}$(date +%M%S)")
    uuid=${uuid%%-*}
    [ $# -eq 0 ] && echo "${uuid}-${ERLANG_NODE}"
    [ $# -eq 1 ] && echo "${uuid}-${1}-${ERLANG_NODE}"
    [ $# -eq 2 ] && echo "${uuid}-${1}@${2}"
}

# stop epmd if there is no other running node
stop_epmd()
{
    "$EPMD" -names 2>/dev/null | grep -q name || "$EPMD" -kill >/dev/null
}

# make sure node not already running and node name unregistered
# if all ok, ensure runtime directory exists and make it current directory
check_start()
{
    "$EPMD" -names 2>/dev/null | grep -q " ${ERLANG_NODE%@*} " && {
        pgrep -f "$ERLANG_NODE" >/dev/null && {
            echo "ERROR: The ejabberd node '$ERLANG_NODE' is already running."
            exit 4
        }
        pgrep beam >/dev/null && {
            echo "ERROR: The ejabberd node '$ERLANG_NODE' is registered,"
            echo "       but no related beam process has been found."
            echo "Shutdown all other erlang nodes, and call 'epmd -kill'."
            exit 5
        }
        "$EPMD" -kill >/dev/null
    }
}

# allow sync calls
wait_status()
{
    # args: status try delay
    # return: 0 OK, 1 KO
    timeout="$2"
    status=4
    while [ "$status" -ne "$1" ] ; do
        sleep "$3"
        timeout=$((timeout - 1))
        if [ $timeout -eq 0 ] ; then
            status="$1"
        else
            exec_erl "$(uid ctl)" -hidden -noinput -s ejabberd_ctl \
                     -extra "$ERLANG_NODE" $NO_TIMEOUT status > /dev/null
            status="$?"
        fi
    done
    [ $timeout -gt 0 ]
}

# ensure we can change current directory to SPOOL_DIR
[ -d "$SPOOL_DIR" ] || exec_cmd mkdir -p "$SPOOL_DIR"
cd "$SPOOL_DIR" || {
    echo "ERROR: can not access directory $SPOOL_DIR"
    exit 6
}

# main
case $1 in
    start)
        check_start
        exec_erl "$ERLANG_NODE" $EJABBERD_OPTS -noinput -detached
        ;;
    foreground)
        check_start
        exec_erl "$ERLANG_NODE" $EJABBERD_OPTS -noinput
        ;;
    live)
        livewarning
        check_start
        exec_erl "$ERLANG_NODE" $EJABBERD_OPTS
        ;;
    debug)
        debugwarning
        exec_erl "$(uid debug)" -hidden -remsh "$ERLANG_NODE"
        ;;
    etop)
        exec_erl "$(uid top)" -hidden -node "$ERLANG_NODE" -s etop \
                 -s erlang halt -output text
        ;;
    iexdebug)
        debugwarning
        exec_iex "$(uid debug)" --remsh "$ERLANG_NODE"
        ;;
    iexlive)
        livewarning
        exec_iex "$ERLANG_NODE" --erl "$EJABBERD_OPTS" --app ejabberd
        ;;
    ping)
        PEER=${2:-$ERLANG_NODE}
        [ "$PEER" = "${PEER%.*}" ] && PS="-s"
        exec_cmd "$ERL" ${PS:--}name "$(uid ping "$(hostname $PS)")" $ERLANG_OPTS \
                 -noinput -hidden -eval 'io:format("~p~n",[net_adm:ping('"'$PEER'"')])' \
                 -s erlang halt -output text
        ;;
    started)
        wait_status 0 30 2 # wait 30x2s before timeout
        ;;
    stopped)
        wait_status 3 30 2 && stop_epmd # wait 30x2s before timeout
        ;;
    *)
        exec_erl "$(uid ctl)" -hidden -noinput -s ejabberd_ctl \
                 -extra "$ERLANG_NODE" $NO_TIMEOUT "$@"
        result=$?
        case $result in
            2|3) help;;
            *) :;;
        esac
        exit $result
        ;;
esac