Browse | Submit A New Snippet | Create A Package

 

mongrel_railsd

Type:
Full Script
Category:
UNIX Admin
License:
GNU General Public License
Language:
Unix Shell
 
Description:
Startup script that allow multiple layers to be controlled at the same time.

Versions Of This Snippet::

Philippe Laliberte
Snippet ID Download Version Date Posted Author Delete
3440.42008-02-06 07:07Philippe Laliberte

Download a raw-text version of this code by clicking on "Download Version"

 


Latest Snippet Version: :0.4

#!/bin/bash
#
# mongrel_railsd    Startup script for the Mongrel On Rails Proxy Server
#
# chkconfig: - 85 15
# description: Mongrel is a proxy server for Rails. It is simple to
# use and stable enough for production. Lhttpd is faster but more
# complexe to setup.
# This start script is the product of Philippe Laliberte ©2008 and is public
# domaine and without any warrenty of any sort.
#	       
# processname: mongreil_rails
#
# domainlist: Serves to start multiple proxy server for multiple
#             domaines Needs one at least
#
# vhosts_f: rails applications container (at least one application
#           folder per domain)
#
# <env>Ext: I like to have a current copy of the production (ProdExt)
#           site when I do evolutions and I always run my tests and do
#           my user acceptance tests in a blank site (TestExt) so I
#           have normalised forder names for each application
#           accordingly. _NOT_IN_USES serves to keep this script for
#           trying to do anything on the so marked level of
#           application. Do not create a folder with that extention in
#           the vhosts container, this would try to start it.
#
# <env>Path: the script will look for applications build with the
#            concatenation of $vhosts_f/<domain name><env>Ext to start
#            all the instances 

#            ex. for the domaine r24.ca user acceptance test
#            environment it would be /var/www/vhosts/r24.ca if the
#            TestExt="" or /var/www/vhosts/r24.ca_dev in development
#            if the DevExt=_dev .
#
#            If the script does not find the folder it does not try to
#            start a proxy for that combinasion of domain and
#            level. So do not worry if you have an test site for
#            domain A but not for B. This will handle it nicely.
#
# configuration files are expected in:
#
#            $vhosts_f/<domain name><env>Ext/config/mongrel.cfg
#
#            It is where you tell mongrel_rails where the pid, lock
#            and log files are to be found. It is also where you
#            define all other instance related details (port number,
#            etc.). If it is not found the script will refuse to start
#            the proxy.
#
# To be noted that you must put into a configuation file for apache
# Virtualhost where to find the proxy.
# <VirtualHost *:81>
#    ...
#
#     ProxyPass / http://localhost:5001/
#     ProxyPassReverse / http://localhost:5001
#     ProxyPreserveHost on
#
#     # Do not redirect CSS, images and 
#     # javascripts toward Mongrel
#     ProxyPass /images !
#     ProxyPass /stylesheets !
#     ProxyPass /javascripts !
#
#     # Making sure the relative the server find them
#     Alias /images /var/www/vhosts/r24.ca_prod_bak/public/images
#     Alias /stylesheets /var/www/vhosts/r24.ca_prod_bak/public/stylesheets
#     Alias /javascripts /var/www/vhosts/r24.ca_prod_bak/public/javascripts
#
#  ...
# </VirtualHost>

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/mongrel_rails ]; then
    . /etc/sysconfig/mongrel_rails
fi

# This will prevent initlog from swallowing up a pass-phrase prompt if
# a day there is a needs for pass-phrase from the user.
INITLOG_ARGS=""

# Variables
mongreld=/usr/bin/mongrel_rails
prog=mongrel_railsd
domainlist="r24.ca test_7"
vhosts_f=/var/www/vhosts

# to block a level of service use value _NOT_IN_USES
ProdExt=_prod_bak
TestExt=""
DevExt=_dev

ProdPath=""
TestPath=""
DevPath=""


RETVAL=0

# Just a normal start

