一个简单的连续调用脚本部署代码的案例

前端之家收集整理的这篇文章主要介绍了一个简单的连续调用脚本部署代码的案例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

先简单描述一下环境,Jenkins部署在私有云,从git拉取代码并编译后会调用脚本将jar包部署到阿里云的应用服务器。
阿里云应用服务器无公网IP,通过跳板机登录。就产生了连续调用脚本,穿透跳板机部署到阿里云内网的需求。

服务器信息:
主机名称
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

拓扑图如下:

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

主要实现功能

  • 将跳板机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

#!/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)

主要实现功能

  • 拷贝文件至应用服务器(/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

#!/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.在本例实现过程中,存在很大可以优化的地方

  • 脚本过于简单,很多地方缺少判断,健壮性有待提高
  • 部署脚本相互引用,过于繁琐,如果出现问题,排错有一定难度
  • 跨网络部署是否还有更好的方式,有待学习
原文链接:https://www.f2er.com/bash/387772.html

猜你在找的Bash相关文章