...
|
...
|
@@ -20,22 +20,27 @@ from ansible.inventory.manager import InventoryManager |
|
|
|
|
|
demo:
|
|
|
|
|
|
lbswitch = LBSwitch(secretId="AKID6dwpxxxxxqPv5GK", secretKey="ACJxxxxBsQaW",
|
|
|
az1_lb_id="lb-e14oxiq3", az2_lb_id="lb-e14oxiq3", az3_lb_id="lb-e14oxiq3")
|
|
|
lbswitch.all_to_az1()
|
|
|
|
|
|
|
|
|
lbswitch = LBSwitch(secretId="AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK", secretKey="ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW",
|
|
|
az1_lb_id="lb-dyh6eucn", az2_lb_id="lb-capto5b5", az3_lb_id="lb-capto5b5", az1_innerlb_id="lb-c26tgo5r")
|
|
|
|
|
|
# 全部流量切换到AZ1
|
|
|
ret = lbswitch.all_to_az1()
|
|
|
|
|
|
# 全部流量切换到AZ2
|
|
|
ret = lbswitch.all_to_az2()
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
class LBSwitch:
|
|
|
def __init__(self, secretId, secretKey, az1_lb_id, az2_lb_id, az3_lb_id):
|
|
|
def __init__(self, secretId, secretKey, az1_lb_id, az2_lb_id, az3_lb_id, az1_innerlb_id):
|
|
|
self.SecretId = secretId
|
|
|
self.SecretKey = secretKey
|
|
|
self.az1_lb_id = az1_lb_id
|
|
|
self.az2_lb_id = az2_lb_id
|
|
|
self.az3_lb_id = az3_lb_id
|
|
|
self.az1_innerlb_id = az1_innerlb_id
|
|
|
|
|
|
def all_to_az1(self):
|
|
|
"""
|
...
|
...
|
@@ -45,19 +50,21 @@ class LBSwitch: |
|
|
az1_nginxs = self.get_inventory_ifo("az1", 'java-nginx')
|
|
|
print "get all java nginx: %s for az: %s" % (az1_nginxs, "az1")
|
|
|
|
|
|
ip_weight_dict = {}
|
|
|
az1_nginx_weight = {}
|
|
|
for az1_nginx in az1_nginxs:
|
|
|
ip_weight_dict[az1_nginx] = 10
|
|
|
az1_nginx_weight[az1_nginx] = 10
|
|
|
|
|
|
# az2 公网流量切换到az1
|
|
|
ret_az2 = self.modify_alb_weight(lb_id=self.az2_lb_id, domain="*.yoho.cn", ip_weight_dict=ip_weight_dict)
|
|
|
ret_az2 = self.modify_alb_weight(lb_id=self.az2_lb_id, domain="*.yoho.cn", ip_weight_dict=az1_nginx_weight)
|
|
|
print("switch all incoming request from az2 to az1 result: %s" % ret_az2)
|
|
|
|
|
|
# az1 公网流量切换到az1
|
|
|
ret_az1 = self.modify_clb_weight(lb_id=self.az1_lb_id, ip_weight_dict=ip_weight_dict)
|
|
|
print("switch all incoming request from az1 to az1 result: %s" % ret_az1)
|
|
|
# az1 公网流量、内部流量切换到az1
|
|
|
ret_az1 = self.modify_clb_weight(lb_id=self.az1_lb_id, ip_weight_dict=az1_nginx_weight)
|
|
|
ret_az1_inner = self.modify_alb_weight(lb_id=self.az1_innerlb_id, domain="*.yoho.yohoops.org",
|
|
|
ip_weight_dict=az1_nginx_weight)
|
|
|
print("switch all incoming request from az1 to az1 result: %s inner result: %s" % (ret_az1, ret_az1_inner))
|
|
|
|
|
|
return ret_az1 and ret_az2
|
|
|
return ret_az1 and ret_az1_inner and ret_az2
|
|
|
|
|
|
|
|
|
|
...
|
...
|
@@ -67,17 +74,20 @@ class LBSwitch: |
|
|
:return:
|
|
|
"""
|
|
|
az2_nginx = self.get_inventory_ifo("az2", 'java-nginx')
|
|
|
ip_weight_dict = {}
|
|
|
az2_nginx_weight = {}
|
|
|
for nginx in az2_nginx:
|
|
|
ip_weight_dict[nginx] = 10
|
|
|
az2_nginx_weight[nginx] = 10
|
|
|
|
|
|
ret_az2 = self.modify_alb_weight(lb_id=self.az2_lb_id, domain="*.yoho.cn", ip_weight_dict=ip_weight_dict)
|
|
|
# az2 公网流量切换到az2
|
|
|
ret_az2 = self.modify_alb_weight(lb_id=self.az2_lb_id, domain="*.yoho.cn", ip_weight_dict=az2_nginx_weight)
|
|
|
print("switch all incoming request from az2 to az2 result: %s" % ret_az2)
|
|
|
|
|
|
ret_az1 = self.modify_clb_weight(lb_id=self.az1_lb_id, ip_weight_dict=ip_weight_dict)
|
|
|
print("switch all incoming request from az1 to az2 result: %s" % ret_az1)
|
|
|
# az1 公网流量、内部流量切换到az2
|
|
|
ret_az1 = self.modify_clb_weight(lb_id=self.az1_lb_id, ip_weight_dict=az2_nginx_weight)
|
|
|
ret_az1_inner = self.modify_alb_weight(lb_id=self.az1_innerlb_id, domain="*.yoho.yohoops.org", ip_weight_dict=az2_nginx_weight)
|
|
|
print("switch all incoming request from az1 to az2 result: %s inner lb:%s" % (ret_az1, ret_az1_inner))
|
|
|
|
|
|
return ret_az1 & ret_az2
|
|
|
return ret_az1 and ret_az2 and ret_az1_inner
|
|
|
|
|
|
@staticmethod
|
|
|
def get_inventory_ifo(az, group):
|
...
|
...
|
@@ -179,11 +189,13 @@ class LBSwitch: |
|
|
return return_code.count(0) == len(return_code)
|
|
|
|
|
|
|
|
|
|
|
|
lbswitch = LBSwitch(secretId="AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK", secretKey="ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW",
|
|
|
az1_lb_id="lb-e14oxiq3", az2_lb_id="lb-e14oxiq3", az3_lb_id="lb-e14oxiq3")
|
|
|
az1_lb_id="lb-dyh6eucn", az2_lb_id="lb-capto5b5", az3_lb_id="lb-capto5b5", az1_innerlb_id="lb-c26tgo5r")
|
|
|
|
|
|
#ret = lbswitch.modify_clb_weight(lb_id="lb-e14oxiq3", ip_weight_dict={"10.66.104.15": 10, "10.66.104.13":20})
|
|
|
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})
|
|
|
|
|
|
#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})
|
|
|
#ret = lbswitch.all_to_az1()
|
|
|
ret = lbswitch.all_to_az2()
|
|
|
print ret
|
|
|
|
...
|
...
|
|