start() {
    for dom in $domainlist; do
	echo "Starting: "$dom
	ProdPath=$vhosts_f/$dom$ProdExt
	TestPath=$vhosts_f/$dom$TestExt
	DevPath=$vhosts_f/$dom$DevExt

	if [ -d $ProdPath ]; then
	    echo -n $"Starting $prog "$dom" Prod: "
	    ThisPath=$ProdPath
	    prog_start
	fi
	if [ -d $TestPath ]; then
	    echo -n $"Starting $prog "$dom" Test: "
	    ThisPath=$TestPath
	    prog_start
	fi
	if [ -d $DevPath ]; then
	    echo -n $"Starting $prog "$dom" Dev: "
	    ThisPath=$DevPath
	    prog_start
	fi


    done

}

prog_start() {
    if [ -f $ThisPath/config/mongrel.cfg ]; then
	#echo $ThisPath
	$mongreld start -C $ThisPath/config/mongrel.cfg -c $ThisPath   >/dev/null 2>&1
	RETVAL=$?
	result
	echo
	return $RETVAL
    else
	echo -n You are missing the config file $ThisPath/config/mongrel.cfg
	RETVAL=1
	result
	echo 
	return $RETVAL
    fi

}

stop() {
    for dom in $domainlist; do
	echo "Stoping: "$dom
	ProdPath=$vhosts_f/$dom$ProdExt
	TestPath=$vhosts_f/$dom$TestExt
	DevPath=$vhosts_f/$dom$DevExt

	if [ -d $ProdPath ]; then
	    echo -n $"Stoping $prog Prod: "
	    ThisPath=$ProdPath
	    prog_stop
	fi
	if [ -d $TestPath ]; then
	    echo -n $"Stoping $prog Test: "
	    ThisPath=$TestPath
	    prog_stop
	fi
	if [ -d $DevPath ]; then
	    echo -n $"Stoping $prog Dev: "
	    ThisPath=$DevPath
	    prog_stop
	fi

    done
}

# When stopping mongrel_rails a delay of >10 second is required before
# SIGKILLing the mongrel_rails parent; this gives enough time for the
# mongrel_rails parent to SIGKILL any errant children.

prog_stop() {
    if [ -f $ThisPath/config/mongrel.cfg ]; then
	#echo $ThisPath
	$mongreld stop -c $ThisPath -w 10 > /dev/null 2>&1 
	RETVAL=$?
	result
	echo
	return $RETVAL
    else
	echo -n You are missing the config file $ThisPath/config/mongrel.cfg
	RETVAL=1
	result
	echo
	return $RETVAL
    fi

}


reload() {
    for dom in $domainlist; do
	echo "Reloading: "$dom
	ProdPath=$vhosts_f/$dom$ProdExt
	TestPath=$vhosts_f/$dom$TestExt
	DevPath=$vhosts_f/$dom$DevExt

	if [ -d $ProdPath ]; then
	    echo -n $"Reloading $prog Prod: "
	    ThisPath=$ProdPath
	    prog_reload
	fi
	if [ -d $TestPath ]; then
	    echo -n $"Reloading $prog Test: "
	    ThisPath=$TestPath
	    prog_start
	fi
	if [ -d $DevPath ]; then
	    echo -n $"Reloading $prog Dev: "
	    ThisPath=$DevPath
	    prog_start
	fi
    done

}

prog_reload () {

    if [ -f $ThisPath/config/mongrel.cfg ]; then
	$mongreld restart -s -c $ThisPath > /dev/null 2>&1
        RETVAL=$?
	result 
	echo
	return $RETVAL
    else
	echo -n You are missing the file $ThisPath/config/mongrel.cfg
	RETVAL=1
	result
	echo
	return $RETVAL

    fi
}

result() {
    if [ $RETVAL -eq 0 ]; then
	success
    else
	failure
    fi

}

# See how we were called.
case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    restart)
	stop
	start
	;;
    reload)
        reload
	;;
    -h)
	$mongreld help
	;;
    *)
	echo $"Usage: $prog {start|stop|restart|reload|-h}"
	exit 1
esac

exit $RETVAL

		

Submit a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..