Back to Posts

OpenShift

Posted in Programming

There is a lot of movement in the PaaS world, many companies are trying to showcase what is the best platform and to be honest I don’t really know.

However I decided to try it out and I’ve choose Red Hat OpenShift.

Red Hat has done with OpenShift a great work, and any one could try it out with their free accounts.

I’ve redo my MEAN example that I original create to run in AWS example.

The results could be seen here, and the code is available in this github repository, the code includes a guide to clone this and get just a instance running of the same code.

These are the step that I’ve follow to get this done:

  • Create an account in OpenShift
  • Download the client tools as is described in this guide, as prerequisite you need Git and Ruby
  • Create a folder and then set-up the tool
$ rhc setup

The set-up will create the domain for the apps and it will create & store a SSH certificate that identify the account.

  • Create an application, it could be done as part of the set-up as well. I’ve choose NodeJS and MongoDB cartridge
$ rhc app create <app name> nodejs-0.10 mongodb-2.4
  • Edit package.json to edit the modules, I’ve add mongoose and bower, express was already there.
  • Modify server.js code to connect to mongo using mongoose. They are environment variables already defined as its a cartridge in the app:
$OPENSHIFT_MONGODB_DB_HOST
$OPENSHIFT_MONGODB_DB_PORT
$OPENSHIFT_MONGODB_DB_USERNAME
$OPENSHIFT_MONGODB_DB_PASSWORD
  • Add your front side scripts dependencies using bower.json, like AngularJS.
  • I’ve add and extra script step in package.json to launch bower when the app its build.
{
"postinstall": "HOME=$OPENSHIFT_REPO_DIR bower install || bower install"
}
  • Now to build the app.
$ git add
$ git commit
$ git push origin master

If something went wrong we could view our logs, to access them:

$ rhc ssh
$ cd $OPENSHIFT_LOG_DIR
$ tail/vi/cat.....

Or use

$ rhc tail -a <app name>

Each time that we do a push the application will be sent to OpenShift and build, including update bower scripts. Them in OpenShift we could any time switch between builds in the Git repository or scale our app automatically.

We could choose to instead to do the bower step when we build, to do it in local and them upload to the repository the scripts dependencies, but the idea of a PaaS is that this is a standalone self contained app and it could be rebuild any time anywhere.

Additional we could add our own repository, as github just adding another remote:

$ git remote add github <git hub repo url>

So we could push to github whenever we like to:

$ git push github master

Two prepare a local environment that we could use to build and test our app before sent to OpenShift we need to do some additional steps:

  • Download and install NodeJS and MongoDB.
  • Since we are going to build locally add .gitnore to node_module and bower_components folders so we don’t upload them to OpenShift.
  • Configure MongoDB, including creating users and run the server.
  • Set-up enviroment variables as the ones in OpenShift
$OPENSHIFT_NODEJS_IP
$OPENSHIFT_NODEJS_PORT
$OPENSHIFT_MONGODB_DB_HOST
$OPENSHIFT_MONGODB_DB_PORT
$OPENSHIFT_MONGODB_DB_USERNAME
$OPENSHIFT_MONGODB_DB_PASSWORD

Now we could build and run locally using

$ npm install
$ npm start

And that is the basic idea.

As I said I really like how OpenShift works and I’m looking forward to do more test with it.

About Juan Medina
I'm just a normal geek that code all kind of stuff, from complex corporate applications to games.

Games, music, movies and traveling are my escape pods.