From c6ee330be6b7892e98693b9e2f0568586e79c5e5 Mon Sep 17 00:00:00 2001 From: Eric Uldall Date: Tue, 10 Jun 2014 12:06:55 -0700 Subject: [PATCH 1/6] created init script and readme --- sh/linux/README | 15 ++++ sh/linux/init.d/manta-server | 163 +++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 sh/linux/README create mode 100644 sh/linux/init.d/manta-server diff --git a/sh/linux/README b/sh/linux/README new file mode 100644 index 0000000..a2c092b --- /dev/null +++ b/sh/linux/README @@ -0,0 +1,15 @@ +#Manta Server Linux Init Script +_______________________________ + +Complete the following steps after installing node-manta: + +1. Install Node Forever: `npm -g install forever` +2. Grab the init script from init.d and copy to your /etc/init.d as root, then chmod the file: `chomd 0755 manta-server` +3. Update system service definitions: `update-rc.d my-application defaults` +4. Then add your environment variables to the persistent scope `vi /etc/environment` and add the following lines: + MANTA_KEY_ID="Key Id (can be found in your account settings SSH Keys)" + MANTA_URL="https://us-east.manta.joyent.com" + MANTA_USER="Your username" +5. Then source the env vars: `source /etc/environment` +6. Last but not least, create your log folder, `mkdir /var/log/manta-server` `touch /var/log/manta-server/error.log` +7. Now you're ready to run it: `/etc/init.d/manta-server (start|stop|restart)` diff --git a/sh/linux/init.d/manta-server b/sh/linux/init.d/manta-server new file mode 100644 index 0000000..9dd0442 --- /dev/null +++ b/sh/linux/init.d/manta-server @@ -0,0 +1,163 @@ +#!/bin/bash +# +# An init.d script for running a Node.js process as a service using Forever as +# the process monitor. For more configuration options associated with Forever, +# see: https://github.com/nodejitsu/forever +# +# This was written for Debian distributions such as Ubuntu, but should still +# work on RedHat, Fedora, or other RPM-based distributions, since none of the +# built-in service functions are used. So information is provided for both. +# +### BEGIN INIT INFO +# Provides: my-application +# Required-Start: $syslog $remote_fs +# Required-Stop: $syslog $remote_fs +# Should-Start: $local_fs +# Should-Stop: $local_fs +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: My Application +# Description: My Application +### END INIT INFO +# +### BEGIN CHKCONFIG INFO +# chkconfig: 2345 55 25 +# description: My Application +### END CHKCONFIG INFO +# +# Based on: +# https://gist.github.com/3748766 +# https://github.com/hectorcorrea/hectorcorrea.com/blob/master/etc/forever-initd-hectorcorrea.sh +# https://www.exratione.com/2011/07/running-a-nodejs-server-as-a-service-using-forever/ +# +# The example environment variables below assume that Node.js is installed by +# building from source with the standard settings. +# +# It should be easy enough to adapt to the paths to be appropriate to a package +# installation, but note that the packages available in the default repositories +# are far behind the times. Most users will be building from source to get a +# suitably recent Node.js version. +# +# An application name to display in echo text. +# NAME="My Application" +# The full path to the directory containing the node and forever binaries. +# NODE_BIN_DIR="/usr/local/node/bin" +# Set the NODE_PATH to the Node.js main node_modules directory. +# NODE_PATH="/usr/local/lib/node_modules" +# The application startup Javascript file path. +# APPLICATION_PATH="/home/user/my-application/start-my-application.js" +# Process ID file path. +# PIDFILE="/var/run/my-application.pid" +# Log file path. +# LOGFILE="/var/log/my-application.log" +# Forever settings to prevent the application spinning if it fails on launch. +# MIN_UPTIME="5000" +# SPIN_SLEEP_TIME="2000" + +NAME="Manta Server" +NODE_BIN_DIR="/usr/local/node/bin" +NODE_PATH="/usr/local/lib/node_modules" +APPLICATION_PATH="$NODE_PATH/manta-nfs/server.js" +PIDFILE="/var/run/manta-server.pid" +LOGFILE="/var/log/manta-server/error.log" +MIN_UPTIME="5000" +SPIN_SLEEP_TIME="2000" + +# Add node to the path for situations in which the environment is passed. +PATH=$NODE_BIN_DIR:$PATH +# Export all environment variables that must be visible for the Node.js +# application process forked by Forever. It will not see any of the other +# variables defined in this script. +export NODE_PATH=$NODE_PATH + +start() { + echo "Starting $NAME" + # We're calling forever directly without using start-stop-daemon for the + # sake of simplicity when it comes to environment, and because this way + # the script will work whether it is executed directly or via the service + # utility. + # + # The minUptime and spinSleepTime settings stop Forever from thrashing if + # the application fails immediately on launch. This is generally necessary to + # avoid loading development servers to the point of failure every time + # someone makes an error in application initialization code, or bringing down + # production servers the same way if a database or other critical service + # suddenly becomes inaccessible. + # + # The pidfile contains the child process pid, not the forever process pid. + # We're only using it as a marker for whether or not the process is + # running. + # + # Note that redirecting the output to /dev/null (or anywhere) is necessary + # to make this script work if provisioning the service via Chef. + forever \ + --pidFile $PIDFILE \ + -a \ + -l $LOGFILE \ + --minUptime $MIN_UPTIME \ + --spinSleepTime $SPIN_SLEEP_TIME \ + start $APPLICATION_PATH 2>&1 > /dev/null & + RETVAL=$? +} + +stop() { + if [ -f $PIDFILE ]; then + echo "Shutting down $NAME" + # Tell Forever to stop the process. + forever stop $APPLICATION_PATH 2>&1 > /dev/null + # Get rid of the pidfile, since Forever won't do that. + rm -f $PIDFILE + RETVAL=$? + else + echo "$NAME is not running." + RETVAL=0 + fi +} + +restart() { + stop + start +} + +status() { + # On Ubuntu this isn't even necessary. To find out whether the service is + # running, use "service my-application status" which bypasses this script + # entirely provided you used the service utility to start the process. + # + # The commented line below is the obvious way of checking whether or not a + # process is currently running via Forever, but in recent Forever versions + # when the service is started during Chef provisioning a dead pipe is left + # behind somewhere and that causes an EPIPE exception to be thrown. + # forever list | grep -q "$APPLICATION_PATH" + # + # So instead we add an extra layer of indirection with this to bypass that + # issue. + echo `forever list` | grep -q "$APPLICATION_PATH" + if [ "$?" -eq "0" ]; then + echo "$NAME is running." + RETVAL=0 + else + echo "$NAME is not running." + RETVAL=3 + fi +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + status) + status + ;; + restart) + restart + ;; + *) + echo "Usage: {start|stop|status|restart}" + exit 1 + ;; +esac +exit $RETVAL From e8a844d51e342c94280696caecf3deb24cb45610 Mon Sep 17 00:00:00 2001 From: Eric Uldall Date: Tue, 10 Jun 2014 12:12:47 -0700 Subject: [PATCH 2/6] updated readme with new markdown for ease of reading --- sh/linux/README | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/sh/linux/README b/sh/linux/README index a2c092b..758a8d7 100644 --- a/sh/linux/README +++ b/sh/linux/README @@ -1,15 +1,17 @@ -#Manta Server Linux Init Script +# Manta Server Linux Init Script _______________________________ Complete the following steps after installing node-manta: -1. Install Node Forever: `npm -g install forever` -2. Grab the init script from init.d and copy to your /etc/init.d as root, then chmod the file: `chomd 0755 manta-server` -3. Update system service definitions: `update-rc.d my-application defaults` -4. Then add your environment variables to the persistent scope `vi /etc/environment` and add the following lines: +1. Install Node Forever: *npm -g install forever* +2. Grab the init script from init.d and copy to your /etc/init.d as root, then chmod the file: *chomd 0755 manta-server* +3. Update system service definitions: *update-rc.d my-application defaults* +4. Then add your environment variables to the persistent scope *vi /etc/environment* and add the following lines: + ``` MANTA_KEY_ID="Key Id (can be found in your account settings SSH Keys)" MANTA_URL="https://us-east.manta.joyent.com" MANTA_USER="Your username" -5. Then source the env vars: `source /etc/environment` -6. Last but not least, create your log folder, `mkdir /var/log/manta-server` `touch /var/log/manta-server/error.log` -7. Now you're ready to run it: `/etc/init.d/manta-server (start|stop|restart)` + ``` +5. Then source the env vars: *source /etc/environment* +6. Last but not least, create your log folder, *mkdir /var/log/manta-server;* *touch /var/log/manta-server/error.log;* +7. Now you're ready to run it: */etc/init.d/manta-server (start|stop|restart)* From 0404ffbc7a59744999850c7b375b26fe19ea5db2 Mon Sep 17 00:00:00 2001 From: Eric Uldall Date: Tue, 10 Jun 2014 12:14:09 -0700 Subject: [PATCH 3/6] updated filename --- sh/linux/{README => README.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sh/linux/{README => README.md} (100%) diff --git a/sh/linux/README b/sh/linux/README.md similarity index 100% rename from sh/linux/README rename to sh/linux/README.md From 49eb34ac4384ebf6ae949a1e2543da98f46a20cc Mon Sep 17 00:00:00 2001 From: Eric Uldall Date: Tue, 10 Jun 2014 12:20:28 -0700 Subject: [PATCH 4/6] updated markdown with code blocks when necessary --- sh/linux/README.md | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/sh/linux/README.md b/sh/linux/README.md index 758a8d7..7b938c3 100644 --- a/sh/linux/README.md +++ b/sh/linux/README.md @@ -1,17 +1,41 @@ # Manta Server Linux Init Script -_______________________________ -Complete the following steps after installing node-manta: +_Complete the following steps after installing node-manta:_ -1. Install Node Forever: *npm -g install forever* -2. Grab the init script from init.d and copy to your /etc/init.d as root, then chmod the file: *chomd 0755 manta-server* -3. Update system service definitions: *update-rc.d my-application defaults* -4. Then add your environment variables to the persistent scope *vi /etc/environment* and add the following lines: +Step 1 - Install Node Forever: +``` +npm -g install forever +``` + +Step 2 - Grab the init script from init.d and copy to your */etc/init.d* as root, then chmod the file: +``` +chomd 0755 manta-server +``` + +Step 3 - Update system service definitions: +``` +update-rc.d my-application defaults +``` + +Step 4 - Then add your environment variables to the persistent scope *vi /etc/environment* and add the following lines: ``` MANTA_KEY_ID="Key Id (can be found in your account settings SSH Keys)" MANTA_URL="https://us-east.manta.joyent.com" MANTA_USER="Your username" ``` -5. Then source the env vars: *source /etc/environment* -6. Last but not least, create your log folder, *mkdir /var/log/manta-server;* *touch /var/log/manta-server/error.log;* -7. Now you're ready to run it: */etc/init.d/manta-server (start|stop|restart)* + +Step 5 - Then source the env vars: +``` +source /etc/environment +``` + +Step 6 - Last but not least, create your log folder: +``` +mkdir /var/log/manta-server; +touch /var/log/manta-server/error.log; +``` + +Step 7 - Now you're ready to run it: +``` +/etc/init.d/manta-server (start|stop|restart) +``` From 3e8db9511139ef3a7f8bbb2c7e6619a60fd96011 Mon Sep 17 00:00:00 2001 From: Eric Uldall Date: Tue, 10 Jun 2014 12:21:36 -0700 Subject: [PATCH 5/6] fixed env var code block --- sh/linux/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sh/linux/README.md b/sh/linux/README.md index 7b938c3..ce6363c 100644 --- a/sh/linux/README.md +++ b/sh/linux/README.md @@ -18,11 +18,11 @@ update-rc.d my-application defaults ``` Step 4 - Then add your environment variables to the persistent scope *vi /etc/environment* and add the following lines: - ``` - MANTA_KEY_ID="Key Id (can be found in your account settings SSH Keys)" - MANTA_URL="https://us-east.manta.joyent.com" - MANTA_USER="Your username" - ``` +``` +MANTA_KEY_ID="Key Id (can be found in your account settings SSH Keys)" +MANTA_URL="https://us-east.manta.joyent.com" +MANTA_USER="Your username" +``` Step 5 - Then source the env vars: ``` From 5a19ca12bcc9d0bb1f33661456dfa5ba3c5963a8 Mon Sep 17 00:00:00 2001 From: Eric Uldall Date: Tue, 10 Jun 2014 12:23:11 -0700 Subject: [PATCH 6/6] made system service update code sample specific to manta-server --- sh/linux/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sh/linux/README.md b/sh/linux/README.md index ce6363c..cf404b3 100644 --- a/sh/linux/README.md +++ b/sh/linux/README.md @@ -14,7 +14,7 @@ chomd 0755 manta-server Step 3 - Update system service definitions: ``` -update-rc.d my-application defaults +update-rc.d manta-server defaults ``` Step 4 - Then add your environment variables to the persistent scope *vi /etc/environment* and add the following lines: