Skip to content

Instantly share code, notes, and snippets.

@BarnacleBob
Created January 5, 2016 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BarnacleBob/de7cc0eb1ad21d0fb465 to your computer and use it in GitHub Desktop.
Save BarnacleBob/de7cc0eb1ad21d0fb465 to your computer and use it in GitHub Desktop.
#!/bin/bash
log () {
echo "$(date): $@"
logger -p local4.info -t "$APP_NAME[installer]" -- "$@"
}
die () {
local st="$?"
log "Error: $@"
exit "$st"
}
HOME="$(getent passwd $(whoami) | cut -d: -f6)"
source "${HOME}/config" || die "Could not find config for $(whoami):${HOME}"
[ -z "$APP_NAME" ] && die "config did not define APP_NAME"
[ -z "$REPO" ] && die "config did not define REPO"
[ -z "$BRANCH" ] && die "config did not define BRANCH"
OUT="$(
echo "---------$(date)----------"
exec 2>&1
set -x
(
flock -nx 9 || die "unable to obtain exclusive lock for install. unexpected, cron should not be setup yet"
log "Staring install or reinstall"
APP_HOME="$HOME/app"
GIT_DIR="$APP_HOME/$APP_NAME"
[[ -e "/etc/serice/${APP_NAME}/run" ]] && die "cannot app install with a working service for the app. use updater"
if [ ! -e "$GIT_DIR" ]; then
# initial checkout
log "doing initial clone of $REPO branch $BRANCH"
git clone $REPO $GIT_DIR || die "failed to git clone"
fi
cd $GIT_DIR || die "failed to change into git clone directory"
log "checking out latest $BRANCH and updating submodules"
git fetch || die "failed to get fetch"
git checkout $BRANCH || die "failed to checkout branch $BRANCH"
git pull || die "failed to update branch"
git submodule init
git submodule update
log "installing npm modules"
if [[ "$USE_NVM" == "true" ]]; then
source $HOME/.nvm/nvm.sh
npm install || die "failed to install npms"
else
/usr/bin/npm install || die "failed to install npms"
fi
if [ -n "$UPDATE_HOOK" ]; then
$UPDATE_HOOK || die "update hook failed"
fi
log "Finished (re)install"
exit 0
) 9>/var/lock/app_update-$APP_NAME
)"
RET=$?
echo "$OUT" >> /var/log/$APP_NAME/app_installer.log
if [[ $RET != 0 ]]; then
echo "$OUT"
exit $RET
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment