Andrew Wilkinson

Random Ramblings on Programming

Using Fabric For Deployment

with 2 comments

robot invasion by donsoloIn a previous post I discussed what you want from an automatic deployment system. In this post I’ll discuss how use to solve the repeatability and scalability requirements that I set out.

Fabric is a tool which lets you write scripts to automate repetitive tasks. So far, so bash like. What sets Fabric apart is the tools it gives you to run commands on remote servers. Fabric allows you to run the same commands on multiple machines, and to move files between the hosts easily.

To get started with Fabric you’ll need to install it, but a simple sudo easy_install fabric should be enough to get you up and running. The Fabric website has excellent documentation, including a tutorial, but before I discuss how to integrate Fabric with your Django deployment process, lets go over the basics.

A Fabric control file is a Python file named fabfile.py. In it, you define a series functions, one for each command that you want to run on the remote servers.

from fabric.context_managers import cd
from fabric.operations import sudo

env.hosts = ['host1', 'host2']

def update():
    with cd('/data/site'):
        sudo('svn up')
        sudo('/etc/init.d/apache2 graceful')

We’ve defined the function update which can be run by typing fab update in the same directory as fabfile.py. When run, Fabric will connect in turn to host1 and host2 and run svn up in /data/site and then restart Apache.

Typically I define two functions. An update command, like that above is used to update the site where is has previously been deployed. A deploy command is used to checkout the site onto a new machine. Fabric lets you override the host list on the command line using the -H option. To deploy one of my sites on a new box I just have to type fab -H new-machine deploy and the box is set up for me.

Fabric helps you fulfil a few of the requirements for a perfect deployment system. It is scaleable, as to extend your system to a new machine you only need to add the hostname to the env.hosts list. It is also repeatable, providing you put every command you need to run to update your site into your fabfile.

With an automated deployment system in place we can now move on to looking a dependency, settings and database change management, but those are subjects for a future post.


Photo of robot invasion by donsolo.

About these ads

Written by Andrew Wilkinson

June 16, 2010 at 1:18 pm

2 Responses

Subscribe to comments with RSS.

  1. Never used fabric before. But from your article, it could be quite handy indeed. Looking forward to using this tool later :-)

    memo

    June 17, 2010 at 10:07 am

  2. I use fabric to deploy my website and it is really great.

    miniwebtool

    June 19, 2010 at 3:00 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 102 other followers

%d bloggers like this: