Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Fabric deployment for Django

Project Description


There is full working example:


from fabdeploy.api import DefaultConf

class BaseConf(DefaultConf):
    django_dir = 'project_name'

class StagingConf(BaseConf):
    address = ''

class ProdConf(BaseConf):
    address = ''


from fabdeploy import monkey; monkey.patch_all()
from fabric.api import *
from fabdeploy.api import *; setup_fabdeploy()

def user_create():'~/.ssh/')

def deploy():

Fabdeploy uses two system (linux) users:

  • sudo_user to perform tasks that require sudo right (root by default).
  • user for other tasks (SSH user by default).

In Ubuntu root user is disabled by default. You can create special fabdeploy user using following command:

fab fabd.default_conf:address=user@host,sudo_user=user fabd.create_user

Then you should tell fabdeploy to use new sudo_user:

class ProdConf(BaseConf):
    sudo_user = 'fabdeploy'

List of available tasks:

fab --list

List of available variables:

fab fabd.debug

This is useful to test configuration:

$ fab fabd.conf:prod fabd.debug:django_path


$ fab fabd.conf:prod fabd.debug:cpu_count


$ fab fabd.conf:prod fabd.debug:current_time

To deploy project you may use:

$ fab fabd.conf:staging deploy
$ fab fabd.conf:prod deploy


Control where logs are stored

from fabdeploy.api import DefaultConf

class ProdConf(DefaultConf):
    my_task__log_path = '/var/log/my_task'

from fabdeploy.api import Task

class MyTask(Task):
    def do(self):
        print self.conf.log_path

my_task = MyTask()


$ fab fabd.conf:prod my_task

You can also temporarily set log path:

$ fab fabd.conf:prod my_task:log_path='/var'

This works for all variables and all tasks.

Multiple databases

from fabdeploy.api import DefaultConf

class ProdConf(DefaultConf):
    # default DB
    db_name = 'name1'
    db_user = 'user1'
    db_password = 'pass1'
    # logging DB
    loggingdb__db_name = 'name2'
    loggingdb__db_user = 'user2'
    loggingdb__db_password = 'pass2'

from fabdeploy import postgres

def dump_db():  # dump default DB'loggingdb__')  # dump logging DB

Built-in tasks customization

Fabdeploy is written to be highly configurable. For example, there is built-in tar task, which by default packs whole project, uploads it to server and unpacks it there.

But you can freely use it to upload custom dirs:

from fabdeploy import tar

def push_static():
        src_dir=os.path.join(env.conf.django_ldir, 'static'),
        target_dir=posixpath.join(env.conf.django_dir, 'static'))

Different DBs for development and production

from fabdeploy import api
from fabdeploy.api import DefaultConf

class DevConf(DefaultConf):
    address = 'user@localhost'
    db = getattr(fabdeploy, 'mysql')

class ProdConf(DefaultConf):
    address = 'user@localhost'
    db = getattr(fabdeploy, 'postgres')

def execute():
    print env.conf.db.execute


There are some conventions how to configure fabdeploy:

  • You should extend DefaultConf:

    from fabdeploy.api import DefaultConf
    class BaseConf(DefaultConf):
  • Each value can contain Python formatting:

    class BaseConf(DefaultConf):
        supervisor__log_dir = '%(var_dir)s/log/supervisor'
  • Remote pathes should have posfix _path. You can and should use task fabd.mkdirs to create all remote dirs with one command. It will look like this:

    $ fab fabd.conf:staging_conf fabd.mkdirs
    mkdir --parents /path/to/dir1 /path/to/dir2 /path/to/dir3
  • Remote dirs (e.g. var) have postfix _dir.

  • Local pathes have postfix _lpath. Local dirs have postfix _ldir. This is similar to Fabric cd and lcd tasks.

  • Dirs (postfix _dir and _ldir) and pathes (postfix _path and _lpath) can be Python lists. These lists will be passed to os.path.join() or posixpath.join(). Previous example can look like this:

    from fabdeploy.api import DefaultConf
    class BaseConf(DefaultConf):
        supervisor__log_dir = ['%(var_dir)s', 'log', 'supervisor']
  • Function can be decorated with conf decorator. For example, current_time task looks like this:

    from fabdeploy.api import DefaultConf
    class BaseConf(DefaultConf):
        def current_time(self):
            return datetime.datetime.utcnow().strftime(self.time_format)

    You can use it in your task like this:

    from fabdeploy.api import Task
    class MyTask(Task):
        def do(self):
  • You can configure each task individually:

    class BaseConf(DefaultConf):
        postgres__db_name = 'postgresql_db'  # module=postres
        mysql__db_name = 'mysql_db'          # module=mysql
        mysql__create_db__db_user = 'root'   # module=mysql, task=create_db

Configuration is stored in task instance variable self.conf. Each task has its own copy of configuration. Configuration variables are searched in following places:

  • task keyword argument var (fab task:foo=bar);
  • task instance method var() decorated with @conf();
  • key var in env.conf, which is populated by fabd.conf task;
  • ask user to provide variable var using fabric prompt.

Global configuration is stored in env.conf.

Writing your task

Your task is class-based fabric class except fabdeploy manages configuration for you:

from fabric.api import puts
from fabdeploy.api import Task, conf

class MessagePrinter(Task):
    def message(self):
        if 'message' in self.conf:
            return self.conf.message
        return 'Hi!'

    def do(self):
        if self.conf.secret == '123':

message_printer = MessagePrinter()

Then you can run this task like this:

$ fab message_printer
> secret = 123
$ fab message_printer:message='Hello world!'
> secret = 123
Hello world!

Fabfile example

Typical fabfile may look like this:

from fabdeploy import monkey; monkey.patch_all()
from fabric.api import *
from fabdeploy.api import *; setup_fabdeploy()

def install():'~/.ssh/')

    with settings(warn_only=True):

    for app in ['supervisor']:

def setup():


def deploy():'gunicorn')'celeryd')

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(23.9 kB) Copy SHA256 Hash SHA256
Source None May 28, 2012

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Google Google Cloud Servers