Created
June 5, 2020 00:17
-
-
Save hparadiz/e38b9203f2aa2ba6b0a0da3a19b020c5 to your computer and use it in GitHub Desktop.
Daemon wrapper for hubot written in bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# detect our real working directory through symlinks | |
SOURCE="${BASH_SOURCE[0]}" | |
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink | |
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" | |
SOURCE="$(readlink "$SOURCE")" | |
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located | |
done | |
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" | |
# set out working directory | |
cd "$DIR/.." | |
pidfile="bin/hubot.pid" | |
me="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")" | |
PACKAGE_VERSION=$(cat package.json \ | |
| grep version \ | |
| head -1 \ | |
| awk -F: '{ print $2 }' \ | |
| sed 's/[",]//g') | |
usage() { echo "Usage: $me [status|start|reload|stop]" 1>&2; exit 1; } | |
function loadenv() { | |
if [ -f ".env" ] | |
then | |
envtype=$(cat .env) | |
envfile="conf/$envtype" | |
if [ -f $envfile ] | |
then | |
. $envfile | |
echo "Loaded environment from $envfile" | |
else | |
echo "$envfile not found." | |
fi | |
else | |
echo 'Environment type definition not found. Attempting production.' | |
if [ -f 'conf/production' ] | |
then | |
. conf/production | |
else | |
echo 'Production environment definition not found.' | |
fi | |
fi | |
} | |
function status { | |
if [ -f $pidfile ] | |
then | |
PID=`cat $pidfile` | |
if ps -p $PID > /dev/null | |
then | |
echo "hubot is running on $PID" | |
else | |
echo 'hubot is not running' | |
fi | |
else | |
echo 'PID file not found.' | |
fi | |
} | |
function stop { | |
echo 'Stopping hubot' | |
PID=`cat $pidfile` | |
if ps -p $PID > /dev/null | |
then | |
kill `cat $pidfile` | |
fi | |
rm $pidfile | |
} | |
function start() { | |
loadenv | |
executable='node_modules/.bin/hubot -d --name "hubot" -a slack "$@"' | |
log="log/hubot.log" | |
if [ -f $pidfile ]; then | |
echo "PID file found: " && cat $pidfile && exit 0 | |
fi | |
daemon() { | |
echo 'Starting hubot' | |
$executable >> $log 2>&1 & | |
pid="$!" | |
echo $pid > $pidfile | |
} | |
npm install --no-save | |
export PATH="node_modules/.bin:node_modules/hubot/node_modules/.bin:$PATH" | |
daemon | |
} | |
while :; do | |
case $1 in | |
status) | |
status | |
break; | |
;; | |
start) | |
start | |
break | |
;; | |
reload) | |
stop | |
start | |
break | |
;; | |
stop) | |
stop | |
break | |
;; | |
-h|-\?|--help) | |
usage | |
break | |
;; | |
-v|--version) | |
echo $PACKAGE_VERSION | |
exit 1; | |
;; | |
*) | |
usage | |
break | |
;; | |
esac | |
shift | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment