Plasticx Blog

Capable of being shaped or formed

Gentoo init.d script for a Rails mongrel cluster

Posted by Mike 12/23/2007 at 02:45PM

Here is my Gentoo init.d script for a mongrel cluster running a Rails app.

/etc/init.d/mongrel

#!/sbin/runscript

depend() {
        need net
        use apache2 mysql
        after apache2 mysql
}

start() {
        ebegin "Starting mongrels"
        start-stop-daemon --start --chuid $USER --exec $MONGREL \
        -- cluster::start -C $MONGREL_CONF
        eend $?
}

stop() {
        ebegin "Stopping mongrels"
        start-stop-daemon --start --chuid $USER --exec $MONGREL \
        -- cluster::stop -C $MONGREL_CONF
        eend $?
}

status() {
        ebegin "mongrel status"
        start-stop-daemon --start --chuid $USER --exec $MONGREL \
        -- cluster::status -C $MONGREL_CONF
        eend $?
}

/etc/conf.d/mongrel

Gentoo will load script variables for an init.d script from the /etc/conf.d directory where the conf file is the same name is the init.d file. Thus in /etc/conf.d/mongrel

USER=deployer:deployer
MONGREL=/usr/bin/mongrel_rails
MONGREL_CONF=/var/www/mysite/rails_app/shared/mongrel_cluster.conf

Some things to point out:

  • this integrates perfectly with Gentoo’s rc-status command
  • the mongrel cluster will be running as the user/group defined in USER (i.e. safer non-root)
  • mongrel_cluster.conf is a standard mongrel cluster configuration that you would have generated with Capistrano, Vlad or written by hand
  • start-stop-daemon command is executed in —start mode because all of the commands ‘start’ ‘stop’ ‘restart’ ‘status’ are delegated to the mongrel_rails executable
  • because you are running the commands as a non-root user your Capistrano or Vlad tasks (rake vlad:start_app) will still work from your remote shell (you’re not running as root through ssh right!?)

Posted in , , |

Trackbacks<

Use the following link to trackback from your own site:
http://plasti.cx/trackbacks?article_id=810

  1. Josh Nichols
    01/06/2008 at 11:42AM

    This looks pretty nice.

    The only thing missing, for me, is that you might be hosting multiple deployments. To address this, I could imagine doing something like this in the conf.d file:

    
    MONGREL_CONFS="/var/www/mysite/rails_app/shared/mongrel_cluster.conf /var/www/other_site/rails_app/shared/mongrel_cluster.conf"
    

    And then, when starting it:

    
    local mongrel_conf
    for mongrel_conf in ${MONGREL_CONFS}; do
            start-stop-daemon --start --chuid ${USER} --exec ${MONGREL} \
            -- cluster::start -C ${mongrel_conf}
    done
    

    You can imagine something similar for stop and status.

    Two other lesser things:

    • Idiomatic use of variables in scripts/ebuilds in Gentoo prefer using the brackets, to avoid ambiguities, ie $USERfoo versus ${USER}foo.
    • Should probably use MONGREL_USER instead of USER, since USER typically contains the name of the current user. I’m not sure if this would hurt anything, but it’s probably better to not clobber the variable.
  2. monde
    02/23/2008 at 09:13PM

    Thanks for the tips Josh.

    I switched my variables to ${MONGREL_USER} for the user mongrel runs as, ${MONGREL_PROC} for the actual mongrel process/command, and ${MONGREL_CONF} for the mongrel configuration file in the init.d scripts. And I updated the conf.d files accordingly.

    Yes, I’m hosting multiple sites. I would like to keep each application in its own init.d script so combining all of them together in one configuration doesn’t fit my needs.

  3. mooo
    06/12/2008 at 12:01PM

    Shouldn’t the stop section be
    stop() {
    ebegin “Stopping mongrels”
    start-stop-daemon -stop —chuid $USER —exec $MONGREL \
    -
    cluster::stop -C $MONGREL_CONF
    eend $?
    }

    instead of start-stop-daemon —start? It wouldn’t shut down properly until I changed that.


Web Statistics