Archive for the Programming Category

Installing Go on Ubuntu

Posted in How-To, Linux, Programming, Sys Admin, Ubuntu with tags , , , , on November 13, 2009 by rubayeet

When Google announced their newly developed system programming language Go, I decided to give it a try on my Ubuntu machine, the Mac would have to wait. The installation was supposed to be a walk in the park, but it wasn’t. I realized I didn’t have some other essential tools installed, and encountered one problem after another, frantically googling for solutions. So in this post I’ll detail the step by step process to install and run Go on your Ubuntu machine.

Step 1: Setting Up Environment Variables

Prior to installation you need to have these environment variables set up:

$GOROOT – The root of the Go tree. Typically this is $HOME/go but it can be any directory.

$GOOS – Name of the target operating system. Since this is Ubuntu the value would be ‘linux’

$GOARCH – Name of the target compilation architecture.  If you’re on a 32 bit x86 machine this would be ‘386′. Check out this section in Go online docs for other options.

$GOBIN – The location where binaries will be installed. You can set this to $HOME/bin or whatever path of your choice. Then you have to add this to your $PATH so that newly built Go-specific command such as the compiler can be found during the build.

Fire up the terminal and type the following command

sudo vi /etc/profile

This’ll open up the system-wide bash profile file in vi editor. You need to add the following lines at the end of the file.

export GOROOT=$HOME/go
export GOOS=linux
export GOARCH=386
export GOBIN=$HOME/bin

Now add $GOBIN to your $PATH. Open .bashrc in your $HOME directory

sudo vi $HOME/.bashrc

and add the line

export PATH=${PATH}:$GOBIN

Restart the machine for these changes to take effect.
Reload the files for the changes to take effect(thanks peter vahlu)

source /etc/profile
source ~/.bashrc

Step 2: Install Mercurial and clone Go repository

Google uses Mercurial to store Go source code, so you have to install it and fetch the repository.

Since 1.0, Mercurial has been installable by easy-install. So you need to get the python setuptools, header files and other essential  tools installed  first.

sudo apt-get install python-setuptools python-dev build-essential

Now install Mecurial

sudo easy_install -U mercurial

This part was quite confusing. Mercurial was throwing up some errors when I tried to clone Go’s repo.

*** failed to import extension hgext.hbisect: No module named hbisect

The extension bisect is a built-in command since version 1.0 and so should not be used. If you get this error, open the configuration file(/etc/mercurial/hgrc.d/hgext) and remove/comment out the line hbisect=

Make sure the $GOROOT directory does not exist or is empty. Then check out the repository:

hg clone -r release https://go.googlecode.com/hg/ $GOROOT

Step 3: Build Go from source

The Go tool chain is written in C. To build it, you need to have GCC, the standard C libraries, the parser generator Bison, make and the text editor ed installed.

sudo apt-get install bison gcc libc6-dev ed make

Next, build Go from source

cd $GOROOT/src

./all.bash

If all.bash runs without trouble, it’ll finish by printing

--- cd ../test

N known bugs; 0 unexpected bugs

Where N is the number of bugs, changes from release to release.

You now have GO installed on your Ubuntu. Happy coding!

Further Reading

  1. The installation page at Go website.
  2. Article on installing Mercurial on Ubuntu and dealing with its tantrums.

Django: How to make a variable available in all templates

Posted in Django, How-To, Programming, Python with tags , on October 31, 2009 by rubayeet

Sometimes when building a web application with Django, you have a common piece of information that is available at all the templates. Foe example, you may have a dynamically built tree menu appearing in multiple templates.  It’s possible to achieve so by adding the data to the context of each template. But that goes against Django’s policy on code reuse. The right way to do that, is to use template context processors and ReuestContext objects.

The sections on Django official documentation are either too short or under misleading headings. So I’m trying to write this in a paint-by-numbers sort of way.

First, the template

Let’s suppose you have the following silly piece of HTML in each of your templates

Hello World! My name is {{name}}

Next, create a custom context processor

Under the directory of your Django application create a new file. Let’s name it ‘custom_context_processors.py’. This is your custom context processor, that will list a number of methods, each of which will have a HttpRequest object as parameter. Suppose you want to have a variable named ‘domain’ available at all template. Let’s add a method to the processor by that name:

define name(request):
   return {'name': 'Django Guru'}

Install the Context Processor

Open settings.py. Add the following line in the TEMPLATE_CONTEXT_PROCESSORS

'myapp.custom_context_processors.domain,'

Add the RequestContext

In your views.py, import the RequestContext module

from django.templates import RequestContext

Now when you render the template using render_to_response() method, a RequestContext object has to be added as the optional context_instance argument. The RequestContext object takes a HttpRequest object as a parameter

