Shell Programming-project Deployment (optimization)

Source: Internet
Author: User

In the actual work of small and medium-sized to encounter a problem that is when I go to operate the deployment script, another person can also operate, this how can do, and then the small part of the thought to re-optimize the code, add a process lock

The usual, first comb the idea:

At the same time, the script can only allow one person to operate, that is to say

As long as the script executes to create a file, as long as the file exists, then let the script exit, and prompt "script is really running", the script is completed, delete the created file

The code is as follows

  

  
 
  1.  #!/bin/bash
  2. # 功能:打包代码
  3. # 名称:deploy.sh
  4. # 灰度
  5. # 版本:0.4
  6. # 联系方式:xxxxx
  7. # 日志功能
  8. write_log(){
  9. LOG_DATE=`date +%F`
  10. LOG_TIME=`date +%T`
  11. buzhou="$1"
  12. echo " ${LOG_DATE}  ${LOG_TIME}$0${buzhou}" >> /data/logs/deploy.log
  13. }
  14. # 增加锁文件
  15. add_lock(){
  16. echo "增加锁文件"
  17. touch /tmp/deploy.pid
  18. write_log "增加锁文件"
  19. }
  20. # 删除锁文件
  21. del_lock(){
  22. echo "删除锁文件"
  23. rm -f /tmp/deploy.pid
  24. write_log "删除锁文件"
  25. }
  26. # 打包代码
  27. tar_code(){
  28. echo "打包代码"
  29. ssh [email protected].168.56.12 "/bin/bash /data/scripts/code_tar.sh"
  30. write_log "打包代码"
  31. }
  32. # 传输代码
  33. scp_code(){
  34. echo "传输代码"
  35. cd /data/codes/
  36. [ -f django.tar.gz ] && rm -f django.tar.gz
  37. [ -d django ] && rm -rf django
  38. scp [email protected].168.56.12:/data/code/django.tar.gz ./
  39. write_log "传输代码"
  40. }
  41. # 关闭应用
  42. serv_stop(){
  43. echo "关闭nginx应用"
  44. /data/server/nginx/sbin/nginx -s stop
  45. write_log "关闭nginx应用"
  46. echo "关闭django应用"
  47. django_pid=$(lsof -Pti :8000)
  48. kill "${django_pid}"
  49. write_log "关闭django应用"
  50. }
  51. # 解压代码
  52. untar_code(){
  53. echo "解压代码"
  54. cd /data/codes/
  55. tar xf django.tar.gz
  56. write_log "解压代码"
  57. }
  58. # 放置代码
  59. fangzhi_code(){
  60. echo "备份原文件"
  61. B_TIME=$(date +%Y%m%d%H%M%S)
  62. mv /data/server/itcast/test1/views.py /data/backup/views.py-"${B_TIME}"
  63. write_log "备份原文件"
  64. echo "放置新文件"
  65. mv /data/codes/django/views.py  /data/server/itcast/test1/
  66. write_log "放置新文件"
  67. }
  68. # 开启应用
  69. serv_star(){
  70. echo "开启django应用"
  71. source /data/virtual/venv/bin/activate
  72. cd /data/server/itcast/
  73. python manage.py runserver >>/dev/null 2>&1 &
  74. deactivate
  75. write_log "开启django应用"
  76. echo "开启nginx应用"
  77. /data/server/nginx/sbin/nginx
  78. write_log "开启nginx应用"
  79. }
  80. # 检查
  81. check(){
  82. echo "检查部署效果"
  83. netstat -tnulp | grep ‘:80‘
  84. write_log "检查部署效果"
  85. }
  86. main(){
  87. if [ -f /tmp/deploy.pid ]
  88. then
  89. echo "$0 脚本正在执行中..."
  90. exit
  91. else
  92. add_lock
  93. tar_code
  94. scp_code
  95. serv_stop
  96. untar_code
  97. fangzhi_code
  98. serv_star
  99. check
  100. del_lock
  101. fi
  102. }
  103. main


Shell Programming-project Deployment (optimization)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.