Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
diarmuidbourke diarmuidbourke
file 164 lines (123 sloc) 4.571 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
# -*- coding: utf-8 -*-
# fabric-gunicorn
# Copyright: (c) 2012 Christoph Heer <Christoph.Heer@googlemail.com>
# License: BSD, see LICENSE for more details.

from time import sleep

from fabric import colors
from fabric.api import task, env, run, cd
from fabric.utils import abort, puts
from fabric.context_managers import hide


def set_env_defaults():
    env.setdefault('remote_workdir', '~')
    env.setdefault('gunicorn_pidpath', env.remote_workdir + '/gunicorn.pid')
    env.setdefault('gunicorn_bind', '127.0.0.1:8000')

set_env_defaults()


def gunicorn_running():
    return run('ls ' + env.gunicorn_pidpath, quiet=True).succeeded


def gunicorn_running_workers():
    count = None
    with hide('running', 'stdout', 'stderr'):
        count = run('ps -e -o ppid | grep `cat %s` | wc -l' %
                    env.gunicorn_pidpath)
    return count


@task
def status():
    """Show the current status of your Gunicorn process"""

    set_env_defaults()

    if gunicorn_running():
        puts(colors.green("Gunicorn is running."))
        puts(colors.yellow('Active workers: %s' % gunicorn_running_workers()))
    else:
        puts(colors.blue("Gunicorn isn't running."))


@task
def start():
    """Start the Gunicorn process"""

    set_env_defaults()

    if gunicorn_running():
        puts(colors.red("Gunicorn is already running!"))
        return

    if 'gunicorn_wsgi_app' not in env:
        abort(colors.red('env.gunicorn_wsgi_app not defined'))

    with cd(env.remote_workdir):
        prefix = []
        if 'virtualenv_dir' in env:
            prefix.append('source %s/bin/activate' % env.virtualenv_dir)
        if 'django_settings_module' in env:
            prefix.append('export DJANGO_SETTINGS_MODULE=%s' %
                          env.django_settings_module)

        prefix_string = ' && '.join(prefix)
        if len(prefix_string) > 0:
            prefix_string += ' && '

        options = [
            '--daemon',
            '--pid %s' % env.gunicorn_pidpath,
            '--bind %s' % env.gunicorn_bind,
        ]
        if 'gunicorn_workers' in env:
            options.append('--workers %s' % env.gunicorn_workers)
        if 'gunicorn_worker_class' in env:
            options.append('--worker-class %s' % env.gunicorn_worker_class)
        options_string = ' '.join(options)

        if 'paster_config_file' in env:
            run('%s gunicorn_paster %s %s' % (prefix_string, options_string,
                                   env.paster_config_file))
        else:
            run('%s gunicorn %s %s' % (prefix_string, options_string,
                                   env.gunicorn_wsgi_app))

        if gunicorn_running():
            puts(colors.green("Gunicorn started."))
        else:
            abort(colors.red("Gunicorn wasn't started!"))


@task
def stop():
    """Stop the Gunicorn process"""

    set_env_defaults()

    if not gunicorn_running():
        puts(colors.red("Gunicorn isn't running!"))
        return

    run('kill `cat %s`' % (env.gunicorn_pidpath))

    for i in range(0, 5):
        puts('.', end='', show_prefix=i == 0)

        if gunicorn_running():
            sleep(1)
        else:
            puts('', show_prefix=False)
            puts(colors.green("Gunicorn was stopped."))
            break
    else:
        puts(colors.red("Gunicorn wasn't stopped!"))
        return


@task
def restart():
    """Restart hard the Gunicorn process"""
    stop()
    start()


@task
def reload():
    """Gracefully reload the Gunicorn process and the wsgi application"""

    set_env_defaults()
    if not gunicorn_running():
        puts(colors.red("Gunicorn isn't running!"))
        return
    puts(colors.yellow('Gracefully reloading Gunicorn...'))
    run('kill -HUP `cat %s`' % (env.gunicorn_pidpath))


@task
def add_worker():
    """Increase the number of your Gunicorn workers"""
    set_env_defaults()
    if not gunicorn_running():
        puts(colors.red("Gunicorn isn't running!"))
        return

    puts(colors.green('Increasing number of workers...'))
    run('kill -TTIN `cat %s`' % (env.gunicorn_pidpath))
    puts(colors.yellow('Active workers: %s' % gunicorn_running_workers()))


@task
def remove_worker():
    """Decrease the number of your Gunicorn workers"""
    set_env_defaults()
    if not gunicorn_running():
        puts(colors.red("Gunicorn isn't running!"))
        return

    puts(colors.green('Decreasing number of workers...'))
    run('kill -TTOU `cat %s`' % (env.gunicorn_pidpath))
    puts(colors.yellow('Active workers: %s' % gunicorn_running_workers()))
Something went wrong with that request. Please try again.