一個簡單的連續調用腳本部署代碼的案例

先簡單描述一下環境,Jenkins部署在私有云,從git拉取代碼並編譯後會調用腳本將jar包部署到阿里雲的應用服務器。
阿里雲應用服務器無公網IP,經過跳板機登陸。就產生了連續調用腳本,穿透跳板機部署到阿里雲內網的需求。java

服務器信息:
主機名稱
IP
git server 10.150.27.51
jenkins server 10.150.27.52
jump server 1.1.1.1/192.168.2.20
app server1 192.168.2.21
app server1 192.168.2.22

拓撲圖以下:
Markdownnode

1.部署腳本01-backend-earphone-8888.sh (Jenkins server)

主要實現功能:git

  • 將跳板機1.1.1.1上面臨時目錄/data/deploy/pccode/earphone內容狀況
  • 拷貝Jenkins編譯生成的jar包文件(earphone-1.08888.jar)至跳板機/data/deploy/pccode/earphone目錄
  • 調用跳板機腳本進行部署/data/deploy/script/earphone.sh

[root@bobo365 prod]# more backend-earphone-8888.sh shell

#!/bin/bash

. /etc/init.d/functions

node="1.1.1.1"

current="/home/bobo365/scripts/prod"

dest_user='remote'
dest_passwd='xxxxxxx'
package_name="earphone-1.08888.jar"
source_dir="${JENKINS_HOME}/jobs/${PROMOTED_JOB_NAME}/builds/${PROMOTED_NUMBER}/archive"
source_file="${PROMOTED_JOB_NAME}/builds/${PROMOTED_NUMBER}/archive/$package_name"
dest_temp="/data/deploy/pccode/earphone"
back_dir="/data/backup"
shell_dir="/data/deploy/script"
shell="earphone.sh"
back_prog="backend-earphone"
back_time=`date +"%Y-%m-%d-%s"`

NOW=`date +"%Y/%m/%d %T"`

echo "Clean the dir."
sshpass -p $dest_passwd ssh $dest_user@$node -p 22 -o StrictHostKeychecking=no "rm -rf '$dest_temp'/*" 

    if [ $? = 0 ]
    then
        echo $node $NOW clean the dir success | tee -a $current/logs/backend-earphone.log
    else
        echo $node $NOW clean the dir failed | tee -a $current/logs/backend-earphone.log
        echo  >> $current/backend-earphone.log
        exit 1
    fi

echo "Scp the file."
sshpass -p $dest_passwd scp -P22 -o StrictHostKeyChecking=no $source_dir/$package_name $dest_user@$node:$dest_temp/$package_name 

    if [ $? = 0 ]
    then
        echo $node $NOW scp code success | tee -a $current/logs/backend-earphone.log
    else
        echo $node $NOW scp code failed | tee -a $current/logs/backend-earphone.log
        echo  >> $current/backend-earphone.log
        exit 1
    fi

echo "Deploy the file."
sshpass -p $dest_passwd ssh $dest_user@$node -p 22 -o StrictHostKeychecking=no "/bin/bash $shell_dir/$shell" 

    if [ $? = 0 ]
    then
        echo $node $NOW deploy code success | tee -a $current/logs/backend-earphone.log
    else
        echo $node $NOW deploy code failed | tee -a $current/logs/backend-earphone.log
        echo  >> $current/backend-earphone.log
        exit 1
    fi

NOW=`date +"%Y/%m/%d %T"`
echo -e "$NOW: $prog complete of deploy ."

2.部署腳本02-earphone.sh (Jump server)

主要實現功能:bash

  • 拷貝文件至應用服務器(/data/deploy/pccode/earphone/earphone-1.08888.jar)
  • 調用應用服務器腳本(restart-headset.sh)進行代碼的替換
  • 使用ansible
    • more /etc/ansible/hosts
      [promote]
      192.168.2.21
      192.168.2.22

[root@JUMP script]# more earphone.sh 服務器

#!/bin/bash

current="/data/deploy/script"
source_dir="/data/deploy/pccode/earphone"
package_name="earphone-1.08888.jar"

ansible promote -m copy -a "src=$source_dir/$package_name dest=/root"

sleep 10

ansible promote -a "/bin/bash /root/restart-headset.sh"

3.部署腳本03-restart-headset.sh (application server)

主要實現功能:網絡

  • 刪掉現有earphone進程並從新啓動

[root@app ~]# more restart-headset.sh app

#!/bin/bash
package_name="earphone-1.08888.jar"
PID=`ps -ef|grep 8888|grep -v grep|awk -F " " '{print $2}'`
kill -9 $PID

nohup /usr/local/jdk1.8.0_60/bin/java -jar $package_name &

4.在本例實現過程當中,存在很大能夠優化的地方

  • 腳本過於簡單,不少地方缺乏判斷,健壯性有待提升
  • 部署腳本相互引用,過於繁瑣,若是出現問題,排錯有必定難度
  • 跨網絡部署是否還有更好的方式,有待學習
相關文章
相關標籤/搜索