Keystone-classic: Deploying Keystone on OpenShift

Created on 16 May 2014  路  9Comments  路  Source: keystonejs/keystone-classic

Sorry if this isn't the appropriate place to ask this, but has anyone successfully deployed keystone to OpenShift?(https://www.openshift.com). I haven't had any luck, but I'm assuming I just need to know how to configure things properly. Thanks

All 9 comments

It's possible to run it on openshift. If you bootstraped your application with the yeoman generator try these steps:

In package.json add

{
...
  "scripts": {
    "start": "keystone.js"
  },
  "main": "keystone.js"
}

In keystone.js add the following to the very top:

if(process.env.OPENSHIFT_MONGODB_DB_PASSWORD){
  process.env.MONGO_URI = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ":" +
  process.env.OPENSHIFT_MONGODB_DB_PASSWORD + "@" +
  process.env.OPENSHIFT_MONGODB_DB_HOST + ':' +
  process.env.OPENSHIFT_MONGODB_DB_PORT + '/' +
  process.env.OPENSHIFT_APP_NAME;
}

if(process.env.OPENSHIFT_NODEJS_IP) process.env.HOST = process.env.OPENSHIFT_NODEJS_IP;
if(process.env.OPENSHIFT_NODEJS_PORT) process.env.PORT = process.env.OPENSHIFT_NODEJS_PORT;

It's a bit dirty to set environment Variables like this, but i did not found a better solution.

There's a (slightly) neater way to work with the environment variables in keystone.js:

var keystone = require('keystone');

if (process.env.OPENSHIFT_MONGODB_DB_PASSWORD) {
    keystone.set('mongo uri', process.env.OPENSHIFT_MONGODB_DB_USERNAME + ":" +
        process.env.OPENSHIFT_MONGODB_DB_PASSWORD + "@" +
        process.env.OPENSHIFT_MONGODB_DB_HOST + ':' +
        process.env.OPENSHIFT_MONGODB_DB_PORT + '/' +
        process.env.OPENSHIFT_APP_NAME);
}

if (process.env.OPENSHIFT_NODEJS_IP) {
  keystone.set('host', process.env.OPENSHIFT_NODEJS_IP);
}
if (process.env.OPENSHIFT_NODEJS_PORT) {
  keystone.set('port', process.env.OPENSHIFT_NODEJS_PORT;
}

Since we already support some paas-specific options (e.g. MONGOLAB_URI for Heroku) in Keystone itself, it we could add support for the OPENSHIFT ones too if they're consistent. Might be a bit neater.

I've added the "main" script to the yeoman generator's package.json so that part's taken care of :)

From their docs, it looks like there's a simpler way to handle the Mongo config too:

https://www.openshift.com/blogs/run-your-nodejs-projects-on-openshift-in-two-simple-steps

//provide a sensible default for local development
mongodb_connection_string = 'mongodb://127.0.0.1:27017/' + db_name;
//take advantage of openshift env vars when available:
if(process.env.OPENSHIFT_MONGODB_DB_URL){
  mongodb_connection_string = process.env.OPENSHIFT_MONGODB_DB_URL + db_name;
}

This is basically the same as

if (process.env.OPENSHIFT_MONGODB_DB_URL) {
    keystone.set('mongo url', process.env.OPENSHIFT_MONGODB_DB_URL);
}

Which is much neater. Will check some more, but it should be pretty easy to make this work out of the box.

process.env.OPENSHIFT_MONGODB_DB_URL is indeed neater, didn't know about this.

Successfully deployed with these settings. Thanks Jed.

if (process.env.OPENSHIFT_MONGODB_DB_URL) {
    keystone.set('mongo', process.env.OPENSHIFT_MONGODB_DB_URL);
}
if (process.env.OPENSHIFT_NODEJS_IP) {
  keystone.set('host', process.env.OPENSHIFT_NODEJS_IP);
}
if (process.env.OPENSHIFT_NODEJS_PORT) {
  keystone.set('port', process.env.OPENSHIFT_NODEJS_PORT);
}

Awesome that worked. Thanks for the help guys

I've added new options to keystone to support openshift natively; it should now automatically pick up on the process.env.OPENSHIFT_MONGODB_DB_URL, process.env.OPENSHIFT_NODEJS_IP and process.env.OPENSHIFT_NODEJS_PORT environment variables and configure itself automatically.

I haven't actually had a chance to test it on openshift yet though, so if someone could give it a go and let me know that'd be great.

There's also a new option called db name which you can use to control the database you want to connect to, from what I could tell from the openshift docs, the process.env.OPENSHIFT_MONGODB_DB_URL only specifies the server and auth details, not the database. The database name will default to a slugified version of your name option.

The changes are live in 0.2.19.

I'll test this now and let you know how it goes. Thanks Jed

Hi,

I'm new to OpenShift and deploying my node js application. But after completing all the set up it's not working as expected. It's showing the default OpenShift page rather than my application page.

Please help.

@sanatpanda3 I recommend posting to the google group or gitter chat for help, we use these issues to track development (not support)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

javierpelozo picture javierpelozo  路  5Comments

stennie picture stennie  路  5Comments

sorryididntmeantto picture sorryididntmeantto  路  3Comments

jacqueslareau picture jacqueslareau  路  5Comments

josephg picture josephg  路  4Comments