jenkinsdockerelkzookeeper
#!/usr/bin/env bash
unset BUILD_ID
help(){
echo " 参数: "
echo " ./$0 start|stop|status|restart|dst|deploy|help"
echo " -j jar包存放目录 /xxx/xxx"
echo " -c config文件目录 /xxx/xxx"
echo " -g 配置文件名称 application.config"
echo " -d pid文件名称 application.pid"
echo " -s jenkins打包出来的jar文件"
echo " -m 目标机器 127.0.0.1"
echo " -p 端口号port 8282"
echo " -v 版本version 201804082112_afsd12"
echo " -x 设置jar启动的最大堆内存"
echo " -h 帮助文档"
}
checkPidFile() {
echo ${pid_file}
if [ -f ${pid_file} ];then
echo "pid file is exist so is running"
return 1
else
echo "pid file is not exist so is not running"
return 0
fi
}
start() {
echo "start ....."
checkPidFile
if [ $? -eq 1 ];then
echo $project is running...
exit 2
fi
cd ${jar_dir}
echo $PWD
`nohup java ${xmx} -jar $PWD/${version}.jar > /dev/null 2> nohup.out &`
echo $PWD
progress_line=0
count=0
while(($progress_line == 0))
do
sleep 2;
checkPidFile
progress_line=$?
count=$(($count+1))
echo "第${count}次检查完毕"
[ ${count} -eq 20 ] && echo start is fault .... && exit 3
done
echo start is successful ....
}
stop() {
echo "stop ....."
checkPidFile
if [ $? -eq 1 ];then
echo `cat ${pid_file}`
kill `cat ${pid_file}`
fi
progress_line=1
count=0
while(($progress_line != 0))
do
checkPidFile
progress_line=$?
sleep 2;
count=$(($count+1))
echo "第${count}次检查完毕"
[ ${count} -eq 20 ] && echo stop is fault .... && exit 3
done
echo "stop is successful ...."
}
distribute() {
ssh $machine "mkdir -pv ${jar_dir}"
scp ${jenkins_jar_file} $machine:${jar_dir}/${version}.jar
if [ $? -eq 0 ];then
echo "distribute ${version} is successful...."
else
echo "distribute ${version} is fault...."
exit 4;
fi
}
deploy(){
jarfile=${jar_dir}/${version}.jar
echo $jarfile
if [ -f ${jarfile} ];then
stop
start
else
echo ${version}"版本文件不存在"
exit 2
fi
}
restart() {
stop
start
}
checkStatus(){
checkPidFile
if [ $? -eq 1 ];then
echo "Running"
else
echo "not Running"
fi
}
if [ $# -eq 0 ]; then
help
exit 1
fi
COMMAND=$1
shift
while getopts "j:c:g:d:s:m:p:v:x:h" opt
do
echo "export param $opt, $OPTARG"
case $opt in
j) jar_dir=$OPTARG;;
c) config_dir=$OPTARG;;
g) config_file=$OPTARG;;
d) pid_file=$OPTARG;;
s) jenkins_jar_file=$OPTARG;;
m) machine=$OPTARG;;
p) port=$OPTARG;;
v) version=$OPTARG;;
x) xmx=$OPTARG;;
h) help
echo "export port $opt" ;;
esac
done
if [ ${COMMAND} == start ]; then
start
elif [ ${COMMAND} == status ];then
checkStatus
elif [ ${COMMAND} == stop ]; then
stop
elif [ ${COMMAND} == restart ]; then
stop
start
elif [ ${COMMAND} == dst ];then
distribute
elif [ ${COMMAND} == deploy ];then
deploy
elif [ ${COMMAND} == "help" ];then
help
elif [ ${COMMAND} == "h" ];then
help
else
help
fi
export JAVA_HOME=/home/hlvan/setup/jdk1.8.0
export JRE_HOME=/home/hlvan/setup/jdk1.8.0/jre
app_name="hm"
tomcat_name="tomcat-hm"
tomcat_dir="/jenkins/tomcat/${tomcat_name}"
project_dir="/jenkins/project/${app_name}"
http_address=$(hostname)
printHelp(){
echo " Usage: "
echo " ./$0 start|stop|status|restart|distribute|deploy "
echo " -p port "
echo " -c dev/conf the conf file to use"
echo " -v version"
echo " -t ${app_name} / ${app_name}_ro"
echo " -b is bms.."
}
SUB_COMMAND=$1
shift
while getopts "p:c:v:t:f" opt
do
echo "export param $opt, $OPTARG"
case $opt in
p)
PORT=$OPTARG;;
c)
echo "export port $opt, $OPTARG"
CONF_FILE=$OPTARG;;
v)
VERSION=$OPTARG;;
t)
TYPE=$OPTARG;;
f)
FORCE=1;;
\?) printHelp
echo "export port $opt"
esac
done
if [ $# -eq 0 ]; then
printHelp
exit 1
fi
port1=`echo ${PORT} | cut -d" " -f1`
port2=`echo ${PORT} | cut -d" " -f2`
port3=`echo ${PORT} | cut -d" " -f3`
target_dir="${project_dir}/target"
pid_file="${project_dir}/${port1}.pid"
version_dir="${project_dir}/${VERSION}"
sourcefile="/jenkins/war/${app_name}/${VERSION}.war"
mark() {
last_version=`ls -ld ${target_dir} | awk '{print $NF}'|awk -F'/' '{print $NF}'`
echo ${last_version} > "${project_dir}/${TYPE}.version"
}
isrunning() {
progress_line=`netstat -lnutp|grep -c ":${port1}[[:space:]]"`
if [ ${progress_line} -ge 1 ];then
echo $port is running...
return 0
else
echo $port is not running...
return 1
fi
}
start() {
echo "start ....."
isrunning
if [ $? -eq 0 ];then
echo $port1 is running...
exit 2
fi
BUILD_ID=dontKillMe
${tomcat_dir}/bin/startup.sh
progress_line=0
count=0
while(($progress_line == 0))
do
sleep 2;
progress_line=`netstat -lnutp|grep -c ":$port1[[:space:]]"`
count=$(($count+1))
echo $count
[ ${count} -eq 20 ] && echo start is fault .... && exit 3
done
echo start is successful ....
}
stop() {
echo "stop ....."
isrunning
if [ $? -eq 0 ];then
progress=`netstat -lnupt|grep ":$port1[[:space:]]"|awk -F "[ /]+" '{print $7}' `
echo "kill $progress"
BUILD_ID=dontKillMe
${tomcat_dir}/bin/shutdown.sh
fi
progress_line=1
count=0
while(($progress_line != 0))
do
progress_line=`netstat -lnutp|grep -c ":$port1[[:space:]]"`
sleep 2;
count=$(($count+1))
echo $count
[ ${count} -eq 20 ] && echo stop is fault .... && exit 3
done
echo "stop is successful ...."
}
distribute() {
flag=${FORCE}
echo ${flag}
if [ ! -z $flag ];then
if [ $flag -eq 1 ];then
stop
if [ $? -eq 0 ];then
rm -rf ${version_dir}
fi
fi
fi
if [ -d ${version_dir} ];then
echo ${version_dir} is exist ....
exit 3;
fi
unzip -oq ${sourcefile} -d ${version_dir} 1>/dev/null
if [ $? -eq 0 ];then
echo "distribute ${VERSION} is successful...."
else
echo "distribute ${VERSION} is fault...."
exit 4;
fi
}
deploy(){
if [ ! -d ${version_dir} ];then
echo ${version_dir} is not exist ....
exit 3;
fi
stop
mark
echo "delete soft link...."
/bin/rm -rf ${target_dir}
echo "create soft link...."
/bin/ln -s ${version_dir} ${target_dir}
start
}
restart() {
stop
start
}
checkStatus(){
isRunning
if [ $? -eq 0 ];then
echo "Running"
else
echo "not Running"
fi
}
if [ ${SUB_COMMAND} == start ]; then
start
elif [ ${SUB_COMMAND} == status ];then
checkStatus
elif [ ${SUB_COMMAND} == stop ]; then
stop
elif [ ${SUB_COMMAND} == restart ]; then
stop
start
elif [ ${SUB_COMMAND} == dst ];then
distribute
elif [ ${SUB_COMMAND} == deploy ];then
deploy
else
printHelp
fi