From: dsc Date: Fri, 27 Apr 2012 19:50:29 +0000 (-0700) Subject: Improves deploy script with help, options. X-Git-Url: http://git.less.ly:3516/?a=commitdiff_plain;h=f2856258ce6c928914e36f48d681665bd174761a;p=limn-bak.git Improves deploy script with help, options. --- diff --git a/deploy.sh b/deploy.sh index 3c014f0..93e0889 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,29 +1,91 @@ #!/bin/bash -#* deploy.sh -#* Builds deploy package and rsync's the files. - -# The most important line in any shell program. +#/ deploy.sh +#/ +#/ Usage: $( basename $0 ) [options] [DEPLOY_HOST] [DEPLOY_PATH] +#/ +#/ Deploys the GraphKit framework to a reportcard instance. +#/ +#/ Arguments: +#/ DEPLOY_HOST Target host for deploy. [Default: reportcard2.pmtpa.wmflabs] +#/ Also checks env for KRAKEN_DEPLOY_HOST. +#/ DEPLOY_PATH Path on target host. [Default: /srv/reportcard/kraken-ui/] +#/ Also checks env for KRAKEN_DEPLOY_PATH. +#/ +#/ Options: +#/ -h --help Displays this help. +#/ -v Verbose logging. +#/ -n Dry-run: run through all local steps, but don't do anything on the server. +#/ -P Do not fix remote file permissions. +#/ -r Restart server post-deploy. +#/ -d Host of the development server to query for some info. +#/ [Default: localhost:8081] +#/ -u Deploy user. [Default: www-data] +#/ -g Deploy group. [Default: www] +#/ + + +# The most important line in any shell program. (Terminate on subcommand errors) set -e +VERBOSE='' +DRY_RUN='' +FIX_PERMISSIONS=1 +RESTART_SERVER='' +DEV_HOST="${KRAKEN_DEV_HOST:-localhost:8081}" +DEPLOY_HOST="${KRAKEN_DEPLOY_HOST:-reportcard2.pmtpa.wmflabs}" +DEPLOY_PATH="${KRAKEN_DEPLOY_PATH:-/srv/reportcard/kraken-ui/}" +DEPLOY_USER="www-data" +DEPLOY_GROUP="www" -VERBOSE='-v' -KRAKEN_DEV_HOST="${KRAKEN_DEV_HOST:-localhost:8081}" -KRAKEN_DEPLOY_HOST="${KRAKEN_DEPLOY_HOST:-reportcard2.pmtpa.wmflabs}" -KRAKEN_DEPLOY_PATH="${KRAKEN_DEPLOY_PATH:-/srv/reportcard/kraken-ui/}" -KRAKEN_DEPLOY_USER="www-data" -KRAKEN_DEPLOY_GROUP="www" -# Utilities -log () { [ "x$VERBOSE" != 'x' ] && echo -e "$*" >&2; } -fail () { echo >&2; echo "Error: $*" >&2; exit 1; } +# Utilities +log () { [ "$VERBOSE" ] && echo -e "$*" >&2; :; } +logs () { [ "$VERBOSE" ] && printf "%s" "$*"; :; } +fail () { echo >&2; echo "[ERROR] $*" >&2; exit 1; } count () { echo $#; } nth () { local n="$1"; shift; [ -z "$n" ] && return 1; [[ "$n" < 0 ]] && n=$(( 1 + $# + $n )); echo "${!n}"; } -join () { local sep="$1" old="$IFS"; export IFS=\n; read -t1 out; while read -t1 line; do out="$out$sep$line"; done; echo "$out"; export IFS=$old; } +# join () { local sep="$1" old="$IFS"; export IFS=\n; read -t1 out; while read -t1 line; do out="$out$sep$line"; done; echo "$out"; export IFS=$old; } +# join () { local sep="$1"; shift; [ -z "$*" ] && return 1; printf "$1"; shift; for a in $*; do printf "$sep$a"; done; echo; } + +# Print Help +SELF="$0" +halp () { grep '^#/' <"$SELF" | cut -c4-; :; } +for opt in $*; do echo $opt | grep -Exq -e '--?h(e(lp?)?)?' && { halp; exit 0; }; done + +# Parse Args +SHIFT=0 +incshift () { SHIFT=$(( $SHIFT + ${1:-1} )); } +while getopts "vd:u:g:nPr" opt; do + case $opt in + v ) VERBOSE="-v"; incshift ;; + d ) DEV_HOST="$OPTARG"; incshift 2 ;; + u ) DEPLOY_USER="$OPTARG"; incshift 2 ;; + g ) DEPLOY_GROUP="$OPTARG"; incshift 2 ;; + n ) DRY_RUN='-n'; incshift ;; + P ) FIX_PERMISSIONS=''; incshift ;; + r ) RESTART_SERVER=1; incshift ;; + * ) fail "Unknown option: -$opt=$OPTARG" ;; + esac +done +shift $SHIFT + +if [ "$1" ]; then + DEPLOY_HOST="$1" +fi +if [ "$2" ]; then + DEPLOY_PATH="$2" +fi + -# makes sure everything is owned by -fix_permissions () { echo "Fixing permissions on $KRAKEN_DEPLOY_HOST:$KRAKEN_DEPLOY_PATH"; ssh -t $KRAKEN_DEPLOY_HOST "/usr/bin/sudo /bin/chmod --changes -R g+w $KRAKEN_DEPLOY_PATH && /usr/bin/sudo /bin/chown --changes -R $KRAKEN_DEPLOY_USER:$KRAKEN_DEPLOY_GROUP $KRAKEN_DEPLOY_PATH"; } +# makes sure everything is owned by www-data +fix_permissions () { + [ -n "$FIX_PERMISSIONS" ] && return 0 + log "Fixing permissions on $DEPLOY_HOST:$DEPLOY_PATH ..." + log "ssh -t $DEPLOY_HOST '/usr/bin/sudo /bin/chmod --changes -R g+w $DEPLOY_PATH && /usr/bin/sudo /bin/chown --changes -R $DEPLOY_USER:$DEPLOY_GROUP $DEPLOY_PATH'" + [ -n "$DRY_RUN" ] && ssh -t $DEPLOY_HOST "/usr/bin/sudo /bin/chmod --changes -R g+w $DEPLOY_PATH && /usr/bin/sudo /bin/chown --changes -R $DEPLOY_USER:$DEPLOY_GROUP $DEPLOY_PATH" +} # Ensure gitrev is up to date coke update_version @@ -37,7 +99,7 @@ log "Collapsing serve trees..." rsync -Ca static/ var/ data tmp/dist/ rsync -Ca lib/ tmp/dist/src/ # browserify -o tmp/dist/vendor/browserify.js -r events -r seq -curl --silent --fail --url http://$KRAKEN_DEV_HOST/vendor/browserify.js > tmp/dist/vendor/browserify.js +curl --silent --fail --url http://$DEV_HOST/vendor/browserify.js > tmp/dist/vendor/browserify.js log "ok!" # Bundle Vendor JS Bundle @@ -76,15 +138,15 @@ log "ok!" log "Deploying files..." # fix ownership and permissions before and after rsync. fix_permissions -log "rsync -Caz $VERBOSE --progress tmp/dist $KRAKEN_DEPLOY_HOST:$KRAKEN_DEPLOY_PATH" -rsync -Crz $VERBOSE --progress tmp/dist $KRAKEN_DEPLOY_HOST:$KRAKEN_DEPLOY_PATH +log "rsync -Caz $VERBOSE $DRY_RUN --progress tmp/dist $DEPLOY_HOST:$DEPLOY_PATH" +rsync -Crz $VERBOSE $DRY_RUN --progress tmp/dist $DEPLOY_HOST:$DEPLOY_PATH fix_permissions log "ok!" -echo -n "Restart reportcard server? [y/n] " -read answer -if [ $answer == 'y' ]; then - ssh -t $KRAKEN_DEPLOY_HOST "sudo supervisorctl restart reportcard" +if [ "$RESTART_SERVER" ]; then + log "Restarting Reportcard Server post-deploy..." + log "ssh -t $DEPLOY_HOST 'sudo supervisorctl restart reportcard'" + [ -n "$DRY_RUN" ] && ssh -t $DEPLOY_HOST "sudo supervisorctl restart reportcard" fi log "Done!"