Skip to content

Instantly share code, notes, and snippets.

@thefotios
Created September 19, 2012 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefotios/2cbccaa65b2a38cd8778 to your computer and use it in GitHub Desktop.
Save thefotios/2cbccaa65b2a38cd8778 to your computer and use it in GitHub Desktop.

So you have this shiny, new Rails app that you want to run on OpenShift. But how do you go about taking an existing app and adding "all of the OpenShift magic"? Simple, there isn't much magic involved! (at least as far as you and your app are concerned). We've gone through a lot of effort to make sure you don't have to code your app any differently to work on OpenShift. And to prove it, we're going to show just how easy it is!

There are only a few steps involved.

  1. Adding the .openshift directory to your app
    • This folder contains some scripts that help initialize your application when you deploy it to OpenShift
  2. Modifying some variables to use the OpenShift environment variables
    • Don't worry, there aren't many. Mostly, the only ones you'll be concerned with are the location and credentials for your database, or your log and data directories
  3. Ensuring your libraries can be installed on OpenShift

And to prove just quick and painless this is, I followed the advanced directions in this asciicast. My app was modified and ready to be merged in under 2 minutes (and that includes providing color commentary for the viewers) [1].

Create an OpenShift App

Obviously, you will need an OpenShift account; if you don't have one, sign up now, it's free!

The next step will be to create a new application. If you are using Rails with MySQL, you're in luck; we have an instant application that does this in a single step. If you want to use another database (such as Mongo or Postgres), simply create a Ruby 1.9 application and then embed your desired database.

Add Your Old Code to Your OpenShift App

In a nutshell, the first things we have to do is replace your new application code with the old code. We will show two ways to accomplish this. The first is the simplest, but involves using your OpenShift repo as your main repo.

If you want to continue using another origin repository, like one on GitHub, you most certainly can. That way, you can continue to work exactly like you have been, but you'll have the ability to also publish your code to OpenShift. This approach requires a bit more understanding of git (like understanding remotes, branches, and merging).

From now on, if the steps are different, we will refer to the different steps as Simple and Advanced. Also, we will refer to your existing code and application as "old", and your OpenShift code and application as "new".

Simple

From now on, all commands will be run from your new app's git repo. If you created the app via the web console, you will need to checkout the code before continuing.

  1. Remove OpenShift code

    # PLEASE make sure you're in your OpenShift git repo
    git rm -rf ./*
    git commit -am "Removed OpenShift app code"
    
  2. Add your old code to your new repo. Don't forget any hidden files/directories.

  3. Commit the new code

    git add -A
    git commit -m "Added old app code"
    

Advanced

Using the advanced approach, we preserve your existing git repository (like if you're using github). We'll add OpenShift as a remote repo and create an OpenShift branch for any OpenShift specific changes.

All of these commands will be run from your old git repo and make some minor assumptions:

  • Your existing repository is called origin
  • Your existing code is in the master branch
  1. Get the git URL for your new application, this can be done a few ways

    • This was shown to you when you created your application, either through the web console or the CLI tools.
    • You can run rhc app show and it will show you the URL
  2. Add your OpenShift repo as a remote repo

    git remote add openshift <OpenShift repo URL>
    git fetch openshift
    
  3. Create a new branch from your new code

    git checkout -t -b openshift openshift/master
    
  4. Remove OpenShift code

    # PLEASE make sure you're in your OpenShift git repo
    git rm -rf ./*
    git commit -am "Removed OpenShift app code"
    
  5. Merge your old code into your new branch. We set the upstream first so you can always compare your openshift branch against your master

    git branch --set-upstream openshift origin/master
    git pull
    
  6. Normally, you will be working in your master branch. When you have changes you want to push to OpenShift, you will need to merge them into the openshift branch. Because there are very few OpenShift specific changes (explained in the next section), you should not have any conflicts. The main reason this may happen is if you modify a line, like in one of your configuration files, that you've also modified in your openshift branch (simply adding code to other parts of the file should not cause a problem). If you do have a conflict, be sure to pay special attention that you do not remove the OpenShift changes you've made.

    When you want to merge your changes, it's as simple as:

    git checkout openshift
    git merge master
    

Configuring for OpenShift

In order for your application to properly run on OpenShift, you'll need to make a few changes. OpenShift makes available a number of Environment Variables that prevent you from having to hard code (or even remember) things like usernames and ports.

Also, since OpenShift runs your Rails app in "production" mode, you won't need to modify your "development" or "test" environments.

The following are some places that you will need to modify your code so that it runs properly on OpenShift.

Note, if you followed the Advanced directions above, you should only make these changes to your openshift branch.

Gemfile

OpenShift uses a gem mirror to speed up gem installation. You will need to add the mirror to your Gemfile and your Gemfile.lock.

Add the following source at the top of your Gemfile (before any other sources):

source 'http://mirror1.prod.rhcloud.com/mirror/ruby/'

Add the following line to your Gemfile.lock before any other sources:

remote: http://mirror1.prod.rhcloud.com/mirror/ruby/

If you are running any bundle commands locally on your development machine (like bundle install or bundle update), you will need to comment out the line in your Gemfile before running the commands. After you run the commands, you will need to make sure to add the lines back to both files.

Database

Since your database address may change, you will need to access it using an environment variable. A random username and password was also generated for you when your application was created. But you don't need to hard code it into your application, there are environment variables for that too!

MySQL/PostgreSQL

You will need to modify the values in your config/database.yml for your database host and credentials. The [Environment Variables][env_var] you will probably need are ENV["OPENSHIFT_DB_HOST"], ENV["OPENSHIFT_DB_USERNAME"], and ENV["OPENSHIFT_DB_PASSWORD"]. This file is a great example from our "Rails Quickstart" that our Rails Instant Application uses.

Mongo

If you are using Mongo, you can modify your configuration similarly. The only difference is that your environment variables will be something like ENV['OPENSHIFT_NOSQL_DB_???'].

Persistent Storage

Because OpenShift recreates your repository directories every deploy, anything that is not stored in one of the persistent directories will be lost.

These are two common places you will want to modify your storage locations.

Log Files

If you have a custom logger, make sure it is storing files in ENV['OPENSHIFT_LOG_DIR']. In addition to this directory being persistent, it will also allow you to tail them using the rhc app tail command.

Other Data

If you need to store files (for instance, if users can upload files), you will need to make sure to store them in ENV['OPENSHIFT_DATA_DIR'].

Deploying Your App to Openshift

Now that your app is configured properly, it's time to deploy it to OpenShift. To do that, simply run a git push.

Advanced

You will need to make sure you are pushing your branch to the correct remote repository. You can do this with the following command:

git push openshift openshift:master

If you want to set this as the default target, add the -u option to the command. The downside to doing your git status command will compare your local openshift branch against the remote openshift:master branch. It is probably more useful to compare your branch against the local master branch.

Finishing Up

If everything went well, your app should deploy and be accessible. If not, pay attention to the output from the git push, if anything failed, it will tell you there.


[1]: Ok, I concede that I've run through these commands a few times and knew exactly what to change. But it still doesn't take that long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment