“unable to resolve host” in Amazon EC2

This weekend I spent some time moving my Classic EC2 instances to VPC, changing their zones to keep the redundant etc… After that, I was getting a very annoying message every time that I used the “sudo” command.

unable to resolve host xxx.xxx.xxx.xxx

I solved it just adding my new hostname to the hosts files. In a trick way you can do that just running the following command:

echo "127.0.0.1 $(hostname)" >> /etc/hosts

Cheers!

Detecting string charset in Python

echo “öasd” | iconv -t ISO-8859-1 | python -c ‘import chardet,sys; print chardet.detect(sys.stdin.read())’
{‘confidence’: 0.5, ‘encoding’: ‘windows-1252’}

from: http://superuser.com/questions/301552/how-to-auto-detect-text-file-encoding

Finding Large Files

Today I was looking for a parameter that could help me to find large files. Fortunately, Google browsed me to a very good post (http://linuxlookup.com/howto/find_all_large_files_linux_system).

In a Brief:
Simple way:

find ~ -size +20M

Nice Way:

find / -type f -size +20000k -exec ls -lh {} \; 2> /dev/null | awk '{ print $NF ": " $5 }' | sort -nrk 2,2

Or…. If you want find large directories

du -hsx * | sort -rh | head -10

Creating an USB boot drive

I was trying to use Startup disk creator to create a bootable linux distro.. But a strange error message was being displayed: An uncaught exception was raised: Invalid version string ‘GNU/Linux’ the solution was to run dd directly from the terminal:

sudo dd bs=4M if=filaname.iso of=/dev/sdx && sync

Route 53 + CloudFront + Load Balancer + EC2 = Entire Site Cached!

Yeaph, I spent all the weekend figuring out how to Cache my entire site in CloudFront. I had really a lot of issues until understand the correct configuration to do things work well.

I this approach it’s not necessary install any cache plugin (ie W3 Total Cache) in WordPress.

Here is the current scenario:

  1. Route 53 with A alias pointing to CloudFront (for my naked domain and www)
  2. CloudFront with Origin Domain Name = load balancer DNS name, also the cname are configured to naked domain and www
  3. Load Balancer with EC2
  4. EC2 with several sites (I just want use CF in one of them)

 

All changes that did was in CloudFront configuration, no changes in the application or server were necessary.

I took a couple of screenshots to show exactly how is the configuration in CF.

CloudFront General Information

CloudFront General Information

Origin Settings in CloudFront

Origin Settings in CloudFront

The trick is in the Behaviors options. First, in changed a little but the Default to:

Edit Behavior CloudFront

Edit Behavior CloudFront

You can see that I did a couple of changes from the defaults settings, but the most important here was set the Forward Headers to Whitelist and add Host in the Header, without the CloudFront was caching my default index.html from Apache, the reason of that is because the CloudFront was hitting the LoadBalancer URL, so a default index was being shown.

Another trick, is to try to avoid admin pages to being cached. So Added:

Behaviors CloudFront

Behaviors

Cache Behavior Settings CloudFront

Cache Behavior Settings CloudFront

 

 

 

 

Well, now things seems work fine!

 

Developer Tool - Hitting cloudfront

Developer Tool – Hitting cloudfront

Just quick note: In this way, all changes that you do in your site will be take effect just 24 hours after publish. Or, you need “invalidate” objects in CF.

How much time my code takes?

A very short code (in Python) to check how much time some function takes to be executed:

import time
start_time = time.time()
main()
print time.time() - start_time, "seconds"

Python – Dojo – Bissextile Year

This week we are starting to do Dojo’s session in the company that I am working. To do a little warm up the first Dojo Exercise was to identify when a year is Bissextile (see http://en.wikipedia.org/wiki/Leap_year).

bissexto-test.py

import unittest
from bissexto import bissexto

class bissexto_test(unittest.TestCase):
    def test_bissexto(self):
        self.assertEqual(bissexto(1600),True)
        self.assertEqual(bissexto(1732),True)
        self.assertEqual(bissexto(1888),True)
        self.assertEqual(bissexto(1944),True)
        self.assertEqual(bissexto(2008),True)

    def test_nao_bissexto(self):
        self.assertEqual(bissexto(1742),False)
        self.assertEqual(bissexto(1889),False)
        self.assertEqual(bissexto(1951),False)
        self.assertEqual(bissexto(2011),False)

if __name__ == "__main__":
    unittest.main()

 
bissexto.py

# -*- coding: utf-8 -*-

def bissexto(year):
    if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
        return True
    else:
        return False

if __name__ == "__main__":
    bissexto(2000)

 

 

 

Comparing directories using diff

As I said in last post, Meld is incredible, beautiful and easy to use. But sometimes we need compare directories very quick or just create an automation to analyze the output. In this way we can use the very old command “diff”

 

diff -rq dir1/ dir2/

 

Where: (from diff manual)

-q, –brief
report only when files differ

-n, –rcs
output an RCS format diff

Meld and Nautilus on Ubuntu

I guess that don’t have a single day that I don’t need to compare codes or directories files… Linux have a great software to do that, it’s called meld. But sometimes is annoying to enter the software and then select the files/directory that you want to compare.

So quick way to do that is just to install nautilus-compare. Then, you will be able to compare just using nautilus 😉

 

sudo apt-get install meld nautilus-compare
nautilus -q

 

Multiple Python Version on the same computer

In the place that I work we have some legacy servers with really old packages installed. I did a Python script using version 2.7.5 and when I finished my development I figured out that the server was using version 2.4 (what was launch in 2004!). So… To test it before move to production, I needed install this old version. To do that I just:

To install it (per the instructions in the link above), you do the following:

steve@ubuntu64 ~ $ sudo add-apt-repository ppa:fkrull/deadsnakes

Then you need to update your cache:

steve@ubuntu64 ~ $ sudo apt-get update

Finally, simply install the other versions (I'm running on ubuntu 12.04 LTS, so I have python 2.7 already):

steve@ubuntu64 ~ $ sudo apt-get install python2.4 python2.5 python2.6

(Check details on: http://bhfsteve.blogspot.com.br/2012/05/run-multiple-python-versions-on-your.html)

After install it I removed the repository:

sudo add-apt-repository ppa:fkrull/deadsnakes --remove

To execute the old version just type:
python2.4