#!/bin/bash
#author jiabao.wang
#email jiabao.wang@leappmusic.com
#version 0.1
SOURCE_DIR="/data/deploy-source"
BACKUP_DIR="/data/deploy-backup"
#DEPLOY_DIR="/data/web-api"
DEPLOY_DIR="/data/tomcat-project"
DEPLOY_SOURCE="/data/tomcat-source"
RUN_CMD_COMMENT="run cmd : "web
ARGS=`/usr/bin/getopt -a -o j:b:f:h:e:l: -l help,job-name:,build-id:,file:,hosts:,exclude:,log-path: -- "$@"`
JOB_NAME=""
BUILD_ID=""
COMPRESS_FILE=""
UNCOMPRESS_FILE=""
HOSTS_STR=""
HOSTS=""
EXCLUDE=""
SOURCE_EXCLUDE_LIST=""
BACKUP_EXCLUDE_LIST=""
LOG_PATH=""
SOURCE_FILE_DIR=""
BACKUP_FILE_DIR=""
DEPLOY_FILE_DIR=""api
main(){
getOpt
unzip
info
echo "Now deploying tomcat-project"
for host in ${HOSTS[@]};do
rsync ${host}
done
}tomcat
help(){
echo "usage: tomcat-deploy [options] [args]"
echo ""
echo "Options:"
echo " --help"
echo " Display help information"
echo ""
echo " -j, --job-name <arg>"
echo " Hudson job name"
echo ""
echo " -b, --build-id <arg>"
echo " Hudson build id"
echo ""
echo " -f, --compress-file <arg>"
echo " Project package compress file name, support type : tar.gz, zip"
echo ""
echo " -h, --hosts <arg>"
echo " Deploy hosts(multiple \",\" separated by commas)"
echo " Example: 192.168.1.1,192.168.1.2"
echo ""
echo " -e, --exclude <arg>"
echo " Rsync exclude content"
echo ""
echo " -l, --log-path <arg>"
echo " Server log path"
echo ""
}bash
getOpt() {
eval set -- "${ARGS}"
while true
do
case "$1" in
--help)
help
exit 1
;;
-j|--job-name)
JOB_NAME=$2
shift
;;
-b|--build-id)
BUILD_ID=$2
shift
;;
-f|--file)
COMPRESS_FILE=$2
shift
;;
-h|---hosts)
HOSTS_STR=$2
HOSTS=(${HOSTS_STR//,/ })
shift
;;
-e|---exclude)
EXCLUDE=$2
shift
;;
-l|---log-path)
LOG_PATH=$2
shift
;;
--)
shift
break
;;
*)
echo "Internal error!"
help
exit 1
;;
esac
shift
done
if [ -z "${JOB_NAME}" ];then
echo "Job name is null"
help
exit 1
fi
if [ -z "${BUILD_ID}" ];then
echo "Build id is null"
help
exit 1
fi
if [ -z "${COMPRESS_FILE}" ];then
echo "Compress file is null"
help
exit 1
fi
if [ -z "${HOSTS_STR}" ];then
echo "Hosts is null"
help
exit 1
fi
if [ -z "${LOG_PATH}" ];then
echo "Warning : Log path is null, will not make symbolic link, not recommend"
fi
}app
unzip(){
SOURCE_FILE_PARENT_DIR="${SOURCE_DIR}/${JOB_NAME}/${BUILD_ID}"
if [ ! -d "${SOURCE_FILE_PARENT_DIR}" ];then
echo "${SOURCE_FILE_PARENT_DIR} is not a directory, the compress file need put into this directory"
help
exit 1
fissh
if [ ! -f "${SOURCE_FILE_PARENT_DIR}/${COMPRESS_FILE}" ];then
echo "Source compress file : ${SOURCE_FILE_PARENT_DIR}/${COMPRESS_FILE} is not exists"
help
exit 1
fiui
echo "${RUN_CMD_COMMENT}cd ${SOURCE_FILE_PARENT_DIR}"
cd ${SOURCE_FILE_PARENT_DIR}
UNCOMPRESS_FILE=${COMPRESS_FILE%.*}
# extension=${COMPRESS_FILE##*.}
# if [ "${extension}" == "gz" ];then
# echo "Uncompress ${COMPRESS_FILE} ..."
# tarCmd="/bin/tar -xvzf ${COMPRESS_FILE}"
# echo "${RUN_CMD_COMMENT}${tarCmd}"
# ${tarCmd}
# UNCOMPRESS_FILE=`/bin/tar -tf ${COMPRESS_FILE} | head -1 | awk -F'/' '{print $1}'`
# elif [ "${extension}" == "zip" ];then
# echo "Uncompress file..."
# unzipCmd="/usr/bin/unzip -o ${COMPRESS_FILE}"
# echo "${RUN_CMD_COMMENT}${unzipCmd}"
# ${unzipCmd}
# UNCOMPRESS_FILE=`/usr/bin/unzip -l ${COMPRESS_FILE} | head -4 | tail -1 | awk '{print $4}'|tr -d '/'`
# else
# echo "Compress file extension not supported"
# help
# exit 1
# fi
if [ -z "${COMPRESS_FILE}" ];then
echo "$Uncompress file is null, please check the compress file"
help
exit 1
fithis
DEPLOY_FILE_DIR="${DEPLOY_DIR}/${UNCOMPRESS_FILE}/"
DEPLOY_SOURCE_FILE_DIR="${DEPLOY_SOURCE}/${UNCOMPRESS_FILE}/"
BACKUP_FILE_DIR="${BACKUP_DIR}/${JOB_NAME}/${BUILD_ID}/${UNCOMPRESS_FILE}/"
}orm
info(){
echo "================Deploy info================"
echo " job name ${JOB_NAME}"
echo " build id ${BUILD_ID}"
echo " compress file ${COMPRESS_FILE}"
echo " hosts ${HOSTS_STR}"
echo " exclude content ${EXCLUDE}"
echo " log path ${LOG_PATH}"
echo " local source directory ${SOURCE_FILE_DIR}"
echo " remote deploy directory ${DEPLOY_FILE_DIR}"
echo " remote backup directory ${BACKUP_FILE_DIR}"
echo "================Deploy info================"
}server
rsync(){
host=$1
echo "================Host ${host} deploy start================"
sshCmd="ssh root@${host} "
rsyncHost="root@${host}:"
if [ "localhost" == "${host}" ];then
sshCmd="sh -c "
rsyncHost=""
fi
#mkdirCmd="mkdir -p ${BACKUP_DIR}/${JOB_NAME}/ && mkdir -p ${DEPLOY_FILE_DIR}lib/ && mkdir -p ${DEPLOY_FILE_DIR}logs/"
mkdirCmd="mkdir -p ${BACKUP_DIR}/${JOB_NAME}/ && mkdir -p ${DEPLOY_SOURCE_FILE_DIR}"
echo "${RUN_CMD_COMMENT}${sshCmd}${mkdirCmd}"
${sshCmd}"${mkdirCmd}"
#scpCmd="scp -r /data/deploy-sh/boot-web/base/* root@${host}:${DEPLOY_FILE_DIR}"
#echo "${scpCmd}"
#${scpCmd}
rmCmd="cd ${DEPLOY_SOURCE_FILE_DIR}; ls | grep -v uploadFiles | xargs rm -rf"
echo "${RUN_CMD_COMMENT}${sshCmd}${rmCmd}"
${sshCmd}"${rmCmd}"
scpCmd="scp ${SOURCE_FILE_PARENT_DIR}/${COMPRESS_FILE} root@${host}:${DEPLOY_SOURCE_FILE_DIR}"
echo "${scpCmd}"
${scpCmd}
unWar="cd ${DEPLOY_SOURCE_FILE_DIR}; jar -xf ${COMPRESS_FILE}"
echo "${RUN_CMD_COMMENT}${sshCmd}${unWar}"
${sshCmd}"${unWar}"
if [ ! -z "${LOG_PATH}" ];then
echo "make symbolic link for log ..."
lnCmd="test ! -d ${LOG_PATH}/${UNCOMPRESS_FILE}/ && test ! -d ${DEPLOY_FILE_DIR}logs && mkdir -p ${LOG_PATH}/${UNCOMPRESS_FILE}/ && ln -s ${DEPLOY_FILE_DIR}logs ${LOG_PATH}/${UNCOMPRESS_FILE}/"
echo "${RUN_CMD_COMMENT}${sshCmd}${lnCmd}"
${sshCmd}"${lnCmd}"
fi
echo "Stop server ..."
stopCmd="source /etc/profile;cd ${DEPLOY_FILE_DIR}bin;./shutdown.sh"
echo "${RUN_CMD_COMMENT}${sshCmd}${stopCmd}"
${sshCmd}"${stopCmd}"
echo "Start server ..."
startCmd="source /etc/profile;cd ${DEPLOY_FILE_DIR}bin;./startup.sh"
echo "${RUN_CMD_COMMENT}${sshCmd}${startCmd}"
EXCODE=`${sshCmd}${startCmd};echo "qwe$?qwe"`
echo "EXCODE: ${EXCODE}"
RET=`echo ${EXCODE}|awk 'END {print}'|grep "qwe0qwe"`
if [[ $RET == "" ]];then
echo "${host} build fail !"
exit 1
fi
echo "================Host ${host} deploy end================"
}
main