Pages

Friday, 19 July 2013

Changing Private-Public Key pair of an Existing Amazon EC2 Instance.

Changing private-public key pair of an existing amazon EC2 instance has been a constrain for me. I done lot of brainstorming on this. But in most of the places I found that taking new instance is the way or rather better option. But after lot of research I found out a way. It may not be straight forward but will do the job. But as a software engineer it is pleasure to do some creepy interesting things that will make life easier.We can do it in three steps.

Step 1: Creating New Key Pair

  • Log in to AWS console
  • Go to Services -> EC2 -> From left navigator(Menu on left side) select Key Pairs
  • From there we can get the details of existing key pairs.
  • Click on the Option "Create New Key Pair" from the top menu
  • Give a name for New Key Pair and Click on create.The new key pair will be crated and downloaded as a pem file.

Step 2: Extracting Pubic Key From Key Pair

  • Here we need to use the public key with SSH client.so weed to get public key based on that
  • Use the following command for generating public key
    ssh-keygen -y -f private_key.pem > public_key.pub

Step 3: Replacing the Old Public Key with New in the Instance

  • Log in to the Instance using Old Key Pair using ssh client
  • Follow the below steps
    1. cd ~/.ssh 
    2. sudo vi authorized_keys
    3. Open the new public key file(public_key.pub) and copy paste the content to "authorized_keys".
    4. After a space add the Privet Key base name(For eg: Here private key name is "private_key.pem".So after public key put a space and add "private_key".)
    5. Delete the old public key from the file "authorized_keys" and save
Now try to log in with new key pair. Its done.

Wednesday, 8 May 2013

Pushing Existing Folder/Project To Github

                                      If we need to push an existing project/folder to Github. We can do it with out cloning the empty repository. First we need to create a repository in github. It is good if the name of the repository is same as the parent folder that contains the data to be push. After creating the repository we will get an url of that repository, For eg: https://github.com/user_name/repository_name


After this enter into the project folder and do the following steps.
git init
git remote add origin https://github.com/user_name/repository_name
git add file_names
git commit added_file_names
git push origin master
git pull origin master

After the first pull and and push you can do the git operations as usual. Here in the above command we are using remote add 'origin', besides 'origin' if you are using 'master' rr some other name.It will also work. But when we pull or push,The command will change, instead of origin we need to give the name that we provided at the time of remote add    

Tuesday, 7 May 2013

Queue Based Delayed Job

Delayed Job, also known as DJ, makes it easy to add background tasks to your Rails applications. Delayed_job  encapsulates the common pattern of asynchronously executing longer tasks in the background. The main situations we use delayed job are sending mails solr reindexing,uploading  and parsing big files and other background jobs.
                                               Delayed job 3.x only support rails 3.x. The main feature of delayed job 3.x is queue based serving. We can run delayed job with or without queue parameter. The main advantage of running job with queue is, we can run more than one job simultaneously. So we can categorise delayed job to take care specific group of task.
                                               For example in an application we are using delayed job for sending mails it includes the activation mail that we send immediately after registration and other mails. If we are using single job it works on FIFO manner, so the activation mail only go after processing the early added jobs. But if we are running two delayed jobs process with different queue names,eg: "activation"  only for activation mails and "messages" for all other mails. Then the activation job only take care of activation mails and it will send with out any delay.

                                                Here I am using delayed job with active record.

Installation   

gem 'delayed_job'
gem 'delayed_job_active_record'
gem 'daemons'

Then bundle install . The back end requires a delayed job table, we can creat it using the following command
rails generate delayed_job:active_record
rake db:migrate
The first command create a migration for creating the delayed_jobs table. When we calling a delayed job it is added to this table and it is helpfull to kep track of the process and finding the errors.

Upgrading from delayed_job 2.x to 3.x on Active record

First we need to change the gem file. The main difference between2.x and 3.x is the latter introduces a new column to the table. So run the upgrade generator to create migration to add the column.
rails generate delayed_job:upgrade
rake db:migrate
The first command will create a migration to add one column to the table. If it is showing any error while running the best way to upgrade is create a migration manually to add the column.
rails generate migration add_column_to_delayed_job
Edit the migration file to add the column. After editing migration file will look like
def up
  add_column :delayed_jobs,:queue,:string
