Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
244 lines (195 sloc) 5.44 KB
#!/bin/bash
# This is a handy initscript for controlling PSGI applications using
# plackup
### BEGIN INIT INFO
# Provides: <%= name %>
# Required-Start: $all
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: <%= name %> server
# Description: Start up plack daemon for the site <%= name %>
### END INIT INFO
. /lib/lsb/init-functions
<% if preload_script %>
. <%= preload_script %>
<% end %>
# variables from puppet definition
NAME=<%= name %>
BASEDIR="<%= path %>"
PORT=<%= port %>
PSGI="$BASEDIR/<%= psgi %>"
WORKERS=<%= workers %>
MODULE=<%= appmodule %>
SERVER=<%= server %>
USER=<%= owner %>
GROUP=<%= group %>
PLACK_SERVER_MODULE=<%= server %>
# how long to wait for new process when doing graceful restarts
RESTART_INTERVAL=<%= restart_delay %>
SPAWN_INTERVAL=<%= spawn_interval %>
<% if verbose %>
# verbose
STARMAN_DEBUG=1
<% else %>
STARMAN_DEBUG=0
<% end %>
# start_server startup errors go here
DAEMONLOG="$BASEDIR/server_starter.log"
# user:group to run as
CHUSER="$USER:$GROUP"
# perl5 lib in homedir?
eval PERLLOCAL=~$USER/perl5
if [[ -d "$PERLLOCAL" ]]; then
export PERL_LOCAL_LIB_ROOT="$PERLLOCAL";
export PERL_MB_OPT="--install_base $PERLLOCAL";
export PERL_MM_OPT="INSTALL_BASE=$PERLLOCAL";
export PERL5LIB="$PERLLOCAL/lib/perl5/x86_64-linux-gnu-thread-multi:$PERLLOCAL/lib/perl5";
export PATH="$PERLLOCAL/bin:$PATH";
fi
# pidfile location
STATUSDIR=/tmp
PIDFILE=$STATUSDIR/${NAME}.pid
STATUSFILE=$STATUSDIR/${NAME}.status
# app libs, assume in $BASEDIR/lib
LIBDIR="$BASEDIR/lib"
# daemon command (make sure this is in your $PATH if possible)
if [[ -e /usr/local/bin/start_server ]]; then
# sometimes S-S-D really really wants an absolute path
DAEMON=/usr/local/bin/start_server
elif [[ -e $PERLLOCAL/bin/start_server ]]; then
DAEMON=$PERLLOCAL/bin/start_server
else
echo "System start_server was not found. Please install it."
DAEMON=start_server
fi
DAEMONARGS="--port=$PORT --pid-file $PIDFILE --status-file $STATUSFILE --log-file $DAEMONLOG --interval=$RESTART_INTERVAL"
SERVERARGS="plackup -s $PLACK_SERVER_MODULE --app $PSGI -I$LIBDIR -E deployment"
if [[ "$PLACK_SERVER_MODULE" = 'Starman' ]]; then
# Starman
DAEMONARGS="$DAEMONARGS --signal-on-hup=QUIT"
SERVERARGS="$SERVERARGS --workers=$WORKERS"
else
# assume Starlet/Monoceros
SERVERARGS="$SERVERARGS --max-workers=$WORKERS --spawn-interval=$SPAWN_INTERVAL"
fi
<% if preload_modules.any? %>
# preload modules in master process
<% preload_modules.each do |preload_module| %>
SERVERARGS="$SERVERARGS -M<%= preload_module %>"
<% end %>
<% end %>
# s-s-d command-line
SSD="/sbin/start-stop-daemon --pidfile $PIDFILE --chdir $BASEDIR --startas $DAEMON -c $CHUSER -v"
############ CHECK LOGFILE PERMISSIONS
# check log permissions
unset LOGWRITEOK
# parent directory writable?
if [ $EUID = 0 ]; then
# run permissions tests as $USER:$GROUP
su -l $USER -c 'test -w "$DAEMONLOG"' && LOGWRITEOK=1
su -l $USER -c 'test \! -e "$DAEMONLOG" && test -w $BASEDIR' && LOGWRITEOK=1
else
# test as current user
[[ -w "$DAEMONLOG" || ( ! -e "$DAEMONLOG" && -w "$BASEDIR" ) ]] && LOGWRITEOK=1
fi
if [ -z "$LOGWRITEOK" ]; then
log_failure_msg "Write access to the logfile $DAEMONLOG is required"
exit 4
fi
############
check_running() {
[ -s $PIDFILE ] && kill -0 $(cat $PIDFILE) >/dev/null 2>&1
}
hup() {
$DAEMON $DAEMONARGS --restart
}
check_compile() {
CHECKMODULE=""
[ ! -z "$MODULE" ] && CHECKMODULE="-M$MODULE";
if ( cd $BASEDIR ; perl -Ilib $CHECKMODULE -ce1 ) ; then
return 1
else
return 0
fi
}
_start() {
log_action_begin_msg "start_server arguments" "$DAEMONARGS"
echo
log_action_cont_msg "$PLACK_SERVER_MODULE arguments" "$SERVERARGS"
log_action_end_msg 0
BACKGROUND="--background"
if [[ $TEST ]]; then BACKGROUND=""; fi
$SSD --start $BACKGROUND -- $DAEMONARGS -- $SERVERARGS || return 1
log_success_msg "Backgrounded $NAME superserver. Log is at $DAEMONLOG "
return 0
}
start() {
if check_running; then
log_failure_msg "already running"
exit 0
fi
rm -f $PIDFILE 2>/dev/null
_start
RET=$?
log_end_msg $RET
return $RET
}
stop() {
log_action_begin_msg "Stopping $NAME" $DAEMON
$SSD --stop --oknodo
RET=$?
log_action_end_msg $RET
return $RET
}
restart() {
if ! check_running; then
log_failure_msg "not running"
return 1
fi
log_action_begin_msg "Checking syntax for $MODULE"
if check_compile ; then
log_action_cont_msg "Error detected; not restarting."
log_action_end_msg 1
return 1
fi
log_action_cont_msg "Sending HUP"
if hup; then
log_action_end_msg 0
return 0
else
log_action_end_msg 1
return 1
fi
}
# need to update to return proper LSB status
status() {
if check_running; then
log_info_msg "$NAME: running"
echo
return 0
else
log_info_msg "$NAME: not running"
echo
return 3
fi
}
############
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart|force-reload)
restart
;;
*)
echo $"Usage: $0 {start|status|stop|restart}"
exit 5
esac