Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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
Tweet

More Decks by Martin Brochhaus

Other Decks in Programming

Transcript

  1. Read the docs! • https://fabric.readthedocs.org • 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
  2. It's like makefiles • Create a `fabfile.py` • Create a

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

    in all your tasks • from fabric.api import env env.user = 'username' env.hosts = \ ['dev.fooba.com','prod.foobar.com'] def foobar(): run('pwd')
  4. Split local tasks and remote tasks • Folder structure: fabfile/

    ..__init__.py ..local.py ..remote.py • Both files can quickly become quite big, so it's good to seperate them. • Import `from local import *` and `from remote import *` in __init__.py.
  5. Create a fabric_settings.py • Users should not need to change

    local.py or remote.py • Create fabric_settings.py for things that might differ from user to user • i.e. USER, PASSWORDS, or certain installation folders
  6. 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`
  7. Create a servers.py 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 = 'dev.bigee.net' env.hosts = [env.host_string, ] def stage(): common_conf() ...
  8. Use your server.py • Fabric allows to call several tasks

    at once: • fab dev foobar • This would set the dev environment and then run the foobar task
  9. Outsource stuff in a utils.py 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