Quickly Restrict Amazon service to Single Country

Sometimes you need to restrict a service to only your country. Quickest approach is via Amazon security groups. Note that Amazon security groups accepts a maximum of 100 rules.


  1. Download a csv file with country IP block ranges from http://www.nirsoft.net e.g. http://www.nirsoft.net/countryip/ug.html
  2. With help of IPy and Summarize, you could use the python script below to summarize the IP blocks and generate Amazon CLI commands to add the security groups. Redirect output to script file you can run at ounce.
    from summarize import summarize
    import csv
    if __name__ == "__main__":    
        ifile  = open('ug.csv', "rb")
        reader = csv.reader(ifile)        
        for row in reader:
            if len(row) > 1:
                blockSummary = summarize(row[0],row[1])
                print 'aws ec2 authorize-security-group-ingress --group-id your_security_group_id --protocol tcp --port %s --cidr %s' % (80, blockSummary[0])
Categories: 1

N900 All telephony functions, including emergency calls, are disabled due to communication error. To recover, you might have to reboot the device

For the last two months I have been contemplating what to do with my N900 that started showing the error below:-

All telephony functions, including emergency calls, are disabled due to communication error. To recover, you might have to reboot the device.

When looking for a fix, placing a piece of paper above the simcard was my first option, but it didn’t work.

After giving up the one the phone, I was ready to try the risky approach of adding pressure to the GSM module as explained . Follow the instructions in the previous link and also make use of N900 Disassembly instructions.

To add pressure to the GSM modules, I also made of springs found in pens. Cut a total of four rings from the spring. Dip them in glue and place them on top of the modules as shown here.

My N900 is back to working again. Many thanks to ForeverYoung

My Bad: A Guest VirtualBox Machine on windows only shares internet on host via a proxy

Two months ago I took on a personal ambition to reduce my power bill. One of my culprits was to shutdown the home server that was a storage server, media streaming server, XBMC media center etc. It was also the internet gateway. I moved the entertainment functions to the MK802 device running android 4.0

The home devices:- phones, MK802 and PS3 had to continue to access the internet.

I chose my working laptop as the gateway sharing the 3G modem internet. The laptop runs windows 7 and it so happens that enabling ICS stops the VPN connections from working. This is a known issue. Windows Internet Connection Sharing (ICS) is not compatible with the VPN client. It produces errors such as:-

  1. The vpn client agent was unable to create the interprocess communication depot.
  2. Sometime it seems the tunnel is being established but terminated.

So turning off ICS fixes the error. Read more…

Categories: Linux, Networking, Windows

Django and SQLAlchemy session transaction errors

Issue experienced here is related to deploying a django application with SQLAlchemy as the ORM not the in-built Django ORM. Its my first time to deploy Django with a different ORM, in this case SQL Alchemy. The error faced here didn’t happen when running on the django dev server but in Multi-threaded environment (Using Gunicorn). My production environment is comprised of Gunicorn and Nginx on an Amazon micro instance running Ubuntu AMI. Reason for using SQLAlchemy is shared common lib between django web application, RabbitMQ workers and other python tools


The transaction is inactive due to a rollback in a subtransaction. On subsequent requests,
This Session’s transaction has been rolled back due to a previous exception during flush.

How I am using the session?

GLOBAL_ENGINE = get_engine()
SESSION_MAKER = scoped_session(sessionmaker(bind=GLOBAL_ENGINE))

def get_session():
    return SESSION_MAKER()

Why the error?

Quote from the SQLAlchemy site:-
This is an error that occurs when a flush() raises an exception, rolls back the transaction, but further commands upon the Session are called without an explicit call to rollback() or close().

How to fix:

Create a middleware class that commits and closes the session at the end of the request and on any exception
My middle ware class

class SqlAlchemySessionMiddleWare(object):    
    def process_response(self, request, response): 
            session = Repository.get_session()
        except Exception, err: 
        return response 

    def process_exception(self, request, exception): 
            session = Repository.get_session()
        except Exception, err: 

Register the middleware class in settings


Restart the web server.

Categories: Programming Tags: ,

Redgate Saves Me Again:- Source of Memory Leak Found

