#!/bin/bash
#author liyunpeng
#version 0.1
SOURCE_DIR="/data/deploy-source"
BACKUP_DIR="/data/deploy-backup"
DEPLOY_DIR="/data/job"
EXCLUDE_LIST_NAME="exclude.list"
RUN_CMD_COMMENT="run cmd : "bash
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=""ssh
main(){
getOpt
unzip
info
echo "Now deploying dubbo provider"
for host in ${HOSTS[@]};do
rsync ${host}
done
}ide
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 ""
}ui
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
}this
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
fiorm
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
fiserver
echo "${RUN_CMD_COMMENT}cd ${SOURCE_FILE_PARENT_DIR}"
cd ${SOURCE_FILE_PARENT_DIR}ip
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 "${UNCOMPRESS_FILE}" ];then
echo "$Uncompress file is null, please check the compress file"
help
exit 1
fi
SOURCE_EXCLUDE_LIST=${SOURCE_DIR}/${JOB_NAME}/${EXCLUDE_LIST_NAME}
BACKUP_EXCLUDE_LIST=${BACKUP_DIR}/${JOB_NAME}/${EXCLUDE_LIST_NAME}
if [ -z "${EXCLUDE}" ];then
EXCLUDE="log\nlogs"
echo "Using default exclude"
fi
echo -e ${EXCLUDE} > ${SOURCE_EXCLUDE_LIST}
SOURCE_FILE_DIR="${SOURCE_FILE_PARENT_DIR}/${UNCOMPRESS_FILE}/"
if [ ! -d "${SOURCE_FILE_DIR}" ];then
echo "Uncompress file : ${SOURCE_FILE_DIR} is not a directory, compress file may be wrong or compress fail"
help
exit 1
fi
DEPLOY_FILE_DIR="${DEPLOY_DIR}/${UNCOMPRESS_FILE}/"
BACKUP_FILE_DIR="${BACKUP_DIR}/${JOB_NAME}/${BUILD_ID}/${UNCOMPRESS_FILE}/"
}rem
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================"
}get
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}"
echo "${RUN_CMD_COMMENT}${sshCmd}${mkdirCmd}"
${sshCmd}"${mkdirCmd}"
syncExcludeListCmd="/usr/bin/rsync -aqP ${SOURCE_EXCLUDE_LIST} ${rsyncHost}${BACKUP_EXCLUDE_LIST}"
echo "${RUN_CMD_COMMENT}${syncExcludeListCmd}"
${syncExcludeListCmd}
echo "Backup ${DEPLOY_FILE_DIR} ..."
backupCmd="test -d ${DEPLOY_FILE_DIR} && test ! -d ${BACKUP_FILE_DIR} && mkdir -p ${BACKUP_FILE_DIR} && /usr/bin/rsync -aqP --delete --exclude-from=${BACKUP_EXCLUDE_LIST} ${DEPLOY_FILE_DIR} ${BACKUP_FILE_DIR}"
echo "${RUN_CMD_COMMENT}${sshCmd}${backupCmd}"
${sshCmd}"${backupCmd}"
echo "Rsync ${host} ..."
rsyncCmd="/usr/bin/rsync -avP --delete --exclude-from=${SOURCE_EXCLUDE_LIST} ${SOURCE_FILE_DIR} ${rsyncHost}${DEPLOY_FILE_DIR}"
echo "${RUN_CMD_COMMENT}${rsyncCmd}"
${rsyncCmd}
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 "sh cmd start ."
scpShCmd="scp -r /data/deploy-sh/job/bin/ root@${host}:${DEPLOY_FILE_DIR}"
${scpShCmd}
echo "Stop server ..."
stopCmd="source /etc/profile;sh ${DEPLOY_FILE_DIR}bin/stop.sh"
echo "${RUN_CMD_COMMENT}${sshCmd}${stopCmd}"
${sshCmd}"${stopCmd}"
echo "Start server ..."
startCmd="source /etc/profile;sh ${DEPLOY_FILE_DIR}bin/start.sh"
echo "${RUN_CMD_COMMENT}${sshCmd}${startCmd}"
${sshCmd}"${startCmd}"
echo "================Host ${host} deploy end================"
}
main