end

def down
  remove_column :delayed_jobs, :queue
end

Starting Delayed Job with Queue Parameter

The command for staring queue based delayed job is giving below
 RAILS_ENV=(environment:development or staging or production) script/delayed_job -i=(Serial Number:1,2,3....) --queue=(Queue name) start 
We can start number of jobs with different queue name. The main thing we need to take care is the serial number should be different for each job.Otherwise the same job will be replaced to another queue name.

Queueing Jobs

Call .delay.method(params) on any object and it will be processed in the background. For eg:
@user.send_mail(@name)
@user.delay.send_mail(@name)
By doing this the action will execute in background. When we user queue based system and running more than one job the queueing will like below. We need to pass the queue name to understand which job should take care of the corresponding action.
@user.delay(:queue => 'activation').send_activation_mail(@name)
@user.delay(:queue => 'messages').send_reminder_mail(@name)
Here the first action will executed by the job which started with the queue name 'activation' and the second with queue name 'messages'


For detailed reference please follow the link Delayed Job                     

Friday, 4 January 2013

Destructive Functions in Ruby - Part 1


As I said in the previous post those functions does not change the parent array. But adding a very slight difference we can get the job done. We can do this by using "Destructive" function. Don't be afraid by seeing the name. This functions are the function with same name. The only difference is there will be an exclamatory symbol at the end of function name. Let see an example. Lets take the same above example.

select!

 
>> a = ["Hai","Tendulker","Hello","Fantastic"]
>> a.select! {|x| x if x.length > 5 }
=> ["Tendulker", "Fantastic"]
>> a
=> ["Tendulker", "Fentastic"]
In the above example the parent array has been changed after the function execution. So thats why these functions are called "Destructive" functions

map!/collect!

These functions are also "destructive" functions and change the parent array.Lets see the examples.
>> a = [2,6,4,9,3]
>> a.map! {|x| x+1}
=> [3,7,5,10,4]
>> a
>> => [3,7,5,10,4]
>> a = [2,6,4,9,3]
>> a.collect! {|x| x+1}
=> [3,7,5,10,4]
>> a
>> => [3,7,5,10,4]
I think that the above examples will give a basic introduction to "Destructive" functions in ruby. Will discuss about more such type of function in the coming posts.
                                                                                                     To be continued

Thursday, 3 January 2013

Fascinating Ruby - Part 1

Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. It was also influenced by Eiffel and Lisp. Ruby supports multiple programming paradigms, including functional, object oriented, imperative and reflective. It also has a dynamic type system and automatic memory management; it is therefore similar in varying respects to Smalltalk, Python, Perl, Lisp, Dylan, Pike, and CLU.

The above is a small technical introduction to ruby.  When I started working with ruby I understands ruby is very powerful, efficient and with easy syntax that very helpful for coding. Ruby have a very large set of powerful array and string functions. By using these functions we can get the job done very easily by writing single line of code. For same purpose we need to write number of lines of code in may other languages. Here am going to introduce some such function that come across my work and very useful.

map/collect

There is no functional difference between map and collect as per my knowledge. This is very useful and powerful ruby array function. Using this function we can manipulate array by writing a single line of code. Let see an example.
>> a = [2,6,4,9,3]
>> a.map {|x| x+1}
=> [3,7,5,10,4]
If we use collect also same result will get.
>> a=[2,6,4,9,3]
>> a.collect {|x| x+1}
=> [3,7,5,10,4]
Here we are adding 1 to each element of an array. And doing it in single line."map and collect" is useful when we need to manipulate every element of an array.

select

If we need to implement some conditions on elements of an array and return the values here comes select.Let see an example.
>> a = [2,5,8,4,9,3,6,7]
>> a.select {|x| x if x%2==0}
=> [2, 8, 4, 6]
Here the array contains both even and odd number. We need to get only the even numbers. The select function doing it easily in single line. Let look in to a string example. Consider an array of strings with different lengths. We need to get the words with length greater than 5. Using select we can do it easily.
>> a = ["Hai","Tendulker","Hello","Fantastic"]
>> a.select {|x| x if x.length > 5 }
=> ["Tendulker", "Fantastic"]


                                                  Here one thing you should notice, these functions returning the result. The parent array or list is not changed. After the process if you print the array it will give the old array as unchanged. So while using these functions you need an assignment.
 
