|
@@ -20,22 +20,27 @@ from ansible.inventory.manager import InventoryManager |
|
@@ -20,22 +20,27 @@ from ansible.inventory.manager import InventoryManager |
20
|
|
20
|
|
21
|
demo:
|
21
|
demo:
|
22
|
|
22
|
|
23
|
- lbswitch = LBSwitch(secretId="AKID6dwpxxxxxqPv5GK", secretKey="ACJxxxxBsQaW",
|
|
|
24
|
- az1_lb_id="lb-e14oxiq3", az2_lb_id="lb-e14oxiq3", az3_lb_id="lb-e14oxiq3")
|
|
|
25
|
- lbswitch.all_to_az1()
|
|
|
26
|
-
|
|
|
27
|
|
23
|
|
|
|
24
|
+lbswitch = LBSwitch(secretId="AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK", secretKey="ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW",
|
|
|
25
|
+ az1_lb_id="lb-dyh6eucn", az2_lb_id="lb-capto5b5", az3_lb_id="lb-capto5b5", az1_innerlb_id="lb-c26tgo5r")
|
|
|
26
|
+
|
|
|
27
|
+# 全部流量切换到AZ1
|
|
|
28
|
+ret = lbswitch.all_to_az1()
|
|
|
29
|
+
|
|
|
30
|
+# 全部流量切换到AZ2
|
|
|
31
|
+ret = lbswitch.all_to_az2()
|
28
|
|
32
|
|
29
|
"""
|
33
|
"""
|
30
|
|
34
|
|
31
|
|
35
|
|
32
|
class LBSwitch:
|
36
|
class LBSwitch:
|
33
|
- def __init__(self, secretId, secretKey, az1_lb_id, az2_lb_id, az3_lb_id):
|
37
|
+ def __init__(self, secretId, secretKey, az1_lb_id, az2_lb_id, az3_lb_id, az1_innerlb_id):
|
34
|
self.SecretId = secretId
|
38
|
self.SecretId = secretId
|
35
|
self.SecretKey = secretKey
|
39
|
self.SecretKey = secretKey
|
36
|
self.az1_lb_id = az1_lb_id
|
40
|
self.az1_lb_id = az1_lb_id
|
37
|
self.az2_lb_id = az2_lb_id
|
41
|
self.az2_lb_id = az2_lb_id
|
38
|
self.az3_lb_id = az3_lb_id
|
42
|
self.az3_lb_id = az3_lb_id
|
|
|
43
|
+ self.az1_innerlb_id = az1_innerlb_id
|
39
|
|
44
|
|
40
|
def all_to_az1(self):
|
45
|
def all_to_az1(self):
|
41
|
"""
|
46
|
"""
|
|
@@ -45,19 +50,21 @@ class LBSwitch: |
|
@@ -45,19 +50,21 @@ class LBSwitch: |
45
|
az1_nginxs = self.get_inventory_ifo("az1", 'java-nginx')
|
50
|
az1_nginxs = self.get_inventory_ifo("az1", 'java-nginx')
|
46
|
print "get all java nginx: %s for az: %s" % (az1_nginxs, "az1")
|
51
|
print "get all java nginx: %s for az: %s" % (az1_nginxs, "az1")
|
47
|
|
52
|
|
48
|
- ip_weight_dict = {}
|
53
|
+ az1_nginx_weight = {}
|
49
|
for az1_nginx in az1_nginxs:
|
54
|
for az1_nginx in az1_nginxs:
|
50
|
- ip_weight_dict[az1_nginx] = 10
|
55
|
+ az1_nginx_weight[az1_nginx] = 10
|
51
|
|
56
|
|
52
|
# az2 公网流量切换到az1
|
57
|
# az2 公网流量切换到az1
|
53
|
- ret_az2 = self.modify_alb_weight(lb_id=self.az2_lb_id, domain="*.yoho.cn", ip_weight_dict=ip_weight_dict)
|
58
|
+ ret_az2 = self.modify_alb_weight(lb_id=self.az2_lb_id, domain="*.yoho.cn", ip_weight_dict=az1_nginx_weight)
|
54
|
print("switch all incoming request from az2 to az1 result: %s" % ret_az2)
|
59
|
print("switch all incoming request from az2 to az1 result: %s" % ret_az2)
|
55
|
|
60
|
|
56
|
- # az1 公网流量切换到az1
|
|
|
57
|
- ret_az1 = self.modify_clb_weight(lb_id=self.az1_lb_id, ip_weight_dict=ip_weight_dict)
|
|
|
58
|
- print("switch all incoming request from az1 to az1 result: %s" % ret_az1)
|
61
|
+ # az1 公网流量、内部流量切换到az1
|
|
|
62
|
+ ret_az1 = self.modify_clb_weight(lb_id=self.az1_lb_id, ip_weight_dict=az1_nginx_weight)
|
|
|
63
|
+ ret_az1_inner = self.modify_alb_weight(lb_id=self.az1_innerlb_id, domain="*.yoho.yohoops.org",
|
|
|
64
|
+ ip_weight_dict=az1_nginx_weight)
|
|
|
65
|
+ print("switch all incoming request from az1 to az1 result: %s inner result: %s" % (ret_az1, ret_az1_inner))
|
59
|
|
66
|
|
60
|
- return ret_az1 and ret_az2
|
67
|
+ return ret_az1 and ret_az1_inner and ret_az2
|
61
|
|
68
|
|
62
|
|
69
|
|
63
|
|
70
|
|
|
@@ -67,17 +74,20 @@ class LBSwitch: |
|
@@ -67,17 +74,20 @@ class LBSwitch: |
67
|
:return:
|
74
|
:return:
|
68
|
"""
|
75
|
"""
|
69
|
az2_nginx = self.get_inventory_ifo("az2", 'java-nginx')
|
76
|
az2_nginx = self.get_inventory_ifo("az2", 'java-nginx')
|
70
|
- ip_weight_dict = {}
|
77
|
+ az2_nginx_weight = {}
|
71
|
for nginx in az2_nginx:
|
78
|
for nginx in az2_nginx:
|
72
|
- ip_weight_dict[nginx] = 10
|
79
|
+ az2_nginx_weight[nginx] = 10
|
73
|
|
80
|
|
74
|
- ret_az2 = self.modify_alb_weight(lb_id=self.az2_lb_id, domain="*.yoho.cn", ip_weight_dict=ip_weight_dict)
|
81
|
+ # az2 公网流量切换到az2
|
|
|
82
|
+ ret_az2 = self.modify_alb_weight(lb_id=self.az2_lb_id, domain="*.yoho.cn", ip_weight_dict=az2_nginx_weight)
|
75
|
print("switch all incoming request from az2 to az2 result: %s" % ret_az2)
|
83
|
print("switch all incoming request from az2 to az2 result: %s" % ret_az2)
|
76
|
|
84
|
|
77
|
- ret_az1 = self.modify_clb_weight(lb_id=self.az1_lb_id, ip_weight_dict=ip_weight_dict)
|
|
|
78
|
- print("switch all incoming request from az1 to az2 result: %s" % ret_az1)
|
85
|
+ # az1 公网流量、内部流量切换到az2
|
|
|
86
|
+ ret_az1 = self.modify_clb_weight(lb_id=self.az1_lb_id, ip_weight_dict=az2_nginx_weight)
|
|
|
87
|
+ ret_az1_inner = self.modify_alb_weight(lb_id=self.az1_innerlb_id, domain="*.yoho.yohoops.org", ip_weight_dict=az2_nginx_weight)
|
|
|
88
|
+ print("switch all incoming request from az1 to az2 result: %s inner lb:%s" % (ret_az1, ret_az1_inner))
|
79
|
|
89
|
|
80
|
- return ret_az1 & ret_az2
|
90
|
+ return ret_az1 and ret_az2 and ret_az1_inner
|
81
|
|
91
|
|
82
|
@staticmethod
|
92
|
@staticmethod
|
83
|
def get_inventory_ifo(az, group):
|
93
|
def get_inventory_ifo(az, group):
|
|
@@ -179,11 +189,13 @@ class LBSwitch: |
|
@@ -179,11 +189,13 @@ class LBSwitch: |
179
|
return return_code.count(0) == len(return_code)
|
189
|
return return_code.count(0) == len(return_code)
|
180
|
|
190
|
|
181
|
|
191
|
|
|
|
192
|
+
|
182
|
lbswitch = LBSwitch(secretId="AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK", secretKey="ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW",
|
193
|
lbswitch = LBSwitch(secretId="AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK", secretKey="ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW",
|
183
|
- az1_lb_id="lb-e14oxiq3", az2_lb_id="lb-e14oxiq3", az3_lb_id="lb-e14oxiq3")
|
194
|
+ az1_lb_id="lb-dyh6eucn", az2_lb_id="lb-capto5b5", az3_lb_id="lb-capto5b5", az1_innerlb_id="lb-c26tgo5r")
|
184
|
|
195
|
|
185
|
#ret = lbswitch.modify_clb_weight(lb_id="lb-e14oxiq3", ip_weight_dict={"10.66.104.15": 10, "10.66.104.13":20})
|
196
|
#ret = lbswitch.modify_clb_weight(lb_id="lb-e14oxiq3", ip_weight_dict={"10.66.104.15": 10, "10.66.104.13":20})
|
186
|
-ret = lbswitch.modify_alb_weight(lb_id="lb-fodpm611", domain="*.yoho.cn", ip_weight_dict={"10.66.0.2": 10, "10.66.0.3": 10})
|
|
|
187
|
-
|
197
|
+#ret = lbswitch.modify_alb_weight(lb_id="lb-fodpm611", domain="*.yoho.cn", ip_weight_dict={"10.66.0.2": 10, "10.66.0.3": 10})
|
|
|
198
|
+#ret = lbswitch.all_to_az1()
|
|
|
199
|
+ret = lbswitch.all_to_az2()
|
188
|
print ret
|
200
|
print ret
|
189
|
|
201
|
|