Reason #4 on why Django is great

Middleware!

I can’t tell you how great middleware is in Django. It makes life so easy! Here is a good example. Django doesn’t handle ajax errors very good. It prints them to the client. Sure I can use firebug but I mainly use chrome now and don’t like to touch firefox anymore. So I wrote a bit of middleware to only print out errors in all ajax requests if the app was in DEBUG mode.


from django.conf import settings

import traceback

class AjaxErrorMiddleware:
    def process_exception(self, request, exception):
        if request.META.has_key('HTTP_X_REQUESTED_WITH') and settings.DEBUG:
            print traceback.format_exc() 

I have added this to my Github project also for all my middleware. You just load it by adding it to your middleware section in your settings.py

Reason #3 on why Django is great

Making custom model fields are a great thing to save a lot of time. If you need a lot of custom validation around your modelforms and don’t want to do a lot of copy and pasting then create a custom model field.

For example, I have the need for a user to input a lot of hostnames and domains and got sick of doing custom validation in each model form I created. So I created a model field for it.

So here it is


class HostnameField(models.CharField):
    def clean(self, value):
        value = super(HostnameField, self).clean(value)
        
        import re
        regex = re.compile("^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$")
        r = regex.search(string)
        if len(r.groups()) == 0:
            raise models.ValidationError("You need to enter a valid hostname/domain")
         
        return value

The regex might not be the best, but it seems to cover all the use cases I tried on it.

Install all needed RPMS for a rpmbuild

Ever had this happen?

[mzupan@laptop SPECS]$ rpmbuild -ba php.spec 
cat: /usr/include/httpd/.mmn: No such file or directory
error: Failed build dependencies:
        curl-devel >= 7.9 is needed by php-5.2.11-2.fc12.src
        db4-devel is needed by php-5.2.11-2.fc12.src
        gmp-devel is needed by php-5.2.11-2.fc12.src
        expat-devel is needed by php-5.2.11-2.fc12.src
        httpd-devel >= 2.0.46-1 is needed by php-5.2.11-2.fc12.src
        sqlite-devel >= 3.0.0 is needed by php-5.2.11-2.fc12.src
        readline-devel is needed by php-5.2.11-2.fc12.src
        libc-client-devel is needed by php-5.2.11-2.fc12.src
        postgresql-devel is needed by php-5.2.11-2.fc12.src
        unixODBC-devel is needed by php-5.2.11-2.fc12.src
        net-snmp-devel is needed by php-5.2.11-2.fc12.src
        libxslt-devel >= 1.0.18-1 is needed by php-5.2.11-2.fc12.src
        libXpm-devel is needed by php-5.2.11-2.fc12.src
        t1lib-devel is needed by php-5.2.11-2.fc12.src
        libmcrypt-devel is needed by php-5.2.11-2.fc12.src
        mhash-devel is needed by php-5.2.11-2.fc12.src
        libtidy-devel is needed by php-5.2.11-2.fc12.src
        freetds-devel is needed by php-5.2.11-2.fc12.src
        aspell-devel >= 0.50.0 is needed by php-5.2.11-2.fc12.src
        recode-devel is needed by php-5.2.11-2.fc12.src
        libevent-devel >= 1.4.12 is needed by php-5.2.11-2.fc12.src

Well the boring way is doing this

yum -y install libevent-devel recode-devel aspell-devel ...

That can be extremely painful. So lets do this


sudo yum install `rpmbuild -ba php.spec 2>&1 | grep needed | awk {' print $1 '}`

A better Jira init.d script for Redhat/CentOS

I wrote a bit better init.d script for Jira for Redhat/CentOS

The first thing you want to do is create a sysconfig file. Below is the filename

/etc/sysconfig/jira

The file has the following contents in it

#
# The user Jira runs as
#
JIRA_USER=jira

#
# The home directory of Jira
#
JIRA_HOME=/opt/jira/current

Now create the init.d script

/etc/init.d/jira

It has the following contents in it.

#!/bin/sh
#
# JIRA startup script
#
# chkconfig: 2345 80 05
# description: JIRA

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/jira ]; then
    . /etc/sysconfig/jira
fi

prog=jira
RETVAL=0

start() {
        echo -n $"Starting $prog: "
        /bin/su -m $JIRA_USER -c "cd $JIRA_HOME/logs && $JIRA_HOME/bin/startup.sh &> /dev/null"
        RETVAL=$?
        if [ $RETVAL = 0 ]
        then
                echo $! > /var/run/jira.pid
                echo_success
        else
                echo_failure
        fi

        echo

        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        /bin/su -m $JIRA_USER -c "$JIRA_HOME/bin/shutdown.sh &> /dev/null"
        RETVAL=$?
        if [ $RETVAL = 0 ]
        then
                rm -f /var/run/jira.pid
                echo_success
        else
                echo_failure
        fi
        echo

        return $RETVAL
}

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                stop
                sleep 5
                start
                ;;
        *)
                echo $"Usage: /etc/init.d/$prog {start|restart|stop}"
                exit 1
                ;;
esac

exit $RETVAL

Now make permissions correct

chmod +x /etc/init.d/jira

Now you can chkconfig it

chkconfig jira on

This will make it look a bit more Redhat/CentOS like.

Init.d script for puppet-dashboard

In the #puppet IRC channel on freenode a user was asking if anyone knew of a init.d script for redhat/fedora for puppet-dashboard. I have a blog post about installing puppet-dashboard on Redhat/CentOS and I don’t think there is any init.d script out there. So time to write one

The first thing you will create is the following file

/etc/sysconfig/puppet-dashboard

This is a simple file with the following in it

#
# path to where you installed puppet dashboard
#
DASHBOARD_HOME=/opt/puppet-dashboard

You probably need to change the DASHBOARD_HOME variable to match what you have. Its the root of the project.

Now for the init.d script. Create the file called

/etc/init.d/puppet-dashboard

With the following contents in it.

#!/bin/bash
#
# Init script for puppet-dashboard
#
# chkconfig: - 85 15
# description: Init script for puppet-dashboard

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/puppet-dashboard ]; then
    . /etc/sysconfig/puppet-dashboard
fi

prog=puppet-dashboard
RETVAL=0

start() {
        echo -n $"Starting $prog: "
        /usr/bin/ruby ${DASHBOARD_HOME}/script/server >/dev/null 2>&1 &
        RETVAL=$?
        if [ $RETVAL = 0 ]
        then
            echo $! > /var/run/puppet-dashboard.pid
            echo_success
        else
            echo_failure
        fi 

        echo

        return $RETVAL
}
stop() {
        echo -n $"Stopping $prog: "
        killproc puppet-dashboard
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/run/puppet-dashboard.pid
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo $"Usage: $prog {start|stop|restart}"
        exit 1
esac

exit $RETVAL

This might not be perfect but it works nicely

SSH Trick

To start off with.. I know you can do this in DNS using a search parameter but I don’t want to do that in my envirnoment.

Sometimes you are working with a hostname that is long due to the domain name. This is the case where I am currently working, the domain is theopenskyproject.com. So I did the following and added into my ~/.bashrc

function ssh() { /usr/bin/ssh $1.theopenskyproject.com; }

You can now do the following

ssh node1
ssh user@node1

Right now its pretty simple. Flags won’t work with ssh. If you want you can make it more shorthand by calling the function s