Thejesh GN

A Blog, A Website and A container for all my views with excerpts from technology, travel, films, india, photography, kannada, friends and other interests. I am Thejesh GN. Friends call me Thej

Deployment of PHP or Python Applications using Mercurial and Fabfile

Posted by Thejesh GN On February - 1 - 2012

Deployments are usually very painful. We generally write scripts to make it automated as much as possible. I wanted my method to be as easy as running single line on command prompt from anywhere in the world. I didn’t want to worry about anything. At last with some experimentation I have found my way. This blog talks about the deployment of php/python applications using mercurial as code repo and fabfile. Well you actually can use any scripting format instead of fabfile. But fabile makes it easy to log into a remote machine and perform tasks. Also the scripting language of fabfile is python. This gives a lot of flexibility to customize and I dont have to learn anything new.

This process is inspired by Heroku git deployment feature. This tutorial works with hg, git and mostly with any other DVCS with minor alteration. It has two major steps
STEP 1: On your *nix Server

  1. Install mercurial on your server – it should be easy
  2. Setup SSH access to mercurial repository
    Your server should be able to login to code repository and pull the latest code. Its easier to use SSH than passwords.
    On your server machine:

    1. Open terminal
    2. Enter ssh-keygen
    3. Give a name or you can use the default name id_rsa
    4. When it asks “Enter passphrase (empty for no passphrase):” press enter. No Password
    5. Once the key generation is complete. You can verify the same using ls -a ~/.ssh
    6. Add this new identity to SSH agent ssh-add ~/.ssh/id_rsa
    7. Now we need to add this public key to bitbucket or any other provider cat ~/.ssh/ Copy the output
    8. For bitbucket, go to account -> SSH keys, add the above output to your ssh keys

    Now your server is set to access your repositories with out the need of password.

  3. Now we need to setup the Hg repository inside web accessible directory. For example, your web accessible folder could be
    or /var/www/html
    or in case of phython it can be anywhere /home/user_home/my_project_code
  4. To deploy php application tweet4blood, clone the repo inside the directory using ssh url

    cd /home/user_home/public_html
    hg clone ssh://
  5. Make sure to make the .hg folder (actual repository) inaccessible to the webserver either by .htaccess rule or changing the permissions etc

STEP 2: On your desktop

  1. Install fabfile or fabric. On ubuntu search for fabric in Synaptic Package Manager
  2. Create your fabfile, you can find the latest version of below example fabfile in my snippets project
  3. To call any method in fabfile

    $ cd /home/thej/my_deployment_scripts/tweet4blood/
    $ fab hello
  4. Fabric can also chain the calls

    $ cd /home/thej/my_deployment_scripts/tweet4blood/
    $ fab test hello

    Here it calls the test method first which sets the env variables and then calls hello
  5. None of the env variables are necessary but providing env.user, env.hosts, env.password will avoid typing them everytime
  6. BTW env.user, env.hosts, env.password are that of SERVER machine
  7. To deploy the latest version to test

    $ cd /home/thej/my_deployment_scripts/tweet4blood/
    $ fab test deploy:tip

    In this case,

    • test method sets the env variables corresponding to TEST env
    • test method also sets application env specific consumer_key which later we will use to setup config.php, similarly you can use define databas_name, database_user_name etc
    • then as per chain deploy method is called with input variable version whose value now is “tip”
    • inside deploy the first call is cd (change directory) on remote server
    • at this point fab logs into the remote server using env.user, env.hosts, env.password
    • then control goes to the repo directory
    • runs hg pull which gets everything from bitbucket
    • runs hg update -C tip which is clean update to “tip” version
    • then CDs into auth folder
    • uses the Linux sid command to replace the env specific values in config.php
  8. To deploy any other version to test. I usually tag the versions, so I will pass the tag name

    $ cd /home/thej/my_deployment_scripts/tweet4blood/
    $ fab test deploy:v.0.1.0
  9. In case you want to shutdown the apache before the deployment and restart later, you can chain them too
    $ cd /home/thej/my_deployment_scripts/tweet4blood/
    $ fab test apache_stop deploy:tip apache_start

STEP 3: Go deploy
Below I have embedded a rough version of for quick refrence. But as I told you can find the latest version of the same in my snippets.

Questions and suggestions are welcome.
Read the rest of this entry »

hg4subl – Sublime Text plugin for Mercurial

Posted by Thejesh GN On December - 26 - 2011

I love Sublime Text and Mercurial. All most all my projects use these two tools. Clearly I need a tighter integration between these two. Sublime Texts provides Python based plugin API architecture. Its quite easy to follow and write a plugin. So I took sublime-text-2-git and modified it to work with Mercurial. I have added a few extra features and intend to develop in future.

Now I can work with my repository from inside Sublime Text.

Its easy to install:

  • On Linux go to command prompt
  • cd ~/.config/sublime-text-2/Packages
  • hg clone
  • Restart you Sublime Text

Now your tools menu should have Hg and related menus. Some of the menu’s are enabled only when you are inside a repo work folder. I have tested it only on Linux. If you are a windows user, let me know how it goes.
You can also use Ctrl+Shift+P to quickly access the commands.
hg4subl options

hg4subl show logs

The bottom panel is used to display the notifications to users. For example branch change in the working project

The plugin also can update itself. Go to Tools-Hg->hg4subl update. The notification panel show the information about the update

You can fork the project on bitbucket-hg4subl and send me pull requests if you have any.

Get in touch