def ip(lb, _type, device, internalip, internalnetmask, \ internalgateway, extraip=None, extranetmask=None, \ extragateway=None): """ 配置 lb 的 ip. """ time.sleep(120) # 配置 IP. cmd = "sudo -i wdconfig lvsfullnat_ip" rc, so, se = utils.remote_cmd(lb, cmd) if rc != 0: logger.error("Cfg %s ip failed - %s" % (lb, se)) return False cmd = "cd /tmp/post_config/lvsfullnat_ip_config "\ "&& sudo sh lvsfullnat_ip_config.sh "\ "%s %s %s %s %s %s %s %s "\ "&>/tmp/.lvsfullnat_ip_config.log &" % ( _type, device, internalip, internalnetmask, internalgateway, extraip, extranetmask, extragateway) sshcmd = """ ssh -oConnectTimeout=3 -oStrictHostKeyChecking=no """\ """op@%s "%s" & """ % (lb, cmd) os.system(sshcmd) logger.info("Cfg lb ip: %s" % lb) # 先获取当前的内网 IP. origin_internalip = utils.dns_resolv([lb])[0] # 内网 IP 被改了, 修改 DNS. ret = dnsapi.modify_wrapper(lb, internalip) if ret == "failed": message = "Change %s dns ip from %s to %s failed" % \ (lb, origin_internalip, internalip) logger.error(message) return False message = "Change %s dns ip from %s to %s success" %\ (lb, origin_internalip, internalip) logger.info(message) return True
def generate(_type, lbinfos, vip2ws, vipnets, device): """ 生成和传输 keepalived 配置. """ j2_env = Environment(loader=FileSystemLoader(LVS_TEMPLATE_DIR), trim_blocks=True) # 各个模板文件名. keepalived_template = "keepalived.conf" sub_keepalived_template = "sub_keepalived.conf" zebra_template = "zebra.conf" ospfd_template = "ospfd.conf" # 建立存放 lvs 临时配置文件基目录. # lb 的配置文件分别几个部分, 一个是 keepalived.conf 主配置 # 文件, 这个文件需要引用每个 VIP 的配置, 额外还需要 ospfd.conf # 和 zebra.conf 配置. 对于 VIP 配置, 所有 lb 都是一样的, # keepalived.conf 主配置文件、 ospfd.conf 和 zebra.conf # 则每个 lb 都不一样. # 基目录下目录结构这么存放: # lbcommon 里面存 每个 lb 都一样的 VIP 配置; # 对于 每个 lb 建立一个目录, 下面有两个子目录, 分别是: # keepalived 和 ospfd, keepalived 下面是 keepalived.conf, # ospfd 下面是 ospfd.conf 和 zebra.conf. # 比如: # ./lbcommon/xxx.xxx.xxx.xxx.conf # ./$lb/ospfd/ospfd.conf # ./$lb/ospfd/zebra.conf # ./$lb/keepalived/keepalived.conf now = time.strftime("%Y%m%d%H%I%M%S") base_dir = LVS_CFG_TMP_DIR + "/" + now os.mkdir(base_dir) # 拿到 lb 和 vip. lbs = [i["hostname"] for i in lbinfos] vips = [i["vip"] for i in vip2ws] try: for lbinfo in lbinfos: lb = lbinfo["hostname"] internalip = lbinfo["internalip"] internalnetmask = lbinfo["internalnetmask"] internalgateway = lbinfo["internalgateway"] routerid = lbinfo["routerid"] ospfnet = lbinfo["ospfnet"] if _type == "extra": extraip = lbinfo["extraip"] extranetmask = lbinfo["extranetmask"] extragateway = lbinfo["extragateway"] _lips = lips.get(internalip, internalnetmask) # 对每个 lb 创建临时目录. lb_dir = base_dir + "/" + lb lb_keepalived_dir = lb_dir + "/keepalived" lb_osfpd_dir = lb_dir + "/ospfd" os.mkdir(lb_dir) os.mkdir(lb_keepalived_dir) os.mkdir(lb_osfpd_dir) # 对于 lb 生成配置. with open(lb_keepalived_dir + "/keepalived.conf", 'w') as f: f.writelines( j2_env.get_template(keepalived_template).render( lips=_lips, vips=vips, lb=lb.split(".")[0] ) ) with open(lb_osfpd_dir + "/zebra.conf", 'w') as f: f.writelines( j2_env.get_template(zebra_template).render( lb=lb ) ) with open(lb_osfpd_dir + "/ospfd.conf", 'w') as f: f.writelines( j2_env.get_template(ospfd_template).render( lb=lb, routerid=routerid, device=device, ospfnet=ospfnet, vipnets=vipnets ) ) # 创建公共临时目录. lb_common_dir = base_dir + "/lbcommon" os.mkdir(lb_common_dir) # 获取 ports 和 wss. for vip in vips: for i in vip2ws: if vip == i["vip"]: if "ports" not in i: ports = [ {"sport": 80, "dport": 80, "synproxy": 1, "persistence_timeout": 50 }, {"sport": 443, "dport": 443, "synproxy": 1, "persistence_timeout": 50 } ] else: ports = list() for j in i["ports"]: if "synproxy" not in j: j["synproxy"] = 1 elif "persistence_timeout" not in j: j["persistence_timeout"] = 50 ports.append(j) wss = i["wss"] break # 把 wss 解析成 IP. wss_ip = utils.dns_resolv(wss) if not wss_ip: message = "Some ws dns resolv failed, wss:%s" % wss logger.error(message) return False # 生成公共的 VIP 配置. with open(lb_common_dir + "/" + vip + ".conf", 'w') as f: f.writelines( j2_env.get_template(sub_keepalived_template).render( vip=vip, ports=ports, wss=wss_ip ) ) message = "Make keepalived cfg success" logger.info(message) except Exception, e: message = "Make keepalived cfg failed:%s" % e logger.error(message) return False