Deploy Ruby on Rails applications using Rake

So you are a Rails developer (or just starting to test Rails), and you would like to find an easy way to deploy a new version of your web application to your production machine, just uploading the files you have changed, and not the entire project.

Rake is Ruby's version of Make, and uses Rakefiles (clever, huh?). You can use Ruby to make your own Makefiles. We can then use Rake to deploy our application.

You will need the rsync client (you don't need the server) installed in your destination server. Otherwise, the process will fail.

Create a new file, named deploy.rake in lib/tasks/, containing the following code:

task :deploy => :environment do
  puts "Deploying to server"
  system("rsync -rltvz -e ssh app public root@server:/var/www/webapp/")
  system("ssh root@server chown -R www-data:www-data /var/www/webapp/")
  system("ssh root@server /etc/init.d/apache2 reload")
end


This will sync both app/ and public/ directories (the application itself, images and stylesheets), but you can add as much directories as you need. The second line changes the permissions to the user that runs the web server, and the third, as you may see, reloads Apache to force the server to refresh the controllers.

And what if you have several servers where to deploy? We can use some Ruby style loops:

task :deploy => :environment do
  servers = %w{server1.domain server2.domain server3.domain}
  servers.each do |server|
    puts "Deploying app to #{server}"
    system("rsync -rltvz -e ssh app public root@#{server}:/var/www/webapp/")
    system("ssh root@#{server} chown -R www-data:www-data /var/www/webapp/")
    system("ssh root@server /etc/init.d/apache2 reload")
  end
end


Now, in your main application directory, just run rake deploy and the remote host will be synced with your local application.

Still, if you don't have already enabled key authentication for your ssh account, you will be asked for a password each time you deploy your application.

If you want to do this process without having to enter your password, you can generate your own authentication key:

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/daniel/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/daniel/.ssh/id_rsa.
Your public key has been saved in /home/daniel/.ssh/id_rsa.pub.
The key fingerprint is:
e3:c6:4d:88:c0:75:7b:2a:65:52:91:19:b4:99:e1:8f daniel@client

If you don't specify a password, you won't need to enter it to unlock the private key. It is safe to leave it blank, but don't let anyone get to your id_rsa private key, or they will be able to log in to your servers.

The public key is contained in .ssh/id_rsa.pub. This is the key you have to upload to your servers.

$ scp .ssh/id_rsa.pub root@server:
root@server's password:
id_rsa.pub                                                           100%  233     0.2KB/s   00:00   

Log in now to your server as the user you want to have authorized, and copy the public key to the authorized_keys file:
# cat id_rsa.pub >> .ssh/authorized_keys



 


Trackback

Trackback URL for this entry:
http://www.gra2.com/trackback.php/deploy-ruby-on-rails-applications-rake

No trackback comments for this entry.

Comments

Post a comment

Good post. I add it to my Bookmarks.

Ricard

Anonymous on Friday, July 06 2007 @ 02:10 PM CEST Reply | #

For people who use a diferent port on ssh connections:


system("rsync -rltvz --rsh="ssh -p2288" app public root@server:/var/www/webapp/")


I want to remark that rsync must be installed on both server and client

Ricard

Anonymous on Saturday, July 07 2007 @ 03:34 AM CEST Reply | #

Dont reivent the wheel...

http://www.capify.org/

Anonymous on Saturday, August 04 2007 @ 08:27 PM CEST Reply | #

Search



About

newton.gra2.com is a blog about technology, opinion and random thoughts written by Daniel Alvarez, a computer engineer currently living in Zurich, Switzerland.

Topics

News (20/0)
Manuals (24/0)
Security (7/0)
Music (3/0)
Weeklog (1/0)
Personal (34/0)
Photos (3/0)
Opinion (14/0)
Windows (5/0)

Blogroll

Pros i contres (Jordi)
Entrepa de fusta (Oriol)
Spaghetti Code (Isaac)
Made in net (Eric)
Nogare (Juan)
Blog de Isaac Jimenez
Web d'en Jaume Benet
Montcada Wireless (Fran)
Blog d'en Ricard Forniol
Angela Fabregues
in.solit.us

Libertad Digital
FOX News
The Wall Street Journal
The Washington Times
The Jerusalem Post

Michelle Malkin
Eurabian News
Nihil Obstat
Barcepundit
Expose the left
Davids Medienkritik
Johan Norberg
Ayaan Hirsi Ali

User Functions

:

:


Lost your password?

Latest posts

Stories

No new stories

Comments last 2 days


Trackbacks last 2 days

No new trackback comments

Links last 2 weeks

No recent new links