Introducing Fabric

Fabric Basics

When we left off, we had git on our servers. We can update our application by manually running git pull origin master:

# Login
ssh serial-app

# Then pull in the latest code
cd ~/serialapp.com/current/repo
# Get the latest & update composer dependencies
git pull origin master
composer update

Let's automate this process using Fabric, an SSH task runner.

Pip and Virtualenv

To install Fabric, we can use first install pip, and then either install fabric globally or within a virtual environment:

# Install pip
sudo easy_install pip
# Update pip
sudo pip install -U pip
# Install virtualenv
sudo pip install virtualenv

# Install `fabric` globally
sudo pip install fabric

# or install in a virtualenv
virtualenv .venv
source .venv/bin/activate
pip install fabric

Fabric

We've installed Fabric in our virtualenv, now we can create the fabfile.py in the project root and run it:

from __future__ import with_statement
from fabric.api import cd run env
from fabric.decorators import task

env.use_ssh_config = True
env.hosts = ['serial-app']
# env.hosts = ['45.55.209.211']
# env.user = 'serial'
# env.key_filename = '~/.ssh/id_series'

@task
def deploy():
    # Change into repository directory
    with cd('/home/serial/serialapp.com/current/repo'):
        # Update git repository
        # assuming we got into the directory
        run('git pull origin master')
        run('composer install')

Push up some changes to git and then run fab deploy to run the deploy task!