#!/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("錯誤的輸入!")