Running nodejs 0.8.x (or any other version) on OpenShift

This week I had a quick look at Red Hat’s OpenShift platform. After a very good first impression I decided to create a sort of “hello world” node app to see the gears in motion. So I look around…. cartdriges… and voila! There it is, nodejs… but its 0.6… hmmm… scroll down a bit more… DIY [experimental]… aha! Next thought: Why don’t I use the DIY thing to try and install node 0.8.x, or even better, something that allows me to chose which nodejs version I want to run.

To my surprise, two coffees later there it was, working fine and doing what I was expecting it to do. Even if this is a half-baked solution (to say the least), it may still help someone out there trying to run nodejs v0.8.x on OpenShift. So why not share it. Before I wrote up this post I found a post on the OpenShift community explaining how to run 0.8 on OpenShift, but I thought this could be simplified a bit using nave, a script by Isaac Z. Schlueter based on Tim Caswell’s “nvm” and Kris Kowal’s “sea” programs.

To start with I created a new app using the DIY cartridge and cloned the git repo on my computer. Looking at the files included by default in the repo there was a directory named .openshift which contained action_hooks and cron. In this article I won’t go into the cron stuff, thats a completely different topic. But the action_hooks look like they would give us the tools we need, together with the incredibly helpful nave.

After messing about for a bit my file structure looked a bit like this:

.
??? .gitignore
??? .openshift
??? ??? action_hooks
??? ??? ??? build
??? ??? ??? deploy
??? ??? ??? post_deploy
??? ??? ??? pre_build
??? ??? ??? start
??? ??? ??? stop
??? ??? cron (omitting dir contents)
??? README
??? app.js
??? package.json
??? public
??? ??? favicon.ico
??? ??? style.css
??? views
    ??? index.hbs
    ??? layout.hbs

I added a package.json file with the normal stuff including dependencies. Im using handlebars to render views and serving static content from public/. I also added app.js which contains the application code. Now we need to make sure the hooks get to install and choose the right version of nodejs and then start the server using this version of nodejs. To do this we concentrate on 4 files:

  • .openshift/action_hooks/pre_build
  • .openshift/action_hooks/start
  • .openshift/action_hooks/stop
  • app.js

The gists below should speak for themselves.

.openshift/action_hooks/pre_build

.openshift/action_hooks/start

.openshift/action_hooks/stop

app.js

With all of this in place, when I commit my changes and push them to the server the pre_build script is doing its job and installing all my dependencies. The app also starts and stops normally and all of it running nodejs v0.8.10. Nice.

Bear in mind that this example is very rough and nowhere near perfect, but it shows some of the possibilities and how easy it is to put the different parts together. For the time being the code in this example is running here: http://test-lupo.rhcloud.com/.

Categories: Uncategorized
  • Nice work, also package.json gist desired?

  • Hanspeter Schlapbach

    would like to see your view-files and package.json too…
    which version of express supports handlebars?i get?Error: Cannot find module ‘hbs’

  • Hanspeter Schlapbach

    ok. package.json looks like

    {? “name”: “LupoMontero”,? “version”: “0.0.0”,? “description”: “Lupo Montero Sample Application”,? “engines”: {? ? “node”: “0.8.x”,? ? “npm”: “1.1.x”? },? “dependencies”: {? ? “express”: “>= 1.2.0” ,? ? “hbs”: “>= 0.9.0”? },? “devDependencies”: {},? “bundleDependencies”: [],? “private”: true}

    seems to work.?

    get following error opening my site :

    Error: ENOENT, open ‘/var/lib/stickshift/6b0bf5e306894d639f1ac48b56b2f4ef/app-root/runtime/repo/views/layout.hbs’so would be interested in contents of the view-files…

  • Hanspeter Schlapbach

    ok:
    layout.hbs looks like this now:

    ? ? ? ? ? ? ? {{{body}}}?

    index.hbs looks like this:

    ? A test app | sofim by lupo montero A test app by Lupo Montenero—— Running node v0.8.10———-

    result: look at image

  • Hanspeter Schlapbach

    bash-4.1$ cat views/index.hbs? A test app | sofim by lupo montero A test app by Lupo Montenero—— Running node v0.8.10———-

    bash-4.1$ cat views/layout.hbs? ? ? ? ? ? ? {{{body}}}?

  • Hanspeter Schlapbach

    one problem:
    i have to set?chmod a+x ?app.js after every deployment…

    otherwise i get access-error

  • Hanspeter Schlapbach

    ok. this in file post_deploy works and heals:
    chmod a+x $OPENSHIFT_REPO_DIR’app.js’

  • Lasse

    Thanks for good tutorial.

    It didn’t work right away though. Problem is the newest version of nave. Older (3.1 something) version works just as you described.

  • Pingback: OpenShift PaaS Week in Review - Sept 30, 2012 - Blog Import Demo Site()

  • Pingback: OpenShift PaaS Week in Review - Sept 30, 2012 - OpenShift Blog()