Pro Yearly is on sale from $80 to $50! »

Fabric Files for Multi-Tier Server Architectures

Fabric Files for Multi-Tier Server Architectures

Some tricks and utilities I find myself using regularly for all my Django projects.

This was a spontaneous talk given at the weekly Learn Python User Group Meetup in Singapore at Hackerspace.SG


Martin Brochhaus

December 03, 2012


  1. Fabric Files for Multi-Tier Server Architectures Martin Brochhaus (@mbrochh) 3.

    December 2012 Learn Python Study Group Meetup
  2. Read the docs! • • Fabric is a Python

    (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. • pip install Fabric
  3. It's like makefiles • Create a `` • Create a

    function in that file def foobar(): print "Hello World" • Call that "fab task" on the terminal: • > fab foobar
  4. The environment dictionary • It's a singleton you can use

    in all your tasks • from fabric.api import env env.user = 'username' env.hosts = \ ['',''] def foobar(): run('pwd')
  5. Split local tasks and remote tasks • Folder structure: fabfile/ • Both files can quickly become quite big, so it's good to seperate them. • Import `from local import *` and `from remote import *` in
  6. Create a • Users should not need to change or • Create for things that might differ from user to user • i.e. USER, PASSWORDS, or certain installation folders
  7. Naming convention • Fab tasks that do somethin on a

    server, start with `run_` • Fab tasks that only do things locally, don't start with `run_` • i.e. `fab flake8` or `fab run_deploy_website`
  8. Create a from fabric.api import env import fabric_settings as

    cfg def common_conf(): env.user = cfg.USER env.port = '22' env.machine = None def dev(): common_conf() env.machine = 'dev' env.host_string = '' env.hosts = [env.host_string, ] def stage(): common_conf() ...
  9. Use your • Fabric allows to call several tasks

    at once: • fab dev foobar • This would set the dev environment and then run the foobar task
  10. Outsource stuff in a from functools import wraps from

    fabric.api import env from fabric.colors import red from fabric.utils import abort def require_server(fn): @wraps(fn) def wrapper(*args, **kwargs): if env.machine is None: abort(red('ERROR: You must provide a server name to call this' ' task!')) return fn(*args, **kwargs) return wrapper
  11. Another nice utility: def run_workon(command): return run( 'workon {0} &&

    {1}'.format( env.venv_name, command))