551   Java Linux Shell Tomcat

每次在Linux上发布Java项目都要做重复步骤:svn update,mvn clean install,backup code ,update code ,kill tomcat ,start tomcat
写个shell脚本,一键发布,无需重复敲命令:


#!/bin/bash

##
# function start
##



# 获取代码
function svn_update
{
	# 代码目录
	local FUNC_CODE_DIR="/data/hgaop/test-rp/source/rp-v0.0.1-20150324-0"
	echo -e "\n"
	echo -e "START : svn update \n"
	echo -e "FUNC_CODE_DIR : ${FUNC_CODE_DIR} \n"
	
	cd ${FUNC_CODE_DIR}
	# 获取最新代码
	svn update 
		
	echo -e "\n"
	echo -e "END : svn update \n"

}

# 打包代码
function package_code
{
	# 代码项目主目录
	local FUNC_WCS_DIR="/data/hgaop/test-rp/source/rp-v0.0.1-20150324-0/rp"
	# 打包配置文件
	local FUNC_PACKAGE_FILE="pom.xml"

	echo -e "\n"
	echo -e "START : package code \n"
	echo -e "FUNC_WCS_DIR : ${FUNC_WCS_DIR} \n"
	echo -e "FUNC_PACKAGE_FILE : ${FUNC_PACKAGE_FILE} \n"
	
	cd ${FUNC_WCS_DIR}

	# 打包
	mvn -T 1C -f ${FUNC_PACKAGE_FILE} clean install 
	
	
	echo -e "\n"
	echo -e "END : package code \n"
	
}


# 检查tomcat是否存在
function check_tomcat
{
	local FUNC_TOMCAT_PATH="/data/hgaop/test-rp/tomcat6-web"
	echo -e "\n"
	echo -e "Start : check_tomcat \n"
	ps -ef | grep ${FUNC_TOMCAT_PATH} | grep -v grep
	echo -e "End : check_tomcat \n"
	
}
# kill 掉tomcat
function kill_tomcat
{
	local FUNC_TOMCAT_PATH="/data/hgaop/test-rp/tomcat6-web"
	echo -e "\n"
	echo -e "Start : kill_tomcat \n"
	# 查找tomcat的进程,并强制杀死
	ps -ef | grep ${FUNC_TOMCAT_PATH} | grep -v grep | awk '{print $2}' | xargs kill -9
	# 等待进程死掉
	sleep 1
	echo -e "End : kill_tomcat \n"
}

# 启动tomcat
function start_tomcat
{
	local FUNC_TOMCAT_PATH="/data/hgaop/test-rp/tomcat6-web"
	echo -e "\n"
	echo -e "Start : start_tomcat \n"
	# 启动tomcat
	${FUNC_TOMCAT_PATH}/bin/startup.sh
	# 等待tomcat启动
	sleep 1
	echo -e "End : start_tomcat \n"
}




# 备份
function backup_code
{

	# 发布包的目录
	local FUNC_PROD_PATH="/home/hgaop/data/test-rp/tomcat6-web/webapps"
	# 备份包的目录
	local FUNC_BACK_PATH="/home/hgaop/data/test-rp/backup_code"
	# 备份包名
	local FUNC_BACKUP_NAME=`date '+%Y%m%d%H%M'`.tar
	
	echo -e "\n"
	echo -e "START : backup_code \n"
	echo -e "\n"
	# 进行备份,并显示备份文件
	cd ${FUNC_PROD_PATH}
	tar -cf ${FUNC_BACKUP_NAME} *.war
	mv ${FUNC_BACKUP_NAME} ${FUNC_BACK_PATH}
	cd ${FUNC_BACK_PATH}
	pwd
	ls -l ${FUNC_BACKUP_NAME}
	
	echo -e "\n"
	echo -e "END : backup_code \n"
}

