Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get Sails.js running on Openshift

#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",
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.) Git push

You you can make “git push” and enjoy your Sails.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'
};
{
"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"
}
#!/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
@ndamnjanovic

This comment has been minimized.

Copy link

@ndamnjanovic ndamnjanovic commented Jun 5, 2014

Hey, thanks for sharing this. But I still have slight problem. When I use pre_start_nodejs, like defined in example, I receive

Failed to execute: 'control start' for /var/lib/openshift/539077bd5973caf0320000d0/nodejs.
Any ideas what's wrong at my env?

@fifthorbit

This comment has been minimized.

Copy link

@fifthorbit fifthorbit commented Oct 14, 2014

This almost worked for me (Sails 0.10.5). Also had to add the following to supervisor_opts file so that the app does not get restarted when Sails is writing .tmp files at startup.

supervisor_opts

-i .tmp
@r-massanet

This comment has been minimized.

Copy link

@r-massanet r-massanet commented Oct 16, 2014

This used to work for me, but today stopped working. I am guessing they changed something in Openshift that now requires fifthorbit's fix. Could you please elaborate on where that file supervisor_opts is (or should be) at? I certainly don't have in my Openshift environment and I can't find documentation on it.

@maciek134

This comment has been minimized.

Copy link

@maciek134 maciek134 commented Oct 17, 2014

In your app's main directory create a file named supervisor_opts.

@patelsan

This comment has been minimized.

Copy link

@patelsan patelsan commented Nov 6, 2014

Thanks for sharing. Sails seems to be lifting without any errors now, however it is not accessible through *.rhcloud.com URL or through the IP mentioned in the log. Does it need any other configuration? Following is the log

Server lifted in /var/lib/openshift/545ac737e0b8cd8ad80001fa/app-root/runtime/repo
To see your app, visit http://127.6.141.129:8080

Environment : production
Host : 127.6.141.129
Port : 8080

@patelsan

This comment has been minimized.

Copy link

@patelsan patelsan commented Nov 6, 2014

Please ignore my previous question. I was able to fix it by adding
explicitHost: process.env.OPENSHIFT_APP_DNS || "localhost"

Previously I was trying to set explicitHost to OPENSHIFT_NODEJS_IP, however it is suppose to be OPENSHIFT_APP_DNS.

@Lujaw

This comment has been minimized.

Copy link

@Lujaw Lujaw commented Nov 16, 2014

Thanks for sharing..
Just deployed an app. Helped a lot..

@daywong1119

This comment has been minimized.

Copy link

@daywong1119 daywong1119 commented Mar 14, 2015

My server is started. Unfortunately, server cannot load any views inside "/projectRoot/views", I tried to login to the shell, the "/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/views/user" folder is actually exist, have any idea?

