标签:sha shanghai time slb test server 权重 sage core
#CentOS7 源码编译安装Python3.8
安装依赖:
yum install gcc openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel libffi-devel tk-devel wget curl-devel
下载:
wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
解压:
tar -xzvf Python-3.8.0.tgz
编译安装:
mkdir /usr/local/python3
/root/Python-3.8.0
./configure --prefix=/usr/local/python3/
make && make install
建立软链接:
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
#!/usr/bin/env python
#-*-coding:utf-8 -*-
#@Time : 2020/06/16
#@Author : Joey
import sys
import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkslb.request.v20140515 import DescribeLoadBalancersRequest
from aliyunsdkslb.request.v20140515 import DescribeLoadBalancerAttributeRequest
from aliyunsdkslb.request.v20140515 import SetBackendServersRequest
from aliyunsdkslb.request.v20140515 import AddBackendServersRequest
from aliyunsdkslb.request.v20140515 import RemoveBackendServersRequest
#Access Key 信息
AccessKey = ‘ak‘
AccessKeySecret = ‘ak123‘
RegionId = ‘cn-shanghai‘
client = AcsClient(AccessKey, AccessKeySecret, RegionId)
Info = ‘‘‘
Basic Commands :
get 获取SLB信息
edit 配置SLB的后端权重
add 添加SLB的后端实例
remove 删除SLB的后端实例
Usage:
获取所有实例的信息
python slb.py get all
获取某一实例的信息
python slb.py get lb-uf6rb7g4a3op3f1qi19yx
编辑单个权重
python slb.py edit lb-uf6rb7g4a3op3f1qi19yx [{\"ServerId\":\"i-ecs\",\"Weight\":\"95\",\"ServerIp\":\"10.10.10.10\",\"Type\":\"ecs\"}]
编辑多个权重(最多20个)
python slb.py edit lb-uf6rb7g4a3op3f1qi19yx [{"ServerId":"i-ecs1","Weight":"100"},{"ServerId":"i-ecs2","Weight":"100"}]
添加单个后端ecs
python slb.py add lb-uf6rb7g4a3op3f1qi19yx [{"ServerId":"i-ecs01","Weight":"100"}]
添加多个后端ecs
python slb.py add lb-uf6rb7g4a3op3f1qi19yx [{"ServerId":"i-ecs01","Weight":"100"},{"ServerId":"i-ecs02","Weight":"100"}]
删除单个后端ecs
python slb.py remove lb-uf6rb7g4a3op3f1qi19yx [{"ServerId":"i-ecs01","Weight":"100"}]
删除多个后端ecs
python slb.py remove lb-uf6rb7g4a3op3f1qi19yx [{"ServerId":"i-ecs01","Weight":"100"},{"ServerId":"i-ecs02","Weight":"100"}]
‘‘‘
def InfoSLB():
print(Info)
def DescribeLoadBalancers(resource):
request = DescribeLoadBalancersRequest.DescribeLoadBalancersRequest() #SLB实例列表
response = client.do_action_with_exception(request) #发起请求
SLBInfo = json.loads(response) #处理 response Json字符串对象
LoadBalancerIdList = []
for SLBInstance in SLBInfo[‘LoadBalancers‘][‘LoadBalancer‘]: #LoadBalancers 返回负载均衡实例列表, LoadBalancer 负载均衡实例
LoadBalancerIdList.append(SLBInstance[‘LoadBalancerId‘]) #通过 LoadBalancerId 得到 SLB实例
print("SLBInstance实例详细信息: %s " % SLBInstance)
if resource == ‘all‘:
Ali_Slb_Info = {}
for SLBInstance in SLBInfo[‘LoadBalancers‘][‘LoadBalancer‘]: #SLB 实例
request = DescribeLoadBalancerAttributeRequest.DescribeLoadBalancerAttributeRequest() #SLB实例详细信息
request.set_LoadBalancerId(SLBInstance[‘LoadBalancerId‘])
#request.get_LoadBalancerId()
response = client.do_action_with_exception(request)
Ali_Slb_Info[SLBInstance[‘LoadBalancerId‘]] = json.loads(response.decode(‘utf-8‘)) #Json字符串 反序列化成 python对象, SLB实例列表 存储到set
print(LoadBalancerIdList)
# print(Ali_Slb_Info[SLBInstance[‘LoadBalancerId‘]])
elif resource in LoadBalancerIdList:
request = DescribeLoadBalancerAttributeRequest.DescribeLoadBalancerAttributeRequest()
request.set_LoadBalancerId(resource) #某一SLB实例详细信息
response = client.do_action_with_exception(request)
response = json.loads(response.decode(‘utf-8‘))
print("SLB后端ECS服务器列表: %s " % response[‘BackendServers‘]) #SLB后端服务器列表
else:
print("输入错误,请输入 all 或 SLB实例ID !")
#设置SLB后端服务器权重等信息
def SetBackendServers(resource,BackendServers):
request = SetBackendServersRequest.SetBackendServersRequest() #调用SetBackendServers设置后端服务器权重、Port、ECS等信息
request.set_accept_format(‘json‘)
request.set_BackendServers(BackendServers)
request.set_LoadBalancerId(resource)
print("后端ECS信息: %s" % BackendServers)
response = client.do_action_with_exception(request)
print("SLB返回结果为: %s" % str(response, encoding=‘utf-8‘))
#增加SLB后端实例
def AddBackendServers(resource,BackendServers):
request = AddBackendServersRequest.AddBackendServersRequest() #调用 AddBackendServersRequest 调整增加后端实例
request.set_accept_format(‘json‘)
request.set_BackendServers(BackendServers)
request.set_LoadBalancerId(resource)
response = client.do_action_with_exception(request)
print(str(response, encoding=‘utf-8‘))
#移除SLB后端实例
def RemoveBackendServers(resource,BackendServers):
request = RemoveBackendServersRequest()
request.set_accept_format(‘json‘)
request.set_BackendServers(BackendServers)
request.set_LoadBalancerId(resource)
response = client.do_action_with_exception(request)
print(str(response, encoding=‘utf-8‘))
if __name__ == ‘__main__‘:
if len(sys.argv) == 1:
main()
else:
userInput = sys.argv[1:]
if userInput[0] == ‘get‘ and userInput[1]: #userInput[1] LoadBalancerId or all
resource = userInput[1]
print("SLB实例ID: %s " % resource)
DescribeLoadBalancers(resource)
elif userInput[0] == ‘edit‘ and userInput[1] and userInput[2]: #userInput[1] LoadBalancerId; userInput[2] BackendServers
resource = userInput[1]
BackendServers = userInput[2]
print("编辑的SLB实例ID: %s " % resource)
print("编辑后端ECS,传入的参数信息: %s " % BackendServers)
SetBackendServers(resource, BackendServers)
elif userInput[0] == ‘add‘ and userInput[1] and userInput[2]:
resource = userInput[1]
BackendServers = userInput[2]
AddBackendServers(resource, BackendServers)
elif userInput[0] == ‘remove‘ and userInput[1] and userInput[2]:
resource = userInput[1]
BackendServers = userInput[2]
RemoveBackendServers(resource, BackendServers)
else:
print("错误的输入!")
Jenkins+Python调用阿里云API实现发布自动修改SLB权重
标签:sha shanghai time slb test server 权重 sage core
原文地址:https://blog.51cto.com/10874766/2505716