image.pngimage.pngimage.png
    image.png
    image.png
    Jenkins控制台脚本

    1. #!/bin/bash
    2. #author:zhangyingguang
    3. #time:2020-03
    4. #项目上线 负载均衡在思图智能上,所有负载均衡脚本不同 !!!
    5. source /etc/profile
    6. # projectName 是code目录名字,自定义
    7. projectName=vwdata-web
    8. #packageName 是打包后的名字,自定义
    9. packageName=trutheye-fe
    10. #远程服务器的名字,自定义
    11. remoteServerName=vwdata_01
    12. #负载均衡配置,自定义
    13. LoadBalancerId=lb-uf6vdl3jk
    14. serverId_01=i-uf66fpq1
    15. serverId_02=i-uf66k8r0
    16. status=$Status
    17. branch=$BRANCH
    18. codeDir=/root/code/$projectName
    19. backDir=$codeDir/back_up
    20. #远程服务器项目目录
    21. remoteProjectDir=/usr/local/seektruth/web-static/vwdata-static
    22. echo $status $branch $projectName $codeDir $backDir
    23. #切换负载均衡
    24. SlbOverturn()
    25. {
    26. if [ $# -eq 3 ]
    27. then
    28. echo "------------ 切换负载均衡为0 ---------------"
    29. result=$(python /root/code/aliyun-slb-zhineng.py $1 $2 $3)
    30. if [ $result = "success" ]
    31. then
    32. echo " -------------------- 切换负载均衡 $2 的权重为 $3 成功 ! -------------------"
    33. else
    34. echo $result
    35. exit 88
    36. fi
    37. else
    38. echo "not enough parameters"
    39. exit 88
    40. fi
    41. }
    42. BUILD_CODE(){
    43. cd $codeDir
    44. cnpm install && npm run build:prod
    45. #mvn clean install -Dmaven.test.skip=true
    46. [ $? -ne 0 ] && echo -e '\033[31m[ error ] Failed to build the code\033[0m' && exit 1
    47. echo "---------------- BUILD_CODE success! ---------"
    48. }
    49. BackupAndPackage(){
    50. cd $codeDir
    51. [ ! -d $backDir ] && mkdir -p $backDir
    52. #备份
    53. [ -f $backDir/${packageName}.gz ] && cp -f $backDir/${packageName}.gz $backDir/${packageName}_last.gz
    54. #打包
    55. tar -zcf $backDir/${packageName}.gz ${packageName}/
    56. echo "------------------- backup and package done ----------------------"
    57. }
    58. # Ansible推送脚本
    59. TransforAndDeploy(){
    60. ansible $remoteServerName -m ping
    61. ansible-playbook /root/code/deploy-web.yml -e host=$remoteServerName -e backDir=${backDir} -e packageName=${packageName} -e webDir=${remoteProjectDir}
    62. [ $? -ne 0 ] && echo -e '\033[31m[ error ] Failed to ansible deploy the code\033[0m' && exit 88
    63. echo "------------------------------------------ Transfor And Deploy done ----------------------------------------------------------"
    64. }
    65. deploy()
    66. {
    67. #firstly 切换负载均衡为 0,并睡5分钟
    68. SlbOverturn $LoadBalancerId $serverId_01 0 && sleep 300
    69. #secondly 上线
    70. echo "------------------------------------------------- 正在上线 -------------------------------------------------------------------"
    71. cd $codeDir
    72. echo "---------------- BUILD_CODE ---------"
    73. BUILD_CODE
    74. echo "------------- backup and package-------------------------------"
    75. BackupAndPackage
    76. echo "------------- transfor And Deploy ----------------------------------- "
    77. TransforAndDeploy
    78. sleep 20
    79. #finally 切换负载均衡为 已上线的改为100,未上线的改为0
    80. SlbOverturn $LoadBalancerId $serverId_01 100 && sleep 30
    81. #SlbOverturn $LoadBalancerId $serverId_02 0 && sleep 5
    82. echo "------------------------- 上线成功 ----------------------------------------------"
    83. }
    84. rollback(){
    85. #firstly 切换负载均衡为 0,并10秒钟
    86. SlbOverturn $LoadBalancerId $serverId_01 0 && sleep 300
    87. echo "--------------------------------------------- 正在回滚 ------------------------------------------------------------- "
    88. [ ! -d $backDir ] && mkdir -p $backDir
    89. cp -f $backDir/${packageName}_last.gz $backDir/${packageName}.gz
    90. echo "------------- transfor And Deploy ----------------------------------- "
    91. TransforAndDeploy
    92. #finally 切换负载均衡为 100
    93. SlbOverturn $LoadBalancerId $serverId_01 100 && sleep 10
    94. echo "------------------------- 回滚成功 ----------------------------------------------"
    95. }
    96. case "$status" in
    97. 'Deploy')
    98. deploy
    99. ;;
    100. 'RollBACK')
    101. rollback
    102. ;;
    103. *)
    104. echo "Usage: $0 {Deploy | RollBACK}"
    105. exit 1
    106. esac
    107. exit 0

    deploy-web.yml(代码推送)

    1. - hosts: "{{ host }}"
    2. remote_user: root
    3. tasks:
    4. - name: rm project
    5. file: path={{ webDir }}/{{ packageName }} state=absent
    6. - name: pause
    7. pause: seconds=20
    8. - name: upload and untar gz
    9. unarchive: src={{ backDir}}/{{ packageName }}.gz dest={{ webDir }}/ remote_src=no
    10. notify:
    11. - nginx reload
    12. handlers:
    13. - name: nginx stop
    14. service: name=nginx state=stopped
    15. - name: nginx start
    16. service: name=nginx state=started
    17. - name: nginx reload
    18. systemd: name=nginx state=reloaded
    19. - name: pause
    20. pause: seconds=20

    aliyun-slb-zhineng.py(作用就是切负载均衡)

    1. #!/root/anaconda3/bin/python
    2. # -*- coding: UTF-8 -*-
    3. # pip install aliyun-python-sdk-core-v3,aliyun-python-sdk-slb
    4. import sys
    5. from aliyunsdkcore.client import AcsClient
    6. from aliyunsdkcore.acs_exception.exceptions import ClientException
    7. from aliyunsdkcore.acs_exception.exceptions import ServerException
    8. from aliyunsdkslb.request.v20140515.SetBackendServersRequest import SetBackendServersRequest
    9. #获取参数变量
    10. if len(sys.argv) == 4:
    11. LoadBalancerId = sys.argv[1]
    12. ServerId = sys.argv[2]
    13. Weight = sys.argv[3]
    14. try:
    15. Weight = int(Weight)
    16. client = AcsClient('LTAIrsTqz', 'Xay7zigjMNbHc', 'cn-beijing')
    17. request = SetBackendServersRequest()
    18. request.set_accept_format('json')
    19. request.set_BackendServers([{"ServerId": ServerId, "Weight": Weight}])
    20. request.set_LoadBalancerId(LoadBalancerId)
    21. response = client.do_action_with_exception(request)
    22. print("success")
    23. except Exception as e:
    24. print(e)
    25. else:
    26. print("not enough parameters")