From 7e600a76766c3a4ddfa7775bdf3a4bff46aefd07 Mon Sep 17 00:00:00 2001 From: ramr Date: Sat, 22 Sep 2012 19:28:34 -0700 Subject: [PATCH 01/19] Initial commit --- README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..c0abb1a --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +nodejs-custom-version-openshift +=============================== + +Node.js quickstart application to run the latest or any custom Node.js version on OpenShift \ No newline at end of file From cd62867ca13a10948fc630370429bd367036acb5 Mon Sep 17 00:00:00 2001 From: Ram Ranganathan Date: Sat, 22 Sep 2012 21:24:38 -0700 Subject: [PATCH 02/19] Initial version to allow a user to run a custom version of Node.js on OpenShift. The user can easily switch between Node versions(e.g. 0.8.9 to 0.9.1) by just editing the .openshift/markers/NODEJS_VERSION file. --- .openshift/action_hooks/build | 32 ++++ .openshift/action_hooks/deploy | 16 ++ .openshift/action_hooks/post_deploy | 4 + .openshift/action_hooks/post_start_nodejs-0.6 | 41 ++++ .openshift/action_hooks/post_stop_nodejs-0.6 | 14 ++ .openshift/action_hooks/pre_build | 21 +++ .openshift/action_hooks/pre_start_nodejs-0.6 | 25 +++ .openshift/action_hooks/pre_stop_nodejs-0.6 | 29 +++ .openshift/cron/README.cron | 22 +++ .openshift/cron/daily/.gitignore | 0 .openshift/cron/hourly/.gitignore | 0 .openshift/cron/minutely/.gitignore | 0 .openshift/cron/monthly/.gitignore | 0 .openshift/cron/weekly/README | 16 ++ .openshift/cron/weekly/chrono.dat | 1 + .openshift/cron/weekly/chronograph | 3 + .openshift/cron/weekly/jobs.allow | 12 ++ .openshift/cron/weekly/jobs.deny | 7 + .openshift/lib/setup_custom_nodejs_env | 40 ++++ .openshift/lib/utils | 151 +++++++++++++++ .openshift/markers/NODEJS_VERSION | 9 + .openshift/markers/README | 7 + README | 67 +++++++ README.md | 69 ++++++- deplist.txt | 13 ++ index.html | 166 ++++++++++++++++ node_modules/read.me | 8 + npm_global_module_list | 21 +++ package.json | 35 ++++ server.js | 178 ++++++++++++++++++ 30 files changed, 1004 insertions(+), 3 deletions(-) create mode 100755 .openshift/action_hooks/build create mode 100755 .openshift/action_hooks/deploy create mode 100755 .openshift/action_hooks/post_deploy create mode 100755 .openshift/action_hooks/post_start_nodejs-0.6 create mode 100755 .openshift/action_hooks/post_stop_nodejs-0.6 create mode 100755 .openshift/action_hooks/pre_build create mode 100755 .openshift/action_hooks/pre_start_nodejs-0.6 create mode 100755 .openshift/action_hooks/pre_stop_nodejs-0.6 create mode 100644 .openshift/cron/README.cron create mode 100644 .openshift/cron/daily/.gitignore create mode 100644 .openshift/cron/hourly/.gitignore create mode 100644 .openshift/cron/minutely/.gitignore create mode 100644 .openshift/cron/monthly/.gitignore create mode 100644 .openshift/cron/weekly/README create mode 100644 .openshift/cron/weekly/chrono.dat create mode 100755 .openshift/cron/weekly/chronograph create mode 100644 .openshift/cron/weekly/jobs.allow create mode 100644 .openshift/cron/weekly/jobs.deny create mode 100644 .openshift/lib/setup_custom_nodejs_env create mode 100755 .openshift/lib/utils create mode 100644 .openshift/markers/NODEJS_VERSION create mode 100644 .openshift/markers/README create mode 100644 README create mode 100644 deplist.txt create mode 100644 index.html create mode 100644 node_modules/read.me create mode 100644 npm_global_module_list create mode 100644 package.json create mode 100755 server.js diff --git a/.openshift/action_hooks/build b/.openshift/action_hooks/build new file mode 100755 index 0000000..85794bd --- /dev/null +++ b/.openshift/action_hooks/build @@ -0,0 +1,32 @@ +#!/bin/bash +# This is a simple build script and will be executed on your CI system if +# available. Otherwise it will execute while your application is stopped +# before the deploy step. This script gets executed directly, so it +# could be python, php, ruby, etc. + + +# Source utility functions. +source "$OPENSHIFT_REPO_DIR/.openshift/lib/utils" + +# Setup path to include the custom Node[.js] version. +_SHOW_SETUP_PATH_MESSAGES="true" setup_path_for_custom_node_version + + +# So we we moved the package.json file out of the way in pre_build, +# so that the OpenShift git post-receive hook doesn't try and use the +# old npm version to install the dependencies. Move it back in. +tmp_package_json="$(get_node_tmp_dir)/package.json" +if [ -f "$tmp_package_json" ]; then + # Only overlay it if there is no current package.json file. + [ -f "${OPENSHIFT_REPO_DIR}/package.json" ] || \ + mv "$tmp_package_json" "${OPENSHIFT_REPO_DIR}/package.json" +fi + + +# Do npm install with the new npm binary. +if [ -f "${OPENSHIFT_REPO_DIR}"/package.json ]; then + echo " - Installing dependencies w/ new version of npm ... " + echo + (cd "${OPENSHIFT_REPO_DIR}"; export TMPDIR="/tmp"; npm install -d) +fi + diff --git a/.openshift/action_hooks/deploy b/.openshift/action_hooks/deploy new file mode 100755 index 0000000..ab6d097 --- /dev/null +++ b/.openshift/action_hooks/deploy @@ -0,0 +1,16 @@ +#!/bin/bash +# This deploy hook gets executed after dependencies are resolved and the +# build hook has been run but before the application has been started back +# up again. This script gets executed directly, so it could be python, php, +# ruby, etc. + + +# Source utility functions. +source "$OPENSHIFT_REPO_DIR/.openshift/lib/utils" + + +# On slave/serving gears, need to do the install as part of deploy +# so check if its needed. Just ensure the custom Node[.js] version is +# installed. +ensure_node_is_installed + diff --git a/.openshift/action_hooks/post_deploy b/.openshift/action_hooks/post_deploy new file mode 100755 index 0000000..eabd36a --- /dev/null +++ b/.openshift/action_hooks/post_deploy @@ -0,0 +1,4 @@ +#!/bin/bash +# This is a simple post deploy hook executed after your application +# is deployed and started. This script gets executed directly, so +# it could be python, php, ruby, etc. diff --git a/.openshift/action_hooks/post_start_nodejs-0.6 b/.openshift/action_hooks/post_start_nodejs-0.6 new file mode 100755 index 0000000..b3ccc71 --- /dev/null +++ b/.openshift/action_hooks/post_start_nodejs-0.6 @@ -0,0 +1,41 @@ +#!/bin/bash + +# The pre_start_cartridge and pre_stop_cartridge hooks are *SOURCED* +# immediately before (re)starting or stopping the specified cartridge. +# They are able to make any desired environment variable changes as +# well as other adjustments to the application environment. + +# The post_start_cartridge and post_stop_cartridge hooks are executed +# immediately after (re)starting or stopping the specified cartridge. + +# Exercise caution when adding commands to these hooks. They can +# prevent your application from stopping cleanly or starting at all. +# Application start and stop is subject to different timeouts +# throughout the system. + +# Source utility functions. +source "$OPENSHIFT_REPO_DIR/.openshift/lib/utils" + +# Get the node version. +ver=$(get_node_version) + + +# Set URI to the custom sample /env route. +app_uri="http://$OPENSHIFT_GEAR_DNS/env" + +# Wait a bit to give the server time to come up. +sleep 5 + +# Check if the app_uri is available - user code could have removed the +# one in the sample -- we just print this for sanity testing. +zcode=$(curl --write-out %{http_code} -s -o /dev/null "$app_uri") + +if [ "$zcode" -eq 200 ]; then + echo "" + echo " - Using Node.js version $ver, checking app URI ... " + echo " - test URI = $app_uri" + echo " - Version from test URI = $(curl -s $app_uri | grep 'Version')" + echo "" +fi + + diff --git a/.openshift/action_hooks/post_stop_nodejs-0.6 b/.openshift/action_hooks/post_stop_nodejs-0.6 new file mode 100755 index 0000000..ff0debd --- /dev/null +++ b/.openshift/action_hooks/post_stop_nodejs-0.6 @@ -0,0 +1,14 @@ +#!/bin/bash + +# The pre_start_cartridge and pre_stop_cartridge hooks are *SOURCED* +# immediately before (re)starting or stopping the specified cartridge. +# They are able to make any desired environment variable changes as +# well as other adjustments to the application environment. + +# The post_start_cartridge and post_stop_cartridge hooks are executed +# immediately after (re)starting or stopping the specified cartridge. + +# Exercise caution when adding commands to these hooks. They can +# prevent your application from stopping cleanly or starting at all. +# Application start and stop is subject to different timeouts +# throughout the system. diff --git a/.openshift/action_hooks/pre_build b/.openshift/action_hooks/pre_build new file mode 100755 index 0000000..505fa76 --- /dev/null +++ b/.openshift/action_hooks/pre_build @@ -0,0 +1,21 @@ +#!/bin/bash +# This is a simple script and will be executed on your CI system if +# available. Otherwise it will execute while your application is stopped +# before the build step. This script gets executed directly, so it +# could be python, php, ruby, etc. + + +# Source utility functions. +source "$OPENSHIFT_REPO_DIR/.openshift/lib/utils" + +# Ensure custom node version if not installed. +echo "" +ensure_node_is_installed + + +# We need to move the package.json file out of the way in pre_build, so +# that the OpenShift git post-receive hook doesn't try and use the old +# npm version to install the dependencies. +mv "${OPENSHIFT_REPO_DIR}/package.json" "$(get_node_tmp_dir)" + + diff --git a/.openshift/action_hooks/pre_start_nodejs-0.6 b/.openshift/action_hooks/pre_start_nodejs-0.6 new file mode 100755 index 0000000..1ebd683 --- /dev/null +++ b/.openshift/action_hooks/pre_start_nodejs-0.6 @@ -0,0 +1,25 @@ +#!/bin/bash + +# The pre_start_cartridge and pre_stop_cartridge hooks are *SOURCED* +# immediately before (re)starting or stopping the specified cartridge. +# They are able to make any desired environment variable changes as +# well as other adjustments to the application environment. + +# The post_start_cartridge and post_stop_cartridge hooks are executed +# immediately after (re)starting or stopping the specified cartridge. + +# Exercise caution when adding commands to these hooks. They can +# prevent your application from stopping cleanly or starting at all. +# Application start and stop is subject to different timeouts +# throughout the system. + + +# Source utility functions. +source "$OPENSHIFT_REPO_DIR/.openshift/lib/utils" + +# Setup path to include the custom Node[.js] version. +ver=$(get_node_version) +echo "" +echo " - pre_start_nodejs: Adding Node.js version $ver binaries to path" +_SHOW_SETUP_PATH_MESSAGES="true" setup_path_for_custom_node_version + diff --git a/.openshift/action_hooks/pre_stop_nodejs-0.6 b/.openshift/action_hooks/pre_stop_nodejs-0.6 new file mode 100755 index 0000000..447cbcc --- /dev/null +++ b/.openshift/action_hooks/pre_stop_nodejs-0.6 @@ -0,0 +1,29 @@ +#!/bin/bash + +# The pre_start_cartridge and pre_stop_cartridge hooks are *SOURCED* +# immediately before (re)starting or stopping the specified cartridge. +# They are able to make any desired environment variable changes as +# well as other adjustments to the application environment. + +# The post_start_cartridge and post_stop_cartridge hooks are executed +# immediately after (re)starting or stopping the specified cartridge. + +# Exercise caution when adding commands to these hooks. They can +# prevent your application from stopping cleanly or starting at all. +# Application start and stop is subject to different timeouts +# throughout the system. + + +# First time in .openshift/lib/utils might not exist, so add a check +# to ensure file exists. +if [ -f "$OPENSHIFT_REPO_DIR/.openshift/lib/utils" ]; then + # Source utility functions. + source "$OPENSHIFT_REPO_DIR/.openshift/lib/utils" + + # Setup path to include the custom Node[.js] version. + ver=$(get_node_version) + echo "" + echo " - pre_stop_nodejs: Adding Node.js version $ver binaries to path" + _SHOW_SETUP_PATH_MESSAGES="true" setup_path_for_custom_node_version +fi + diff --git a/.openshift/cron/README.cron b/.openshift/cron/README.cron new file mode 100644 index 0000000..aad9138 --- /dev/null +++ b/.openshift/cron/README.cron @@ -0,0 +1,22 @@ +Run scripts or jobs on a periodic basis +======================================= +Any scripts or jobs added to the minutely, hourly, daily, weekly or monthly +directories will be run on a scheduled basis (frequency is as indicated by the +name of the directory) using run-parts. + +run-parts ignores any files that are hidden or dotfiles (.*) or backup +files (*~ or *,) or named *.{rpmsave,rpmorig,rpmnew,swp,cfsaved} + +The presence of two specially named files jobs.deny and jobs.allow controls +how run-parts executes your scripts/jobs. + jobs.deny ===> Prevents specific scripts or jobs from being executed. + jobs.allow ===> Only execute the named scripts or jobs (all other/non-named + scripts that exist in this directory are ignored). + +The principles of jobs.deny and jobs.allow are the same as those of cron.deny +and cron.allow and are described in detail at: + http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/ch-Automating_System_Tasks.html#s2-autotasks-cron-access + +See: man crontab or above link for more details and see the the weekly/ + directory for an example. + diff --git a/.openshift/cron/daily/.gitignore b/.openshift/cron/daily/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/.openshift/cron/hourly/.gitignore b/.openshift/cron/hourly/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/.openshift/cron/minutely/.gitignore b/.openshift/cron/minutely/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/.openshift/cron/monthly/.gitignore b/.openshift/cron/monthly/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/.openshift/cron/weekly/README b/.openshift/cron/weekly/README new file mode 100644 index 0000000..7c3e659 --- /dev/null +++ b/.openshift/cron/weekly/README @@ -0,0 +1,16 @@ +Run scripts or jobs on a weekly basis +===================================== +Any scripts or jobs added to this directory will be run on a scheduled basis +(weekly) using run-parts. + +run-parts ignores any files that are hidden or dotfiles (.*) or backup +files (*~ or *,) or named *.{rpmsave,rpmorig,rpmnew,swp,cfsaved} and handles +the files named jobs.deny and jobs.allow specially. + +In this specific example, the chronograph script is the only script or job file +executed on a weekly basis (due to white-listing it in jobs.allow). And the +README and chrono.dat file are ignored either as a result of being black-listed +in jobs.deny or because they are NOT white-listed in the jobs.allow file. + +For more details, please see ../README.cron file. + diff --git a/.openshift/cron/weekly/chrono.dat b/.openshift/cron/weekly/chrono.dat new file mode 100644 index 0000000..fc4abb8 --- /dev/null +++ b/.openshift/cron/weekly/chrono.dat @@ -0,0 +1 @@ +Time And Relative D...n In Execution (Open)Shift! diff --git a/.openshift/cron/weekly/chronograph b/.openshift/cron/weekly/chronograph new file mode 100755 index 0000000..61de949 --- /dev/null +++ b/.openshift/cron/weekly/chronograph @@ -0,0 +1,3 @@ +#!/bin/bash + +echo "`date`: `cat $(dirname \"$0\")/chrono.dat`" diff --git a/.openshift/cron/weekly/jobs.allow b/.openshift/cron/weekly/jobs.allow new file mode 100644 index 0000000..8d32abc --- /dev/null +++ b/.openshift/cron/weekly/jobs.allow @@ -0,0 +1,12 @@ +# +# Script or job files listed in here (one entry per line) will be +# executed on a weekly-basis. +# +# Example: The chronograph script will be executed weekly but the README +# and chrono.dat files in this directory will be ignored. +# +# The README file is actually ignored due to the entry in the +# jobs.deny which is checked before jobs.allow (this file). +# +chronograph + diff --git a/.openshift/cron/weekly/jobs.deny b/.openshift/cron/weekly/jobs.deny new file mode 100644 index 0000000..73c9450 --- /dev/null +++ b/.openshift/cron/weekly/jobs.deny @@ -0,0 +1,7 @@ +# +# Any script or job files listed in here (one entry per line) will NOT be +# executed (read as ignored by run-parts). +# + +README + diff --git a/.openshift/lib/setup_custom_nodejs_env b/.openshift/lib/setup_custom_nodejs_env new file mode 100644 index 0000000..b55b774 --- /dev/null +++ b/.openshift/lib/setup_custom_nodejs_env @@ -0,0 +1,40 @@ +# Utility functions for bash session - sourced in via the user's +# bash profile ($OPENSHIFT_DATA_DIR/.bash_profile). + +# Source utility functions. +source $OPENSHIFT_REPO_DIR/.openshift/lib/utils + + +# Internal function to setup path and remove the wrappers. +function _setup_path_and_remove_wrappers() { + # First invocation of npm or node, so setup the custom path and + # unset the wrappers. Add the custom node binaries to the PATH. + [ -z "$ZDEBUG" ] || echo "Setting path to include custom Node version" + setup_path_for_custom_node_version + unset node + unset npm + unset _setup_path_and_remove_wrappers + +} # End of function _setup_path_and_remove_wrappers. + + +# Temporary wrapper function to setup path before invoking npm. +function npm() { + # Setup path, remove wrappers and reinvoke npm. + _setup_path_and_remove_wrappers + npm "$@" + +} # End of function npm. + + +# Temporary wrapper function to setup path before invoking node. +function node() { + # Setup path, remove wrappers and reinvoke node. + _setup_path_and_remove_wrappers + node "$@" + +} # End of function node. + + +# +# EOF diff --git a/.openshift/lib/utils b/.openshift/lib/utils new file mode 100755 index 0000000..9eec1bc --- /dev/null +++ b/.openshift/lib/utils @@ -0,0 +1,151 @@ +#!/bin/bash +# +# Utility functions. +# + + +# Returns the configured Node version - defaults to 0.8.9. +function get_node_version() { + marker="$OPENSHIFT_REPO_DIR/.openshift/markers/NODEJS_VERSION" + nodejs_ver=$(egrep -v "^\s*#.*" "$marker" | egrep -v "^\s*$" | tail -1) + echo "${nodejs_ver:-"0.8.9"}" + +} # End of function get_node_version. + + +# Returns the directory where Node is to be installed/is installed. +function get_node_install_dir() { + echo "$OPENSHIFT_DATA_DIR" + +} # End of function get_node_install_dir. + + +# Returns the path to the npm binary. +function get_npm_bin_path() { + ver=${1:-"$(get_node_version)"} + echo "$(get_node_install_dir)/node-v$ver-linux-x64/bin" + +} # End of function get_npm_bin_path. + + +# Returns the path to the node binary. +function get_node_bin_path() { + echo "$(get_npm_bin_path $@)" + +} # End of function get_node_bin_path. + + +# Returns the temporary directory we use for processing. +function get_node_tmp_dir() { + ztmpdir="$OPENSHIFT_DATA_DIR/.nodejs.tmp" + + # Ensure temp directory is created. + [ -d "$ztmpdir" ] || mkdir -p "$ztmpdir" + + echo "$ztmpdir" + +} # End of function get_node_tmp_dir. + + +# +# Download and install the specified Node.js version. +# +function _install_nodejs() { + ver=${1:-"$(get_node_version)"} + + # Sample download links: + # http://nodejs.org/dist/v0.8.9/node-v0.8.9-linux-x64.tar.gz + # http://nodejs.org/dist/v0.9.1/node-v0.9.1-linux-x64.tar.gz + zfile="node-v${ver}-linux-x64.tar.gz" + zlink="http://nodejs.org/dist/v${ver}/${zfile}" + + instdir="$(get_node_install_dir)" + + # Download and extract the gzipped tarball. + dldir="$OPENSHIFT_DATA_DIR/downloads" + mkdir -p "$dldir" + + echo " - Downloading and extracting $zlink ... " + + if ! curl -L -o "$dldir/$zfile" "$zlink"; then + echo " - ERROR -- download failed for $zlink" + echo " - download uri = $dldir/$zfile" + return 1 + fi + + (cd "$instdir"; tar -zxf "$dldir/$zfile") + echo " - Done installing Node.js version $ver" + +} # End of function _install_nodejs. + + + +# Ensure the shell env setup bits are added to user's .bash_profile. +function _ensure_bash_profile_setup() { + dot_bash_profile=$OPENSHIFT_DATA_DIR/.bash_profile + pattern='\s*source(.*)\.openshift/lib/setup_custom_nodejs_env\s*(.*)\s*' + if ! egrep "$pattern" $dot_bash_profile > /dev/null 2>&1 ; then + + cat >> $dot_bash_profile <> .openshift/markers/NODEJS_VERSION + + +The action_hooks in this application will use that NODEJS_VERSION marker +file to download and extract that Node version if it is available on +nodejs.org and will automatically set the paths up to use the node/npm +binaries from that install directory. + + See: .openshift/action_hooks/ for more details. + + Note: The last non-blank line in the .openshift/markers/NODEJS_VERSION + file.determines the version it will install. + + +Okay, now onto how can you get a custom Node.js version running +on OpenShift. + + +Steps to get a custom Node.js version running on OpenShift +---------------------------------------------------------- + +Create an account at http://openshift.redhat.com/ + +Create a namespace, if you haven't already do so + + rhc domain create + +Create a nodejs-0.6 application (you can name it anything via -a) + + rhc app create -a palinode -t nodejs-0.6 + +Add this `github nodejs-custom-version-openshift-quickstart` repository + + cd palinode + git remote add upstream -m master git@github.com:ramr/nodejs-custom-version-openshift-quickstart.git + git pull -s recursive -X theirs upstream master + +Optionally, specify the custom version of Node.js you want to run with +(Default is v0.8.9). +If you want to more later version of Node (example v0.9.1), you can change +to that by just writing it to the end of the NODEJS_VERSION file. + + echo "0.9.1" >> .openshift/markers/NODEJS_VERSION + +Then push the repo to OpenShift + + git push + +That's it, you can now checkout your application at: + + http://palinode-$yournamespace.rhcloud.com + ( See env @ http://palinode-$yournamespace.rhcloud.com/env ) -Node.js quickstart application to run the latest or any custom Node.js version on OpenShift \ No newline at end of file diff --git a/deplist.txt b/deplist.txt new file mode 100644 index 0000000..c6dc5df --- /dev/null +++ b/deplist.txt @@ -0,0 +1,13 @@ +# +# *************************************************************************** +# +# Note: This file has been deprecated and exists for backward compatibility. +# Please use package.json to add dependencies to the Node modules +# your application requires. +# +# *************************************************************************** +# + +# +# For a list of globally installed modules - see file: npm_global_module_list. +# diff --git a/index.html b/index.html new file mode 100644 index 0000000..4e32fc8 --- /dev/null +++ b/index.html @@ -0,0 +1,166 @@ + + + + + + Welcome to OpenShift + + + + + OpenShift logo +
OpenShift
+
+

