Painless deployment with Fabric

Deployment of code in test/staging/production  servers is one of the important part of modern web applications development cycle.

Deploying code were painful because its repetitive same tasks we have to do every time we want to push code, during deployment  if something goes wrong the application will go down too. But the scenario has changed, now we have many tools to make the deployment easier and fun. I have used Capistrano and Fabric for deployment. Found Fabric really painless and as its a Python battery, it was easier for me to adopt and get things done.

I am going to cover fundamental operations and finally a simple fabric script(like boilerplate) for writing your own fabric script.

env = its a Python dictionary like subclass where we define specific settings like password,user etc

local = runs command in  local host(where fabric script is being run)

run = runs command in a remote host

You can use these code tasks in many different ways, to do that check the Fabric Office Documentation from here.

from fabric.api import local, run, env, put
env.graceful = False
def test_server():
env.user = 'your_user_name'
env.serverpath = '/'
env.site_root = 'your_app_root'
env.password = 'your_pass' #ssh password for user
# env.key_filename = ''# specify server public key
#lis of hossts in env.hosts
env.hosts = [
'test.com',
]
#sample method for git pull
def pull(branch_name):
env.site_root = 'your_project_path'
run('cd %s && git pull origin %s' % (env.site_root, branch_name))
#deploy current directories all code without fabfile.py
def deploy():
env.files = '*'
env.site_name = 'your_app_name'
env.site_path = 'your_application_path'
run('rm -rf %s/%s' % (env.site_path,env.site_name))
local('zip -r %s.zip -x=fabfile.py %s' % (env.site_name, env.files))
put(env.site_name+'.zip',env.site_path)
run('cd %s && unzip %s.zip -d %s && rm %s.zip' % (env.site_root, \
env.site_name, env.site_name, env.site_name))
local('rm -rf %s.zip' % env.site_name)
#restart apache of remote host
def restart_apache():
cmd = "/usr/local/apache2/bin/apachectl -k graceful" if (env.graceful is True) \
else "service httpd restart"
run(cmd)
def latest_access_log():
cmd = "tail -n 10 /var/log/apache2/access.log"
run(cmd)
def latest_error_log():
cmd = "tail -n 10 /var/log/apache2/error.log"
run(cmd)

view raw
fabfile.py
hosted with ❤ by GitHub

#!/bin/bash
sudo apt-get install python-pip
sudo pip install fabric

view raw
ubuntu.sh
hosted with ❤ by GitHub

First gist is a sample fabric script,second one is a bash script to install fabric in your ubuntu machine.

 After setting username,password and host information into the script you cab check your server’s access log by running  fab test_server latest_access_log 

I am using fabric for around two years and used for different small,medium and large projects.

There are many interesting open source projects going on top of Fabric. I found these two projects really promising.

1.Fabtools

2.Graphite_fabric

Search through github,you will find many advance level Fabric use.

Happy Coding!

2 comments

  1. rubayeet · May 9, 2013

    Use rsync_project for syncing/uploading files to the remote server http://docs.fabfile.org/en/1.4.0/api/contrib/project.html?#fabric.contrib.project.rsync_project

  2. quickshiftin · October 21, 2013

    Nice primer! Checkout some of my thoughts on usage conventions around Fabric.

    http://quickshiftin.com/blog/2013/10/fabric-application-deployments/

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.