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

3949eadbf8213b05e415415b2c17eaee?s=128

Martin Brochhaus

December 03, 2012
Tweet

Transcript

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

    December 2012 Learn Python Study Group Meetup
  2. 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
  3. 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
  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 = \ ['dev.fooba.com','prod.foobar.com'] def foobar(): run('pwd')
  5. 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.
  6. 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
  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 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() ...
  9. 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
  10. 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
  11. Another nice utility: def run_workon(command): return run( 'workon {0} &&

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