예제 #1
0
파일: lb.py 프로젝트: alex1528/lvs_cluster
def add(name, new_lbinfos):
    """ 增加 lb.

    """
    # 检查集群是否存在.
    checkdict = {
        "name": name,
    }
    if not funcs.check(checkdict, check_exist=True):
        logger.error("No cluster:%s" % name)
        return False

    # 获取新增 lb.
    new_lbs = [i["hostname"] for i in new_lbinfos]
    if len(new_lbs) != len(set(new_lbs)):
        logger.error("Lb duplicates")
        return False

    # 检查新增 lb 是否已经存在.
    checkdict = {
        "lbs": new_lbs
    }
    if not funcs.check(checkdict, check_exist=False):
        logger.error("Some lbs exist")
        return False

    # 对所有 lb 安装 lvs fullnat.
    ret = funcs.lb_multi(new_lbs)
    fails = [ i for i in ret if not i["result"]]
    if fails != []:
        message = "Some lbs install failed:%s" % \
            ",".join(fails)
        logger.error(message)
        return False
    message = "All lbs install success:%s" % \
        ",".join(new_lbs)
    logger.info(message)

    # 获取已有集群信息.
    key = "cluster:%s" % name
    _type = client.hget(key, "type")
    old_lbinfos = eval(client.hget(key, "lbinfos"))
    device = client.hget(key, "device")

    # 获取新的 lbinfos.
    lbinfos = copy.deepcopy(old_lbinfos)
    lbinfos.extend(new_lbinfos)
    del old_lbinfos

    # 配置 lb.
    for lbinfo in new_lbinfos:
        lb = lbinfo["hostname"]
        internalip = lbinfo["internalip"]
        internalnetmask = lbinfo["internalnetmask"]
        internalgateway = lbinfo["internalgateway"]

        # 如果 _type 是 extra, 需要公网 IP;
        # 如果 _type 是 internal, 不需要公网 IP.
        extraip = lbinfo.get("extraip", None)
        extranetmask = lbinfo.get("extranetmask", None)
        extragateway = lbinfo.get("extragateway", None)

        # 配置 IP.
        ret = funcs.ip(lb, _type, device, internalip, 
                       internalnetmask, internalgateway, extraip, 
                       extranetmask, extragateway)
        if not ret:
            return False

        # 配置 lip.
        _lips = lips.get(internalip, internalnetmask)
        ret = funcs.lips(lb, internalip, _lips)
        if not ret:
            logger.error("Cfg lb lips failed:%s" % lb)
            return False
        logger.info("Cfg lb lips success:%s" % lb)

    # 保存集群信息.
    client.hset(key, "lbinfos", lbinfos)
    
    logger.info("Lbs added:%s" % ",".join(new_lbs)) 
    return True
예제 #2
0
def add(name, _type, lbinfos, vip2ws, vipnets, device):
    """ 增加一个 cluster.

    """
    # 拿到 lb 信息.
    lbs = [i["hostname"] for i in lbinfos]
    if len(lbs) != len(set(lbs)):
        logger.error("Lb duplicates")
        return False

    # 拿到 vip 信息.
    vips = [i["vip"] for i in vip2ws]
    if len(vips) != len(set(vips)):
        logger.error("Vip duplicates")
        return False

    # 检查 name, lb 和 vip 是否不存在.
    check_dict = {
        "name": name,
        "lbs": lbs,
        "vips": vips,
    }
    if not funcs.check(check_dict, check_exist=False):
        logger.error("Cluster or lbs or vips has exists.")
        return False

    # 对所有 lb 安装 lvs fullnat.
    ret = funcs.lb_multi(lbs)
    fails = [ i for i in ret if not i["result"]]
    if fails != []:
        message = "Some lbs install failed:%s" % \
            ",".join(fails)
        logger.error(message)
        return False
    else:
        message = "All lbs install success:%s" % \
            ",".join(lbs)
        logger.info(message)

    # 配置 lb.
    for lbinfo in lbinfos:
        lb = lbinfo["hostname"]
        internalip = lbinfo["internalip"]
        internalnetmask = lbinfo["internalnetmask"]
        internalgateway = lbinfo["internalgateway"]

        # 如果 _type 是 extra, 需要公网 IP;
        # 如果 _type 是 internal, 不需要公网 IP.
        extraip = lbinfo.get("extraip", None)
        extranetmask = lbinfo.get("extranetmask", None)
        extragateway = lbinfo.get("extragateway", None)

        # 配置 IP.
        ret = funcs.ip(lb, _type, device, internalip, 
                       internalnetmask, internalgateway, extraip, 
                       extranetmask, extragateway)
        if not ret:
            return False

        # 配置 lip.
        _lips = lips.get(internalip, internalnetmask)
        ret = funcs.lips(lb, internalip, _lips)
        if not ret:
            logger.error("Cfg lb lips failed:%s" % lb)
            return False
        logger.info("Cfg lb lips success:%s" % lb)

    # 保存集群信息.
    key = "cluster:%s" % name
    client.hset(key, "type", _type)
    client.hset(key, "lbinfos", lbinfos)
    client.hset(key, "vip2ws", vip2ws)
    client.hset(key, "vipnets", vipnets)
    client.hset(key, "device", device)

    logger.info("Cluster added:%s" % name)        
    return True
