当前位置:   article > 正文

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

、部署脚本调用脚本

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

服务器信息:
主机名称
IP
git server10.150.27.51
jenkins server10.150.27.52
jump server1.1.1.1/192.168.2.20
app server1192.168.2.21
app server1192.168.2.22

拓扑图如下:
Markdown

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

  1. #!/bin/bash
  2. . /etc/init.d/functions
  3. node="1.1.1.1"
  4. current="/home/bobo365/scripts/prod"
  5. dest_user='remote'
  6. dest_passwd='xxxxxxx'
  7. package_name="earphone-1.08888.jar"
  8. source_dir="${JENKINS_HOME}/jobs/${PROMOTED_JOB_NAME}/builds/${PROMOTED_NUMBER}/archive"
  9. source_file="${PROMOTED_JOB_NAME}/builds/${PROMOTED_NUMBER}/archive/$package_name"
  10. dest_temp="/data/deploy/pccode/earphone"
  11. back_dir="/data/backup"
  12. shell_dir="/data/deploy/script"
  13. shell="earphone.sh"
  14. back_prog="backend-earphone"
  15. back_time=`date +"%Y-%m-%d-%s"`
  16. NOW=`date +"%Y/%m/%d %T"`
  17. echo "Clean the dir."
  18. sshpass -p $dest_passwd ssh $dest_user@$node -p 22 -o StrictHostKeychecking=no "rm -rf '$dest_temp'/*"
  19. if [ $? = 0 ]
  20. then
  21. echo $node $NOW clean the dir success | tee -a $current/logs/backend-earphone.log
  22. else
  23. echo $node $NOW clean the dir failed | tee -a $current/logs/backend-earphone.log
  24. echo >> $current/backend-earphone.log
  25. exit 1
  26. fi
  27. echo "Scp the file."
  28. sshpass -p $dest_passwd scp -P22 -o StrictHostKeyChecking=no $source_dir/$package_name $dest_user@$node:$dest_temp/$package_name
  29. if [ $? = 0 ]
  30. then
  31. echo $node $NOW scp code success | tee -a $current/logs/backend-earphone.log
  32. else
  33. echo $node $NOW scp code failed | tee -a $current/logs/backend-earphone.log
  34. echo >> $current/backend-earphone.log
  35. exit 1
  36. fi
  37. echo "Deploy the file."
  38. sshpass -p $dest_passwd ssh $dest_user@$node -p 22 -o StrictHostKeychecking=no "/bin/bash $shell_dir/$shell"
  39. if [ $? = 0 ]
  40. then
  41. echo $node $NOW deploy code success | tee -a $current/logs/backend-earphone.log
  42. else
  43. echo $node $NOW deploy code failed | tee -a $current/logs/backend-earphone.log
  44. echo >> $current/backend-earphone.log
  45. exit 1
  46. fi
  47. NOW=`date +"%Y/%m/%d %T"`
  48. 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
      1. [promote]
      2. 192.168.2.21
      3. 192.168.2.22

[root@JUMP script]# more earphone.sh

  1. #!/bin/bash
  2. current="/data/deploy/script"
  3. source_dir="/data/deploy/pccode/earphone"
  4. package_name="earphone-1.08888.jar"
  5. ansible promote -m copy -a "src=$source_dir/$package_name dest=/root"
  6. sleep 10
  7. ansible promote -a "/bin/bash /root/restart-headset.sh"

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

主要实现功能:

  • 删掉现有earphone进程并重新启动

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

  1. #!/bin/bash
  2. package_name="earphone-1.08888.jar"
  3. PID=`ps -ef|grep 8888|grep -v grep|awk -F " " '{print $2}'`
  4. kill -9 $PID
  5. nohup /usr/local/jdk1.8.0_60/bin/java -jar $package_name &

4.在本例实现过程中,存在很大可以优化的地方

  • 脚本过于简单,很多地方缺少判断,健壮性有待提高
  • 部署脚本相互引用,过于繁琐,如果出现问题,排错有一定难度
  • 跨网络部署是否还有更好的方式,有待学习

转载于:https://blog.51cto.com/bobo365/2129809

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/483167
推荐阅读
相关标签
  

闽ICP备14008679号