Felipe Koch's Endeavours
Connecting to other databases on Heroku

As previously stated, I’m a big fan of Heroku for hosting Ruby apps.

They automatically setup your application with the connection settings for their PostgreSQL database, which is cool, but can get on your way if you need to connect to another, external database. You could, of course, use their Amazon RDS addon, but for development and small apps a dedicated database like that can be too expensive ($85/month). 

To setup your app to connect to another database, simply run:

$ heroku config:add DATABASE_URL=mysql://user:pass@host/database

Simple as that!

Rails3 and Shoulda

The current version of shoulda (2.10.3) is not compatible with Rails 3 and Bundler. It’s not that hard to make it work though. In your Gemfile:

group :test do
  gem ‘shoulda’, :git => ‘git://github.com/thoughtbot/shoulda.git’, :branch => ‘rails3’, :require => nil

The “:require => nil” means shoulda will not be automatically loaded. To load it, simply require it after the other requires on test_helper.rb:

require ‘rails/test_help’
require ‘shoulda’

Voilá! You should have shoulda working with rails3 then.

Getting RubyCAS-Server running on Heroku

Since I’m building this system that will have many apps that will require a central authentication server (CAS), and since I’m most experienced with Ruby, I chose the great RubyCAS-Server to handle the heavy lifting.

My host of choice for Ruby apps is Heroku. They take the trouble of maintaining web servers and installing software and configuring applications away from you. And they do it while providing a very simple deployment strategy (git push heroku). All that while not charging that much more than what you would pay for a bare bones instance on Amazon AWS.

All that goodness comes with a price: in order for your app to be scalable in a cloud environment, you don’t have access to a file system, you don’t have access to a shell, so you don’t have root access which is what RubyCAS-Server requires by default.

If you try to run it, it will complain:

Loading configuration for “rubycas-server” from “/etc/rubycas-server/config.yml”…


Attempting to copy sample configuration from ‘/disk1/home/slugs/194894_4dff271_c804/mnt/config.example.yml’ to ‘/etc/rubycas-server/config.yml’…

It appears that you do not have permissions to create the ‘/etc/rubycas-server/config.yml’ file. Try running this command using sudo (as root).

So, in order to make it run on Heroku you need to first create your copy of the sample config file:

$ cp config.example.yml rubycas-server.yml

Then edit the file lib/casserver/conf.rb. The initial declaration of the local var “conf_defaults” becomes:

  conf_defaults = {
    :maximum_unused_login_ticket_lifetime => 5.minutes,
    :maximum_unused_service_ticket_lifetime => 5.minutes,
    :maximum_session_lifetime => 1.month,
    :log => {:file => ‘casserver.log’, :level => ‘DEBUG’},
    :uri_path => “/”,
    :conf_file => ‘rubycas-server.yml’

And the first call to “$CONF.load_from_file” becomes:

$CONF.load_from_file($APP_NAME, $APP_ROOT, conf_defaults[:conf_file])

This should be it! Now when you deploy to Heroku it will read the correct configuration file automatically.

I still wasn’t able to make it connect to the Heroku provided PostgreSQL database through ENV[‘DATABASE_URL’], but when I do I’ll post it here.