Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging in a basic OpenShift application skeleton and instructions on how to deploy to openshift #7

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .openshift/action_hooks/build
Original file line number Diff line number Diff line change
@@ -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


# 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

16 changes: 16 additions & 0 deletions .openshift/action_hooks/deploy
Original file line number Diff line number Diff line change
@@ -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

4 changes: 4 additions & 0 deletions .openshift/action_hooks/post_deploy
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 20 additions & 0 deletions .openshift/action_hooks/pre_build
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/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)"

24 changes: 24 additions & 0 deletions .openshift/action_hooks/pre_start_nodejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/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
22 changes: 22 additions & 0 deletions .openshift/cron/README.cron
Original file line number Diff line number Diff line change
@@ -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.

Empty file.
Empty file.
Empty file.
Empty file.
16 changes: 16 additions & 0 deletions .openshift/cron/weekly/README
Original file line number Diff line number Diff line change
@@ -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.

1 change: 1 addition & 0 deletions .openshift/cron/weekly/chrono.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Time And Relative D...n In Execution (Open)Shift!
3 changes: 3 additions & 0 deletions .openshift/cron/weekly/chronograph
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "`date`: `cat $(dirname \"$0\")/chrono.dat`"
12 changes: 12 additions & 0 deletions .openshift/cron/weekly/jobs.allow
Original file line number Diff line number Diff line change
@@ -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

7 changes: 7 additions & 0 deletions .openshift/cron/weekly/jobs.deny
Original file line number Diff line number Diff line change
@@ -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

40 changes: 40 additions & 0 deletions .openshift/lib/setup_custom_nodejs_env
Original file line number Diff line number Diff line change
@@ -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
164 changes: 164 additions & 0 deletions .openshift/lib/utils
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#!/bin/bash
#
# Utility functions.
#


# Returns the configured Node version
function get_node_version() {
# 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" 2>/dev/null | egrep -v "^\s*$" | tail -1)

# use the OpenShift cart default of 0.6.20 as a final option:
if [ -z $engine ]; then
echo ${marker_ver-"0.6.20"}
else
echo ${engine}
fi

} # 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 <<SRCEOF

# Setup shell env for the custom Node[.js] version.
source "\$OPENSHIFT_REPO_DIR/.openshift/lib/setup_custom_nodejs_env"
SRCEOF

echo " - Added source setup_custom_nodejs_env to .bash_profile"
fi

} # End of function _ensure_bash_profile_setup.


# Check and install custom Node[.js] version.
function ensure_node_is_installed() {
ver=${1:-"$(get_node_version)"}

echo " - Checking to see if Node.js version $ver is installed ... "

#
# To re-download and reinstall Node.js, uncomment these lines.
# rm -f downloads/node-v${ver}-linux-x64.tar.gz
# rm -rf $OPENSHIFT_DATA_DIR/node-v${ver}-linux-x64/
#
# Note: This function could be run multiple times on every git push,
# so use w/ caution (do once).
#

if [ -d "$(get_node_bin_path)" ]; then
echo " - Node.js version $ver is already installed"
else
_install_nodejs "$ver"
fi

# Ensure .bash_profile sets up path for custom Node[.js] version.
_ensure_bash_profile_setup

} # End of function ensure_node_is_installed.


# Sets up PATH to include the custom node version binaries.
function setup_path_for_custom_node_version() {
# Get version.
ver=${1:-"$(get_node_version)"}

# Get node binary path.
node_bin_path=$(get_node_bin_path "$ver")

# Add the node binary path to the PATH.
export PATH="$node_bin_path:${PATH}"

if [ -n "$_SHOW_SETUP_PATH_MESSAGES" ]; then
echo " - PATH set to include custom node version ($ver) from"
echo " $node_bin_path "
echo " PATH = $PATH"
fi

} # End of function setup_path_for_custom_node_version.



#
# EOF
#
7 changes: 7 additions & 0 deletions .openshift/markers/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Markers
===========

Adding marker files to this directory will have the following effects:

force_clean_build - Will remove any previously installed npm modules and
re-install all the required modules from scratch
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ https://github.com/okfn/annotator-store

```heroku config:add `cat .env` ```

### Using OpenShift

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
https://github.com/okfn/annotator/
Expand Down
Loading