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.

Steps:-

  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])
        ifile.close()
    
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

Errors

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): 
        try: 
            session = Repository.get_session()
            session.commit() 
            session.close() 
        except Exception, err: 
            pass
        return response 

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

Register the middleware class in settings

MIDDLEWARE_CLASSES = (
    ...
    'MiddleWare.SqlAlchemySessionMiddleWare'
)

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: