Skip to content

Instantly share code, notes, and snippets.

@zenril
Created March 4, 2019 04:50
Show Gist options
  • Save zenril/f2ae19f29cd0333f28b05ca520401408 to your computer and use it in GitHub Desktop.
Save zenril/f2ae19f29cd0333f28b05ca520401408 to your computer and use it in GitHub Desktop.
deploy
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )";
NAME="thing"
NODE_BIN_DIR=$(nvm which node | sed 's/\([/][^/]*\)\{1\}$//g')
NODE_PATH="$(nvm which node | sed 's/\([/][^/]*\)\{2\}$//g')/lib/node_modules"
SOURCEDIR="$DIR/app"
APPLICATION_PATH="$SOURCEDIR/bot.js"
LOGFILE="$DIR/forever.log"
ERRORFILE="$DIR/err.log"
OUTFILE="$DIR/out.log"
SPIN_SLEEP_TIME="300000"
MIN_UPTIME="600000"
# 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"
forever stop $APPLICATION_PATH
rm -rf app
git clone [email protected]:wootosmash/talkbot.git app
cd app
latesttag=$(git describe --tags $(git rev-list --tags --max-count=1))
echo checking out ${latesttag}
git checkout ${latesttag}
rm -rf config
ln -s ../shared/config config
nvm use
npm install
npm rebuild
sudo GOOGLE_APPLICATION_CREDENTIALS="/root/.google/auth.json" forever \
-a \
-l $LOGFILE \
-o $OUTFILE \
-e $ERRORFILE \
--minUptime $MIN_UPTIME \
--workingDir $SOURCEDIR \
--spinSleepTime $SPIN_SLEEP_TIME \
--append \
start $APPLICATION_PATH
cd ..
}
stop() {
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.
}
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."
else
echo "$NAME is not running."
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo "Usage: {start|stop|status|restart}"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment