xmpp.chapril.org-ejabberd/ejabberdctl.template

332 lines
10 KiB
Plaintext
Raw Normal View History

#!/bin/sh
# define default configuration
POLL=true
SMP=auto
ERL_MAX_PORTS=32000
ERL_PROCESSES=250000
ERL_MAX_ETS_TABLES=1400
FIREWALL_WINDOW=""
ERLANG_NODE=ejabberd@localhost
# define default environment variables
ERL={{erl}}
2015-01-29 18:43:47 +01:00
IEX={{bindir}}/iex
2016-05-01 21:29:59 +02:00
EPMD={{epmd}}
INSTALLUSER={{installuser}}
# check the proper system user is used if defined
2017-05-31 18:11:45 +02:00
EXEC_CMD="false"
if [ -n "$INSTALLUSER" ] ; then
if [ $(id -g) -eq $(id -g $INSTALLUSER || echo -1) ] ; then
EXEC_CMD="as_current_user"
2017-05-31 18:11:45 +02:00
else
id -Gn | grep -q wheel && EXEC_CMD="as_install_user"
fi
else
EXEC_CMD="as_current_user"
fi
2017-05-31 18:11:45 +02:00
if [ "$EXEC_CMD" = "false" ] ; then
echo "ERROR: This command can only be run by root or the user $INSTALLUSER" >&2
exit 7
fi
2017-05-31 18:11:45 +02:00
# set backward compatibility on command line parameters
set -- $(echo "$*" | sed -e \
"s/--node/-n/;s/--spool/-s/;s/--logs/-l/;\
s/--config/-f/;s/--ctl-config/-c/;s/--config-dir/-d/;\
s/--no-timeout/-t/")
# parse command line parameters
2017-05-31 18:11:45 +02:00
while getopts n:s:l:f:c:d:tx opt; do
case $opt in
n) ERLANG_NODE_ARG=$OPTARG;;
s) SPOOL_DIR=$OPTARG;;
l) LOGS_DIR=$OPTARG;;
f) EJABBERD_CONFIG_PATH=$OPTARG;;
c) EJABBERDCTL_CONFIG_PATH=$OPTARG;;
d) ETC_DIR=$OPTARG;;
t) NO_TIMEOUT="--no-timeout";;
esac
done
2017-05-31 18:11:45 +02:00
# keep extra command line parameters for ejabberd
shift $((OPTIND-1))
2017-05-31 18:11:45 +02:00
# 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"
[ "$ERLANG_NODE_ARG" != "" ] && ERLANG_NODE=$ERLANG_NODE_ARG
: ${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 [ "$FIREWALL_WINDOW" != "" ] ; then
2017-05-31 18:11:45 +02:00
ERLANG_OPTS="$ERLANG_OPTS -kernel " \
"inet_dist_listen_min ${FIREWALL_WINDOW%-*} " \
"inet_dist_listen_max ${FIREWALL_WINDOW#*-}"
fi
if [ "$INET_DIST_INTERFACE" != "" ] ; then
2017-05-31 18:11:45 +02:00
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 [ "$INET_DIST_INTERFACE2" != "" ] ; then
ERLANG_OPTS="$ERLANG_OPTS -kernel inet_dist_use_interface \"$INET_DIST_INTERFACE2\""
fi
fi
if [ "$ERLANG_NODE" = "${ERLANG_NODE%.*}" ] ; then
NAME="-sname"
else
NAME="-name"
fi
2017-05-31 18:11:45 +02:00
ERL_LIBS={{libdir}}
ERL_CRASH_DUMP="$LOGS_DIR"/erl_crash_$(date "+%Y%m%d-%H%M%S").dump
ERL_INETRC="$ETC_DIR"/inetrc
2017-05-31 18:11:45 +02:00
# define ejabberd parameters
rate=$(sed '/^[ ]*log_rate_limit/!d;s/.*://;s/ *//' "$EJABBERD_CONFIG_PATH")
rotate=$(sed '/^[ ]*log_rotate_size/!d;s/.*://;s/ *//' "$EJABBERD_CONFIG_PATH")
count=$(sed '/^[ ]*log_rotate_count/!d;s/.*://;s/ *//' "$EJABBERD_CONFIG_PATH")
date=$(sed '/^[ ]*log_rotate_date/!d;s/.*://;s/ *//' "$EJABBERD_CONFIG_PATH")
[ -z "$rate" ] || EJABBERD_OPTS="log_rate_limit $rate"
2017-05-31 18:11:45 +02:00
[ -z "$rotate" ] || EJABBERD_OPTS="$EJABBERD_OPTS log_rotate_size $rotate"
[ -z "$count" ] || EJABBERD_OPTS="$EJABBERD_OPTS log_rotate_count $count"
[ -z "$date" ] || EJABBERD_OPTS="$EJABBERD_OPTS log_rotate_date '$date'"
[ -z "$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
2017-05-31 18:11:45 +02:00
# run command either directly or via su $INSTALLUSER
exec_cmd()
2015-01-29 18:43:47 +01:00
{
2017-05-31 18:11:45 +02:00
case $EXEC_CMD in
as_install_user) su -c '"$0" $@"' "$INSTALLUSER" -- "$@" ;;
as_current_user) "$@" ;;
esac
2015-01-29 18:43:47 +01:00
}
2017-05-31 18:11:45 +02:00
exec_erl()
2015-01-29 18:43:47 +01:00
{
2017-05-31 18:11:45 +02:00
NODE=$1; shift
exec_cmd $ERL $NAME $NODE $ERLANG_OPTS "$@"
2015-01-29 18:43:47 +01:00
}
2017-05-31 18:11:45 +02:00
exec_iex()
{
2017-05-31 18:11:45 +02:00
NODE=$1; shift
exec_cmd $IEX -$NAME $NODE --erl "$ERLANG_OPTS" "$@"
}
2017-05-31 18:11:45 +02:00
# usage
2015-01-29 18:43:47 +01:00
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"
2015-01-29 18:43:47 +01:00
echo "Press return to continue"
read foo
echo ""
fi
}
2015-01-29 18:43:47 +01:00
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"
2015-01-29 18:43:47 +01:00
echo "Press return to continue"
read foo
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 ""
}
2017-05-31 18:11:45 +02:00
# generic erlang node ping feature
ping()
{
2017-05-31 18:11:45 +02:00
PEER=${1:-$ERLANG_NODE}
if [ "$PEER" = "${PEER%.*}" ] ; then
PING_NAME="-sname"
PING_NODE=$(hostname -s)
else
PING_NAME="-name"
PING_NODE=$(hostname)
fi
exec_cmd $ERL $PING_NAME $(uid ping $PING_NODE) $ERLANG_OPTS \
-noinput -hidden -eval 'io:format("~p~n",[net_adm:ping('"$PEER"')])' \
-s erlang halt -output text
}
2017-05-31 18:11:45 +02:00
# dynamic node name helper
2016-04-21 12:00:51 +02:00
uid()
{
2016-04-21 12:00:51 +02:00
uuid=$(uuidgen 2>/dev/null)
2017-05-31 18:11:45 +02:00
[ -z "$uuid" -a -f /proc/sys/kernel/random/uuid ] && uuid=$(cat /proc/sys/kernel/random/uuid)
2016-04-21 12:00:51 +02:00
[ -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
2017-05-31 18:11:45 +02:00
# if all ok, ensure runtime directory exists and make it current directory
check_start()
{
"$EPMD" -names 2>/dev/null | grep -q " ${ERLANG_NODE%@*} " && {
ps ux | grep -v grep | grep -q " $ERLANG_NODE " && {
echo "ERROR: The ejabberd node '$ERLANG_NODE' is already running."
exit 4
} || {
ps ux | grep -v grep | grep -q beam && {
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
}
}
2017-05-31 18:11:45 +02:00
} || {
cd "$SPOOL_DIR" || {
echo "ERROR: ejabberd can not access directory $SPOOL_DIR"
exit 6
}
}
}
# allow sync calls
2017-05-31 18:11:45 +02:00
wait_status()
{
# args: status try delay
# return: 0 OK, 1 KO
timeout=$2
status=4
while [ $status -ne $1 ] ; do
sleep $3
timeout=`expr $timeout - 1`
2017-05-31 18:11:45 +02:00
if [ $timeout -eq 0 ] ; then
status=$1
2017-05-31 18:11:45 +02:00
else
ctl status > /dev/null
status=$?
2017-05-31 18:11:45 +02:00
fi
done
2017-05-31 18:11:45 +02:00
[ $timeout -gt 0 ]
}
2017-05-31 18:11:45 +02:00
# 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)
ping $2
;;
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