>> a = ["Hai","Tendulker","Hello","Fantastic"]
>> b = a.select {|x| x if x.length > 5 }
=> ["Tendulker", "Fantastic"]
>> a
=> ["Hai","Tendulker","Hello","Fantastic"]
>> b
=> ["Tendulker", "Fentastic"]
We will discuss about more such cute functions in the coming posts.                                                                                                                              To be continued

Thursday, 6 December 2012

Setting up Sunspot-Solr in Production with jdk And Apache Tomcat

As you all know Apache Solr is the popular, blazing fast open source enterprise search platform from the Apache Lucene project. Its major features include powerful full-text search, hit highlighting, faceted search, dynamic clustering, database integration, rich document (e.g., Word, PDF) handling, and geospatial search. Solr is highly scalable, providing distributed search and index replication, and it powers the search and navigation features of many of the world's largest internet sites.

Sunspot is a ruby library for the expressive, powerful interaction with the solr search engine. Sunspot is built on the top of RSolr library. Which provides a low level interface for solr interaction, Sunspot provides a simple, intuitive, expressive DSL backed by powerful features for indexing objects and searching for them.

When we are working with RoR we can install sunspot by adding two gems to the Gemfile and do bundle install.

gem 'sunspot_rails'

gem 'sunspot_solr'

Then do bundle insatall.

bundle install

Then generate a default configaration file.

rails generate sunspot_rails:install

Now lets look how we can setting up solar on the system. For installing Solr we need Java in our system. So first we can set up  and configure Java. Install latest jdk to your system. You can either go to sun website and download the package and install it or use apt-get. For the later first run

apt-get update

It will update our source and find the latest packages. Then run
apt-get install openjdk-version-jdk

After completion of installation you should set the path and the java_home to .bashrc file for both root and local.

1. /home/user/.bashrc
2. /root/.bashrc
Then add
1. export PATH=~/java_installation_path(eg:/usr/lib/jvm/java-version)/bin:$PATH
2. export JAVA_HOME=~/java_installation_path(eg:/usr/lib/jvm/java-version)

After adding the above please do "source .bashrc" to affect changes.

If the above mentioned didn't went well please go through the below link. It is a lengthy process bu the job will get done definitely.

http://www.wikihow.com/Install-Oracle-Java-on-Ubuntu-Linux

Next step is installing latest tomcat to your system.

apt-get install tomcat7 tomcat7-admin tomcat7-common tomcat7-user tomcat7-docs

apt-get install libmysql-java(optional)

To test tomcat run
/etc/init.d/tomcat7 start

/etc/init.d/tomcat7 status

Now we need to give permission to user for accessing this.
nano /etc/tomcat7/tomcat-users.xml


It will open the above file and add the following lines to the file.
role rolename="manager"
role rolename="admin"
user username="admin" password="SPECIAL" roles="manager,admin"
Note: Each line should add as xml, means there should be starting and ending tags.

Then add Data Directory and give permission.
mkdir /var/lib/tomcat7/solr/data
chown -R tomcat7:tomcat7 /var/lib/tomcat6/solr/data/

There are some more permissions we have to give.
chown -R tomcat7:tomcat7 /var/lib/tomcat7/
chmod 775 /var/lib/tomcat7/conf/tomcat-users.xml
chmod 775 /var/lib/tomcat7/conf/Catalina/localhost/solr.xml

Wow we almost done. It is the time to test. Re-start Tomcat!!!!
/etc/init.d/tomcat7 restart
To test:
curl http://127.0.0.1:8080/solr/admin/

One last thing!!!!!!!! If you are using Rails for your site please do the following step. Copying the rails config file:
cp RAILS_ROOT/solr/conf/schema.xml /var/lib/tomcat7/solr/conf/schema.xml
/etc/init.d/tomcat7 restart