Skip to content

Instantly share code, notes, and snippets.

@grational
Created December 4, 2015 18:01
Show Gist options
  • Save grational/6bd644812ec7a454fcd8 to your computer and use it in GitHub Desktop.
Save grational/6bd644812ec7a454fcd8 to your computer and use it in GitHub Desktop.
#!/bin/bash
# set -e: if a command fails, it will make the whole script exit
# set -u: treat unset variables as an error, and immediately exit.
set -eu
SCRIPT_NAME="${0##*/}"
usage() {
echo "${SCRIPT_NAME} usage:"
echo '<*> [-P | --profile] <local|test|production>'
echo ' [-S | --skip-update]'
echo ' [-d | --debug]'
echo ' [-h | --help]'
echo '<*>: mandatory parameter'
}
repeat() {
printf "${1}%.0s" `seq 1 ${2}`; echo
}
slog() {
if [[ 'x-hg' = "x${1}" ]]; then
shift; TEXT="[${SCRIPT_NAME}] ${@}"
repeat '=' "${#TEXT}"
echo "${TEXT}"
repeat '=' "${#TEXT}"
else
echo "[${SCRIPT_NAME}] ${@}"
fi
}
if [[ $# -lt 2 ]]; then
usage
exit 4
fi
# Initialization
DAB_PROJECT="${HOME}/dab"
DAB_SERVICE="tomcat-dab"
MAVEN_DEFAULT_PROFILE="production"
PROFILE="${MAVEN_DEFAULT_PROFILE}"
TARGET_PATH="${DAB_PROJECT}/target"
WAR_FILE='dab.war'
DEPLOY_PATH='/store/tomcat/dab/webapps'
SLEEP_TIME='5'
DEBUG='no'
SKIP_UPDATE='no'
# Handle Command line parameters
SHORT='hdSP:'
LONG='help,debug,skip-update,profile:'
PARSED=`getopt --options ${SHORT} --longoptions ${LONG} --name "$0" -- "$@"`
if [[ $? != 0 ]]; then
exit 2
fi
# Add -- at the end of line arguments
eval set -- "${PARSED}"
while true; do
case "$1" in
-h|--help)
usage
exit 5
;;
-d|--debug)
DEBUG='yes'
shift
;;
-S|--skip-update)
SKIP_UPDATE='yes'
shift
;;
-P|--profile)
PROFILE="$2"
shift
shift
;;
--)
shift
break
;;
*)
slog "Parameters error"
exit 3
;;
esac
done
slog -hg "START RELEASE - $(date +%d/%m/%Y-%H:%M:%S)"
if [[ "xyes" = "x${DEBUG}" ]]; then
# Move into the dab project directory
slog cd "${DAB_PROJECT}"
# Update the code
[[ 'xyes' != "x${SKIP_UPDATE}" ]] && slog svn update
# Clean and rebuild the project
slog mvn clean package -P"${PROFILE}"
# Stop tomcat
slog service "${DAB_SERVICE}" stop
# some sleeping, just to be sure
slog sleep "${SLEEP_TIME}"
# remove old war and directory files
slog rm -rf "${DEPLOY_PATH}"/dab*
# copy the new war file
slog cp "${TARGET_PATH}/${WAR_FILE}" "${DEPLOY_PATH}"
# start tomcat again
slog service "${DAB_SERVICE}" start
else
# Move into the dab project directory
cd "${DAB_PROJECT}"
# Update the code
if [[ 'xyes' != "x${SKIP_UPDATE}" ]]; then
slog 'update the code'
svn update
fi
# Clean and rebuild the project
slog 'rebuild the project'
mvn clean package -P"${PROFILE}"
# Stop tomcat
slog "stop the ${DAB_SERVICE} service"
service "${DAB_SERVICE}" stop
# some sleeping, just to be sure
sleep "${SLEEP_TIME}"
# remove old war and directory files
slog 'clean old war'
rm -rf "${DEPLOY_PATH}"/dab*
# copy the new war file
slog 'copy new war'
cp "${TARGET_PATH}/${WAR_FILE}" "${DEPLOY_PATH}"
# start tomcat again
service "${DAB_SERVICE}" start
fi
slog -hg "END RELEASE - $(date +%d/%m/%Y-%H:%M:%S)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment