shell脚本 服务器间更新jar包

restart_all.sh

#!/usr/bin/env bash

set -e

service=$1

max_memory=3096m

start_success=0

time=`date "+%Y-%m-%d %H:%M:%S "`

kill_service() {
    service_port=$1
    service_pid=$(netstat -tnlp | grep ${service_port} |awk '{print $7}' |awk -F'/' '{print $1}')
    if [ ! -n "$service_pid" ]; then
        echo "$2 service is not started, skiping..."
    else
        echo "Begin to kill $2 service..."
        kill -9 $service_pid
        echo "$2 service stopped"
    fi
}

exit_script() {
        echo "${time} start $1 service failed. Please check log folder for detail information."
        echo "${time} start $1 service failed. Please check log folder for detail information." >> start.log
}

# startDiscovery() {
#         echo ">>> Begin to start discovery service"
#         i=0
#         start_success=0
#         cd registry
#         nohup java -Xmx512m -jar registry.war >/dev/null 2>&1 &
#         echo -n "Waiting for discovery service......"
#         while [ -z ${DISCOVERY_SERVICE_READY} ] && [ ${i} -lt 120 ]; do
#           echo -n "......"
#           if [ "$(curl --silent localhost:5001/management/health 2>&1 | grep -q '\"status\"[[:space:]]*:[[:space:]]*\"UP\"'; echo $?)" = 0 ]; then
#               DISCOVERY_SERVICE_READY=true;
#               start_success=1
#               echo -e "\ndiscovery service started"
#           fi
#           i=`expr $i + 1`;
#           sleep 1
#         done
#         cd ..
#         if [ ${start_success} -eq 0 ]; then
#             exit_script "discovery"
#         fi
# }

startEdge() {
        echo ">>> Begin to start edge service"
        i=0
        start_success=0
        cd /root/common/gateway
        nohup java -Xmx512m -jar gateway.war >/dev/null 2>&1 &
        echo -n "Waiting for edge service......"
        while [ -z ${EDGE_SERVICE_READY} ] && [ ${i} -lt 120 ]; do
          echo -n "......"
          if [ "$(curl --silent localhost:9001/management/health 2>&1 | grep -q '\"status\"[[:space:]]*:[[:space:]]*\"UP\"'; echo $?)" = 0 ]; then
              EDGE_SERVICE_READY=true;
              start_success=1
              echo -e "\nedge service started"
          fi
          i=`expr $i + 1`;
          sleep 1
        done
        cd ..
        if [ ${start_success} -eq 0 ]; then
            exit_script "edge"
        fi
}

# startUser() {
#         echo ">>> Begin to start user service"
#         i=0
#         start_success=0
#         cd user
#         nohup java -Xmx512m -jar net-user-service.jar >/dev/null 2>&1 &
#         echo -n "Waiting for user service......"
#         while [ -z ${USER_SERVICE_READY} ] && [ ${i} -lt 120 ]; do
#           echo -n "......"
#           if [ "$(curl -i localhost:8181/actuator/health 2>&1 | grep -q '\"status\":\"UP\"'; echo $?)" = 0 ]; then
#               USER_SERVICE_READY=true;
#               start_success=1
#               echo -e "\nuser service started"
#           fi
#           i=`expr $i + 1`;
#           sleep 1
#         done
#         cd ..
#         if [ ${start_success} -eq 0 ]; then
#             exit_script "user"
#         fi
# }

startBlock() {
        echo ">>> Begin to start block service"
        i=0
        start_success=0
        cd block
        nohup java -Xmx${max_memory} -jar gorich-block-service-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
        # nohup java -Xmx${max_memory} -jar block-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
        echo -n "Waiting for block service......"
        while [ -z ${BLOCK_SERVICE_READY} ] && [ ${i} -lt 240 ]; do
          echo -n "......"
          if [ "$(curl --silent localhost:7003/rest/block/check 2>&1 | grep -q '\"result\"[[:space:]]*:[[:space:]]*true'; echo $?)" = 0 ]; then
              BLOCK_SERVICE_READY=true;
              start_success=1
              echo -e "\nblock service started"
          fi
          i=`expr $i + 1`;
          sleep 1
        done
        cd ..
        if [ ${start_success} -eq 0 ]; then
            exit_script "block"
        fi
}

startNlp() {
        echo ">>> Begin to start nlp service"
        i=0
        start_success=0
        cd nlp
        nohup java -Xmx${max_memory} -jar gorich-nlp-service-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
        # nohup java -Xmx${max_memory} -jar nlp-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
        echo -n "Waiting for nlp service......"
        while [ -z ${NLP_SERVICE_READY} ] && [ ${i} -lt 240 ]; do
          echo -n "......"
          if [ "$(curl --silent localhost:7002/rest/word-segmenter/check 2>&1 | grep -q '\"result\"[[:space:]]*:[[:space:]]*true'; echo $?)" = 0 ]; then
              NLP_SERVICE_READY=true;
              start_success=1
              echo -e "\nnlp service started"
          fi
          i=`expr $i + 1`;
          sleep 1
        done
        cd ..
        if [ ${start_success} -eq 0 ]; then
            exit_script "nlp"
        fi
}

startArticle() {
        echo ">>> Begin to start article service"
        i=0
        start_success=0
        cd article
        nohup java -Xmx${max_memory} -jar gorich-article-service-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
        # nohup java -Xmx${max_memory} -jar article-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
        echo -n "Waiting for article service......"
        while [ -z ${ARTICLE_SERVICE_READY} ] && [ ${i} -lt 120 ]; do
          echo -n "......"
          if [ "$(curl --silent localhost:7004/management/health 2>&1 | grep -q '\"status\"[[:space:]]*:[[:space:]]*\"UP\"'; echo $?)" = 0 ]; then
              ARTICLE_SERVICE_READY=true;
              start_success=1
              echo -e "\narticle service started"
          fi
          i=`expr $i + 1`;
          sleep 1
        done
        cd ..
        if [ ${start_success} -eq 0 ]; then
            exit_script "article"
        fi
}

startModel() {
        echo ">>> Begin to start model service"
        i=0
        start_success=0
        cd model
        nohup java -Xmx2560m -jar gorich-model-service-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
        # nohup java -Xmx256m -jar model-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
        echo -n "Waiting for model service......"
        while [ -z ${MODEL_SERVICE_READY} ] && [ ${i} -lt 120 ]; do
          echo -n "......"
          if [ "$(curl --silent localhost:7005/management/health 2>&1 | grep -q '\"status\"[[:space:]]*:[[:space:]]*\"UP\"'; echo $?)" = 0 ]; then
              MODEL_SERVICE_READY=true;
              start_success=1
              echo -e "\nmodel service started"
          fi
          i=`expr $i + 1`;
          sleep 1
        done
        cd ..
        if [ ${start_success} -eq 0 ]; then
            exit_script "model"
        fi
}

startWebTag() {
        echo ">>> Begin to start web service"
        i=0
        start_success=0
        cd web
        nohup java -Xmx512m -jar news-insight-web.jar >/dev/null 2>&1 &
        echo -n "Waiting for web service......"
        while [ -z ${WEB_TAG_SERVICE_READY} ] && [ ${i} -lt 120 ]; do
          echo -n "......"
          if [ "$(curl -i localhost:9002/actuator/health 2>&1 | grep -q '\"status\":\"UP\"'; echo $?)" = 0 ]; then
              WEB_TAG_SERVICE_READY=true;
              start_success=1
              echo -e "\nweb service started"
          fi
          i=`expr $i + 1`;
          sleep 1
        done
        cd ..
        if [ ${start_success} -eq 0 ]; then
            exit_script "web"
        fi
}

