ryonakae
11/8/2014 - 10:48 AM

How you get Sail.js running on Openshift.md

#!/bin/bash
echo "Exporting Node Environment (production)"
export NODE_ENV=production

# If there is a grunt file, run $ grunt prod
if [ -f "${OPENSHIFT_REPO_DIR}"/Gruntfile.js ]; then
    (cd "${OPENSHIFT_REPO_DIR}"; node_modules/grunt-cli/bin/grunt prod)
fi
{
  "name": "Sails-Sample-App",
  "version": "1.0.0",
  "description": "Sails.js Sample App",
  "keywords": [
    "OpenShift",
    "Node.js",
    "sailsj"
  ],
  "author": {
    "name": "mdunisch"
  },
  "engines": {
    "node": ">= 0.10",
    "npm": ">= 1.0.0"
  },
  "dependencies": {
    "ejs": "0.8.4",
    "grunt-cli": ">=0.1.13",
    "grunt": "~0.4.4",
    "optimist": "0.3.4",
    "passport": ">=0.2.0",
    "passport-facebook": ">=1.0.3",
    "sails": ">=0.9.16",
    "sails-disk": ">=0.9.0",
    "sails-mongo": ">=0.9.8"
  },
  "scripts": {
    "start": "node app.js",
    "debug": "node debug app.js"
  },
  "devDependencies": {},
  "bundleDependencies": [],
  "private": true,
  "main": "app.js"
}
module.exports = {

  host: process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1",
  port: process.env.OPENSHIFT_NODEJS_PORT || 8080,
  environment: process.env.NODE_ENV || 'development'

};

#How you get Sail.js running on Openshift#

This instruction is tested with:

  • Sails.js v0.9.16
  • Node.js 0.10 on Openshift ( 05 May 2014)

###1) package.json

If you use the package.json build by sails new Projectname than you have to add a few fields for openshift – so the server can start you app automatically. Sails uses Grunt to build minify css/js and so on. Openshift dont have grunt installed so you have to add this also.

See package.json for an example. The most important part for openshift:

"dependencies": {
    "grunt-cli": ">=0.1.13",
    "grunt": "~0.4.4",
    ...
"scripts": {
  "start": "node app.js",
  "debug": "node debug app.js"
 },
"private": true,
"main": "app.js" 

###2) config/local.js

In your Sailsconfig (config/local.js) you have to set Host, Port and Environment. See local.js. Example:

host: process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1",
explicitHost: process.env.OPENSHIFT_APP_DNS || "localhost",
port: process.env.OPENSHIFT_NODEJS_PORT || 8080,
environment: process.env.NODE_ENV || 'development'

###3) .openshift/action_hooks/pre_start_nodejs

On your local machine you can start node app.js --prod if you want to start Sail.js in production-mode. On Openshift you have to create a “pre_start_nodejs”-File. This file is executed before your node-app starts. Here you can set the environment-mode. Also the build-in-grunt from sails dont work right so we manualy do a "grunt prod" befor starting node.

It’s simple:

  • Create a new file “pre_start_nodejs” (no file extension) in the folder “.openshift/action_hooks”
  • Insert the following code:
#!/bin/bash
export NODE_ENV=production

# If there is a grunt file, run $ grunt prod
if [ -f "${OPENSHIFT_REPO_DIR}"/Gruntfile.js ]; then
    (cd "${OPENSHIFT_REPO_DIR}"; node_modules/grunt-cli/bin/grunt prod)
fi

####4) supervisor_opts To preven openshift from restarting the app when somehing is written in .temp, create file named 'supervisor_opts' (no extension) in the root of your application and add following to it

-i .tmp

####5) Configure Postgresql Add in connections.js and set as a connection for production

productionPostgresqlServer: { adapter: 'sails-postgresql', host: process.env.OPENSHIFT_POSTGRESQL_DB_HOST, port: process.env.OPENSHIFT_POSTGRESQL_DB_PORT, user: process.env.OPENSHIFT_POSTGRESQL_DB_USERNAME, password: process.env.OPENSHIFT_POSTGRESQL_DB_PASSWORD, database: 'quickfixapp' }

####5) Git push Make sure your .gitingnore is not ignoring local.js

You you can make “git push” and enjoy your Sails.js!