Esempio n. 1
0
def get_node():
    try:
        scripts = "/root/local/bin/kubectl get node | grep -v NAME | awk '{print $1}'"
        nodes = order(scripts, "readlines")
        return nodes
    except:
        logger.error("Get kubernetes node failed!")
Esempio n. 2
0
def val(items):
    # 合法性检查
    for item in items:
        if type(item) == unicode:
            name = str(item)
            if not re.match(
                    r'^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))$',
                    name):
                logger.error("Error, invalid ip: " + name)
                return False
        else:
            logger.error("Error, invalid ip, the ip must be a string value.")
            return False
    return True
Esempio n. 3
0
def get_json(obj, key):
    """
    获取key对应的value
    :param obj: json对象
    :param key: 键值
    :return: 对应的value值,出错则为False
    """
    if type(key) == unicode:
        key = str(key)
    if key in obj:
        return obj[key]
    else:
        logger.error("Wrong configuration with " + key + "!")
        return False
Esempio n. 4
0
def get_config(path):
    """
    读取配置文件
    :param path:配置文件所在路径
    :return: 读取到的配置文件内容,出错则返回False
    """
    try:
        with open(path, "r") as file:
            data = json.load(file)
            if data:
                return data
            else:
                logger.error("Get config failed!")
                return False
    except:
        logger.error("Illegal configuration file!")
        return False
Esempio n. 5
0
def order(script, action):
    """
    命令执行函数
    :param script:要执行的命令
    :param action:读取结果的方式
    :return:执行结果
    """
    cmd = os.popen(script)
    if action == "read":
        result = cmd.read()
    elif action == "readline":
        result = cmd.readline()
    elif action == "readlines":
        result = cmd.readlines()
    else:
        logger.error("Error arg for order")
    cmd.close()
    return result
Esempio n. 6
0
def label():
    """
    为指定ip打上标签
    打标签之前先检查集群所有节点,防止指定ip地址外有标签的存在
    :return:
    """
    nodes = commonUtils.get_node()
    if len(nodes) > 1:
        logger.error("Illegal node address")
    else:
        scripts = "/root/local/bin/kubectl get node --show-labels | grep pgsql=pgsql | awk '{print $1}'"
        node = commonUtils.order(scripts, "readlines")
        if node:
            logger.info("Node had labled")
        else:
            for node in nodes:
                node = node.split("\n")[0]
                os.system(
                    '/root/local/bin/kubectl label node {} pgsql=pgsql --overwrite=true 2>&1\n'
                    .format(node))
Esempio n. 7
0
def isExist():
    """
    判断集群是否已存在,用于安装前检查
    :return:检查结果,true代表不存在,false代表存在
    """
    # 检查service
    scripts = "/root/local/bin/kubectl get service | grep svc-pgsql | awk '{print $1}'"
    services = commonUtils.order(scripts, "readlines")
    if services:
        for service in services:
            logger.error("service:" + service.split("\n")[0] + " is exist!")
        return True
    # 检查statefulset

    scripts = "/root/local/bin/kubectl get statefulset | grep st-pgsql | awk '{print $1}'"
    sta = commonUtils.order(scripts, "readlines")
    if sta:
        for s in sta:
            logger.error("statefulset:" + s.split("\n")[0] + "is exist!")
        return True
    return False
Esempio n. 8
0
def getFQDN():
    """
    获取zookeeper集群的正式域名
    :return:
    """
    # 获取zookeeper集群总数
    script = "/root/local/bin/kubectl get statefulset zookeeper -o wide | awk 'NR>1{print $3}'"
    current = commonUtils.order(script, "read")
    if int(current) > 0:
        name = []
        for i in range(0, int(current)):
            script = "/root/local/bin/kubectl exec zookeeper-" + str(
                i) + " -- hostname -f"
            fqdn = commonUtils.order(script, "read")[:-2]
            name.append(fqdn)
        result = ""
        for i in name:
            result = result + (i + ":2181,")
        return result[:-1]
    else:
        logger.error("No available zookeeper!")
        return False