cryptonas/debian/cryptobox-server.init

160 lines
4.5 KiB
Bash

#!/bin/sh
#
# runlevel script of the cryptobox package
#
# Copyright (c) 02006, senselab
#
# see LICENSE file in this package for details
#
### BEGIN INIT INFO
# Provides: cryptobox-server
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start CryptoBox webserver
### END INIT INFO
# include some useful functions to unify our output format
. /lib/lsb/init-functions
# read the default setting file, if it exists
[ -r /etc/default/cryptobox-server ] && . /etc/default/cryptobox-server
# set default values (if not defined in /etc/default/cryptobox-server)
NO_START=${NO_START:-0}
RUNAS=${RUNAS:-cryptobox}
PORT=${PORT:-8080}
HOST=${HOST:-}
LOGFILE=/var/log/cryptobox-server/webserver.log
SERVER_OPTS=${SERVER_OPTS:-}
CONF_FILE=/etc/cryptobox-server/cryptobox.conf
DAEMON=/usr/sbin/CryptoBoxWebserver
# default install location is /usr/bin/ - debian installation requires /usr/sbin/
test -x "$DAEMON" || DAEMON=/usr/bin/CryptoBoxWebserver
PYTHON_EXEC=/usr/bin/python
PIDFILE=/var/run/cryptobox-server/webserver.pid
DESC="CryptoBox Daemon (webinterface)"
OPTIONS="-B --pidfile=$PIDFILE --config=$CONF_FILE --logfile=$LOGFILE --host=$HOST --port=$PORT --user=$RUNAS $SERVER_OPTS"
# quit if NO_START is 1 (see /etc/default/cryptobox-server)
[ "$NO_START" = "1" ] && log_daemon_msg "Not starting $DESC please edit /etc/default/cryptobox-server!" && exit 0
# check if the package is installed
test -e "$DAEMON" || exit 0
# this test is specific for ubuntu, which currently lacks python-clearsilver
# support for pythpn 2.6
# see https://bugs.launchpad.net/ubuntu/+source/clearsilver/+bug/386970
check_python_version() {
MODULE_CHECK="import neo_cgi, neo_util, neo_cs"
PYTHON_EXEC_25="/usr/bin/python2.5"
PYTHON_EXEC_24="/usr/bin/python2.4"
if "$PYTHON_EXEC" -c "$MODULE_CHECK" 2>/dev/null; then
# everything is fine
return 0
else
if which "$PYTHON_EXEC_25" >/dev/null; then
if "$PYTHON_EXEC_25" -c "$MODULE_CHECK" 2>/dev/null; then
PYTHON_EXEC="$PYTHON_EXEC_25"
return 0
else
log_daemon_msg "The python-clearsilver package is not installed."
log_end_msg 1
return 1
fi
fi
if which "$PYTHON_EXEC_24" >/dev/null; then
if "$PYTHON_EXEC_24" -c "$MODULE_CHECK" 2>/dev/null; then
PYTHON_EXEC="$PYTHON_EXEC_24"
return 0
else
log_daemon_msg "The python-clearsilver package is not installed."
log_end_msg 1
return 1
fi
fi
# no useable python version installed
log_daemon_msg "Failed to use clearsilver with your current python version."
log_daemon_msg "Please install python2.5 (aptitude install python2.5)."
log_end_msg 1
fi
}
case "$1" in
start )
# check if the default python version supports clearsilver
# change the used python interpreter if possible
check_python_version || exit 1
# create the directory of the pid file if necessary
PIDDIR=$(dirname "$PIDFILE")
if [ ! -d "$PIDDIR" ]
then mkdir -p "$PIDDIR"
# necessary: the cryptobox server needs the permission to remove the pid file
chown $RUNAS:root "$PIDDIR"
chmod 755 "$PIDDIR"
fi
# create the directory of the pid file if necessary
LOGDIR=$(dirname "$LOGFILE")
if [ ! -d "$LOGDIR" ]
then mkdir -p "$LOGDIR"
chown $RUNAS:root "$LOGDIR"
chmod 750 "$LOGDIR"
fi
log_daemon_msg "Starting $DESC"
if start-stop-daemon \
--quiet --start --user $RUNAS --pidfile "$PIDFILE" \
--startas "$PYTHON_EXEC" -- "$DAEMON" $OPTIONS
then log_end_msg 0
else log_end_msg 1
fi
;;
stop )
log_daemon_msg "Stopping $DESC"
# if there is no pid file for some reason, then we try to find the process
if test ! -e "$PIDFILE"
then if start-stop-daemon --quiet --stop --user "$RUNAS" --exec "$PYTHON_EXEC"
then log_end_msg 0
else log_end_msg 1
fi
# there is a pid file - great!
elif start-stop-daemon --quiet --stop \
--pidfile "$PIDFILE" \
--user "$RUNAS"
then test -e "$PIDFILE" && rm "$PIDFILE"
log_end_msg 0
else log_end_msg 1
fi
;;
reload | force-reload | restart )
"$0" stop
sleep 3
"$0" start
;;
status )
echo -n "$DESC "
if start-stop-daemon --stop --signal 0 --quiet \
--pidfile "$PIDFILE" --user "$RUNAS"
then echo "running"
exit 0
else if [ -e "$PIDFILE" ]
then echo "failed"
exit 1
else echo "not running"
exit 0
fi
fi
;;
* )
echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
exit 1
;;
esac
exit 0