startWebEvent() {
         echo ">>> Begin to start web-event service"
         i=0
         start_success=0
         cd web-event
         nohup java -Xmx256m -jar news-insight-web.jar >/dev/null 2>&1 &
         echo -n "Waiting for web-event service......"
         while [ -z ${WEB_EVENT_SERVICE_READY} ] && [ ${i} -lt 120 ]; do
           echo -n "......"
           if [ "$(curl -i localhost:9003/hello 2>&1 | grep -q '/oauth2/authorization/uaa'; echo $?)" = 0 ]; then
               WEB_EVENT_SERVICE_READY=true;
               start_success=1
               echo -e "\nweb-event service started"
           fi
           i=`expr $i + 1`;
           sleep 1
         done
         cd ..
         if [ ${start_success} -eq 0 ]; then
             exit_script "web"
         fi
}

startData() {
        echo ">>> Begin to start data service"
        i=0
        start_success=0
        cd data
        nohup java -Xmx512m -jar gorich-data-service-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
        # nohup java -Xmx512m -jar data-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
        echo -n "Waiting for data service......"
        while [ -z ${DATA_SERVICE_READY} ] && [ ${i} -lt 120 ]; do
          echo -n "......"
          if [ "$(curl --silent localhost:7006/management/health 2>&1 | grep -q '\"status\"[[:space:]]*:[[:space:]]*\"UP\"'; echo $?)" = 0 ]; then
              DATA_SERVICE_READY=true;
              start_success=1
              echo -e "\ndata service started"
          fi
          i=`expr $i + 1`;
          sleep 1
        done
        cd ..
        if [ ${start_success} -eq 0 ]; then
            exit_script "data"
        fi
}


printHelp() {
        echo "Please input following service name:"
        echo "all"
        # echo "discovery"
        # echo "edge"
        # echo "user"
        echo "block"
        echo "nlp"
        echo "article"
        echo "model"
        echo "data"
        echo "web-tag"
        echo "web-event"
}

if [ ! $service ]; then
        printHelp
        
# elif [ $service == 'discovery' ]; then
#         kill_service 5001 "discovery"
#         startDiscovery
        
# elif [ $service == 'edge' ]; then
#         kill_service 9001 "edge"
#         startEdge
        
# elif [ $service == 'user' ]; then
#         kill_service 8181 "user"
#         startUser
        
elif [ $service == 'block' ]; then
        kill_service 7003 "block"
        startBlock
        
elif [ $service == 'nlp' ]; then
        kill_service 7002 "nlp"
        startNlp
        
elif [ $service == 'article' ]; then
        kill_service 7004 "article"
        startArticle
        
elif [ $service == 'model' ]; then
        kill_service 7005 "model"
        startModel
            
elif [ $service == 'data' ]; then
        kill_service 7006 "data"
        startData

elif [ $service == 'web-tag' ]; then
        kill_service 9002 "web-tag"
        startWebTag

elif [ $service == 'web-event' ]; then
         kill_service 9003 "web-event"
         startWebEvent
        
elif [ $service == 'all' ]; then
        kill_service 7006 "data"
        kill_service 9002 "web-tag"
        kill_service 9003 "web-event"
        kill_service 7004 "article"
        kill_service 7002 "nlp"
        kill_service 7003 "block"
        kill_service 7005 "model"
        # kill_service 8181 "user"
        #kill_service 9001 "edge"
        #kill_service 1111 "discovery"
        
        #startDiscovery
        #startEdge
        # startUser
        startBlock
        startNlp
        startArticle
        startModel
                startData
        startWebTag
                startWebEvent
        
else
        printHelp
fi

