mirror of
https://github.com/processone/ejabberd.git
synced 2024-11-20 16:15:59 +01:00
404a7c3381
To avoid unecessary extra quoting, do not call commands with sh -c '' and use the -- option from su to supply arguments. Parse command line parameters is a bit tricky as the previous behavior allows to mix options to ejabberdctl with unknown options given to the next script (usually, the ctl). This is solved by relying on the fact that for loop saves its argument, so we can flush its content with set -- and re-add unknown options with set -- "$@" "$arg". Finally, remove unecessary quotes in mnesia options and in the ping command.
444 lines
13 KiB
Bash
Executable File
444 lines
13 KiB
Bash
Executable File
#!/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
|
|
SCRIPT_DIR=`cd ${0%/*} && pwd`
|
|
ERL={{erl}}
|
|
IEX={{bindir}}/iex
|
|
EPMD={{epmd}}
|
|
INSTALLUSER={{installuser}}
|
|
ERL_LIBS={{libdir}}
|
|
|
|
# check the proper system user is used if defined
|
|
if [ "$INSTALLUSER" != "" ] ; then
|
|
EXEC_CMD="false"
|
|
for GID in `id -G`; do
|
|
if [ $GID -eq 0 ] ; then
|
|
INSTALLUSER_HOME=$(getent passwd "$INSTALLUSER" | cut -d: -f6)
|
|
if [ -n "$INSTALLUSER_HOME" ] && [ ! -d "$INSTALLUSER_HOME" ] ; then
|
|
mkdir -p "$INSTALLUSER_HOME"
|
|
chown "$INSTALLUSER" "$INSTALLUSER_HOME"
|
|
fi
|
|
EXEC_CMD="as_install_user"
|
|
fi
|
|
done
|
|
if [ `id -g` -eq `id -g $INSTALLUSER` ] ; then
|
|
EXEC_CMD="as_current_user"
|
|
fi
|
|
if [ "$EXEC_CMD" = "false" ] ; then
|
|
echo "This command can only be run by root or the user $INSTALLUSER" >&2
|
|
exit 4
|
|
fi
|
|
else
|
|
EXEC_CMD="as_current_user"
|
|
fi
|
|
|
|
# run command either directly or via su $INSTALLUSER
|
|
exec_cmd()
|
|
{
|
|
if [ "EXEC_CMD" = as_install_user ]; then
|
|
su -c '"$0" $@"' "INSTALLUSER" -- "$@"
|
|
else
|
|
"$@"
|
|
fi
|
|
}
|
|
|
|
# parse command line parameters
|
|
next=init
|
|
for arg; do
|
|
# Empty argument list as it is already saved in the for buffer
|
|
if [ "$next" = init ]; then
|
|
next=
|
|
set --
|
|
fi
|
|
case $next in
|
|
node) ERLANG_NODE_ARG=$arg; next=;;
|
|
config-dir) ETC_DIR=$arg; next=;;
|
|
config) EJABBERD_CONFIG_PATH=$arg; next=;;
|
|
ctl-config) EJABBERDCTL_CONFIG_PATH=$arg; next=;;
|
|
logs) LOGS_DIR=$arg; next=;;
|
|
spool) SPOOL_DIR=$arg; next=;;
|
|
"")
|
|
case $arg in
|
|
--) next=raw;;
|
|
--no-timeout) EJABBERD_NO_TIMEOUT="--no-timeout" ;;
|
|
--node) next=node;;
|
|
--config-dir) next=config-dir;;
|
|
--config) next=config;;
|
|
--ctl-config) next=ctl-config;;
|
|
--logs) next=logs;;
|
|
--spool) next=spool;;
|
|
*) set -- "$@" "$arg";; # unknown option, keep it.
|
|
esac;;
|
|
raw) # we are after --, keep options as it is.
|
|
set -- "$@" "$arg";;
|
|
esac
|
|
done
|
|
|
|
# Define ejabberd variable if they have not been defined from the command line
|
|
if [ "$ETC_DIR" = "" ] ; then
|
|
ETC_DIR={{sysconfdir}}/ejabberd
|
|
fi
|
|
if [ "$EJABBERDCTL_CONFIG_PATH" = "" ] ; then
|
|
EJABBERDCTL_CONFIG_PATH=$ETC_DIR/ejabberdctl.cfg
|
|
fi
|
|
if [ -f "$EJABBERDCTL_CONFIG_PATH" ] ; then
|
|
. "$EJABBERDCTL_CONFIG_PATH"
|
|
fi
|
|
if [ "$EJABBERD_CONFIG_PATH" = "" ] ; then
|
|
EJABBERD_CONFIG_PATH=$ETC_DIR/ejabberd.yml
|
|
fi
|
|
if [ "$LOGS_DIR" = "" ] ; then
|
|
LOGS_DIR={{localstatedir}}/log/ejabberd
|
|
fi
|
|
if [ "$SPOOL_DIR" = "" ] ; then
|
|
SPOOL_DIR={{localstatedir}}/lib/ejabberd
|
|
fi
|
|
if [ "$EJABBERD_DOC_PATH" = "" ] ; then
|
|
EJABBERD_DOC_PATH={{docdir}}
|
|
fi
|
|
if [ "$ERLANG_NODE_ARG" != "" ] ; then
|
|
ERLANG_NODE=$ERLANG_NODE_ARG
|
|
fi
|
|
if [ "{{release}}" != "true" -a "$EJABBERD_BIN_PATH" = "" ] ; then
|
|
EJABBERD_BIN_PATH={{libdir}}/ejabberd/priv/bin
|
|
fi
|
|
EJABBERD_LOG_PATH=$LOGS_DIR/ejabberd.log
|
|
DATETIME=`date "+%Y%m%d-%H%M%S"`
|
|
ERL_CRASH_DUMP=$LOGS_DIR/erl_crash_$DATETIME.dump
|
|
ERL_INETRC=$ETC_DIR/inetrc
|
|
|
|
# define mnesia options
|
|
MNESIA_OPTS="-mnesia dir \"$SPOOL_DIR\" $MNESIA_OPTIONS"
|
|
# define erl parameters
|
|
ERLANG_OPTS="+K $POLL -smp $SMP +P $ERL_PROCESSES $ERL_OPTIONS"
|
|
KERNEL_OPTS=""
|
|
if [ "$FIREWALL_WINDOW" != "" ] ; then
|
|
KERNEL_OPTS="${KERNEL_OPTS} -kernel inet_dist_listen_min ${FIREWALL_WINDOW%-*} inet_dist_listen_max ${FIREWALL_WINDOW#*-}"
|
|
fi
|
|
if [ "$INET_DIST_INTERFACE" != "" ] ; then
|
|
INET_DIST_INTERFACE2="$(echo $INET_DIST_INTERFACE | sed 's/\./,/g')"
|
|
if [ "$INET_DIST_INTERFACE" != "$INET_DIST_INTERFACE2" ] ; then
|
|
INET_DIST_INTERFACE2="{$INET_DIST_INTERFACE2}"
|
|
fi
|
|
KERNEL_OPTS="${KERNEL_OPTS} -kernel inet_dist_use_interface \"${INET_DIST_INTERFACE2}\""
|
|
fi
|
|
if [ "$ERLANG_NODE" = "${ERLANG_NODE%.*}" ] ; then
|
|
NAME="-sname"
|
|
else
|
|
NAME="-name"
|
|
fi
|
|
IEXNAME="-$NAME"
|
|
|
|
# define ejabberd environment parameters
|
|
if [ "$EJABBERD_CONFIG_PATH" != "${EJABBERD_CONFIG_PATH%.yml}" ] ; then
|
|
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)
|
|
else
|
|
rate=$(sed '/^[ ]*log_rate_limit/!d;s/.*,//;s/ *//;s/}\.//' $EJABBERD_CONFIG_PATH)
|
|
rotate=$(sed '/^[ ]*log_rotate_size/!d;s/.*,//;s/ *//;s/}\.//' $EJABBERD_CONFIG_PATH)
|
|
count=$(sed '/^[ ]*log_rotate_count/!d;s/.*,//;s/ *//;s/}\.//' $EJABBERD_CONFIG_PATH)
|
|
date=$(sed '/^[ ]*log_rotate_date/!d;s/.*,//;s/ *//;s/}\.//' $EJABBERD_CONFIG_PATH)
|
|
fi
|
|
[ -z "$rate" ] || EJABBERD_OPTS="log_rate_limit $rate"
|
|
[ -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}"
|
|
|
|
[ -d "$SPOOL_DIR" ] || $EXEC_CMD "mkdir -p $SPOOL_DIR"
|
|
cd "$SPOOL_DIR"
|
|
|
|
# export global variables
|
|
export EJABBERD_CONFIG_PATH
|
|
export EJABBERD_LOG_PATH
|
|
export EJABBERD_BIN_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
|
|
|
|
# TODO: Too much copy-and-paste below, factorize!
|
|
# start server
|
|
start()
|
|
{
|
|
check_start
|
|
exec_cmd $ERL \
|
|
$NAME $ERLANG_NODE \
|
|
-noinput -detached \
|
|
$MNESIA_OPTS \
|
|
$KERNEL_OPTS \
|
|
$EJABBERD_OPTS \
|
|
-s ejabberd \
|
|
$ERLANG_OPTS \
|
|
"$@"
|
|
}
|
|
|
|
# attach to server
|
|
debug()
|
|
{
|
|
debugwarning
|
|
NID=$(uid debug)
|
|
exec_cmd $ERL $NAME $NID \
|
|
-remsh $ERLANG_NODE \
|
|
-hidden \
|
|
$KERNEL_OPTS \
|
|
$ERLANG_OPTS \
|
|
"$@"
|
|
}
|
|
|
|
# attach to server using Elixir
|
|
iexdebug()
|
|
{
|
|
debugwarning
|
|
# Elixir shell is hidden as default
|
|
NID=$(uid debug)
|
|
exec_cmd $IEX $IEXNAME $NID \
|
|
-remsh "$ERLANG_NODE" \
|
|
--erl "$KERNEL_OPTS" \
|
|
--erl "$ERLANG_OPTS" \
|
|
--erl "$@"
|
|
}
|
|
|
|
# start interactive server
|
|
live()
|
|
{
|
|
livewarning
|
|
exec_cmd $ERL $NAME $ERLANG_NODE \
|
|
$MNESIA_OPTS \
|
|
$KERNEL_OPTS \
|
|
$EJABBERD_OPTS \
|
|
-s ejabberd \
|
|
$ERLANG_OPTS \
|
|
"$@"
|
|
}
|
|
|
|
# start interactive server with Elixir
|
|
iexlive()
|
|
{
|
|
livewarning
|
|
echo $@
|
|
exec_cmd $IEX $IEXNAME $ERLANG_NODE \
|
|
--erl "-mnesia dir \"$SPOOL_DIR\"" \
|
|
--erl "$KERNEL_OPTS" \
|
|
--erl "$EJABBERD_OPTS" \
|
|
--app ejabberd \
|
|
--erl "$ERLANG_OPTS" \
|
|
--erl "$@"
|
|
}
|
|
|
|
# start server in the foreground
|
|
foreground()
|
|
{
|
|
check_start
|
|
exec_cmd $ERL $NAME $ERLANG_NODE \
|
|
-noinput \
|
|
$MNESIA_OPTS \
|
|
$KERNEL_OPTS \
|
|
$EJABBERD_OPTS \
|
|
-s ejabberd \
|
|
$ERLANG_OPTS \
|
|
"$@"
|
|
}
|
|
|
|
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 foo
|
|
echo ""
|
|
fi
|
|
}
|
|
|
|
livewarning()
|
|
{
|
|
check_start
|
|
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 foo
|
|
echo ""
|
|
fi
|
|
}
|
|
|
|
etop()
|
|
{
|
|
NID=$(uid top)
|
|
exec_cmd $ERL \
|
|
$NAME $NID \
|
|
-hidden -s etop -s erlang halt -output text -node $ERLANG_NODE
|
|
}
|
|
|
|
ping()
|
|
{
|
|
[ -z "$1" ] && PEER=${ERLANG_NODE} || PEER=$1
|
|
if [ "$PEER" = "${PEER%.*}" ] ; then
|
|
PING_NAME="-sname"
|
|
PING_NODE=$(hostname -s)
|
|
else
|
|
PING_NAME="-name"
|
|
PING_NODE=$(hostname)
|
|
fi
|
|
NID=$(uid ping ${PING_NODE})
|
|
exec_cmd $ERL \
|
|
$PING_NAME $NID \
|
|
-hidden $KERNEL_OPTS $ERLANG_OPTS \
|
|
-eval 'io:format("~p~n",[net_adm:ping('"$PEER"')])' \
|
|
-s erlang halt -output text -noinput
|
|
}
|
|
|
|
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 ""
|
|
}
|
|
|
|
# common control function
|
|
ctl()
|
|
{
|
|
NID=$(uid ctl)
|
|
exec_cmd $ERL $NAME $NID \
|
|
-noinput -hidden $KERNEL_OPTS -s ejabberd_ctl \
|
|
-extra $ERLANG_NODE $EJABBERD_NO_TIMEOUT \
|
|
"$@"
|
|
result=$?
|
|
case $result in
|
|
2) help;;
|
|
3) help;;
|
|
*) :;;
|
|
esac
|
|
return $result
|
|
}
|
|
|
|
uid()
|
|
{
|
|
uuid=$(uuidgen 2>/dev/null)
|
|
[ -z "$uuid" -a -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
|
|
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
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
# allow sync calls
|
|
wait_for_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`
|
|
[ $timeout -eq 0 ] && {
|
|
status=$1
|
|
} || {
|
|
ctl status > /dev/null
|
|
status=$?
|
|
}
|
|
done
|
|
[ $timeout -eq 0 ] && return 1 || return 0
|
|
}
|
|
|
|
# main handler
|
|
case $@ in
|
|
'start') start;;
|
|
'debug') debug;;
|
|
'iexdebug') iexdebug;;
|
|
'live') live;;
|
|
'iexlive') iexlive;;
|
|
'foreground') foreground;;
|
|
'ping'*) shift; ping "$@";;
|
|
'etop') etop;;
|
|
'started') wait_for_status 0 30 2;; # wait 30x2s before timeout
|
|
'stopped') wait_for_status 3 30 2 && stop_epmd;; # wait 30x2s before timeout
|
|
*) ctl "$@";;
|
|
esac
|