Jenkins控制台脚本
#!/bin/bash
#author:zhangyingguang
#time:2020-03
#项目上线 负载均衡在思图智能上,所有负载均衡脚本不同 !!!
source /etc/profile
# projectName 是code目录名字,自定义
projectName=vwdata-web
#packageName 是打包后的名字,自定义
packageName=trutheye-fe
#远程服务器的名字,自定义
remoteServerName=vwdata_01
#负载均衡配置,自定义
LoadBalancerId=lb-uf6vdl3jk
serverId_01=i-uf66fpq1
serverId_02=i-uf66k8r0
status=$Status
branch=$BRANCH
codeDir=/root/code/$projectName
backDir=$codeDir/back_up
#远程服务器项目目录
remoteProjectDir=/usr/local/seektruth/web-static/vwdata-static
echo $status $branch $projectName $codeDir $backDir
#切换负载均衡
SlbOverturn()
{
if [ $# -eq 3 ]
then
echo "------------ 切换负载均衡为0 ---------------"
result=$(python /root/code/aliyun-slb-zhineng.py $1 $2 $3)
if [ $result = "success" ]
then
echo " -------------------- 切换负载均衡 $2 的权重为 $3 成功 ! -------------------"
else
echo $result
exit 88
fi
else
echo "not enough parameters"
exit 88
fi
}
BUILD_CODE(){
cd $codeDir
cnpm install && npm run build:prod
#mvn clean install -Dmaven.test.skip=true
[ $? -ne 0 ] && echo -e '\033[31m[ error ] Failed to build the code\033[0m' && exit 1
echo "---------------- BUILD_CODE success! ---------"
}
BackupAndPackage(){
cd $codeDir
[ ! -d $backDir ] && mkdir -p $backDir
#备份
[ -f $backDir/${packageName}.gz ] && cp -f $backDir/${packageName}.gz $backDir/${packageName}_last.gz
#打包
tar -zcf $backDir/${packageName}.gz ${packageName}/
echo "------------------- backup and package done ----------------------"
}
# Ansible推送脚本
TransforAndDeploy(){
ansible $remoteServerName -m ping
ansible-playbook /root/code/deploy-web.yml -e host=$remoteServerName -e backDir=${backDir} -e packageName=${packageName} -e webDir=${remoteProjectDir}
[ $? -ne 0 ] && echo -e '\033[31m[ error ] Failed to ansible deploy the code\033[0m' && exit 88
echo "------------------------------------------ Transfor And Deploy done ----------------------------------------------------------"
}
deploy()
{
#firstly 切换负载均衡为 0,并睡5分钟
SlbOverturn $LoadBalancerId $serverId_01 0 && sleep 300
#secondly 上线
echo "------------------------------------------------- 正在上线 -------------------------------------------------------------------"
cd $codeDir
echo "---------------- BUILD_CODE ---------"
BUILD_CODE
echo "------------- backup and package-------------------------------"
BackupAndPackage
echo "------------- transfor And Deploy ----------------------------------- "
TransforAndDeploy
sleep 20
#finally 切换负载均衡为 已上线的改为100,未上线的改为0
SlbOverturn $LoadBalancerId $serverId_01 100 && sleep 30
#SlbOverturn $LoadBalancerId $serverId_02 0 && sleep 5
echo "------------------------- 上线成功 ----------------------------------------------"
}
rollback(){
#firstly 切换负载均衡为 0,并10秒钟
SlbOverturn $LoadBalancerId $serverId_01 0 && sleep 300
echo "--------------------------------------------- 正在回滚 ------------------------------------------------------------- "
[ ! -d $backDir ] && mkdir -p $backDir
cp -f $backDir/${packageName}_last.gz $backDir/${packageName}.gz
echo "------------- transfor And Deploy ----------------------------------- "
TransforAndDeploy
#finally 切换负载均衡为 100
SlbOverturn $LoadBalancerId $serverId_01 100 && sleep 10
echo "------------------------- 回滚成功 ----------------------------------------------"
}
case "$status" in
'Deploy')
deploy
;;
'RollBACK')
rollback
;;
*)
echo "Usage: $0 {Deploy | RollBACK}"
exit 1
esac
exit 0
deploy-web.yml(代码推送)
- hosts: "{{ host }}"
remote_user: root
tasks:
- name: rm project
file: path={{ webDir }}/{{ packageName }} state=absent
- name: pause
pause: seconds=20
- name: upload and untar gz
unarchive: src={{ backDir}}/{{ packageName }}.gz dest={{ webDir }}/ remote_src=no
notify:
- nginx reload
handlers:
- name: nginx stop
service: name=nginx state=stopped
- name: nginx start
service: name=nginx state=started
- name: nginx reload
systemd: name=nginx state=reloaded
- name: pause
pause: seconds=20
aliyun-slb-zhineng.py(作用就是切负载均衡)
#!/root/anaconda3/bin/python
# -*- coding: UTF-8 -*-
# pip install aliyun-python-sdk-core-v3,aliyun-python-sdk-slb
import sys
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkslb.request.v20140515.SetBackendServersRequest import SetBackendServersRequest
#获取参数变量
if len(sys.argv) == 4:
LoadBalancerId = sys.argv[1]
ServerId = sys.argv[2]
Weight = sys.argv[3]
try:
Weight = int(Weight)
client = AcsClient('LTAIrsTqz', 'Xay7zigjMNbHc', 'cn-beijing')
request = SetBackendServersRequest()
request.set_accept_format('json')
request.set_BackendServers([{"ServerId": ServerId, "Weight": Weight}])
request.set_LoadBalancerId(LoadBalancerId)
response = client.do_action_with_exception(request)
print("success")
except Exception as e:
print(e)
else:
print("not enough parameters")