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

Archive for the ‘Technology’ Category

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/id_rsa.pub. 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
    /home/user_home/public_html
    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://hg@bitbucket.org/thejeshgn/tweet4blood tweet4blood.com
  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 fabfile.py 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 »

Audio Boooks and Pain of DRM

Posted by Thejesh GN On January - 19 - 2012

Since last december I have been listening to books than reading. It makes my daily commute enjoyable. The last three books have been Steve Jobs, Art of Inception and Ghost in the Wires. That’s three books in one and half months, not bad. I used to listen to lot of podcasts while driving. I have reduced podcast listening and shifted to audio books.

I have a platinum account on Audible and I get 2 books for month in that plan. It works great except its DRMed and wont work on Linux. I need to figure out a way to export the audio so I can save them for later use. Audible allows backing up to the CDs through. CDs for heaven’s sake, who uses CDs anymore for backing up? And itunes is a PITA.

I am looking for good software which can undrm the books for me for my own personal usage. I found this list of tools but not sure yet. I would love to pay for the software. HELP.

This such a good example of DRM troubling god fearing, paying and non-pirate customer. All I want to do is backup what I have bought and it shouldnt be such a pain to a paid customer.

Why I oppose SOPA and PIPA ?

Posted by Thejesh GN On January - 18 - 2012

My reasons:

  1. SOPA and PIPA are the worst laws that anybody can propose to stop piracy
  2. They are actually anti-technology than anti-piracy
  3. They break internet
  4. Even though I am an India, It hurts me and my business
  5. Internet is supposed to be international, not yet, it’s mostly controlled by USA
  6. India has an habit of borrowing worst laws
  7. In India we already have stupid lawyers and politicians who don’t get technology. I don’t want US laws to be an example for Indian law makers

Action:

  1. If you are in USA, then call or mail your senator
  2. Not in USA then petition the state department

MySwar is out

Posted by Thejesh GN On January - 10 - 2012

MySwar is the complete guide to Hindi film music. Ever since I joined Mavrix, friends have asked me about the project that I am working on. I have given different versions. But now that MySwar is open to public, here is the almost definitive description “MySwar is the complete guide to Hindi film music. It lets you browse your favorite Hindi film albums and songs and get a treasure trove of information about them. It’s also got a great music recommendation feature. Rate songs you like and we’ll tell you what other songs you might like.”

As of now we have details of 4000+ albums, 22000+ songs and 3200+ artists. We have covered only hind movies from 1971 to 2011. I know, I know there are lots more to do. We need do 50s, 60s hindi films and we also need to do non film music. Well all I can say is its in progress.

Two best parts of the sites are Similar Songs suggestion using our internal recipe for each song. The recipe is not there yet. We are improving it almost on day today basis. But its worth showing off. Have a look and let me know what you feel.

MySwar Similar Songs

MySwar Similar Songs

Next one is discover based on your likes and ratings. Rate the songs you like and MySwar will suggest you songs based on it. Again discover algorithm gets updated almost on daily basis.

Discover music on MySwar

Discover music on MySwar

It’s a standard LAMP stack, python and MongoDB in this case. I will probably write a detailed post on the technology and platform choices one day. Until then go ahead register for an account and enjoy. Of course suggestions are more than welcome.

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 http://code.thejeshgn.com/hg4subl
  • 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
update

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