Showing
3 changed files
with
144 additions
and
63 deletions
@@ -7,7 +7,7 @@ import base64 | @@ -7,7 +7,7 @@ import base64 | ||
7 | import hashlib | 7 | import hashlib |
8 | import time | 8 | import time |
9 | import requests | 9 | import requests |
10 | -import json | 10 | +import json |
11 | 11 | ||
12 | 12 | ||
13 | class QcloudApi: | 13 | class QcloudApi: |
@@ -70,11 +70,6 @@ class QcloudApi: | @@ -70,11 +70,6 @@ class QcloudApi: | ||
70 | hmac_sign = hmac.new(bytes(self.secretKey), bytes(str_full), hashlib.sha1).digest() | 70 | hmac_sign = hmac.new(bytes(self.secretKey), bytes(str_full), hashlib.sha1).digest() |
71 | return base64.b64encode(hmac_sign).decode() | 71 | return base64.b64encode(hmac_sign).decode() |
72 | 72 | ||
73 | -# for test | ||
74 | -if __name__ == "__main__": | ||
75 | - api = QcloudApi(secretId='AK**K', secretKey='AC**QaW') | ||
76 | - api.do_query(params={'Action': 'DescribeLoadBalancers'}, req_url=QcloudApi.URLS_lb) | ||
77 | - | ||
78 | 73 | ||
79 | 74 | ||
80 | 75 |
No preview for this file type
@@ -9,65 +9,151 @@ | @@ -9,65 +9,151 @@ | ||
9 | """ | 9 | """ |
10 | 10 | ||
11 | from qcloud.qcloud_api import QcloudApi | 11 | from qcloud.qcloud_api import QcloudApi |
12 | -import time, sys | ||
13 | - | ||
14 | -def modify_alb_weight(SecretId, SecretKey, lb_id, domain, ip_weight_dict): | ||
15 | - """ | ||
16 | - 修改应用型负载均衡器上服务器的权重 | ||
17 | - @:param lb_id: 应用型负载均衡器ID | ||
18 | - @:param domain: 应用型负载均衡器的domain | ||
19 | - @:param ip_weight_dict: 需要设置权重的dict, 格式为 {'ip': weight } | ||
20 | - @return list | ||
21 | - """ | ||
22 | - | ||
23 | - instance_weight = {} # instance_id --> weight | ||
24 | - instance_port = {} # instance_id --> port | ||
25 | - listener_ids = [] # all linsters | ||
26 | - | ||
27 | - api = QcloudApi(secretId=SecretId, secretKey=SecretKey) | ||
28 | - lb_info = api.do_query(params={'Action': 'DescribeForwardLBBackends', 'loadBalancerId': lb_id}, req_url=QcloudApi.URLS_lb) | ||
29 | - | ||
30 | - # listener : [http , https] | ||
31 | - for listener in lb_info['data']: | ||
32 | - for rule in listener['rules']: | ||
33 | - if rule['domain'] == domain: # domain match | ||
34 | - listener_ids.append(listener['listenerId']) | ||
35 | - for backend in rule['backends']: | ||
36 | - instance_port[backend['unInstanceId']] = backend['port'] | ||
37 | - if backend['lanIp'] in ip_weight_dict.keys(): | ||
38 | - # setup the instance_id --> weight | ||
39 | - instance_weight[backend['unInstanceId']] = ip_weight_dict[backend['lanIp']] | ||
40 | - else: | ||
41 | - instance_weight[backend['unInstanceId']] = 0 | ||
42 | - | ||
43 | - # 检查至少有一个weight | ||
44 | - normal = False | ||
45 | - for v in instance_weight.values(): | ||
46 | - if v != 0: | ||
47 | - normal = True | ||
48 | - | ||
49 | - if not normal: | ||
50 | - sys.exit(" instance weight: %s is all zero. please check again!" % instance_weight) | ||
51 | - | ||
52 | - # 修改七层负载均衡器的权重: https://cloud.tencent.com/document/product/214/8978 | ||
53 | - for lis_id in listener_ids: | ||
54 | - params = {'Action': 'ModifyForwardSeventhBackends', 'loadBalancerId': lb_id, 'listenerId': lis_id, 'domain': domain} | ||
55 | - i = 1 | ||
56 | - for ins_id, ins_weight in instance_weight.iteritems(): | ||
57 | - params['backends.%i.instanceId' % i] = ins_id | ||
58 | - params['backends.%i.weight' % i] = ins_weight | ||
59 | - params['backends.%i.port' % i] = instance_port[ins_id] | ||
60 | - i = i + 1 | ||
61 | - | ||
62 | - rsp = api.do_query(params, req_url=QcloudApi.URLS_lb) | ||
63 | - print(u"response code: %s . success change domain: %s for lis_id : %s. wait for 10s" % (rsp['code'], domain, lis_id)) | ||
64 | - time.sleep(10) | ||
65 | - | ||
66 | - | ||
67 | - | 12 | +import time, sys, os |
13 | +from ansible.parsing.dataloader import DataLoader | ||
14 | +from ansible.inventory.manager import InventoryManager | ||
68 | 15 | ||
69 | 16 | ||
17 | +""" | ||
18 | + | ||
19 | + 切换负载均衡器的流量 | ||
20 | + 依赖: inventory 文件 | ||
21 | + | ||
22 | +""" | ||
70 | 23 | ||
71 | 24 | ||
25 | +class LBSwitch: | ||
26 | + def __init__(self, secretId, secretKey, az1_lb_id, az2_lb_id, az3_lb_id): | ||
27 | + self.SecretId = secretId | ||
28 | + self.SecretKey = secretKey | ||
29 | + self.az1_lb_id = az1_lb_id | ||
30 | + self.az2_lb_id = az2_lb_id | ||
31 | + self.az3_lb_id = az3_lb_id | ||
32 | + | ||
33 | + def all_to_az1(self): | ||
34 | + """ | ||
35 | + 所有流量切换到az1 | ||
36 | + :return: | ||
37 | + """ | ||
38 | + az1_nginxs = self.get_inventory_ifo("az1", 'java-nginx') | ||
39 | + print "get all java nginx: %s for az: %s" % (az1_nginxs, "az1") | ||
40 | + | ||
41 | + ip_weight_dict = {} | ||
42 | + for az1_nginx in az1_nginxs: | ||
43 | + ip_weight_dict[az1_nginx] = 10 | ||
44 | + | ||
45 | + self.modify_alb_weight(lb_id=self.az2_lb_id, domain="*.yoho.cn", ip_weight_dict=ip_weight_dict) | ||
46 | + | ||
47 | + | ||
48 | + def all_to_az2(self): | ||
49 | + """ | ||
50 | + 所有流量切换到AZ2 | ||
51 | + :return: | ||
52 | + """ | ||
53 | + az2_nginx = self.get_inventory_ifo("az2", 'java-nginx') | ||
54 | + ip_weight_dict = {} | ||
55 | + for nginx in az2_nginx: | ||
56 | + ip_weight_dict[nginx] = 10 | ||
57 | + self.modify_alb_weight(lb_id=self.az3_lb_id, domain="*.yoho.cn", ip_weight_dict=ip_weight_dict) | ||
58 | + | ||
59 | + @staticmethod | ||
60 | + def get_inventory_ifo(az, group): | ||
61 | + """ | ||
62 | + get hosts info from ansible inventory file | ||
63 | + """ | ||
64 | + data_loader = DataLoader() | ||
65 | + inventory = InventoryManager(loader=data_loader, | ||
66 | + sources=[os.path.join("./inventories", az, 'hosts')]) | ||
67 | + return inventory.get_groups_dict()[group] | ||
68 | + | ||
69 | + def modify_clb_weight(self, lb_id, ip_weight_dict): | ||
70 | + """ | ||
71 | + 修改传统负载均衡器的权重 | ||
72 | + @:param lb_id 负载均衡器的ID | ||
73 | + @:param ip_weight_dict 需要设置权重的后端IP-权重字典, 格式为 {'ip': weight } | ||
74 | + :return: True: 修改成功 | ||
75 | + """ | ||
76 | + # 1.获取传统型负载均衡器的后端列表:https://cloud.tencent.com/document/api/214/1259 | ||
77 | + instance_weight = {} # instance_id --> weight | ||
78 | + | ||
79 | + api = QcloudApi(secretId=self.SecretId, secretKey=self.SecretKey) | ||
80 | + lb_info = api.do_query(params={'Action': 'DescribeLoadBalancerBackends', 'loadBalancerId': lb_id}, req_url=QcloudApi.URLS_lb) | ||
81 | + for backend in lb_info['backendSet']: | ||
82 | + if backend['lanIp'] in ip_weight_dict.keys(): | ||
83 | + # setup the instance_id --> weight | ||
84 | + instance_weight[backend['unInstanceId']] = ip_weight_dict[backend['lanIp']] | ||
85 | + else: | ||
86 | + instance_weight[backend['unInstanceId']] = 0 | ||
87 | + | ||
88 | + # 2.检查至少有一个weight | ||
89 | + if len(filter(lambda w: w != 0, instance_weight.values())) == 0: | ||
90 | + sys.exit(" instance weight: %s is all zero. please check again!" % instance_weight) | ||
91 | + | ||
92 | + # 3.修改权重: https://cloud.tencent.com/document/api/214/1264 | ||
93 | + modify_params = {'Action': 'ModifyLoadBalancerBackends', 'loadBalancerId': lb_id} | ||
94 | + i = 1 | ||
95 | + for ins_id, ins_weight in instance_weight.iteritems(): | ||
96 | + modify_params['backends.%i.instanceId' % i] = ins_id | ||
97 | + modify_params['backends.%i.weight' % i] = ins_weight | ||
98 | + i = i + 1 | ||
99 | + api = QcloudApi(secretId=self.SecretId, secretKey=self.SecretKey) | ||
100 | + lb_info = api.do_query(params=modify_params, req_url=QcloudApi.URLS_lb) | ||
101 | + return lb_info['code'] == 0 | ||
102 | + | ||
103 | + def modify_alb_weight(self, lb_id, domain, ip_weight_dict): | ||
104 | + """ | ||
105 | + 修改应用型负载均衡器上服务器的权重 | ||
106 | + @:param lb_id: 应用型负载均衡器ID | ||
107 | + @:param domain: 应用型负载均衡器的domain | ||
108 | + @:param ip_weight_dict: 需要设置权重的dict, 格式为 {'ip': weight } | ||
109 | + @return list | ||
110 | + """ | ||
111 | + | ||
112 | + instance_weight = {} # instance_id --> weight | ||
113 | + instance_port = {} # instance_id --> port | ||
114 | + listener_ids = [] # all linsters | ||
115 | + | ||
116 | + api = QcloudApi(secretId=self.SecretId, secretKey=self.SecretKey) | ||
117 | + lb_info = api.do_query(params={'Action': 'DescribeForwardLBBackends', 'loadBalancerId': lb_id}, req_url=QcloudApi.URLS_lb) | ||
118 | + | ||
119 | + # listener : [http , https] | ||
120 | + for listener in lb_info['data']: | ||
121 | + for rule in listener['rules']: | ||
122 | + if rule['domain'] == domain: # domain match | ||
123 | + listener_ids.append(listener['listenerId']) | ||
124 | + for backend in rule['backends']: | ||
125 | + instance_port[backend['unInstanceId']] = backend['port'] | ||
126 | + if backend['lanIp'] in ip_weight_dict.keys(): | ||
127 | + # setup the instance_id --> weight | ||
128 | + instance_weight[backend['unInstanceId']] = ip_weight_dict[backend['lanIp']] | ||
129 | + else: | ||
130 | + instance_weight[backend['unInstanceId']] = 0 | ||
131 | + | ||
132 | + # 检查至少有一个weight | ||
133 | + if len(filter(lambda w: w != 0, instance_weight.values())) == 0: | ||
134 | + sys.exit(" instance weight: %s is all zero. please check again!" % instance_weight) | ||
135 | + | ||
136 | + # 修改七层负载均衡器的权重: https://cloud.tencent.com/document/product/214/8978 | ||
137 | + return_code = [] | ||
138 | + for lis_id in listener_ids: | ||
139 | + params = {'Action': 'ModifyForwardSeventhBackends', 'loadBalancerId': lb_id, 'listenerId': lis_id, 'domain': domain} | ||
140 | + i = 1 | ||
141 | + for ins_id, ins_weight in instance_weight.iteritems(): | ||
142 | + params['backends.%i.instanceId' % i] = ins_id | ||
143 | + params['backends.%i.weight' % i] = ins_weight | ||
144 | + params['backends.%i.port' % i] = instance_port[ins_id] | ||
145 | + i = i + 1 | ||
146 | + | ||
147 | + rsp = api.do_query(params, req_url=QcloudApi.URLS_lb) | ||
148 | + print(u"response code: %s . success change domain: %s for lis_id : %s. wait for 10s" % (rsp['code'], domain, lis_id)) | ||
149 | + return_code.append(rsp['code']) | ||
150 | + time.sleep(10) | ||
151 | + | ||
152 | + | ||
153 | +lbswitch = LBSwitch(secretId="AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK", secretKey="ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW", | ||
154 | + az1_lb_id="lb-e14oxiq3", az2_lb_id="lb-e14oxiq3", az3_lb_id="lb-e14oxiq3") | ||
155 | + | ||
156 | +ret = lbswitch.modify_clb_weight(lb_id="lb-e14oxiq3", ip_weight_dict={"10.66.104.15": 10, "10.66.104.13":20}) | ||
157 | + | ||
158 | +print ret | ||
72 | 159 | ||
73 | -modify_alb_weight('AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK', 'ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW', lb_id='lb-990aeqw1', domain='*.yoho.cn', ip_weight_dict={'10.66.202.5': 10, '10.66.202.12':10}) |
-
Please register or login to post a comment