Package: gunicorn
Version: 0.10.0-1

I'm attaching in this message an initscript to manage gunicorn_django
environments. I currently run gunicorn_django in my servers and manage
differents environtments/projects using this single initscript and the
respective configuration files.

The initscript have a default configuration file[1] and each django
project should have an additional configuration file[2]. The default
configuration have the default parameters and they can/should be
overwrriten in the specific configuration files of the django project.
I don't know if it's the best approach or if we should use a directory
/etc/gunicorn_django.d/<django-project-config> and I would be glad if
you could point a better way. I'm currently using this initiscript in
my production environment and it have been working very well.

[1]. /etc/default/gunicorn_django
[2]. /etc/default/gunicorn_django-myprojectname


A more complete version of the code (with some documentation and usage
examples) are available in my github page[3].

[3]. https://github.com/afurlan/debian-gunicorn_django


-- 
Atenciosamente,

Arthur Furlan (afurlan)
afur...@afurlan.org
http://blog.afurlan.org
Public GPG KeyID: 27D81084

#!/bin/bash

### BEGIN INIT INFO
# Provides:          gunicorn_django
# Required-Start:    $local_fs $syslog
# Required-Stop:     $syslog $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop your gunicorn_django processes
# Description:       Start and stop your gunicorn_django processes
### END INIT INFO

# Author: Arthur Furlan <afur...@afurlan.org>

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="generic gunicorn_django initscript"
NAME="gunicorn_django"
DAEMON=/usr/bin/gunicorn_django
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read global configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

_do_start_project()
{
    NAME=$ORIG_NAME-$1
    PIDFILE=/var/run/$NAME.pid

    # Read project configuration variable file if it is present
    [ -r /etc/default/$NAME ] && . /etc/default/$NAME

    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -d 
$ROOTDIR --test > /dev/null || return 1
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -d 
$ROOTDIR --background --make-pidfile --chuid $RUN_AS --startas $DAEMON -- 
$DAEMON_ARGS || return 2
}

_do_stop_project()
{
    NAME=$ORIG_NAME-$1
    PIDFILE=/var/run/$NAME.pid

    # Read project configuration variable file if it is present
    [ -r /etc/default/$NAME ] && . /etc/default/$NAME

    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE 
> /dev/null # --name $DAEMON
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    # Wait for children to finish too if this is a daemon that forks
    # and if the daemon is only ever run from this initscript.
    # If the above conditions are not satisfied then add some other code
    # that waits for the process to drop all resources that could be
    # needed by services started subsequently.  A last resort is to
    # sleep for some time.
    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
    [ "$?" = 2 ] && return 2
    # Many daemons don't delete their pidfiles when they exit.
    rm -f $PIDFILE
    return "$RETVAL"
}

do_start()
{
    ORIG_NAME=$NAME

    # Start specific projects if specified
    if [ $# -gt 1 ]; then

        for PROJECT in $(echo $@ | cut -d ' ' -f 2-); do
            _do_start_project $PROJECT
        done

    # ... or start all available projects
    else

        for c in /etc/default/gunicorn_django*; do
            [ "$c" == "/etc/default/$NAME" ] && continue

            PROJECT=$(echo "$c" | cut -d '-' -f 2)
            _do_start_project $PROJECT
        done

    fi
}

do_stop()
{
    ORIG_NAME=$NAME

    # Start specific projects if specified
    if [ $# -gt 1 ]; then

        for PROJECT in $(echo $@ | cut -d ' ' -f 2-); do
            _do_stop_project $PROJECT
        done

    # ... or start all available projects
    else

        for c in /etc/default/gunicorn_django*; do
            [ "$c" == "/etc/default/$NAME" ] && continue

            PROJECT=$(echo "$c" | cut -d '-' -f 2)
            _do_stop_project $PROJECT
        done

    fi
}

case "$1" in
  start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start $@
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop $@
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  restart|force-reload)
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop $@
        case "$?" in
          0|1)
                do_start $@
                case "$?" in
                        0) log_end_msg 0 ;;
                        1) log_end_msg 1 ;; # Old process is still running
                        *) log_end_msg 1 ;; # Failed to start
                esac
                ;;
          *)
                # Failed to stop
                log_end_msg 1
                ;;
        esac
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload} [ 
project-list ]" >&2
        exit 3
        ;;
esac

Attachment: signature.asc
Description: Digital signature

Reply via email to