diff --git a/checkService.sh b/checkService.sh index b33343a..359de24 100755 --- a/checkService.sh +++ b/checkService.sh @@ -1,5 +1,5 @@ #!/bin/bash -## Usage +## Usage # 如果虚机上的shell里面不能执行sudu,这个脚本就能派上用场,在root用户下执行此脚本,也就绕过sudo限制了 ## ./checkService.sh ztsjxxxt.jar "/usr/local/jar" ztsjxxxt "/home/ubuntu/" "production/investigate/backEnd/" @@ -12,6 +12,17 @@ ossPath=$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/${ossPath}autoDeploy.log + if [ $? -ne 0 ]; then + echo "error: upload log to oss failed!" + exit 0 + fi +} + cd $workDirectory echo "current path: $(pwd)" if [ ! -f "$fileName" ];then @@ -21,19 +32,19 @@ fi oriSum=$(md5sum $fileName) oriSum="${oriSum% *}" -echo "origin file $fileName md5sum: $oriSum" +echo "origin file $fileName md5sum: $oriSum" if [ ! -d "$projectPath" ]; then echo "文件夹 $prjectPath 不存在!" exit 0 fi - + cd $projectPath desSum="" if [ ! -f "$fileName" ];then cp ~/$fileName . exit 0 -else +else desSum=$(md5sum $fileName) desSum="${desSum% *}" echo "current path: $(pwd)" @@ -44,10 +55,16 @@ if [ ${oriSum} = ${desSum} ];then journalctl -n 1000 -xe -t autoDeploy > ${workDirectory}autoDeploy.log ${workDirectory}ossutil cp ${workDirectory}autoDeploy.log oss://ztupload/${ossPath}autoDeploy.log echo "destination file is same with origin file,no need to replace" + uploadLog exit 0 fi -systemctl restart $serviceName + +systemctl restart $serviceName +if [ $? -ne 0 ]; then + echo "error: systemctl restart ${serviceName} failed!" + exit 0 +fi +echo "ok: systemctl restart ${serviceName} success!" + sleep 10 -journalctl -n 1000 -xe -t autoDeploy > ${workDirectory}autoDeploy.log -${workDirectory}ossutil cp ${workDirectory}autoDeploy.log oss://ztupload/${ossPath}autoDeploy.log - +uploadLog diff --git a/frontEndUpgrade.sh b/frontEndUpgrade.sh index 1d0c396..0dde409 100755 --- a/frontEndUpgrade.sh +++ b/frontEndUpgrade.sh @@ -50,7 +50,9 @@ if [ ${oriSum} = ${desSum} ];then fi cd ~ -unzip $fileName +rm dist -fr +rm $projectName -fr +unzip $fileName -fq echo "projectName: ${projectName}" mv dist ~/$projectName cd $projectPath diff --git a/ossUpgrade.sh b/ossUpgrade.sh index bb5e291..4a00d03 100755 --- a/ossUpgrade.sh +++ b/ossUpgrade.sh @@ -1,62 +1,93 @@ #!/bin/bash -# usage: -## 这个脚本能正常工作的前提是 systemd脚本如果不是root,也能通过sudo 调用高级权限来执行。 -## 否则就要结合checkService.sh一起使用。那个单独放在一个服务里,用root用户执行 +#usage: # ./ossUpgrade.sh "stage/ztsjxxxt/frontEnd/" html.zip ztsjxxxt + +PATH=PATH:/usr/bin:/usr/sbin ossPath=$1 #末尾别忘了加/ fileName=$2 serviceName=$3 projectName="${fileName%.*}" -PATH=PATH:/usr/bin:/usr/sbin -cd ~ -./ossutil stat oss://ztupload/${ossPath}$fileName > ${projectName}_oss.info -ossInfo=$(cat ${projectName}_oss.info) -out=${ossInfo##*Etag} -out=${out%Last*} -out=${out#*: } -ossSum=${out,,} -ossSum="${ossSum% *}" -echo "oss md5:"$ossSum -result=$(echo $ossInfo | grep "error") -if [[ "$result" != "" ]]; then - echo $result - exit 0 -fi -curSum=$(md5sum $fileName) -curSum="${curSum% *}" +function foundNew(){ + cd ~ + ./ossutil stat oss://ztupload/${ossPath}$fileName > ${projectName}_oss.info + ossInfo=$(cat ${projectName}_oss.info) + echo "${fileName} in oss info: " + echo " " $ossInfo + out=${ossInfo##*Length} + out=${out%Content-Md5*} + out=${out#*: } + ossLength=${out,,} + ossLength="${ossLength% *}" + ossLength=$(echo $ossLength) + ossLength="${ossLength%% *}" + echo "ossLength " "|"${ossLength}"|" + result=$(echo $ossInfo | grep "error") + if [[ "$result" != "" ]]; then + echo $result + exit 0 + fi + + curLength=$(ls -l ${fileName} | awk '{print $5}') + if [ "${curLength}" = "" ];then + echo "${fileName} not exist, download it" + ./ossutil cp oss://ztupload/${ossPath}$fileName ${fileName} + curLength=$(ls -l ${fileName} | awk '{print $5}') + curLength=$(sed -i 's/\n//g' $curLength) + fi + echo "ossLength ${fileName} : "$ossLength + echo "curLength ${fileName} : "$curLength + logPath=${ossPath%/*} + logPath=${logPath%/*}/logs/ + osslogPath=oss://ztupload/${logPath}autoDeploy.log + if [ "${ossLength}" = "${curLength}" ];then + return 0 + else + return 1 + fi +} + +function process(){ + cd ~ + rm $fileName + echo "fileName: " $fileName + ./ossutil cp oss://ztupload/${ossPath}${fileName} ${fileName} +} + +# function uploadLog(){ + # journalctl -n 1000 -xe -t autoDeploy > ~/autoDeploy.log + # if [ $? = 0 ];then + # echo "uploaded log to ${osslogPath}" + # fi +# } + +function main(){ + echo " " + echo "start ossUpgrade service" + foundNew + result=$? + echo "result $result" + if [ $result = 0 ];then + # uploadLog + echo "local temp file is same with oss file,no need to replace" + exit 0 + echo "end ossUpgrade service" + echo " " + else + echo "local temp file $fileName is different from oss, will download..." + fi + + process + sleep 10 + # uploadLog + echo "replaced" + echo "end ossUpgrade service" + echo " " +} + +main -if [ "${curSum}" = "" ];then - ./ossutil cp oss://ztupload/${ossPath}$fileName > ${fileName} - curSum=$(md5sum $fileName) - curSum="${curSum% *}" -fi -echo "local file $fileName md5:"$curSum -all=$ossSum -echo "oriMd5: "$all -all=$curSum -echo "curMd5: "$all -logPath=${ossPath%/*} -logPath=${logPath%/*}/logs/ -osslogPath=oss://ztupload/${logPath}autoDeploy.log -if [ ${ossSum} = ${curSum} ];then - sudo journalctl -n 1000 -xe -t autoDeploy > ~/autoDeploy.log - ./ossutil cp ~/autoDeploy.log ${osslogPath} -f - echo "uploaded log to ${osslogPath}" - echo "destination file is same with oss file,no need to replace" - exit 0 -else - echo "local file $fileName is different from oss, will download..." -fi -rm $fileName -echo "fileName: " $fileName -./ossutil cp oss://ztupload/${ossPath}${fileName} ${fileName} -sudo systemctl restart $serviceName -sleep 10 -sudo journalctl -n 1000 -xe -t autoDeploy > ~/autoDeploy.log -./ossutil cp ~/autoDeploy.log osslogPath -f -echo "replaced"