lb_switch.py 3.29 KB
#!/usr/bin/python
# -*- coding: UTF-8 -*-

""" 
修改腾讯云七层负载均衡器的权重

demo(命令行参数): python lb_switch.py --secretid=AKID6**************dwpKK --secretkey=ACJk9**************sQaW --lb_name=ytx-switch-test --domain=search.yohoops.org --hostname=search-service-az2 --weight=10
demo(模块): python lb_switch.py --secretid=AKID6**************dwpKK --secretkey=ACJk9**************sQaW --module=search_to_az2

@see doc:  https://cloud.tencent.com/document/product/214/8978
  
"""

import time, sys, os
import argparse
import json
from lb_action import modify_clb_weight,modify_alb_weight,describe_lbs


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--module',type=str,default=None,help='任务模块名称')
    parser.add_argument('--secretid',type=str,help='腾讯云 SecretId')
    parser.add_argument('--secretkey',type=str,help='腾讯云 SecretKey')
    parser.add_argument('--lb_name',type=str,help='负载均衡器名称')
    parser.add_argument('--domain',type=str,default='',help='应用型负载均衡器的域名')
    parser.add_argument('--hostname',type=str,help='负载均衡器后端绑定的主机名称')
    parser.add_argument('--weight',type=int,help='需要调整的主机权重')
    parser.add_argument('--default_weight',type=int,default=0,help='同域名/负载均衡器下其他主机默认权重')
    args = parser.parse_args()

    #检查是否存在任务模块
    module_file = "modules/{0}.json".format(args.module) if args.module else ''
    if args.module and os.path.isfile(module_file):
        #执行模块
        print '模块{0}已匹配'.format(args.module)
        module = json.load(open(module_file))
        for lb_id in module.keys():
            if module[lb_id].has_key("domain") and module[lb_id]["domain"]:
                if type(module[lb_id]["domain"]) is list:
                    for domain in module[lb_id]["domain"]:
                        modify_alb_weight(args.secretid, args.secretkey, lb_id, domain, hostname_weight_dict=module[lb_id]["mappings"], default_weight=args.default_weight)
                else:
                    modify_alb_weight(args.secretid, args.secretkey, lb_id, module[lb_id]["domain"], hostname_weight_dict=module[lb_id]["mappings"], default_weight=args.default_weight)
            else:
                modify_clb_weight(args.secretid, args.secretkey, lb_id, hostname_weight_dict=module[lb_id]["mappings"], default_weight=args.default_weight)
    else:
        #执行命令行参数
        lbs = describe_lbs(args.secretid,args.secretkey)
        lb_id = ''
        for lb in lbs:
            if lb['loadBalancerName'].encode('utf-8') == args.lb_name:
                lb_id = lb['loadBalancerId']
                break

        if not lb_id:
            print '未找到负载均衡器{0}'.format(args.lb_name)
            sys.exit(1)
        else:
            hostname_weight_dict = {
                args.hostname: args.weight
            }

            if args.domain:
                modify_alb_weight(args.secretid, args.secretkey, lb_id, args.domain, hostname_weight_dict=hostname_weight_dict, default_weight=args.default_weight)
            else:
                modify_clb_weight(args.secretid, args.secretkey, lb_id, hostname_weight_dict=hostname_weight_dict, default_weight=args.default_weight)