updateFile.sh

    if [ $1 ]; then
        for serviceName in $*
        do
            if [ $serviceName == 'article' ]; then
                serviceDir='article'                    #服务目录名
                service='article-0.0.1-SNAPSHOT.jar'    #服务jar包
                rootDir=''                                #项目根目录,不需要/开头

            elif [ $serviceName == 'block' ]; then
                serviceDir='block'
                service='block-0.0.1-SNAPSHOT.jar'

            elif [ $serviceName == 'nlp' ]; then
                serviceDir='nlp'

            elif [ $serviceName == 'model' ]; then
                serviceDir='model'

            elif [ $serviceName == 'data' ]; then
                serviceDir='data'

            elif [ $serviceName == 'web-tag' ]; then
                serviceDir='web-tag'

            elif [ $serviceName == 'web-event' ]; then
                serviceDir='web-event'

            elif [ $serviceName == 'test' ]; then
                serviceDir='test'
                service="name.jar"
                rootDir="root/zsw/"

            else
                echo "input services wrongful !!"
                exit 1
            fi
            echo ">>> Begin to send file [ $service ]..."
            # 更名
            #判断文件是否存在
            fileName=${serviceDir}/${service}
            if [ -f "$fileName" ]; then
                CREATE_DAY_STAMP=`stat -c %Y $fileName`;
                CREATE_DAY=`date '+%m%d' -d @$CREATE_DAY_STAMP`;
                newFileName="${fileName}.${CREATE_DAY}"
                #判断新文件是否已经存在
                i=1
                while [ -f "$newFileName" ]; 
                do
                    number=`printf "%02d\n" $i`
                    newFileName="${newFileName}.${number}"
                    i=`expr $i + 1`
                done
                echo "new serviceName is $newFileName"
                mv $fileName $newFileName
            else
                echo "[error]: file is no found .."
                exit 1
            fi    
        done
    else
        echo "please input file name:"
    fi
    # rm $0

 

send.sh

 

#!/usr/bin/env bash

set -e

time=`date "+%Y-%m-%d %H:%M:%S "`