def my_view(request):
   #view code
   return redner_to_response('my_template.html', {'foo:bar'}, \
                              context_instance=RequestContext(request))

The variable {{name}} will now be available in my_template.html, without explicitly adding it in the template context. If you need to make it available in some_other_template.html, all you need to do is to pass the RequestContext object as the third parameter to render_to_reponse().

Python equivalent of PHP’s ip2long()

Posted in How-To, Programming, Python with tags , , , , on October 23, 2009 by rubayeet

I’ve been spoiled by PHP! It sometimes makes your job too easy with its large collection of library functions. Python, on the other hand, has a lot of powerful tools for doing low-level stuff. To get something done, which would take a single function call in PHP, you may have to meld together a number of those tools and build a new one.

I was looking for a Python equivalent of PHP’s ip2long() function. which converts an IPv4 address from dotted decimal notation(for example 208.69.34.231) to a 32 bit integer(3494191847). After spending few minutes with Google, I realized that Python has no such function in it’s core or standard library. To achieve this, I had to make use of Python’s socket interface and struct library. Here’s what I did:

from socket import inet_aton
from sturct import unpack

def ip2long(ip_addr):
...ip_packed = inet_aton(ip_addr)
...ip = unpack("!L", ip_packed)[0]
...return ip

Note: Dots(.) in the above code represents indentation. WordPress is eating up the whitespace mysteriously!

The first line inside the ip2long() method, the inet_aton() function, converts the dotted-quad IP address to a 32-bit packed binary format, which is string of four characters in length. To make it an integer, you have to unpack it with the unpack() method, which takes the format as the first argument(which is “!L” in this case, for big-endian unsigned integer) and the packed string as the second. It returns a tuple with  the 32-bit integer as the first element.

To make a long2ip() function you can just reverse engineer the above process with struct.pack and and socket.inet_ntoa.

Deploying Rails Application on Apache with Phusion Passenger

Posted in How-To, Linux, Rails, Sys Admin with tags , , , , on September 3, 2009 by rubayeet

facebookdc

This morning I put on my system admin hat at work once again. The challenge was to setup a Rails development environment on our production server at the cloud and then deploy a Rails application on Apache.

This article is not really a tutorial, although it’s posted under How-to category. It’s more like a log of my actions in carrying out this task; the problems I faced and what I did to get around them, so that I can trace them back if I ever partake a similar task in future.

Rails have several deployment options and Phusion Passenger a.k.a. Mod Rails is the principle of them. It supports both Apache and the lightweight Nginx Server. Check out there documentation page.

Here’s how it went

Logged into the remote server(CentOS 5.0) using ssh

Installed Rails

gem install -v=2.2.2 rails

Installed MySQL gem

gem install mysql — –with-mysql-include=/usr/include/mysql –with-mysql-lib=/usr/lib/mysql/

Installed Phusion Passenger

gem install passenger


Warning received. Required Software missing

Apache 2 development headers… not found

Installed Apache 2 Development Headers

yum install httpd-devel

Installed Apache module for Passenger

passenger-install-apache2-module

Configured Apache 2 to load the mod_passenger module by adding these lines in /etc/httpd/conf/httpd.conf

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6
PassengerRuby /usr/bin/ruby

Uploaded a sample Rails App(myrailsapp) to /home

Added following lines in /etc/httpd/conf/httpd.conf to create a new virtual host

<VirtualHost *:80>
ServerName rails.myserver.com
DocumentRoot /home/myrailsapp/public
RailsEnv    development
</VirtualHost>

Restart Apache

service httpd restart

Went to http://rails.myserver.com.

Standard Error page for Phusion Passenger displayed. Instructed to consult Apache error log

Checked out the Apache Error Log

cat /etc/httpd/logs/error_log

The last error was

Rails requires RubyGems >= 1.3.1 (you have 1.2.0). Please `gem update –system` and try again.

Updated RubyGems

gem install rubygems-update
update_rubygems

Restarted Apache and went to http://rails.myserver.com again. The following error was showing on the page

no such file to load — sqlite3

Reason: SQLite3-Ruby Gem missing. Tried to install it

gem install sqlite3-ruby

Failed. The system doesn’t have SQLite3 installed

checking for sqlite3.h… no

Installed SQLite3 by building in from source

wget http://www.sqlite.org/sqlite-amalgamation-3.6.17.tar.gz
tar xvzf sqlite-amalgamation-3.6.17.tar.gz
cd sqlite-3.6.17
./configure
make
make install

Installed SQLite3-Ruby Gem

gem install sqlite3-ruby

Restarted Apache and visited http://rails.myserver.com

SUCCESS!