Sending 500 ("Server Error") response:
Error: ENOENT, no such file or directory '/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/views/user/loginview.ejs'
at Object.fs.openSync (fs.js:427:18)
at fs.readFileSync (fs.js:284:15)
at Object.exports.renderFile (/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/node_modules/ejs/lib/ejs.js:312:37)
at module.exports as engine
at View.render (/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/node_modules/sails/node_modules/express/lib/view.js:76:8)
at Function.app.render (/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/node_modules/sails/node_modules/express/lib/application.js:504:10)
at ServerResponse.res.render (/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/node_modules/sails/node_modules/express/lib/response.js:801:7)
at ServerResponse._addResViewMethod.res.view (/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/node_modules/sails/lib/hooks/views/res.view.js:235:16)
at module.exports.loginView (/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/api/controllers/UserController.js:10:9)
at routeTargetFnWrapper (/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/node_modules/sails/lib/router/bind.js:178:5)
at callbacks (/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
at param (/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
at pass (/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
at nextRoute (/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/node_modules/sails/node_modules/express/lib/router/index.js💯7)
at callbacks (/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
at /var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/node_modules/sails/lib/router/bind.js:186:7 { [Error: ENOENT, no such file or directory '/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/views/user/loginview.ejs']
errno: 34,
code: 'ENOENT',
path: '/var/lib/openshift/5503dbfb4382ecf8be00015e/app-root/runtime/repo/views/user/loginview.ejs',
syscall: 'open' }

----------------------------------------SOLVED-----------------------------------------------------
The problem was about case sensitive file name, i name the view as "loginView.ejs" in folder, server runtime was trying to get "loginview.ejs", This works fine on localhost, but openshift seem doesn't accept that

@Behukob

This comment has been minimized.

Copy link

@Behukob Behukob commented Apr 10, 2015

Cant deploy to Openshift with SailsJS v0.11
becouse sails require "npm": ">= 1.4.0"
but Openshift has 1.3.4
any suggestions here?

@millenniummike

This comment has been minimized.

Copy link

@millenniummike millenniummike commented Apr 28, 2015

npm install npm@1.4.1
npm install sails

worked on the openshift shell.

Changed this in the package.json
"npm": ">= 1.4.0

@ocorsvall

This comment has been minimized.

Copy link

@ocorsvall ocorsvall commented May 7, 2015

How could you do "npm install npm@1.4.1"?
For me it says i need sudo bur can't get that on openshift?

@Behukob

This comment has been minimized.

@Behukob

This comment has been minimized.

Copy link

@Behukob Behukob commented May 13, 2015

I make it!
application with Sails 0.11 started on Openshift
you need to create Openshift application with this repository https://github.com/ryanj/nodejs-custom-version-openshift
than ssh to openshift, check node -v, npm -v, manually update npm if needed
than cd app-root, cd repo, npm install sails
than make what writen on this page
than git push

@rschuetzler

This comment has been minimized.

Copy link

@rschuetzler rschuetzler commented Feb 17, 2016

config/local.js is in .gitignore by default. How do I get that file up to OpenShift, since it won't be in my git repo?

@yayobyte

This comment has been minimized.

Copy link

@yayobyte yayobyte commented Mar 15, 2016

Upload once and then add it to gititnore file again

@ramajd

This comment has been minimized.

Copy link

@ramajd ramajd commented Mar 18, 2016

I got following error while using bower in my sails app:

DEBUG: Sending SIGTERM to child...
DEBUG: Running node-supervisor with
DEBUG:   program 'app.js'
DEBUG:   --watch '/var/lib/openshift/56eaf5c77628e1765b00007f/app-root/data/.nodewatch'
DEBUG:   --ignore 'undefined'
DEBUG:   --extensions 'node|js|coffee'
DEBUG:   --exec 'node'
DEBUG: Starting child process with 'node app.js'
DEBUG: Watching directory '/var/lib/openshift/56eaf5c77628e1765b00007f/app-root/data/.nodewatch' for changes.
Warning: connect.session() MemoryStore is not
designed for a production environment, as it will leak
memory, and will not scale past a single process.
** Grunt :: An error occurred. **
------------------------------------------------------------------------
Aborted due to warnings.
Loading "bower.js" tasks...ERROR
>> Error: EACCES, permission denied '/var/lib/openshift/56eaf5c77628e1765b00007f/.local'
Warning: Task "bower:dev" not found. 
------------------------------------------------------------------------
Looks like a Grunt error occurred--
Please fix it, then **restart Sails** to continue running tasks (e.g. watching for changes in assets)
Or if you're stuck, check out the troubleshooting tips below.
Troubleshooting tips:
 *-> Are "grunt" and related grunt task modules installed locally?  Run `npm install` if you're not sure.
 *-> You might have a malformed LESS, SASS, CoffeeScript file, etc.
 *-> Or maybe you don't have permissions to access the `.tmp` directory?
     e.g., `/var/lib/openshift/56eaf5c77628e1765b00007f/app-root/runtime/repo/.tmp` ?
     If you think this might be the case, try running:
     sudo chown -R 6204 /var/lib/openshift/56eaf5c77628e1765b00007f/app-root/runtime/repo/.tmp
Error: The hook `grunt` is taking too long to load.
Make sure it is triggering its `initialize()` callback, or else set `sails.config.grunt._hookTimeout to a higher value (currently 20000)
    at tooLong [as _onTimeout] (/var/lib/openshift/56eaf5c77628e1765b00007f/app-root/runtime/repo/node_modules/sails/lib/app/private/loadHooks.js:92:21)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15) { [Error: The hook `grunt` is taking too long to load.
Make sure it is triggering its `initialize()` callback, or else set `sails.config.grunt._hookTimeout to a higher value (currently 20000)] code: 'E_HOOK_TIMEOUT' }

is there any solution using bower beside sails in openshift ?

@alexfin90

This comment has been minimized.

Copy link

@alexfin90 alexfin90 commented Sep 6, 2019

I followed the guide and now the deploy is ok!
But all images styles and .js are not showed.
404 not found.
There is somenthing wrong with the .tmp folder on openshift???

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