Monday, June 18, 2012

Deploy Rails 3 Application to Google App Engine Using Warbler

So far Google App Engine does not support ruby/rails applications but there is a way to build a Java servlet using warbler and deploy it to GAE.

Prerequisites

  • GAE SDK 1.6.6
  • Ruby 1.8.7 with rubygems
  • Bundler 1.1.4

Now we're going to install required tools. I already have an account created on GAE but if you don't you should sign-up first to GAE.

Get the latest SDK and extract it locally:

$ sudo unzip google_appengine_1.6.6.zip -d /opt

Install Ruby, Rubygems and Bundler

$ sudo apt-get install ruby rubygems -y

$ sudo gem install bundler

Create a Rails 3 application

Make an application directory and init a Gemfile.

$ mkdir warbler-test && cd warbler-test && bundle init

Add a Rails gem dependency and install it using bundler in a separate directory called vendor. This will make sure your gems are isolated from the system. Also we'll need an extra gem called therubyracer for generating assets. Your Gemfile should look like this:

# A sample Gemfile
source "https://rubygems.org"

gem "rails"
gem "therubyracer", :platforms => :ruby

$ bundle install --path vendor

Bootstrap the Rails 3 application in the current directory (warbler-test). The rails bootstrap script will ask to overwrite the Gemfile. It is safe to allow:

$ bundle exec rails new .

Add warbler gem dependency to the end of Gemfile:

...
# Use warbler to create war
group :development do
  gem "warbler"
end

Run bundle install once again since the Gemfile was changed by the rails script. It is not necessary to specify --path vendor because bundler already configured the application config to use isolated directory

Run the rails server and verify your application is available locally at http://0.0.0.0:3000

$ bundle exec rails server

Deploy the application

When you see it is ready to be deployed, create a war file using warbler. This will create warbler-test.war file taking your current directory as a file name:

$ bundle exec warble war

The final step is to create appengine-web.xml deployment descriptor file and upload using GAE SDK tools.

Extract just generated war file

$ unzip warbler-test.war -d war

Create the descriptor with the following content by changing the [web_app_id] to your GAE application id:



  [web_app_id]
  1
  false

Finally, deploy your application. This step will take some time, probably a couple of minutes to complete:

$ /opt/appengine-java-sdk-1.6.6/bin/appcfg.sh update war

After the deployment completes you should be able to access your application on GAE. Note the prefixed version in the URL, it is a <version>1</version> that we configured in the appengine-web.xml descriptor. In my case the URL is http://1.warbler-test.appspot.com/

2 comments:

  1. Does it work with Ruby 1.9.3?

    ReplyDelete
  2. Very clever. I've been trying to do the same thing myself for a long time without any success.
    One question.

    How is the connection to the DB handled when using this technique?

    ReplyDelete