autoDeploy/checkService.sh
zhangkun9038@dingtalk.com 291a3e314b update
2022-09-10 11:21:19 +00:00

98 lines
2.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
## Usage
# 如果虚机上的shell里面不能执行sudu这个脚本就能派上用场在root用户下执行此脚本也就绕过sudo限制了
## ./checkService.sh ztsjxxxt.jar "/usr/local/jar" ztsjxxxt "/home/ubuntu/" "production/investigate/backEnd/"
fileName=$1
projectPath=$2
serviceName=$3
workDirectory=$4
ossLogPath=$5
PATH=PATH:/usr/bin:/usr/sbin
function uploadLog ()
{
echo "current user is ${USER}"
journalctl -n 1000 -xe -t autoDeploy > ${workDirectory}autoDeploy.log
${workDirectory}ossutil cp ${workDirectory}autoDeploy.log oss://ztupload/${ossLogPath}autoDeploy.log
if [ $? -ne 0 ]; then
echo "error: upload log to oss failed!"
exit 0
fi
}
function compare(){
cd $workDirectory
echo "current path: $(pwd)"
if [ ! -f "$fileName" ];then
echo "文件 $fileName 不存在1!"
return -1
fi
oriSum=$(md5sum $fileName)
oriSum="${oriSum% *}"
echo "origin local temp file $fileName md5sum: |${oriSum}|"
if [ ! -d "$projectPath" ]; then
echo "文件夹 $prjectPath 不存在!"
return -1
fi
cd $projectPath
desSum=""
if [ ! -f "$fileName" ];then
cp $workDirectory$fileName .
return 0
else
desSum=$(md5sum $fileName)
desSum="${desSum% *}"
echo "current path: $(pwd)"
echo "locfile in projectPath ${fileName} md5sum: |${desSum}|"
fi
if [ ${oriSum} = ${desSum} ];then
#journalctl -n 1000 -xe -t autoDeploy > ${workDirectory}autoDeploy.log
return 0
else
return 1
fi
}
function restart(){
systemctl restart $serviceName
if [ $? -ne 0 ]; then
echo "error: systemctl restart ${serviceName} failed!"
exit 0
fi
echo "ok: systemctl restart ${serviceName} success!"
}
function main(){
echo " "
echo "start checkService service"
compare
result=$?
echo "result $result"
if [ $result = 0 ];then
# uploadLog
echo "local temp file ${fileName} is same with file in ${projectPath} no need to replace"
echo "ok: checkService done"
echo " "
exit 0
elif [ $result = 1 ]; then
echo "local temp file $fileName is different from ${projectPath}, will replace..."
else
exit 0
fi
restart
sleep 10
uploadLog
echo "ok: checkService done"
echo " "
exit 0
}
main