It won’t be the first, and for sure not the last. Redgate tool belt has a set of tools which come in handy when your at the edge of giving up. I have used the database refactor tool, to make nightmares go away, and schema migrations horrors are in the past. Have read of memory leaks on blogs but had yet faced the challenge of tracking one.

Over the last month, I have seen my windows service which starts and runs threads to handle different jobs increase in memory till 900MB in a period of two weeks. Restarting the service fixed the issue but i had failed to find the reason why.

I should say, I tried a couple of solutions including live debugging on the production server and my dev machine. Each day I tried a “trick” but zilch. Until it hit me, Redgate has a performance and memory profiler. I last used Redgate Performance profiler to hunt down beasts ages ago, in any case the tools are just click, wait, results and aaaaaahhhh ok, fix that. Performance profiler didn’t get me much. Ran the memory profiler for 3 hours taking snapshots every 30 minutes. Luckily by just looking at the results it was obvious who the culprit was. Googled LocalDataStoreElement, and the results where in my favor. In the code I was storing thread specific data using Thread.AllocateDataSlot etc, and from
http://support.microsoft.com/kb/2540745, the issue occurs because the Thread.AllocateDataSlot method in the .NET Framework 4 creates a memory leak. Sigh!!!. Now how to fix it?   Below I quote from the microsoft site:-

A supported hotfix is now available from Microsoft. However, it is intended to correct only the problem that this article describes. Apply it only to systems that are experiencing this specific problem.

To resolve this problem, contact Microsoft Customer Support Services to obtain the hotfix. For a complete list of Microsoft Customer Support Services telephone numbers and information about support costs, visit the following Microsoft website:

Note In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

Great that I have the code and I can apply the recommended fix for switching from Thread.AllocateDataSlot to ThreadLocal<T>. For minimal changes to code, I made use of ThreadLocal<IDictionary<string, object>>.  I do appreciate the memory profiler tool for working the charms, if it wasn’t for the scary price tag, I would purchase the tool after the trial ends.

Categories: Programming Tags:

Windows: Search for drivers on the internet. Has it ever worked for you?

The desktop I use home dual boots with windows 7 ultimate and Ubuntu. The windows 7 OS has not been in use and connected to the internet for about a year. I tend to spend a lot of time using Ubuntu. This week I needed to transfer a file from a Samsung android phone running version 2.2.1 to the windows host using its USB cable. To much surprise i reached no where with the nagging task bar popup device installation failed. Which when clicked leads to a window where you can search for drivers on the internet. This leads to to why I made this post.

Have you ever been successful with searching for drivers on internet with windows and it worked? Personally it has never worked for me. May be because I use a lot of cheap China devices and not genuine ones. As far as I remember, whenever I have tried this option it was no success. Even with some genuine peripherals. I got the phone to work after installing the utility tools with drivers on the CD that comes with the phone, I didn’t do this on the laptop also running windows 7 ultimate.

Am just curious, Have you ever got windows to search for drivers for your peripheral on the internet and it worked?

Categories: Windows

MSBuild And MVC Area Projects using Autofac as IoC

April 22, 2012 2 comments

I have a Web Project Folder structure MainMVCProject\Areas\MyArea in one of my projects. MyArea is a another MVC project. While setting up an MSBuild project, I realised that on building the main project using the task below, the contents of the child area MyArea where being copied to the parent MainMVCProject.

<MSBuild Projects=”$(WebFolder)\MainMVCProject.csproj”
OutDir=$(DeployFolder)\www\bin\;Configuration=$(Configuration)” />

At first it was not obvious why, but managed to figure it out. The cause was because I had added a reference of MyArea project to MainMVCProject. The reason in the first place of adding that reference was to simplify the registration of controllers in the MyArea child Area project assembly using Autofac. Removing the reference fixed the MSBuild output but still needed to register the controllers with the Autofac Container.

In my solution I use a plugin architecture used in Umbraco, details here. In combination with a TypeFinder, I was already using a DependencyRegistrar for registering types to the Autofac Container. So I just added an implementation of IDependencyRegistrar in the MyArea child project which registers the controllers in that assembly. In Global.asax Application start method I find all types that do implement IDependencyRegistrar and execute the register method passing the container.

This overall fixed resolving controllers at run-time.

Categories: Programming Tags: ,