Skip to content

Instantly share code, notes, and snippets.

@mrenvoize
Last active March 18, 2016 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrenvoize/d615da3127c226950609 to your computer and use it in GitHub Desktop.
Save mrenvoize/d615da3127c226950609 to your computer and use it in GitHub Desktop.
hypnotoad init multi
#!/bin/bash
### BEGIN INIT INFO
# Provides: rebus-list
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Provides Hypnotoad Workers for rebus:list
# Description: This script with start/stop hypnotoad worker
# processes for each enabled rebus:list instance
### END INIT INFO
# Author: Martin Renvoize <martin.renvoize@ptfs-europe.com>
# Add local perl to $PATH
PATH=/home/rebus/.plenv/versions/5.20.2/bin/perl5.20.2:$PATH
DESC="Hypnotoad server"
DAEMON=/home/rebus/.plenv/versions/5.20.2/bin/hypnotoad
APP=/home/rebus/rebus-list/script/rebus
RUNDIR=/var/run/rebus-list
test -x $DAEMON || exit 0
set -e
FILES=(/etc/apache2/sites-enabled/*.conf)
if [ -r "${FILES[0]}" ]
then
CONFIGS=()
for FILE in "${FILES[@]}"
do
# remove prefix
NAME=${FILE#/etc/apache2/sites-enabled/}
# remove suffix
NAME=${NAME%.conf}
# check for optional second parameter
if [ "$#" -ne 2 ]
then
# add to config array
CONFIGS+=($NAME)
elif [ "$2" == "$NAME" ]
then
# just one hynotoad
CONFIGS=($NAME)
break
fi
done
if [ ${#CONFIGS[@]} == 0 ];
then
echo "Config missing for: $2" >&2
exit 1
fi
else
echo "No configs found" >&2
exit 1
fi
CONFIG_NUM=${#CONFIGS[@]}
for ((i=0; i < $CONFIG_NUM; i++))
do
NAME=${CONFIGS[${i}]}
PORT=$(awk -F'[ :]' '/ProxyPass /{print $7}' "/etc/rebus/hosts/${NAME}.conf")
PORT=${PORT%/}
case "$1" in
start)
echo -n "Starting $DESC: "
mkdir -p $RUNDIR
chmod 755 $RUNDIR
if [ -n "$ULIMIT" ]
then
ulimit -n "$ULIMIT"
fi
if MOJO_CONFIG="/etc/rebus/list/${NAME}.conf" MOJO_LISTEN="http://*:${PORT}" $DAEMON $APP
then
echo "$NAME"
else
echo "failed"
fi
;;
stop)
echo -n "Stopping $DESC: "
if MOJO_CONFIG="/etc/rebus/list/${NAME}.conf" MOJO_LISTEN="http://*:${PORT}" $DAEMON --stop $APP
then
echo "$NAME"
else
echo "failed"
fi
sleep 1
;;
restart|force-reload)
ARGS=($@)
CONFIG=${ARGS[@]:1}
${0} stop $CONFIG
${0} start $CONFIG
exit 0;
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload" >&2
exit 1
;;
esac
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment