Monday, May 20, 2013

A Run for World without Leprosy - Run to remember

And My First 10K Marathon [TCS World 10K] - A run for world without leprosy

Was yet another usual day to work post my work out session @Music&Metals. Reached my workstation and booted my ubuntu to start with the proceedings of the day. The moment I logged in to my user, my mouse pointer rushed towards the Chrome Icon in unity launcher. Guess what? It's just to see if there are any updates in my FB home. And there it was, nice red color icon over the world of FB. It was a post from Dr. Manimozhi Natarajan, in our Gym's [Music & Metals] closed group. Urged to read what it is about, and it was an invite to all fiddles in M&M to participate in a 10K Marathon for a cause which was about to happen two week from then. And there you go.. I was realizing my wish to go for a long distance run coming true. And a pause [Ooops].

In my previous trip to our Mumbai Dev Center, I got to know that 4 of our team mates are participating in a 4*5Km relay and they are practicing for weeks to do well on the race day. I being with no practice in running and willing to complete 10K with ease?? Got to be kidding!! What next? I need practice!!, that too without a break [13 more days and you are at the start line]. I know me well, I'll give up very soon on things like these [after all I'm a human with laziness]. So, I badly wanted a partner who can give me a push whenever I get that feeling to stop.

And there he is!! here comes my savior - Durga Prasad. My one and only companion in Bangalore team who also is interested in stuffs like this. I didn't resist to ask him for his wish. And he accepted without taking a moment to think. Just in 10 min after entering the office we got to leave to submit our application to AMICI [AIFO India's Center] and confirm our participation to Dr. Manimozhi. Dr. was so excited that he got such a quick response from his gym pals and it was like attending Kaun Banega Crorepati while filling down our application form.

Dr. : Are you sure you wanna run 10K
We : Yes
Dr. : Odiduveengla pa? [Will you be able to finish?]
We : Yes
Dr. : Kashtamachae.. Idhukku munnadi odirukeengla? [Difficult it is. Have you got some experience?]
We : No, but we'll practice
Dr. : So, I'm locking yes.
We : Yes
Dr. : Computer Ji Lock Yes :P

The next tough moment was, when they didn't accept the registration fee from us. They said that the organization will sponsor us. Oops now we have no other go. We didn't want to feel guilty for making someone pay our fee and in response we fail to make the finish line. So, the responsibility had been kept heavy on our shoulders.

The next morning @5:30 my wake up alarm rang. I didn't wait for that 5 more min after an alarm, which gives you the best sleep in your life. Woke up immediately, prepared myself for the first day run. Meeting at Durga's place [near Leela palace] and completing the onward journey to inner ring road [3.76 Km] was the plan for day 1. Everything went fine till we realized that its time to start. We started our longest run till then [3.76Km, at least in my life]. We had to stop at 2 places just because we couldn't continue running. Terrified now. 12 more days, 7 more Kms without stop [Oops !!!]. We started preparing our mind now that we could make it. We just took the positives that happened on that day to get over the fear of failure :)
Day 2: Same plan. 3.76 Km. Very glad news. We didn't stop :) Was so happy on that day. Our mind accepted the challenge and it was ready.
Day 3: 4Km. Successful, No stop.
Day 4: 6 Km with no stops but walking on every uphill [Good enough in first try] and we made it.
Day 5 to 8: Thanks to CSK Match & weekends :P Both of us had plan to visit our home town and we had no other go, but to discontinue our practice [I continued my workouts, on Day 5 & 6]. Anyways Durga managed to continue it at his home town [Good job].
Day 9: 7.8 Km no stop [300m of walking alone]. I was not in this world when we reached that Domlur Flyover without a stop on our run towards home. We never believed that we would be able to make it.
Day 10: Once again 7.8Km with no stop. Target achieved.
Day 11: Drop in plan as we were very tired of 2 days of long distance running.
Day 12: Just 4Km but fast running. Did at a better pace. But learnt what not to do on the race day [Don't start fast, if so you'll want to stop fast].
Day 13: Rest for the race day.
Day 14: I reached the finish line in 1hr 2Min and my partner finished in 1hr 4min. Oh my goodness!!! We got medals :) Felt so happy that we made it and our practice made our lives easier.

Vote of Thanks:
1. AIFO India [AMICI]: For giving me an opportunity to make our run meaningful and trusting us on our words :) Have decided to represent them in further marathons I might run in my life :) [Too Sentimental huh? :P] Also for the Reebok Tee I got on our pre-event gathering :P [Mr. Jose, Mr. Patrick, Dr. Mani, Mr. Jayanth, Dr. Prasad, Mr. Nyjil and other members of AIFO India]
2. Dr. Manimozhi: Everything just happened was just because of him :) Will accompany you in your dream 10K run :P
3. Jibin Kumar: For Music Metals and making the event memorable with a good bunch [10 - 5.7K runners] of participants. Anddd for the Briyani treat [with Supra] to celebrate the success @nandhini ;) :)

And Some Happy Moments:




This post might be tooooo much for just a marathon finish.. But First - always is special and it is good to register the moment :)
If your mind believes, your body assists with an ease :) Push.. Push.. Push..
Our Next Target: On June 9th..

Wednesday, February 6, 2013

Headless Chrome/Firefox Selenium Java in Amazon EC2

Setting up chrome/firefox in linux based x86_64 EC2 instance
In this post we'll see how to setup chrome/firefox in EC2 instance.
Further we'll discuss the way to setup chromedriver with selenium [java] in EC2 instance aswell.
Why is that difficult?
Linux based EC2 instances lack gtk+, which is a must to launch any GUI enabled applications.

How to solve?
Compile gtk+ from source.
This gist by joekiller has the complete dependency tree resolved for installing gtk+ for x86_64 machines.
Line 77 Basically installs firefox from its tarball. You can comment it out incase if you don't wish to.
Incase Line 42 fails. Do a wget for direct tarball from here, instead the complex recursive wget.
For Ex:
wget http://releases.mozilla.org/pub/mozilla.org/firefox/releases/latest/linux-x86_64/en-US/firefox-18.0.2.tar.bz2
So, All done?
Nopes, Now we can't run either of the browsers as we lack X11 server which does graphics operations & screen outputing.
As our environment is headless we can't go for real X11 server as well [lack of hardware units to do screen outputing].

How do we solve this?
We need Xvfb setup in our instance.
$ yum install Xvfb
$ Xvfb :1 -screen 0 1280x768x24 & 
#starts Xvfb in display ":1"
Should start a X virtual frame buffer (Xvfb) which performs all graphics operation in memory.

All set?
Not really :P Still we haven't configured the instance to use display ":1" for graphics operations.
We can do it by
$ export DISPLAY=:1
Executing the above will remain for the session that user has logged in [similar to standard linux terminal session].
You can even edit the bashrc profile. But it might need a instance restart to take effect [Not very sure].

Problem 2:
Setting up chromedriver[java] isn't a tough task incase you landed in here first :D

Step:1
Install chrome binary from the source or add the source urls of centos to amzn repo and do a yum install
Note: Chrome will refuse to start as root. So make sure your java process will be running as non root profile.
[root@xyz chrome]$ google-chrome 
Xlib:  extension "RANDR" missing on display ":1.0".
[9706:9706:0206/061403:ERROR:chrome_browser_main_extra_parts_gtk.cc(51)] Startup refusing to run as root.
Step:2
Setting up the display is necessary to make chrome to use Xvfb for graphics operations
$ vim /usr/bin/google-chrome
#add export DISPLAY=:1 to the beginning of the script
Step:3
Download the appropriate driver from code.google.com
Locate it in a directory which can be accessed without root permissions

Step:4
See the sample tutorial @Chrome Selenium Binding
Below I'm highlighting the important config code from the same link.
service = new ChromeDriverService.Builder()
        .usingChromeDriverExecutable(new File("path/to/my/chromedriver")) //location of the driver you downloaded
        .usingAnyFreePort()
        .build();
Step:5
Start the java process [non-root] you might see some c errors. Ignore them unless your process contiues to run.
Monitor the status of chromedriver.log [In our system it is located in /opt directory].
Make sure there are no "Connection Refused" or "ShutDown" messages.
These errors will eventually throw an exception at java main thread as TimedOutException.
Then main thread will exit.

As a whole
$ wget "https://gist.github.com/joekiller/4144838/raw/1560dbcf05cd90ba1052e8d999007f8803778c4a/gtk-firefox.sh"
#remember to comment line 77 incase u don't need firefox
$ chmod 777 gtk-firefox.sh
$ sh gtk-firefox.sh
#above command will take atleast 30 min
$ yum install Xvfb
$ Xvfb :1 -screen 0 1280x768x24 &
#install google-chrome via yum [add source urls to repo] or from source
$ google-chrome
#make sure the command doesn't exit till you force it to [no gtk fatal errors or permission issues]
$ which google-chrome
$ /usr/bin/google-chrome
$ vim /usr/bin/google-chrome
# add export DISPLAY=:1
# run the java code it should work
If you face any issues leave a comment will see if I could help.
Hope this helps :)

Tuesday, December 25, 2012

Backbone JS + Chrome App [manifest version 2]

Hello World!!, YAJSBP :)
QUOTE: Getting to know a library or a technology is pretty easy, but things get reversed when you want to apply the same which you felt you knew :)
In this blog post I'll take you through some integration challenges I faced while developing a chrome app along with backbonejs

So, I got to know about this guy Backbone.js an year before at jsFoo 2011.
The moment I noticed him, I visited his birthplace and got an overview of him.
Then I felt that I could claim I know backbone.js. Later realised it was too early to say that.
An year after I decided to try this guy and started developing a chrome app[Features Recorder].
Then I faced the following small challenges[on integerating backbone with chrome app] which I solved my own way :P

Problem: 1 Chrome App manifest version 2 doesn't allow eval or new Function
Error: Uncaught Error: Code generation from strings disallowed for this context
Take a look at Chrome app Content Security Policy

Why do we need this?
Underscore.js microtemplating uses new Function syntax for compiling the templates
Note: The problem might persist with other templating options too

Solutions:
1. As mentioned in the CSP page, relax the rule against eval and related functions
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
2. As I just used underscore.js microtemplates. I pre-compiled my templates offline [not in the app]
var source = _.template("My template code").source
//assigned the source to template property of the view
//This solved the problem
//Ex:
Backbone.View.extend({
 initialize: function () {},
 template : function(obj){ //Precompiled template code
    var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
    with(obj||{}){
    __p+=''+
    ((__t=( title ))==null?'':__t)+
    '';
    }
    return __p;
 }
});
3. Refer this blog

Problem: 2 window.localStorage for offline packaged is disabled
Problem: 3 chrome.storage is asynchronous [get & put are async as ajax]

Error: "window.localStorage is not available in packaged apps. Use chrome.storage.local instead."
I planned to build a offline packaged app.
But this error stunned me up as I can no more use local storage adapter for backbone

Solution:
I built my own localstorage adapter for chrome.storage
As the storage works in async way, callbacks for functions like model.save and create remained a must
var options = {success: function () {  }};
model.save(options);