#!/bin/bash
PATH=/application/jdk/bin:/application/jdk/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
REMOTE=/home/qroot/shangxian
LOCAL_HOST=/home/qroot/shangxian
LOG_CDATE='date "+%Y-%m-%d"'
LOG_CTIME='date "+%H:%M:%S"'

CDATE=$(date "+%Y-%m-%d")
CTIME=$(date "+%H-%M-%S")

SHELL_NAME="deployment_bigdata.sh"
SHELL_DIR="/home/qroot"
SHELL_LOG="${SHELL_DIR}/${SHELL_NAME}.log"

LOCK_FILE="/tmp/deploy_bigdata.lock"


#mkdir -p /home/qroot/shangxian/bigdata/{new_word,old_word,tmp,formal,config/{nginx,replace}}

usage(){
echo "USAGE:$0 AGE{bigdata [bigdata.tar.gz]|10.28.53.{192,223}] }"
}

shell_lock(){
	touch $LOCK_FILE
}

shell_unlock(){
	rm -f $LOCK_FILE
}

writelog(){
	LOGINFO=$1
	echo "${LOG_CDATE} ${LOG_CTIME}: ${SHELL_NAME} : ${LOGINFO} " >> ${SHELL_LOG}
}

tq_tomcat_docker(){
sudo sh -x /application/tomcat/bin/shutdown.sh
PD_1=$(sudo ps -ef|grep '/application/tomcat/bin'|grep -v 'grep'|wc -l)
sleep 30
if [ ${PD_1} -gt 0 ] 
then
PID_1=$(sudo ps -ef|grep '/application/tomcat/bin'|grep -v 'grep'|awk 'NR==1{print $2}')
sudo kill -9 ${PID_1}
fi
sleep 5 

sudo mkdir -p /application/tomcat/old_logs/logs_${CDATE}
sudo mv /application/tomcat/logs/* /application/tomcat/old_logs/logs_${CDATE}/
cd /application/tomcat/work/Catalina/ && sudo rm -fr *
sudo su - tomcat -c "sh /application/tomcat/bin/startup.sh"
}

docker_bigdata_scp() {
	writelog "docker_bigdata_scp"
[ ! -d ${LOCAL_HOST}/bigdata/new_word ] && echo  "${LOCAL_HOST}/bigdata/new_word ---bucunzai" && exit 16 
   if [ -f "${LOCAL_HOST}/bigdata/new_word/${1}" ]
    then
        echo "$(ls ${LOCAL_HOST}/bigdata/new_word/${1})"  
	
	mv ${LOCAL_HOST}/bigdata/new_word/${1} ${LOCAL_HOST}/bigdata/new_word/bigdata_"$CDATE"-${CTIME}.war || exit 16
		if [ -f "${LOCAL_HOST}/bigdata/new_word/bigdata_"$CDATE"-${CTIME}.war" ]
		  then
			echo "$(ls ${LOCAL_HOST}/bigdata/new_word/bigdata_"$CDATE"-${CTIME}.war)"
		  else
			echo "bigdata_"$CDATE"-${CTIME}.war -- File does not exist" && exit 11
		fi
    else
        echo "${1}--File does not exist" && exit 10
   fi
}

docker_bigdata_tar() {
        writelog "docker_bigdata_tar"
	PD_1=$(ls ${REMOTE}/bigdata/new_word/bigdata_"$CDATE"-${CTIME}.war 2>/dev/null|wc -l)
	if [ ${PD_1} -ne 0 ]
	   then
		cd /application/tomcat/web_tmp/ && sudo rm -fr *
		sudo unzip ${LOCAL_HOST}/bigdata/new_word/bigdata_"$CDATE"-${CTIME}.war -d /application/tomcat/web_tmp/bigdata_"$CDATE"-${CTIME}
		mv ${REMOTE}/bigdata/new_word/bigdata_"$CDATE"-${CTIME}.war ${REMOTE}/bigdata/old_word/
		cd ${REMOTE}/bigdata/new_word/ && sudo rm -fr *
		cd /application/tomcat/webapps/ && sudo rm -fr *
		sudo ln -s /application/tomcat/web_tmp/bigdata_"$CDATE"-${CTIME} /application/tomcat/webapps/bigdata_new
                sudo chmod -R +x  /application/tomcat/webapps/bigdata_new/* 
                sudo chown -R tomcat.tomcat /application/tomcat
                sudo chown -R tomcat.tomcat /application/tomcat/*
		sudo chmod -R +x /application/tomcat/webapps/bigdata/WEB-INF/classes/com/*
	   else
	  	echo ${1}-bigdata_"$CDATE"-${CTIME}.war -- File does not exist && exit 12
	fi
}

replace_config() {
	writelog "config"
	PD_2=$(ls ${REMOTE}/bigdata/config/replace/bigdata/* 2>/dev/null|wc -l)
	if [ ${PD_2} -gt 0 ]
	then
		sudo \cp ${REMOTE}/bigdata/config/replace/bigdata/* /application/tomcat/webapps/bigdata_new/WEB-INF/classes/config/db/
	else
		echo "replace===$(ls ${REMOTE}/bigdata/config/replace/* 2>/dev/null|wc -l)"
	fi
}



huizong(){
 if [ -f $LOCK_FILE ];then
   echo "Deploy is running" && exit;
 fi
TAR_NAME=${2}
CASE=${1}
case "${CASE}" in
   bigdata)
	shell_lock
	docker_bigdata_scp ${TAR_NAME} 
	sleep 10
	docker_bigdata_tar 
	sleep 10
	replace_config
	sleep 10
	tq_tomcat_docker
	shell_unlock
;;
*)
	usage
        exit 3
esac
}

mian(){
CMD_1=$1
CMD_2=$2
huizong $CMD_1 $CMD_2 
}

mian ${1}  ${2}