#发送命令
#params : 用户名 ip 密码 目标机根目录 指令 (指令以分号结尾)
sendUpdateCommand(){
    if [ $# -eq 5 ]; then
        echo "->>>> Begin to update jar $5 \'s name "
        USER=$1
        PASSWORD=$3
        IP=$2
        # command=$4
        # 发送命令
        /usr/bin/expect<<-EOF
        set timeout 20
        spawn ssh -p 22 $USER@$IP
        expect "*password:"
        send "$PASSWORD\r"
        expect "]*"
        send "cd /$4 \r"
        expect "]*"
        send "sudo chmod -R 7777 ./ \r"
        expect "]*"
        send "./updateFile.sh $5 \r"
        expect "]*"
        # send "exit"
        exit
        interact
        expect eof
        EOF
        echo "->>>> [SUCCESS] Update jar $5 's name already completed "
    else
        echo '->>>> [ERROR] sendUpdateCommand : Illegal parameters '
    fi
}

# param 重启 
sendRestart(){
    if [ $# -ge 5 ]; then
        echo "->>>> Begin to restart service $5 ...... "
        USER=$1
        PASSWORD=$3
        IP=$2
        # 发送命令
        /usr/bin/expect<<-EOF
        set timeout 20
        spawn ssh -p 22 $USER@$IP
        expect "*password:"
        send "$PASSWORD\r"
        expect "]*"
        send "cd  /$4\r"
        # expect "]*"
        # send "sudo chmod -R 7777 ./ \r"
        expect "]*"
        send "./restart_all.sh $5\r"
        expect "]*"
        # send "exit"
        exit
        interact
        expect eof
        EOF
        echo "->>>> [SUCCESS] Restart service $5 already completed !"
    else
        echo '->>>> [ERROR] sendRestart: Illegal parameters '
    fi
}

# 发送文件
# ex: user ip password targetDir localFile
sendFile() {
    if [ $# -ge 5 ]; then
        USER=$1
        IP=$2
        PASSWORD=$3
        localFile=$4
        targetPath=$5
        echo "->>>> Begin to send file [ $localFile ]..."

        /usr/bin/expect<<-EOF
        set timeout 20
        spawn scp -P 22 $localFile $USER@$IP:/$targetPath
        expect "*password:"
        send "$PASSWORD\r"
        expect "]*"
        # send "exit"
        exit
        interact
        expect eof
        EOF
        echo "->>>> [SUCCESS] Send file [ $localFile ] already completed !"
    else
        echo "->>>> [ERROR] ssendFile : Illegal parameters "
    fi
}

#传输文件,传输所有的包
#params : 用户名 ip 密码 目标机根目录 服务列表..
sendJar() {
    if [ $# -ge 5 ]; then
        USER=$1
        PASSWORD=$3
        IP="$2"
        targetDir=$4
        serviceArr=()
        echo "->>>> Begin to update server [${IP:10:13}] ..."
        
        i=1
        for v in $*
        do
            if [ $i -ge 5 ]; then
                serviceArr[$i]=$v
            fi
            i=`expr $i + 1`;
        done

        sendFile $USER $IP $PASSWORD "updateFile.sh" "$targetDir" 
        # sendCommand $USER $IP $PASSWORD \"cd $targetDir; sudo chmod -R 777 ./ ; ./updateFile.sh ${serviceArr[*]} \"
        
        for serviceName in ${serviceArr[*]}
        do
            if [ $serviceName == 'article' ]; then
                serviceDir='article'                    #服务目录名
                service='gorich-article-service-0.0.1-SNAPSHOT.jar'    #服务jar包

            elif [ $serviceName == 'block' ]; then
                serviceDir='block'
                service='gorich-block-service-0.0.1-SNAPSHOT.jar'

            elif [ $serviceName == 'nlp' ]; then
                serviceDir='nlp'
                service='gorich-nlp-service-0.0.1-SNAPSHOT.jar'

            elif [ $serviceName == 'model' ]; then
                serviceDir='model'
                service='gorich-model-service-0.0.1-SNAPSHOT.jar'

            elif [ $serviceName == 'data' ]; then
                serviceDir='data'
                service='gorich-data-service-0.0.1-SNAPSHOT.jar'

            elif [ $serviceName == 'web-tag' ]; then
                serviceDir='news-insight-tag'
                service='news-insight-web.jar'

            elif [ $serviceName == 'web-event' ]; then
                serviceDir='news-insight-event'
                service='news-insight-event.jar'

            else
                echo "->>>> [ERRPR] sendJar : input services wrongful !!"
            fi
            #更名
            echo "----------------------------------------------UPDATe JAR [$service]"
            sendUpdateCommand $USER $IP $PASSWORD $targetDir $serviceName/$service 
            # 传输文件
            echo "----------------------------------------------SEND JAR[$service]"
            sendFile $USER $IP $PASSWORD "$serviceDir/$service" "$targetDir/$serviceDir"
            #重启
            # sendCommand $USER $IP $PASSWORD "restart_all.sh $serviceName"
            echo "----------------------------------------------RESTART JAR[$service]"
            sendRestart  $USER $IP $PASSWORD $targetDir $serviceName
        done
        echo "->>>> [SUCCESS] Update server [${IP:10:13}] already completed !"
    else
        echo "->>>> [ERROR] sendJar : Illegal parameters "
    fi
}

printHelp() {
    echo "请以如下格式输入参数: [要更新的机器] [要更新的服务列表] "
    echo "example: $0 109 block article nlp"
    # echo -e "\tserver: 109 "
    echo -e "\tserver: local "
    echo -e "\tserver: 113 "
    echo -e "\tserver: 115 "
    echo -e "\tserver: 163 "
    echo -e "\tserver: all "
}

server=$1
services=$2


    if [[ ! $server || ! $services ]]; then
            printHelp
    
    else
        serviceArr=()
        i=1
        for service in $*
        do
            if [ $i != 1 ]; then
                serviceArr[$i]=$service
            fi
            i=`expr $i + 1`;
        done
        
        #send jar
        if [ $server == 'localhost' ]; then
            ./restart_all.sh ${serviceArr[*]}

        elif [ $server == '115' ]; then
            sendJar root 11.114.11.111 password root/zsw/test ${serviceArr[*]}
            
        elif [ $server == '113' ]; then
            sendJar root 11.114.11.111 password root/zsw/test ${serviceArr[*]}

        elif [ $server == '163' ]; then
            sendJar root 11.114.11.111 password root/zsw/test ${serviceArr[*]}
                
        elif [ $server == 'all' ]; then
            echo "all"

        else
                printHelp
        fi
    fi    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值