Slide 1

Slide 1 text

Fabric Files for Multi-Tier Server Architectures Martin Brochhaus (@mbrochh) 3. December 2012 Learn Python Study Group Meetup

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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')

Slide 5

Slide 5 text

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.

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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`

Slide 8

Slide 8 text

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() ...

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Another nice utility: def run_workon(command): return run( 'workon {0} && {1}'.format( env.venv_name, command))