# 更新
function update_code
{
	# 发布包的目录
	local FUNC_PROD_PATH="/home/hgaop/data/test-rp/tomcat6-web/webapps"
	# 打包的目录
	local FUNC_CODE_PATH="/data/hgaop/test-rp/source/rp-v0.0.1-20150324-0/rp-web/target"
	# 包名
	local FUNC_CODE_NAME="rp-web.war"

	echo -e "\n"
	echo -e "START : update_code \n"
	
	# 发布项目
	cp ${FUNC_CODE_PATH}/${FUNC_CODE_NAME} ${FUNC_PROD_PATH}
	cd ${FUNC_PROD_PATH}
	pwd
	ls -l ${FUNC_CODE_NAME}
	
	echo -e "\n"
	echo -e "END : update_code \n"
}


# 用于判断是否要重复执行,还是跳过等
function skip_check
{
        # 获取需要执行的方法名
        local FUNC_CHECK_FUNCTION_NAME="$1"
        local SKIP_CHECK_RETURN="done"

        echo -e "\n"
        echo -e "Next Function: ${FUNC_CHECK_FUNCTION_NAME} \n"
        echo -e "\n"
        echo -e "Input 'done' if done \n"
        echo -e "Input 'skip' if skip \n"
		echo -e "Input 'back' if back \n"
        echo -e "Input 'quit' if quit \n"
        echo -e "\n"
        printf "Your choice : "

        # 捕捉输入
        read FUNC_BREAK_CHECK_READ

        echo -e "\n"

        # 添加X,避免输入空格异常
        if [ ${FUNC_BREAK_CHECK_READ}X == "done"X ]
        then
                echo -e "done ${FUNC_CHECK_FUNCTION_NAME} \n"
                return 1
		elif [ ${FUNC_BREAK_CHECK_READ}X == "skip"X ]
        then
                echo -e "skip ${FUNC_CHECK_FUNCTION_NAME} \n"
                return 2
        elif [ ${FUNC_BREAK_CHECK_READ}X == "back"X ]
        then
                echo -e "back ${FUNC_CHECK_FUNCTION_NAME} \n"
                return 3
        elif [ ${FUNC_BREAK_CHECK_READ}X == "quit"X ]
        then
                echo -e "quit ${FUNC_CHECK_FUNCTION_NAME} \n"
                return 4
        else
                # 如果输入异常,重复执行
		echo -e "Input is not right \n"
                skip_check ${FUNC_CHECK_FUNCTION_NAME}
        fi

}

# 用于循环执行函数
function run_main
{
		# 循环执行的 function 
		local RUN_MAIN_FUNCTION=(
				svn_update
				package_code
				kill_tomcat
				check_tomcat
				backup_code
				update_code
				start_tomcat
				check_tomcat	
		)
		
        local FUNC_FUNCTION_NUMBER=${#RUN_MAIN_FUNCTION[@]}
        local FUNC_SKIP_CHECK_RETURN=1
        local ALL_FUNCTION_RESULT


        local FUNC_COUNTER=0
        # 循环执行方法
        while [ ${FUNC_COUNTER} -lt ${FUNC_FUNCTION_NUMBER} ]
        do
		# 判断是否要执行
                skip_check ${RUN_MAIN_FUNCTION[${FUNC_COUNTER}]}
		# 获取判断值
                FUNC_SKIP_CHECK_RETURN=$?

                if [ ${FUNC_SKIP_CHECK_RETURN} -eq 1 ]
                then
		        #执行函数
			${RUN_MAIN_FUNCTION[${FUNC_COUNTER}]}
			FUNC_COUNTER=`expr ${FUNC_COUNTER} + 1`
                elif [ ${FUNC_SKIP_CHECK_RETURN} -eq 2 ]
                then
			# 跳过
			FUNC_COUNTER=`expr ${FUNC_COUNTER} + 1`
                elif [ ${FUNC_SKIP_CHECK_RETURN} -eq 3 ]
                then
			# 回退
                        if [ ${FUNC_COUNTER} -gt 0 ]
                        then 
                                FUNC_COUNTER=`expr ${FUNC_COUNTER} - 1`
                        fi
                elif [ ${FUNC_SKIP_CHECK_RETURN} -eq 4 ]
                then 
			# 退出
                        break;
                fi


        done

        echo -e "\n\n"
        echo -e "All of functions had be completed \n"



}

##
# function end
##


##
# main start
##

run_main


##
# main end
##



Leave a Reply

Your email address will not be published. Required fields are marked *