Using Capistrano to Deploy to a Media Temple Ruby on Rails GridContainer

Friday, 12 February 2010

What I thought would be quick to set-up nearly took an entire evening. Using Media Temple's Grid Service along with their Ruby on Rails GridContainer seemed straightforward, but they really don't provide much help for what I'd assume most people are using to deploy: Capistrano.

After following the basic Rails container installation steps outlined on their knowledge base, I had to figure out how to use capistrano to deploy. Media Temple does provide a special set of capistrano tasks that work with their service called mt-capistrano, but as far as I can tell, there's no official documentation other than the file itself. The following is a recap of a few of the hurdles I came across.

Deploying With Capistrano

Ok, my grid container is turned on, but how do I add deploy my app to it using capistrano? Contrary to what I've seen in other articles, you do not need to do activate an app in their control panel, but you do have to turn on your rails container. Once the container itself is turned on, you can do everything else from capistrano. This is where mt-capistrano comes it. Media Temple has provided special tasks so you can add a new app directly using capistrano without having to touch the Account Center control panel. Robert Prill's site was an excellent resource for getting everything set up, but I had to add a couple of things to his deploy script:

# I commented out this line since it was causing my deploys to fail
# set(:rake) { "PATH=$PATH:# --snip-- #" }

# necessary for functioning on the (gs)... (my deploys were failing without this)
default_run_options[:pty] = true

# use svn export instead of checkout so we don't include .svn folders
set :deploy_via, :export

After you've set up your deploy.rb file, you can use the mt capistrano tasks to get everything set up. The directions are at the bottom of Robert Prill's deploy file:

The first time you deploy, do it step by step:

cap deploy:setup
cap deploy:update (performs deploy:update_code, deploy:symlink)
cap deploy:migrate
cap mt:add
cap mt:start
cap mt:status
cap mt:generate_htaccess
cap mt:create_link

Going forward:

cap deploy
cap deploy:migrate # if there are database migrations to run

Troubleshooting A 500 Error

Once I had finally successfully deployed, I got a 500 error (the dreaded "We're sorry, but something went wrong." page) and my production.log file was empty. After trying script/console and everything loading fine, I tried script/dbconsole and I got this error message on startup:

Couldn't find database client: sqlite3. Check your $PATH and try again.

I finally realized that this was because my database.yml file was using the default sqlite connection, but sqlite was not installed. The rails app I was using was just a shell with nothing in it so I overlooked the fact that the database would be trying to establish a connection. It's not noted anywhere here by Media Temple but sqlite3 is not installed with the default gems and for some reason manually installing the gem doesn't work:

$ gem install sqlite3-ruby

ERROR:  Error installing sqlite3-ruby:
	sqlite3-ruby requires Ruby version > 1.8.5

So then I tried installing the previous version:

$ gem install sqlite3-ruby -v 1.2.3

Building native extensions.  This could take a while...
ERROR:  Error installing sqlite3-ruby:
	ERROR: Failed to build gem native extension.

	/usr/bin/ruby extconf.rb
	checking for fdatasync() in -lrt... no
	checking for sqlite3.h... no
	*** extconf.rb failed ***
	# --snip-- #

At this point I didn't really feel like figuring out why sqlite was failing to build since I'd be moving to a mysql database eventually anyway, so I just changed my database.yml and added a mysql connection for production and then re-deployed. Everything worked fine.