+ Welcome to OpenShift - <your custom Node.js version here> +

+
+
+      Click here to view your Node.js process version and env.
+    
+
+

+ Place your application here +

+

+ In order to commit to your new project, go to your projects git repo (created with the rhc app create command). Make your changes, then run: +

+
+    git commit -a -m 'Some commit message'
+    git push
+  
+

+ Then reload this page. +

+ +

+ What's next? +

+ + + diff --git a/node_modules/read.me b/node_modules/read.me new file mode 100644 index 0000000..075305b --- /dev/null +++ b/node_modules/read.me @@ -0,0 +1,8 @@ + +This directory allows you to package any Node modules (that your application +depends on) along with your application. + +If you just wish to install module(s) from the npm registry (npmjs.org), you +can specify the module name(s) and optionally version in your application's +dependency list file (../deplist.txt). + diff --git a/npm_global_module_list b/npm_global_module_list new file mode 100644 index 0000000..c2cadfd --- /dev/null +++ b/npm_global_module_list @@ -0,0 +1,21 @@ +# +# This file contains a list of globally installed and available npm modules. +# + +# DB drivers. +mongodb +mysql +pg + +# Other modules (including frameworks, middleware etc). +async +connect +express +formidable +generic-pool +hashish +mime +mkdirp +node-static +qs +traverse diff --git a/package.json b/package.json new file mode 100644 index 0000000..4316b93 --- /dev/null +++ b/package.json @@ -0,0 +1,35 @@ +{ + "name": "OpenShift-Sample-App", + "version": "1.0.0", + "description": "OpenShift Sample Application", + "keywords": [ + "OpenShift", + "Node.js", + "application", + "openshift" + ], + "author": { + "name": "OpenShift", + "email": "ramr@example.org", + "url": "http://openshift.redhat.com/" + }, + "homepage": "http://openshift.redhat.com/", + "repository": { + "type": "git", + "url": "https://gitub.com/openshift/crankcase" + }, + + "engines": { + "node": ">= 0.6.0", + "npm": ">= 1.0.0" + }, + + "dependencies": { + "express": "*" + }, + "devDependencies": {}, + "bundleDependencies": [], + + "private": true, + "main": "server.js" +} diff --git a/server.js b/server.js new file mode 100755 index 0000000..129246b --- /dev/null +++ b/server.js @@ -0,0 +1,178 @@ +#!/bin/env node +// OpenShift sample Node application +var express = require('express'); +var fs = require('fs'); + + +/** + * Define the sample application. + */ +var SampleApp = function() { + + // Scope. + var self = this; + + + /* ================================================================ */ + /* Helper functions. */ + /* ================================================================ */ + + /** + * Set up server IP address and port # using env variables/defaults. + */ + self.setupVariables = function() { + // Set the environment variables we need. + self.ipaddress = process.env.OPENSHIFT_INTERNAL_IP; + self.port = process.env.OPENSHIFT_INTERNAL_PORT || 8080; + + if (typeof self.ipaddress === "undefined") { + // Log errors on OpenShift but continue w/ 127.0.0.1 - this + // allows us to run/test the app locally. + console.warn('No OPENSHIFT_INTERNAL_IP var, using 127.0.0.1'); + self.ipaddress = "127.0.0.1"; + }; + }; + + + /** + * Populate the cache. + */ + self.populateCache = function() { + if (typeof self.zcache === "undefined") { + self.zcache = { 'index.html': '' }; + } + + // Local cache for static content. + self.zcache['index.html'] = fs.readFileSync('./index.html'); + }; + + + /** + * Retrieve entry (content) from cache. + * @param {string} key Key identifying content to retrieve from cache. + */ + self.cache_get = function(key) { return self.zcache[key]; }; + + + /** + * terminator === the termination handler + * Terminate server on receipt of the specified signal. + * @param {string} sig Signal to terminate on. + */ + self.terminator = function(sig){ + if (typeof sig === "string") { + console.log('%s: Received %s - terminating sample app ...', + Date(Date.now()), sig); + process.exit(1); + } + console.log('%s: Node server stopped.', Date(Date.now()) ); + }; + + + /** + * Setup termination handlers (for exit and a list of signals). + */ + self.setupTerminationHandlers = function(){ + // Process on exit and signals. + process.on('exit', function() { self.terminator(); }); + + // Removed 'SIGPIPE' from the list - bugz 852598. + ['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT', + 'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM' + ].forEach(function(element, index, array) { + process.on(element, function() { self.terminator(element); }); + }); + }; + + + /* ================================================================ */ + /* App server functions (main app logic here). */ + /* ================================================================ */ + + /** + * Create the routing table entries + handlers for the application. + */ + self.createRoutes = function() { + self.routes = { }; + + // Routes for /health, /asciimo, /env and / + self.routes['/health'] = function(req, res) { + res.send('1'); + }; + + self.routes['/asciimo'] = function(req, res) { + var link = "http://i.imgur.com/kmbjB.png"; + res.send(""); + }; + + self.routes['/env'] = function(req, res) { + var content = 'Version: ' + process.version + '\n
\n' + + 'Env: {
\n
';
+            //  Add env entries.
+            for (var k in process.env) {
+               content += '   ' + k + ': ' + process.env[k] + '\n';
+            }
+            content += '}\n

\n' + res.send(content); + res.send('\n' + + ' Node.js Process Env\n' + + ' \n
\n' + content + '\n'); + }; + + self.routes['/'] = function(req, res) { + res.set('Content-Type', 'text/html'); + res.send(self.cache_get('index.html') ); + }; + }; + + + /** + * Initialize the server (express) and create the routes and register + * the handlers. + */ + self.initializeServer = function() { + self.createRoutes(); + self.app = express.createServer(); + + // Add handlers for the app (from the routes). + for (var r in self.routes) { + self.app.get(r, self.routes[r]); + } + }; + + + /** + * Initializes the sample application. + */ + self.initialize = function() { + self.setupVariables(); + self.populateCache(); + self.setupTerminationHandlers(); + + // Create the express server and routes. + self.initializeServer(); + }; + + + /** + * Start the server (starts up the sample application). + */ + self.start = function() { + // Start the app on the specific interface (and port). + self.app.listen(self.port, self.ipaddress, function() { + console.log('%s: Node server started on %s:%d ...', + Date(Date.now() ), self.ipaddress, self.port); + }); + }; + +}; /* Sample Application. */ + + + +/** + * main(): Main code. + */ +var zapp = new SampleApp(); +zapp.initialize(); +zapp.start(); + From 100bf6b197f53a99c95d1415a07e07ac50d207c2 Mon Sep 17 00:00:00 2001 From: Ram Ranganathan Date: Sat, 22 Sep 2012 21:29:17 -0700 Subject: [PATCH 03/19] Fix + cleanup README instructions. --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c0f0d81..ee403db 100644 --- a/README.md +++ b/README.md @@ -37,16 +37,16 @@ Create an account at http://openshift.redhat.com/ Create a namespace, if you haven't already do so - rhc domain create + rhc domain create Create a nodejs-0.6 application (you can name it anything via -a) rhc app create -a palinode -t nodejs-0.6 -Add this `github nodejs-custom-version-openshift-quickstart` repository +Add this `github nodejs-custom-version-openshift` repository cd palinode - git remote add upstream -m master git@github.com:ramr/nodejs-custom-version-openshift-quickstart.git + git remote add upstream -m master git@github.com:ramr/nodejs-custom-version-openshift.git git pull -s recursive -X theirs upstream master Optionally, specify the custom version of Node.js you want to run with From dfe8c0aafddab793a0ba37c8787dd4a8e91cd936 Mon Sep 17 00:00:00 2001 From: Ram Ranganathan Date: Sat, 22 Sep 2012 21:35:02 -0700 Subject: [PATCH 04/19] Fix README to add step to git commit marker changes. --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ee403db..1011e7e 100644 --- a/README.md +++ b/README.md @@ -52,9 +52,11 @@ Add this `github nodejs-custom-version-openshift` repository Optionally, specify the custom version of Node.js you want to run with (Default is v0.8.9). If you want to more later version of Node (example v0.9.1), you can change -to that by just writing it to the end of the NODEJS_VERSION file. +to that by just writing it to the end of the NODEJS_VERSION file and +committing that change. echo "0.9.1" >> .openshift/markers/NODEJS_VERSION + git commit . -m 'use Node version 0.9.1' Then push the repo to OpenShift From c003c6026c9b4b65f40aa4345347957f6d4dc773 Mon Sep 17 00:00:00 2001 From: Grant Shipley Date: Mon, 15 Oct 2012 12:43:16 -0300 Subject: [PATCH 05/19] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit change repo  --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1011e7e..c93a3ca 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ Create a nodejs-0.6 application (you can name it anything via -a) Add this `github nodejs-custom-version-openshift` repository cd palinode - git remote add upstream -m master git@github.com:ramr/nodejs-custom-version-openshift.git + git remote add upstream -m master git://github.com/openshift/nodejs-custom-version-openshift.git git pull -s recursive -X theirs upstream master Optionally, specify the custom version of Node.js you want to run with From 9b94a0a9d9771ffc8074931f7dd2d4d7d73a1a98 Mon Sep 17 00:00:00 2001 From: ryanj Date: Fri, 1 Mar 2013 17:07:54 -0800 Subject: [PATCH 06/19] Automatically build a copy of node.js based on the requirements listed in the app's package.json file --- .openshift/lib/utils | 13 +++++++++---- .openshift/markers/NODEJS_VERSION | 9 --------- package.json | 13 ++++++++----- 3 files changed, 17 insertions(+), 18 deletions(-) delete mode 100644 .openshift/markers/NODEJS_VERSION diff --git a/.openshift/lib/utils b/.openshift/lib/utils index 9eec1bc..f592a25 100755 --- a/.openshift/lib/utils +++ b/.openshift/lib/utils @@ -4,11 +4,16 @@ # -# Returns the configured Node version - defaults to 0.8.9. +# Returns the configured Node version function get_node_version() { - marker="$OPENSHIFT_REPO_DIR/.openshift/markers/NODEJS_VERSION" - nodejs_ver=$(egrep -v "^\s*#.*" "$marker" | egrep -v "^\s*$" | tail -1) - echo "${nodejs_ver:-"0.8.9"}" + # attempt to read the app's package.json file + marker="$OPENSHIFT_REPO_DIR/package.json" + + # in order to detect the desired node.js version + nodejs_ver=$(grep "\"node\"" "$marker" | tail -n 1 | sed -e 's/^.*" *[~=<>]*[=<>]* *v*\([0-9][^" ]*\) *[^"]*".*$/\1/p;d') + + #default to the latest supported release version (0.6.0). + echo "${nodejs_ver:-"0.6.0"}" } # End of function get_node_version. diff --git a/.openshift/markers/NODEJS_VERSION b/.openshift/markers/NODEJS_VERSION deleted file mode 100644 index 8eb3e0c..0000000 --- a/.openshift/markers/NODEJS_VERSION +++ /dev/null @@ -1,9 +0,0 @@ -# Uncomment one of the version lines to select the node version to use. -# The last "non-blank" version line is the one picked up by the code in -# .openshift/lib/utils -# Default: 0.8.9 -# -# 0.8.9 -# 0.9.1 -0.8.9 - diff --git a/package.json b/package.json index 4316b93..ce13f57 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "OpenShift-Sample-App", - "version": "1.0.0", - "description": "OpenShift Sample Application", + "name": "OpenShift-Nodejs-Cartridge", + "version": "1.1.0", + "description": "Node.js Cartridge for OpenShift", "keywords": [ "OpenShift", "Node.js", @@ -10,13 +10,13 @@ ], "author": { "name": "OpenShift", - "email": "ramr@example.org", + "email": "ryanj@redhat.com", "url": "http://openshift.redhat.com/" }, "homepage": "http://openshift.redhat.com/", "repository": { "type": "git", - "url": "https://gitub.com/openshift/crankcase" + "url": "https://gitub.com/openshift/nodejs-custom-version-openshift.git" }, "engines": { @@ -31,5 +31,8 @@ "bundleDependencies": [], "private": true, + "scripts": { + "start": "node server.js" + }, "main": "server.js" } From 1e8dd2677f93c275a52614a7b0bed81d9c77c874 Mon Sep 17 00:00:00 2001 From: ryanj Date: Fri, 1 Mar 2013 17:33:23 -0800 Subject: [PATCH 07/19] updating README.md file to match --- README.md | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index c93a3ca..28413e6 100644 --- a/README.md +++ b/README.md @@ -8,23 +8,18 @@ of Node on RedHat's OpenShift PaaS. Selecting a Node version to install/use --------------------------------------- -To select the version of Node.js that you want to run, just edit or add -a version to the .openshift/markers/NODEJS_VERSION file. +To select the version of Node.js that you want to run, +update the 'engines' section of your app's package.json file. - Example: To install Node.js version 0.9.1, you can run: - $ echo -e "0.9.1\n" >> .openshift/markers/NODEJS_VERSION + Example: To install Node.js version 0.8.21, update your package.json file: + $ sed -e 's/"node": ".*"/"node": ">= 0.8.21"/' -i package.json -The action_hooks in this application will use that NODEJS_VERSION marker -file to download and extract that Node version if it is available on -nodejs.org and will automatically set the paths up to use the node/npm -binaries from that install directory. - - See: .openshift/action_hooks/ for more details. - - Note: The last non-blank line in the .openshift/markers/NODEJS_VERSION - file.determines the version it will install. +The action_hooks in this app will automatically download, build, +and install a copy of Node.js that matches the requirements specified in +your app's package.json file. + See: .openshift/action_hooks/ for more informaiton. Okay, now onto how can you get a custom Node.js version running on OpenShift. @@ -49,16 +44,16 @@ Add this `github nodejs-custom-version-openshift` repository git remote add upstream -m master git://github.com/openshift/nodejs-custom-version-openshift.git git pull -s recursive -X theirs upstream master -Optionally, specify the custom version of Node.js you want to run with -(Default is v0.8.9). -If you want to more later version of Node (example v0.9.1), you can change -to that by just writing it to the end of the NODEJS_VERSION file and -committing that change. +If you would like to use a more recent version of Node.js (example v0.9.1), just update the 'engines' section of your app's package.json file: + + sed -e 's/"node": ".*"/"node": ">= 0.8.21"/' -i package.json + +Commit your changes locally: - echo "0.9.1" >> .openshift/markers/NODEJS_VERSION - git commit . -m 'use Node version 0.9.1' + git add package.json + git commit -m 'updating package.json to select Node.js version 0.8.21' -Then push the repo to OpenShift +Then push your updates to OpenShift git push From 39e7fd0f86a052a5327d3fc96617c11b1164f6ed Mon Sep 17 00:00:00 2001 From: ryanj Date: Fri, 1 Mar 2013 17:53:57 -0800 Subject: [PATCH 08/19] cleaning up default H1 content --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index 4e32fc8..6cf71b0 100644 --- a/index.html +++ b/index.html @@ -127,7 +127,7 @@
OpenShift

- Welcome to OpenShift - <your custom Node.js version here> + Welcome to OpenShift


From 1ccd9c7cc2b81249035473ccf0bc4a4b15043fad Mon Sep 17 00:00:00 2001
From: ryanj 
Date: Mon, 4 Mar 2013 10:01:53 -0800
Subject: [PATCH 09/19] cleaning up README instructions

---
 README.md | 66 +++++++++++++++++++++++--------------------------------
 1 file changed, 27 insertions(+), 39 deletions(-)

diff --git a/README.md b/README.md
index 28413e6..17d64f3 100644
--- a/README.md
+++ b/README.md
@@ -1,52 +1,44 @@
-Running a custom/latest Node[.js] version on RedHat's OpenShift PaaS
+Node.js on OpenShift
 ====================================================================
-This git repository is a sample Node application along with the
-"orchestration" bits to help you run the latest or a custom version
-of Node on RedHat's OpenShift PaaS.
+This package includes a dynamic Node.js build stage that will provide your application with a customized Node.js runtime.
+The version of Node that is available will depend on the requirements listed in your application's `package.json` file.
 
+See: `.openshift/action_hooks/` for more informaiton on how the OpenShift build process works.
 
-Selecting a Node version to install/use
----------------------------------------
+Basic Setup
+-----------
 
-To select the version of Node.js that you want to run, 
-update the 'engines' section of your app's package.json file.
+If this is your first time using OpenShift Online or Node.js, you'll have some quick prep-work to do:
 
-    Example: To install Node.js version 0.8.21, update your package.json file:
-       $ sed -e 's/"node": ".*"/"node": ">= 0.8.21"/' -i package.json
+1. [Create an OpenShift Online account](http://openshift.redhat.com/app/account/new)
+2. If you don't already have the rhc (Red Hat Cloud) command-line tools, run: `sudo gem install rhc`
+3. Run `rhc setup` to link your OpenShift Online account with your local development environment, and to select an application namespace
+4. [Download and install Node.js](http://nodejs.org) for use in your local development environment: http://nodejs.org
 
+If you need any additional help getting started, these links may come in handy:
 
-The action_hooks in this app will automatically download, build,
-and install a copy of Node.js that matches the requirements specified in
-your app's package.json file.
+ * https://openshift.redhat.com/community/get-started#cli
+ * https://openshift.redhat.com/community/developers/rhc-client-tools-install
 
-     See: .openshift/action_hooks/ for more informaiton.
+Host your Node.js applications on OpenShift
+-------------------------------------------
 
-Okay, now onto how can you get a custom Node.js version running
-on OpenShift.
+Create a Node.js application.  This example will produce an application named **nodeapp**:
 
+    rhc app create nodeapp nodejs --from-code=git://github.com/openshift/nodejs-custom-version-openshift.git
 
-Steps to get a custom Node.js version running on OpenShift
-----------------------------------------------------------
+The above example will output a folder named after your application which contains your local development source.  Make sure to run it from within a directory where you would like to store your development code.
 
-Create an account at http://openshift.redhat.com/
+That's it!  You should be able to access your application at:
 
-Create a namespace, if you haven't already do so
+    http://nodeapp-$yournamespace.rhcloud.com
 
-    rhc domain create 
+If your app requires a specific version of Node.js, just update the 'engines' section of your app's `package.json` file to specify your runtime requirements:
 
-Create a nodejs-0.6 application (you can name it anything via -a)
-
-    rhc app create -a palinode  -t nodejs-0.6
-
-Add this `github nodejs-custom-version-openshift` repository
-
-    cd palinode
-    git remote add upstream -m master git://github.com/openshift/nodejs-custom-version-openshift.git
-    git pull -s recursive -X theirs upstream master
-
-If you would like to use a more recent version of Node.js (example v0.9.1), just update the 'engines' section of your app's package.json file:
-
-    sed -e 's/"node": ".*"/"node": ">= 0.8.21"/' -i package.json
+    "engines": {
+        "node": ">= 0.8.21",
+        "npm": ">= 1.0.0"
+     },
 
 Commit your changes locally:
 
@@ -57,8 +49,4 @@ Then push your updates to OpenShift
 
     git push
 
-That's it, you can now checkout your application at:
-
-    http://palinode-$yournamespace.rhcloud.com
-    ( See env @ http://palinode-$yournamespace.rhcloud.com/env )
-
+Additional updates can be made via the same `git add`, `git commit`, and `git push` workflow.

From 46f83a978e9e9968ebc3ab4ae177f18c8d556f82 Mon Sep 17 00:00:00 2001
From: ryanj 
Date: Mon, 4 Mar 2013 17:09:09 -0800
Subject: [PATCH 10/19] if sed fails to match, use the NODEJS_VERSION marker as
 a backup

---
 .openshift/lib/utils | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/.openshift/lib/utils b/.openshift/lib/utils
index f592a25..90d2f03 100755
--- a/.openshift/lib/utils
+++ b/.openshift/lib/utils
@@ -6,14 +6,22 @@
 
 #  Returns the configured Node version
 function get_node_version() {
-   # attempt to read the app's package.json file
-   marker="$OPENSHIFT_REPO_DIR/package.json"
-
-   # in order to detect the desired node.js version
-   nodejs_ver=$(grep "\"node\"" "$marker" | tail -n 1 | sed -e 's/^.*" *[~=<>]*[=<>]* *v*\([0-9][^" ]*\) *[^"]*".*$/\1/p;d')
-
-   #default to the latest supported release version (0.6.0).
-   echo "${nodejs_ver:-"0.6.0"}"
+   # read the app's package.json file
+   package_json="$OPENSHIFT_REPO_DIR/package.json"
+
+   # attempt to detect the desired node.js version
+   engine=$(grep "\"node\"" "$package_json" | tail -n 1 | sed -e 's/^.*" *[~=<>]*[=<>]* *v*\([0-9][^" ]*\) *[^"]*".*$/\1/p;d')
+
+   # assume that a deprecated marker value is a reasonable default
+   marker="$OPENSHIFT_REPO_DIR/.openshift/markers/NODEJS_VERSION"
+   marker_ver=$(egrep -v "^\s*#.*" "$marker" | egrep -v "^\s*$" | tail -1)
+   
+   # use the OpenShift cart default of 0.6.0 as a final option:
+   if [ -z $engine ]; then
+       echo ${marker_ver-"0.6.0"}
+   else
+       echo ${engine}
+   fi
 
 }  #  End of function  get_node_version.
 

From 739e836cdb79e821ddf792ad5c74f0ed1dbb6b3c Mon Sep 17 00:00:00 2001
From: ryan jarvinen 
Date: Mon, 11 Mar 2013 23:25:35 -0300
Subject: [PATCH 11/19] v0.10.0 released!

---
 README.md | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 17d64f3..d7b9b30 100644
--- a/README.md
+++ b/README.md
@@ -25,7 +25,7 @@ Host your Node.js applications on OpenShift
 
 Create a Node.js application.  This example will produce an application named **nodeapp**:
 
-    rhc app create nodeapp nodejs --from-code=git://github.com/openshift/nodejs-custom-version-openshift.git
+    rhc app create nodeapp nodejs --from-code=git://github.com/ryanj/nodejs-custom-version-openshift.git
 
 The above example will output a folder named after your application which contains your local development source.  Make sure to run it from within a directory where you would like to store your development code.
 
@@ -36,7 +36,7 @@ That's it!  You should be able to access your application at:
 If your app requires a specific version of Node.js, just update the 'engines' section of your app's `package.json` file to specify your runtime requirements:
 
     "engines": {
-        "node": ">= 0.8.21",
+        "node": ">= 0.10.0",
         "npm": ">= 1.0.0"
      },
 

From 8d7e424ca9eb4de42ac08ee8c8c52c2d4ca7868a Mon Sep 17 00:00:00 2001
From: Dan McPherson 
Date: Wed, 22 May 2013 11:31:10 -0400
Subject: [PATCH 12/19] Updating for V2

---
 server.js | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/server.js b/server.js
index 129246b..b513a30 100755
--- a/server.js
+++ b/server.js
@@ -22,13 +22,13 @@ var SampleApp = function() {
      */
     self.setupVariables = function() {
         //  Set the environment variables we need.
-        self.ipaddress = process.env.OPENSHIFT_INTERNAL_IP;
-        self.port      = process.env.OPENSHIFT_INTERNAL_PORT || 8080;
+        self.ipaddress = process.env.OPENSHIFT_INTERNAL_IP || process.env.OPENSHIFT_NODEJS_IP;
+        self.port      = process.env.OPENSHIFT_INTERNAL_PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080;
 
         if (typeof self.ipaddress === "undefined") {
             //  Log errors on OpenShift but continue w/ 127.0.0.1 - this
             //  allows us to run/test the app locally.
-            console.warn('No OPENSHIFT_INTERNAL_IP var, using 127.0.0.1');
+            console.warn('No OPENSHIFT_NODEJS_IP var, using 127.0.0.1');
             self.ipaddress = "127.0.0.1";
         };
     };

From b1b133a0436934f61f981201a1955f391f4bea88 Mon Sep 17 00:00:00 2001
From: Mrunal Patel 
Date: Thu, 23 May 2013 16:30:34 -0700
Subject: [PATCH 13/19] Add pre-build hook to make this work with v2 cartridge
 format.

---
 .openshift/action_hooks/pre-build | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100755 .openshift/action_hooks/pre-build

diff --git a/.openshift/action_hooks/pre-build b/.openshift/action_hooks/pre-build
new file mode 100755
index 0000000..505fa76
--- /dev/null
+++ b/.openshift/action_hooks/pre-build
@@ -0,0 +1,21 @@
+#!/bin/bash
+# This is a simple script and will be executed on your CI system if
+# available.  Otherwise it will execute while your application is stopped
+# before the build step.  This script gets executed directly, so it
+# could be python, php, ruby, etc.
+
+
+#  Source utility functions.
+source "$OPENSHIFT_REPO_DIR/.openshift/lib/utils"
+
+#  Ensure custom node version if not installed.
+echo ""
+ensure_node_is_installed
+
+
+#  We need to move the package.json file out of the way in pre_build, so
+#  that the OpenShift git post-receive hook doesn't try and use the old
+#  npm version to install the dependencies.
+mv  "${OPENSHIFT_REPO_DIR}/package.json"  "$(get_node_tmp_dir)"
+
+

From f385b982fb6fc7368770c1f20d7f18ea84f72027 Mon Sep 17 00:00:00 2001
From: ryan jarvinen 
Date: Fri, 7 Jun 2013 15:10:41 -0600
Subject: [PATCH 14/19] Updating OPENSHIFT_*_IP and OPENSHIFT_*_PORT env var
 names to support the new V2 cart config

---
 .openshift/action_hooks/build                 |  2 +-
 .openshift/action_hooks/post_start_nodejs-0.6 | 41 -------------------
 .openshift/action_hooks/post_stop_nodejs-0.6  | 14 -------
 .openshift/action_hooks/pre_build             |  1 -
 ...{pre_start_nodejs-0.6 => pre_start_nodejs} |  1 -
 .openshift/action_hooks/pre_stop_nodejs-0.6   | 29 -------------
 .openshift/lib/utils                          | 10 ++---
 deplist.txt                                   | 13 ------
 npm_global_module_list                        | 21 ----------
 server.js                                     |  6 +--
 10 files changed, 9 insertions(+), 129 deletions(-)
 delete mode 100755 .openshift/action_hooks/post_start_nodejs-0.6
 delete mode 100755 .openshift/action_hooks/post_stop_nodejs-0.6
 rename .openshift/action_hooks/{pre_start_nodejs-0.6 => pre_start_nodejs} (99%)
 mode change 100755 => 100644
 delete mode 100755 .openshift/action_hooks/pre_stop_nodejs-0.6
 delete mode 100644 deplist.txt
 delete mode 100644 npm_global_module_list

diff --git a/.openshift/action_hooks/build b/.openshift/action_hooks/build
index 85794bd..89cf6dd 100755
--- a/.openshift/action_hooks/build
+++ b/.openshift/action_hooks/build
@@ -12,7 +12,7 @@ source "$OPENSHIFT_REPO_DIR/.openshift/lib/utils"
 _SHOW_SETUP_PATH_MESSAGES="true" setup_path_for_custom_node_version
 
 
-#  So we we moved the package.json file out of the way in pre_build,
+#  we moved the package.json file out of the way in pre_build,
 #  so that the OpenShift git post-receive hook doesn't try and use the
 #  old npm version to install the dependencies. Move it back in.
 tmp_package_json="$(get_node_tmp_dir)/package.json"
diff --git a/.openshift/action_hooks/post_start_nodejs-0.6 b/.openshift/action_hooks/post_start_nodejs-0.6
deleted file mode 100755
index b3ccc71..0000000
--- a/.openshift/action_hooks/post_start_nodejs-0.6
+++ /dev/null
@@ -1,41 +0,0 @@
-#!/bin/bash
-
-# The pre_start_cartridge and pre_stop_cartridge hooks are *SOURCED*
-# immediately before (re)starting or stopping the specified cartridge.
-# They are able to make any desired environment variable changes as
-# well as other adjustments to the application environment.
-
-# The post_start_cartridge and post_stop_cartridge hooks are executed
-# immediately after (re)starting or stopping the specified cartridge.
-
-# Exercise caution when adding commands to these hooks.  They can
-# prevent your application from stopping cleanly or starting at all.
-# Application start and stop is subject to different timeouts
-# throughout the system.
-
-#  Source utility functions.
-source "$OPENSHIFT_REPO_DIR/.openshift/lib/utils"
-
-#  Get the node version.
-ver=$(get_node_version)
-
-
-#  Set URI to the custom sample /env route.
-app_uri="http://$OPENSHIFT_GEAR_DNS/env"
-
-#  Wait a bit to give the server time to come up.
-sleep 5
-
-#  Check if the app_uri is available - user code could have removed the
-#  one in the sample -- we just print this for sanity testing.
-zcode=$(curl --write-out %{http_code} -s -o /dev/null "$app_uri")
-
-if [ "$zcode" -eq 200 ]; then
-   echo ""
-   echo "  - Using Node.js version $ver, checking app URI ... "
-   echo "  - test URI = $app_uri"
-   echo "  - Version from test URI = $(curl -s $app_uri | grep 'Version')"
-   echo ""
-fi
-
-
diff --git a/.openshift/action_hooks/post_stop_nodejs-0.6 b/.openshift/action_hooks/post_stop_nodejs-0.6
deleted file mode 100755
index ff0debd..0000000
--- a/.openshift/action_hooks/post_stop_nodejs-0.6
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/bin/bash
-
-# The pre_start_cartridge and pre_stop_cartridge hooks are *SOURCED*
-# immediately before (re)starting or stopping the specified cartridge.
-# They are able to make any desired environment variable changes as
-# well as other adjustments to the application environment.
-
-# The post_start_cartridge and post_stop_cartridge hooks are executed
-# immediately after (re)starting or stopping the specified cartridge.
-
-# Exercise caution when adding commands to these hooks.  They can
-# prevent your application from stopping cleanly or starting at all.
-# Application start and stop is subject to different timeouts
-# throughout the system.
diff --git a/.openshift/action_hooks/pre_build b/.openshift/action_hooks/pre_build
index 505fa76..75c7d3b 100755
--- a/.openshift/action_hooks/pre_build
+++ b/.openshift/action_hooks/pre_build
@@ -18,4 +18,3 @@ ensure_node_is_installed
 #  npm version to install the dependencies.
 mv  "${OPENSHIFT_REPO_DIR}/package.json"  "$(get_node_tmp_dir)"
 
-
diff --git a/.openshift/action_hooks/pre_start_nodejs-0.6 b/.openshift/action_hooks/pre_start_nodejs
old mode 100755
new mode 100644
similarity index 99%
rename from .openshift/action_hooks/pre_start_nodejs-0.6
rename to .openshift/action_hooks/pre_start_nodejs
index 1ebd683..694faef
--- a/.openshift/action_hooks/pre_start_nodejs-0.6
+++ b/.openshift/action_hooks/pre_start_nodejs
@@ -22,4 +22,3 @@ ver=$(get_node_version)
 echo ""
 echo "  - pre_start_nodejs: Adding Node.js version $ver binaries to path"
 _SHOW_SETUP_PATH_MESSAGES="true" setup_path_for_custom_node_version
-
diff --git a/.openshift/action_hooks/pre_stop_nodejs-0.6 b/.openshift/action_hooks/pre_stop_nodejs-0.6
deleted file mode 100755
index 447cbcc..0000000
--- a/.openshift/action_hooks/pre_stop_nodejs-0.6
+++ /dev/null
@@ -1,29 +0,0 @@
-#!/bin/bash
-
-# The pre_start_cartridge and pre_stop_cartridge hooks are *SOURCED*
-# immediately before (re)starting or stopping the specified cartridge.
-# They are able to make any desired environment variable changes as
-# well as other adjustments to the application environment.
-
-# The post_start_cartridge and post_stop_cartridge hooks are executed
-# immediately after (re)starting or stopping the specified cartridge.
-
-# Exercise caution when adding commands to these hooks.  They can
-# prevent your application from stopping cleanly or starting at all.
-# Application start and stop is subject to different timeouts
-# throughout the system.
-
-
-#  First time in .openshift/lib/utils might not exist, so add a check
-#  to ensure file exists.
-if [ -f "$OPENSHIFT_REPO_DIR/.openshift/lib/utils" ]; then
-   #  Source utility functions.
-   source "$OPENSHIFT_REPO_DIR/.openshift/lib/utils"
-
-   #  Setup path to include the custom Node[.js] version.
-   ver=$(get_node_version)
-   echo ""
-   echo "  - pre_stop_nodejs: Adding Node.js version $ver binaries to path"
-   _SHOW_SETUP_PATH_MESSAGES="true" setup_path_for_custom_node_version
-fi
-
diff --git a/.openshift/lib/utils b/.openshift/lib/utils
index 90d2f03..139927d 100755
--- a/.openshift/lib/utils
+++ b/.openshift/lib/utils
@@ -7,18 +7,18 @@
 #  Returns the configured Node version
 function get_node_version() {
    # read the app's package.json file
-   package_json="$OPENSHIFT_REPO_DIR/package.json"
+   package_json="${OPENSHIFT_REPO_DIR}/package.json"
 
    # attempt to detect the desired node.js version
    engine=$(grep "\"node\"" "$package_json" | tail -n 1 | sed -e 's/^.*" *[~=<>]*[=<>]* *v*\([0-9][^" ]*\) *[^"]*".*$/\1/p;d')
 
    # assume that a deprecated marker value is a reasonable default
-   marker="$OPENSHIFT_REPO_DIR/.openshift/markers/NODEJS_VERSION"
-   marker_ver=$(egrep -v "^\s*#.*" "$marker" | egrep -v "^\s*$" | tail -1)
+   marker="${OPENSHIFT_REPO_DIR}.openshift/markers/NODEJS_VERSION"
+   marker_ver=$(egrep -v "^\s*#.*" "$marker" 2>/dev/null | egrep -v "^\s*$" | tail -1)
    
-   # use the OpenShift cart default of 0.6.0 as a final option:
+   # use the OpenShift cart default of 0.6.20 as a final option:
    if [ -z $engine ]; then
-       echo ${marker_ver-"0.6.0"}
+       echo ${marker_ver-"0.6.20"}
    else
        echo ${engine}
    fi
diff --git a/deplist.txt b/deplist.txt
deleted file mode 100644
index c6dc5df..0000000
--- a/deplist.txt
+++ /dev/null
@@ -1,13 +0,0 @@
-#
-#  ***************************************************************************
-#
-#    Note: This file has been deprecated and exists for backward compatibility.
-#          Please use package.json to add dependencies to the Node modules
-#          your application requires.
-#
-#  ***************************************************************************
-#
-
-#
-#  For a list of globally installed modules - see file: npm_global_module_list.
-#
diff --git a/npm_global_module_list b/npm_global_module_list
deleted file mode 100644
index c2cadfd..0000000
--- a/npm_global_module_list
+++ /dev/null
@@ -1,21 +0,0 @@
-#
-#  This file contains a list of globally installed and available npm modules.
-#
-
-#  DB drivers.
-mongodb
-mysql
-pg
-
-#  Other modules (including frameworks, middleware etc).
-async
-connect
-express
-formidable
-generic-pool
-hashish
-mime
-mkdirp
-node-static
-qs
-traverse
diff --git a/server.js b/server.js
index 129246b..6fa36ac 100755
--- a/server.js
+++ b/server.js
@@ -22,13 +22,13 @@ var SampleApp = function() {
      */
     self.setupVariables = function() {
         //  Set the environment variables we need.
-        self.ipaddress = process.env.OPENSHIFT_INTERNAL_IP;
-        self.port      = process.env.OPENSHIFT_INTERNAL_PORT || 8080;
+        self.ipaddress = process.env.OPENSHIFT_NODEJS_IP;
+        self.port      = process.env.OPENSHIFT_NODEJS_PORT || 8080;
 
         if (typeof self.ipaddress === "undefined") {
             //  Log errors on OpenShift but continue w/ 127.0.0.1 - this
             //  allows us to run/test the app locally.
-            console.warn('No OPENSHIFT_INTERNAL_IP var, using 127.0.0.1');
+            console.warn('No OPENSHIFT_NODEJS_IP var, using 127.0.0.1');
             self.ipaddress = "127.0.0.1";
         };
     };

From 523c50a34b3100aca988809039372b2788abe807 Mon Sep 17 00:00:00 2001
From: ryanj 
Date: Tue, 11 Jun 2013 15:23:11 -0700
Subject: [PATCH 15/19] bumping default nodejs version to 0.10.9

---
 package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/package.json b/package.json
index ce13f57..2ac1dc6 100644
--- a/package.json
+++ b/package.json
@@ -20,7 +20,7 @@
   },
 
   "engines": {
-    "node": ">= 0.6.0",
+    "node": ">= 0.10.9",
     "npm": ">= 1.0.0"
   },
 

From 43490c26b715fbe6fbd554733a5b23b09b15d6da Mon Sep 17 00:00:00 2001
From: ryanj 
Date: Tue, 11 Jun 2013 18:04:11 -0700
Subject: [PATCH 16/19] marking pre_start init script as executable

---
 .openshift/action_hooks/pre_start_nodejs | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 mode change 100644 => 100755 .openshift/action_hooks/pre_start_nodejs

diff --git a/.openshift/action_hooks/pre_start_nodejs b/.openshift/action_hooks/pre_start_nodejs
old mode 100644
new mode 100755

From a75142c55d565dc2aaeb0c4315ab3a1bea503653 Mon Sep 17 00:00:00 2001
From: ryanj 
Date: Tue, 3 Sep 2013 15:00:09 -0700
Subject: [PATCH 17/19] removing sample server.js application code

---
 server.js | 177 ------------------------------------------------------
 web.js    |   1 +
 2 files changed, 1 insertion(+), 177 deletions(-)
 delete mode 100755 server.js

diff --git a/server.js b/server.js
deleted file mode 100755
index a45256b..0000000
--- a/server.js
+++ /dev/null
@@ -1,177 +0,0 @@
-#!/bin/env node
-//  OpenShift sample Node application
-var express = require('express');
-var fs      = require('fs');
-
-
-/**
- *  Define the sample application.
- */
-var SampleApp = function() {
-
-    //  Scope.
-    var self = this;
-
-
-    /*  ================================================================  */
-    /*  Helper functions.                                                 */
-    /*  ================================================================  */
-
-    /**
-     *  Set up server IP address and port # using env variables/defaults.
-     */
-    self.setupVariables = function() {
-        //  Set the environment variables we need.
-        self.ipaddress = process.env.OPENSHIFT_INTERNAL_IP || process.env.OPENSHIFT_NODEJS_IP;
-        self.port      = process.env.OPENSHIFT_INTERNAL_PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080;
-
-        if (typeof self.ipaddress === "undefined") {
-            //  Log errors on OpenShift but continue w/ 127.0.0.1 - this
-            //  allows us to run/test the app locally.
-            console.warn('No OPENSHIFT_NODEJS_IP var, using 127.0.0.1');
-            self.ipaddress = "127.0.0.1";
-        };
-    };
-
-
-    /**
-     *  Populate the cache.
-     */
-    self.populateCache = function() {
-        if (typeof self.zcache === "undefined") {
-            self.zcache = { 'index.html': '' };
-        }
-
-        //  Local cache for static content.
-        self.zcache['index.html'] = fs.readFileSync('./index.html');
-    };
-
-
-    /**
-     *  Retrieve entry (content) from cache.
-     *  @param {string} key  Key identifying content to retrieve from cache.
-     */
-    self.cache_get = function(key) { return self.zcache[key]; };
-
-
-    /**
-     *  terminator === the termination handler
-     *  Terminate server on receipt of the specified signal.
-     *  @param {string} sig  Signal to terminate on.
-     */
-    self.terminator = function(sig){
-        if (typeof sig === "string") {
-           console.log('%s: Received %s - terminating sample app ...',
-                       Date(Date.now()), sig);
-           process.exit(1);
-        }
-        console.log('%s: Node server stopped.', Date(Date.now()) );
-    };
-
-
-    /**
-     *  Setup termination handlers (for exit and a list of signals).
-     */
-    self.setupTerminationHandlers = function(){
-        //  Process on exit and signals.
-        process.on('exit', function() { self.terminator(); });
-
-        // Removed 'SIGPIPE' from the list - bugz 852598.
-        ['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
-         'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
-        ].forEach(function(element, index, array) {
-            process.on(element, function() { self.terminator(element); });
-        });
-    };
-
-
-    /*  ================================================================  */
-    /*  App server functions (main app logic here).                       */
-    /*  ================================================================  */
-
-    /**
-     *  Create the routing table entries + handlers for the application.
-     */
-    self.createRoutes = function() {
-        self.routes = { };
-
-        // Routes for /health, /asciimo, /env and /
-        self.routes['/health'] = function(req, res) {
-            res.send('1');
-        };
-
-        self.routes['/asciimo'] = function(req, res) {
-            var link = "http://i.imgur.com/kmbjB.png";
-            res.send("");
-        };
-
-        self.routes['/env'] = function(req, res) {
-            var content = 'Version: ' + process.version + '\n
\n' + - 'Env: {
\n
';
-            //  Add env entries.
-            for (var k in process.env) {
-               content += '   ' + k + ': ' + process.env[k] + '\n';
-            }
-            content += '}\n

\n' - res.send(content); - res.send('\n' + - ' Node.js Process Env\n' + - ' \n
\n' + content + '\n'); - }; - - self.routes['/'] = function(req, res) { - res.set('Content-Type', 'text/html'); - res.send(self.cache_get('index.html') ); - }; - }; - - - /** - * Initialize the server (express) and create the routes and register - * the handlers. - */ - self.initializeServer = function() { - self.createRoutes(); - self.app = express.createServer(); - - // Add handlers for the app (from the routes). - for (var r in self.routes) { - self.app.get(r, self.routes[r]); - } - }; - - - /** - * Initializes the sample application. - */ - self.initialize = function() { - self.setupVariables(); - self.populateCache(); - self.setupTerminationHandlers(); - - // Create the express server and routes. - self.initializeServer(); - }; - - - /** - * Start the server (starts up the sample application). - */ - self.start = function() { - // Start the app on the specific interface (and port). - self.app.listen(self.port, self.ipaddress, function() { - console.log('%s: Node server started on %s:%d ...', - Date(Date.now() ), self.ipaddress, self.port); - }); - }; - -}; /* Sample Application. */ - - - -/** - * main(): Main code. - */ -var zapp = new SampleApp(); -zapp.initialize(); -zapp.start(); diff --git a/web.js b/web.js index c9cf1a5..58eaab4 100644 --- a/web.js +++ b/web.js @@ -1,3 +1,4 @@ +#!/bin/env node // Setup var application_root = __dirname, secret = process.env.SECRET, From b844148430be47519d75559d28a79c40815f831a Mon Sep 17 00:00:00 2001 From: ryanj Date: Tue, 3 Sep 2013 15:17:17 -0700 Subject: [PATCH 18/19] updating README notes with OpenShift usage --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 315e40a..45e4c76 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,12 @@ https://github.com/okfn/annotator-store ### Using OpenShift - rhc app create APP_NAME nodejs mongodb-2.2 --env SECRET=YOUR_SECRET_KEY CONSUMER=YOUR_CONSUMER_KEY --from-code=https://github.com/ryanj/MIT-Annotation-Data-Store.git + rhc app create APP_NAME nodejs mongodb-2.2 --env SECRET=YOUR_SECRET_KEY --env CONSUMER=YOUR_CONSUMER_KEY --from-code=https://github.com/ryanj/MIT-Annotation-Data-Store.git + +To update application secrets and keys after the initial build: + + rhc env set CONSUMER=YOUR_CONSUMER_KEY -a APP_NAME + rhc app restart -a APP_NAME ## Dependencies ### OKFN Annotator From f551e529842137d736e632cebcd9a33302942d9f Mon Sep 17 00:00:00 2001 From: ryanj Date: Tue, 3 Sep 2013 16:01:28 -0700 Subject: [PATCH 19/19] removing a few old npm deps --- package.json | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/package.json b/package.json index ce7c464..642cde0 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,7 @@ "jwt-simple": "0.1.0", "express": "3.1.0", "less-middleware": "0.1.6", - "mongoose": "3.5.7", - "timekit": "0.1.9", - "nodetime": "0.8.10", - "v8tools": "0.2.1" + "mongoose": "3.5.7" }, "engines": { "node": "0.8.19",