#!/bin/bash
#author
#version 0.1
SOURCE_DIR="/data/deploy-source"
BACKUP_DIR="/data/deploy-backup"
DEPLOY_DIR="/data/web-api"
RUN_CMD_COMMENT="run cmd : "
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=""
main(){
getOpt
unzip
info
echo "Now deploying web-api"
for host in ${HOSTS[@]};do
rsync ${host}
done
}
help(){
echo "usage: dubbo-provider-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 ""
}
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
}
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
fi
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
fi
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
fi
DEPLOY_FILE_DIR="${DEPLOY_DIR}/${UNCOMPRESS_FILE}/"
BACKUP_FILE_DIR="${BACKUP_DIR}/${JOB_NAME}/${BUILD_ID}/${UNCOMPRESS_FILE}/"
}
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================"
}
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/"
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}
scpCmd="scp ${SOURCE_FILE_PARENT_DIR}/${COMPRESS_FILE} root@${host}:${DEPLOY_FILE_DIR}lib/"
echo "${scpCmd}"
${scpCmd}
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 ${LOG_PATH}/${UNCOMPRESS_FILE}/ ${DEPLOY_FILE_DIR}logs"
echo "${RUN_CMD_COMMENT}${sshCmd}${lnCmd}"
${sshCmd}"${lnCmd}"
fi
echo "Stop server ..."
stopCmd="source /etc/profile;cd ${DEPLOY_FILE_DIR}bin;./stop.sh"
echo "${RUN_CMD_COMMENT}${sshCmd}${stopCmd}"
${sshCmd}"${stopCmd}"
echo "Start server ..."
startCmd="source /etc/profile;cd ${DEPLOY_FILE_DIR}bin;./start.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