Loading...

The Lazy Developer's Guide To Application Deployment With Fabric

Emilian F. Published on 31 August, 2012

I want to introduce to you, Fabric -- no, not the swanky nightclub in London, but the Python library that is used for deploying applications and other system administration tasks. If you spend a lot of time modifying settings files, deploying EC2 instances, or doing any other mindless tasks over and over again then there is a good chance that Fabric can help you out.

If you are like me two years ago, then you like to SSH into your server, and just start pounding the command line. You create folders, you clone repos, you create virtual environments and skeleton home directories for your users and so on. You do that by justifying that it is only for this server. Or that the project is too small and it would not be worth the time to automate the process. I am here to tell you a different story. It is worth it to automate it, and Fabric can help you do it without spending too much time learning the ropes.

How Fabric Helps You

  • it is convenient: you no longer have to SSH into your servers to run commands. Just do it all from the comfort of your own command line
  • it saves time: type just one command to deploy your application. Compare that to typing dozens of commands just to switch directories, pull branches, and run tests
  • it makes the other guys look smart: you no longer have to explain a 50-step deployment process to your colleagues. Just send them the Fabric script and they can become deployment experts in a few minutes.
  • it standardizes your deployments: you no longer have to worry about forgetting a command, or restarting the server. All of that is done from a simple command.

Getting Started With Fabric

You have to install Fabric on your local machine (not the server!) and you can do that by using pip.

$ pip install fabric

If you do not have pip installed, then you can also install it using your system's package manager like so:

$ sudo apt-get install fabric

If you run into any errors during the installation process then just read the official Fabric installation guide which should clear things up.

Baby Steps With Fabric - Restart Apache

Now that you have installed Fabric, you can take the first steps in doing something useful. We will create a Fabric script that restarts Apache on one of your servers. To do that, you need to create a fabfile.py file in your current working directory. Add the following bit of code in the fabfile.py file you just created.

from fabric.api import *

env.hosts = ['user@example.com:22']

def restart_webserver():
    sudo('service apache2 restart')
Make sure to replace the user and example.com placeholders with the appropriate values for your server.

To run the command and restart the Apache webserver just type the following text in your command line (make sure that you are in the directory where the fabfile.py file is):

$ fab restart_webserver

This will run the "service apache2 restart" command on the server you just defined in the env.hosts variable. Useful? Definitely. What if you have three servers and need to run the command on each one of the servers? Just add each individual sever to the env.hosts variable like so:

from fabric.api import *

env.hosts = ['user@example.com:22','user2@example2.com:22', 'user3@example3.com:22']

def restart_webserver():
    sudo('service apache2 restart')

Now when you run the fab restart_webserver command it will run it on all three servers that you defined above (example.com, example2.com and example3.com). It is that simple.

Useful Fabric Tips

I have put together a bunch of tips to help you out when starting to learn Fabric. Read through them and keep them in mind when writing your first Fabric script. They will come in handy!

  • Fabric is a Python library, so you can use variable and string interpolation and all other Python constructs
  • Fabric is installed on your local machine, not on your servers
  • you can run local commands using the local() function
  • you can hold the directory state by using context managers
  • the cd() function runs remotely while lcd() runs locally

We have barely scratched the surface in regards to deploying applications with Fabric. Besides restarting the server you still need to update the repository, pull the changes from your repository, modify settings files and so on. We will touch on these issues in upcoming articles on Fabric. If you want to skip ahead, a good starting point is the official documentation which has a good introductory tutorial to Fabric.


Do you have any questions on using Fabric? If so, then write your question in the comment section below and we can help you out.

Emilian F. Published on 31 August, 2012