def _backup(hosts, remote_dir, base_dir=LB_CFG_BAK_DIR): """ 备份远程主机的目录. """ # 自定义 now = time.strftime("%Y%m%d%H%I%M%S") bak_dir = "%s/%s_%s" % (base_dir, \ remote_dir.replace("/","-"), now) for host in hosts: # 如果远程目录不存在, 就不备份. cmd = "test -d %s" % remote_dir rc, so, se = utils.remote_cmd(host, cmd) if rc != 0: continue # 备份基目录如果不存在, 创建. cmd = "sudo test -d %s ||sudo /bin/mkdir -p %s" % ( base_dir, base_dir) utils.remote_cmd(host, cmd) # 执行备份. cmd = "sudo /bin/cp -a %s %s " % (remote_dir, bak_dir) rc, so, se = utils.remote_cmd(host, cmd) if rc != 0: return (False, se) return (True, None)
def lips(lb, ip, lips): """ 配置 lb 的 lips. """ # 因为改了 DNS, 先清空本地 known_hosts 文件. cmd = "cat /dev/null >%s" % LOCAL_SSH_KNOWN_HOSTS rc, so, se = utils.shell(cmd) if rc != 0: message = "clean %s failed." % LOCAL_SSH_KNOWN_HOSTS logger.error(message) return False message = "Waiting for network administor to change "\ "network conf for %s" % lb logger.info(message) # 发邮件让网络工程师修改 lb 的网络配置. subject = u"[接入集群]请在一小时之内修改%s的网络设置, 否则集群会建立失败" % lb context = "" mail.mail(None, subject, context) # 检查是否能够 ping 通. time.sleep(120) checkcmd = "ping -c 3 %s &>/dev/null" % ip ret = utils.check_wait_null(checkcmd, timeinit=0, interval=5, timeout=3600) if not ret: logger.error("Ping failed, lb:%s, ip:%s" % (lb, ip)) return False # 配置 lip. cmd = "sudo -i wdconfig lvsfullnat_lip" rc, so, se = utils.remote_cmd(ip, cmd) if rc != 0: message = "Cfg lips failed, lb:%s, error:%s" % (lb, se) logger.error(message) return False cmd = "cd /tmp/post_config/lvsfullnat_lip_config && "\ "sudo sh lvsfullnat_lip_config.sh %s " % " ".join(lips) rc, so, se = utils.remote_cmd(ip, cmd) if rc != 0: message = "Cfg lips failed, lb:%s, error:%s" % (lb, se) logger.error(message) return False return True
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 _lb_single(host): """ 安装单台 lb. """ # 执行安装 fullnat 的命令. rc, so, se = utils.remote_cmd(host, LVS_FULLNAT_CMD) # 安装 lvs fullnat 完成后会重启系统, 当 ping 不通时, # 说明已经重启系统. checkcmd = "! ping -c 3 %s &>/dev/null" % host ret = utils.check_wait_null(checkcmd, timeinit=0, interval=5, timeout=2700) if not ret: return False # 当 ping 通时, 说明已经重启完毕. checkcmd = "ping -c 3 %s &>/dev/null" % host ret = utils.check_wait_null(checkcmd, timeinit=0, interval=5, timeout=1200) return {"host": host, "result": ret}
return False # 备份 lb 的配置文件. for dir in "/etc/keepalived/", "/usr/local/etc/": status, message = _backup(lbs, dir) if not status: logger.error(message) return False message = "Backup cfg success, lbs:" % lbs logger.info(message) # 删除 lb 的配置文件. for dir in "/etc/keepalived/", "/usr/local/etc/": cmd = "sudo /bin/rm -rf %s/*" % dir for lb in lbs: rc, so, se = utils.remote_cmd(lb, cmd) if rc != 0: logger.error(se) return False message = "Delete cfg success, lbs:%s" % lbs logger.info(message) # 传输配置. for lb in lbs: lb_dir = base_dir + "/" + lb lb_keepalived_dir = lb_dir + "/keepalived" lb_osfpd_dir = lb_dir + "/ospfd" _dict = { lb_keepalived_dir: "/etc/keepalived/", lb_osfpd_dir: "/usr/local/etc/",