How to Migrate Your Heroku Ruby Application to OpenShift

In this blog post I’ll share my experience migrating an existing application built for Heroku to OpenShift. Hopefully this blog post will give ideas on how to migrate your own application while showing how easy it can be. Every application is different so the steps you may need to perform may be different.

Application details

As we want to support public and non-profit initiatives, we were curious if some applications from Code for America could be run on OpenShift. The application I liked the most was “Adopt a hydrant”. The purpose is to gather people, that are willing to help to track the state of hydrants in a particular area or city and help this way to keep all the citizens safer.

“Adopt a hydrant” application is a general Rails application which uses PostgreSQL as a backend.

Environment

To start we need an environment to deploy the application in. This was done in two simple steps. First, create the environment in OpenShift for a Ruby 1.9 application.

rhc app create hydrant ruby-1.9

(it will print some information for you, note those for later use)

Second, add an embedded PostgreSQL server to the environment

rhc cartridge add postgresql-8.4 -a hydrant

At this point we have a clean environment in the cloud (OpenShift) to deploy the application with Ruby 1.9, Passenger, and PosgreSQL running. We also have a locally cloned git repository in a directory with the same name as the application – “hydrant” in my case. Let’s enter the directory

cd hydrant

Pulling the code

I was contemplating what would be the best way to get the source code from their Github repository into my main code repository. Finally I settled on just pulling the code and overwriting my current files and it worked well enough.

git pull -s recursive -X theirs git://github.com/codeforamerica/adopt-a-hydrant.git

When it’s done, you have the application code in your repository prepared to push it to OpenShift.

Older bundler

In OpenShift an older version of Bundler is used and so you can not use the new directive for setting the required Ruby version. To solve this remove the line from a file called “Gemfile” in the root of your repository.

ruby '1.9.3'

Database connection

OpenShift gives you connection parameters for databases using system environment variables. In this step we change the “config/database.yml” file to reflect this configuration. Change the “development” environment so it has this content:

  adapter: postgresql
  database: <%= ENV["OPENSHIFT_APP_NAME"] %>
  host: <%= ENV["OPENSHIFT_POSTGRESQL_DB_HOST"] %>
  port: <%= ENV["OPENSHIFT_POSTGRESQL_DB_PORT"] %>
  username: <%= ENV["OPENSHIFT_POSTGRESQL_DB_USERNAME"] %>
  password: <%= ENV["OPENSHIFT_POSTGRESQL_DB_PASSWORD"] %>

The last step is to un-comment these three lines in “.openshift/action_hooks/deploy” to enable automatic migrations when the application is being pushed:

pushd ${OPENSHIFT_REPO_DIR} > /dev/null
bundle exec rake db:migrate RAILS_ENV="production"
popd > /dev/null

Deploying the application

Now we can deploy the application to OpenShift. To do that, commit all your changes and push the repository to the “origin” (your git repo living on OpenShift).

git add .
git commit -m "OpenShift deployment"
git push origin master

And when you navigate to the application, you can see it’s running :)

Done? Not yet! No hydrants exist yet.

Importing the default data

To import the data you need to ssh into the application and run the expected Rake task in the directory with your application. The URL to ssh into your application environment is the information you noted before.

ssh <your ssh url>
cd app-root/repo
bundle exec rake db:seed
exit

This takes some time, but once finished, your application will have all the Boston hydrants imported and visible on the map.

Conclusion

Migrating a Heroku Ruby application to OpenShift is simple. OpenShift behaves like a normal operating environment so the only thing you need to do is provide your application with the right configuration. And, in the case of a migration, set up some automation tasks.

What’s Next?

Categories
OpenShift Online, PostgreSQL, Ruby
Tags
,
Comments are closed.