예제 #3
0
def add(name, new_lbinfos):
    """ 增加 lb.

    """
    # 检查集群是否存在.
    checkdict = {
        "name": name,
    }
    if not funcs.check(checkdict, check_exist=True):
        logger.error("No cluster:%s" % name)
        return False

    # 获取新增 lb.
    new_lbs = [i["hostname"] for i in new_lbinfos]
    if len(new_lbs) != len(set(new_lbs)):
        logger.error("Lb duplicates")
        return False

    # 检查新增 lb 是否已经存在.
    checkdict = {"lbs": new_lbs}
    if not funcs.check(checkdict, check_exist=False):
        logger.error("Some lbs exist")
        return False

    # 对所有 lb 安装 lvs fullnat.
    ret = funcs.lb_multi(new_lbs)
    fails = [i for i in ret if not i["result"]]
    if fails != []:
        message = "Some lbs install failed:%s" % \
            ",".join(fails)
        logger.error(message)
        return False
    message = "All lbs install success:%s" % \
        ",".join(new_lbs)
    logger.info(message)

    # 获取已有集群信息.
    key = "cluster:%s" % name
    _type = client.hget(key, "type")
    old_lbinfos = eval(client.hget(key, "lbinfos"))
    device = client.hget(key, "device")

    # 获取新的 lbinfos.
    lbinfos = copy.deepcopy(old_lbinfos)
    lbinfos.extend(new_lbinfos)
    del old_lbinfos

    # 配置 lb.
    for lbinfo in new_lbinfos:
        lb = lbinfo["hostname"]
        internalip = lbinfo["internalip"]
        internalnetmask = lbinfo["internalnetmask"]
        internalgateway = lbinfo["internalgateway"]

        # 如果 _type 是 extra, 需要公网 IP;
        # 如果 _type 是 internal, 不需要公网 IP.
        extraip = lbinfo.get("extraip", None)
        extranetmask = lbinfo.get("extranetmask", None)
        extragateway = lbinfo.get("extragateway", None)

        # 配置 IP.
        ret = funcs.ip(lb, _type, device, internalip, internalnetmask,
                       internalgateway, extraip, extranetmask, extragateway)
        if not ret:
            return False

        # 配置 lip.
        _lips = lips.get(internalip, internalnetmask)
        ret = funcs.lips(lb, internalip, _lips)
        if not ret:
            logger.error("Cfg lb lips failed:%s" % lb)
            return False
        logger.info("Cfg lb lips success:%s" % lb)

    # 保存集群信息.
    client.hset(key, "lbinfos", lbinfos)

    logger.info("Lbs added:%s" % ",".join(new_lbs))
    return True
예제 #4
0
def add(name, _type, lbinfos, vip2ws, vipnets, device):
    """ 增加一个 cluster.

    """
    # 拿到 lb 信息.
    lbs = [i["hostname"] for i in lbinfos]
    if len(lbs) != len(set(lbs)):
        logger.error("Lb duplicates")
        return False

    # 拿到 vip 信息.
    vips = [i["vip"] for i in vip2ws]
    if len(vips) != len(set(vips)):
        logger.error("Vip duplicates")
        return False

    # 检查 name, lb 和 vip 是否不存在.
    check_dict = {
        "name": name,
        "lbs": lbs,
        "vips": vips,
    }
    if not funcs.check(check_dict, check_exist=False):
        logger.error("Cluster or lbs or vips has exists.")
        return False

    # 对所有 lb 安装 lvs fullnat.
    ret = funcs.lb_multi(lbs)
    fails = [i for i in ret if not i["result"]]
    if fails != []:
        message = "Some lbs install failed:%s" % \
            ",".join(fails)
        logger.error(message)
        return False
    else:
        message = "All lbs install success:%s" % \
            ",".join(lbs)
        logger.info(message)

    # 配置 lb.
    for lbinfo in lbinfos:
        lb = lbinfo["hostname"]
        internalip = lbinfo["internalip"]
        internalnetmask = lbinfo["internalnetmask"]
        internalgateway = lbinfo["internalgateway"]

        # 如果 _type 是 extra, 需要公网 IP;
        # 如果 _type 是 internal, 不需要公网 IP.
        extraip = lbinfo.get("extraip", None)
        extranetmask = lbinfo.get("extranetmask", None)
        extragateway = lbinfo.get("extragateway", None)

        # 配置 IP.
        ret = funcs.ip(lb, _type, device, internalip, internalnetmask,
                       internalgateway, extraip, extranetmask, extragateway)
        if not ret:
            return False

        # 配置 lip.
        _lips = lips.get(internalip, internalnetmask)
        ret = funcs.lips(lb, internalip, _lips)
        if not ret:
            logger.error("Cfg lb lips failed:%s" % lb)
            return False
        logger.info("Cfg lb lips success:%s" % lb)

    # 保存集群信息.
    key = "cluster:%s" % name
    client.hset(key, "type", _type)
    client.hset(key, "lbinfos", lbinfos)
    client.hset(key, "vip2ws", vip2ws)
    client.hset(key, "vipnets", vipnets)
    client.hset(key, "device", device)

    logger.info("Cluster added:%s" % name)
    return True