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