コード例 #1
0
def get_host_by_name(servers=None):
    '''Get hostname of the specified servers.
    Kwargs:
        servers (Optional[str]): Get hostnames of the specified servers.
    Returns:
        dict: dict with 'hostname or ip_address" of the server as key and
              'hostname' of the server as value.
    '''
    if servers is None:
        servers = nodes_from_pool_list()

    if not isinstance(servers, list):
        servers = [servers]

    server_hostname_dict = OrderedDict()
    for server in servers:
        server_hostname_dict[server] = socket.gethostbyaddr(server)[0]

    return server_hostname_dict
コード例 #2
0
ファイル: ganesha.py プロジェクト: hsepeng/glusterfs
def get_host_by_name(servers=None):
    '''Get hostname of the specified servers.
    Kwargs:
        servers (Optional[str]): Get hostnames of the specified servers.
    Returns:
        dict: dict with 'hostname or ip_address" of the server as key and
              'hostname' of the server as value.
    '''
    if servers is None:
        servers = nodes_from_pool_list()

    if not isinstance(servers, list):
        servers = [servers]

    server_hostname_dict = OrderedDict()
    for server in servers:
        server_hostname_dict[server] = socket.gethostbyaddr(server)[0]

    return server_hostname_dict
コード例 #3
0
def cluster_auth_setup(no_of_servers=None):
    '''Sets the hacluster password, starts pcsd service and runs
       pcs cluster auth command.
    Kwargs:
        no_of_servers (Optional[int]): The number of nodes on which we have
            to setup the HA cluster. Default it takes the number
            of servers from the pool list.
    Returns:
        bool: True if successfull, False otherwise.
    '''
    if no_of_servers is None:
        servers = nodes_from_pool_list()
        no_of_servers = len(servers)
    else:
        servers = tc.servers[0:no_of_servers]
    result = True
    for node in tc.servers[0:no_of_servers]:
        ret, _, _ = tc.run(node, "echo hacluster | passwd --stdin hacluster")
        if ret != 0:
            tc.logger.error("unable to set password for hacluster on %s" %
                            node)
            return False
        else:
            ret, _, _ = tc.run(node, "service pcsd start")
            if ret != 0:
                tc.looger.error("service pcsd start command failed on %s" %
                                node)
                return False
    server_hostname_dict = get_host_by_name(servers)
    for node in tc.servers[0:no_of_servers]:
        val = ""
        for key in server_hostname_dict:
            val += server_hostname_dict[key]
            val += " "
        ret, _, _ = tc.run(
            node, "pcs cluster auth %s -u hacluster -p "
            "hacluster" % val)
        if ret != 0:
            tc.logger.error("pcs cluster auth command failed on %s" % node)
            result = False

    return result
コード例 #4
0
ファイル: ganesha.py プロジェクト: hsepeng/glusterfs
def cluster_auth_setup(no_of_servers=None):
    '''Sets the hacluster password, starts pcsd service and runs
       pcs cluster auth command.
    Kwargs:
        no_of_servers (Optional[int]): The number of nodes on which we have
            to setup the HA cluster. Default it takes the number
            of servers from the pool list.
    Returns:
        bool: True if successfull, False otherwise.
    '''
    if no_of_servers is None:
        servers = nodes_from_pool_list()
        no_of_servers = len(servers)
    else:
        servers = tc.servers[0:no_of_servers]
    result = True
    for node in tc.servers[0:no_of_servers]:
        ret, _, _ = tc.run(node, "echo hacluster | passwd --stdin hacluster")
        if ret != 0:
            tc.logger.error("unable to set password for hacluster on %s"
                            % node)
            return False
        else:
            ret, _, _ = tc.run(node, "service pcsd start")
            if ret != 0:
                tc.looger.error("service pcsd start command failed on %s"
                                % node)
                return False
    server_hostname_dict = get_host_by_name(servers)
    for node in tc.servers[0:no_of_servers]:
        val = ""
        for key in server_hostname_dict:
            val += server_hostname_dict[key]
            val += " "
        ret, _, _ = tc.run(node, "pcs cluster auth %s -u hacluster -p "
                           "hacluster" % val)
        if ret != 0:
                tc.logger.error("pcs cluster auth command failed on %s" % node)
                result = False

    return result
コード例 #5
0
ファイル: ganesha.py プロジェクト: hsepeng/glusterfs
def set_nfs_ganesha(option=True, mnode=None):
    '''Enables/Disables NFS-Ganesha Cluster
    Kwargs:
        option (Optional[bool]): If True it enables the nfs-ganesha
            HA Cluster, else disables the nfs-ganesha HA Cluster.
            Default value is True.
        mnode (Optional[str]): Node on which the command has
            to be executed. Default value is tc.servers[0].
    Returns:
        bool: True if successful, False otherwise.
    '''
    if mnode is None:
        mnode = tc.servers[0]
    servers = nodes_from_pool_list()
    no_of_servers = len(servers)
    if option:
        ret, _, _ = tc.run(mnode, "gluster nfs-ganesha enable --mode=script")
        if ret == 0:
            tc.logger.info("nfs-ganesha enable success")
            time.sleep(45)
            ret, _, _ = tc.run(mnode, "pcs status")
            ret = validate_ganesha_ha_status(mnode)
            if ret:
                return True
            else:
                return False
        else:
            tc.logger.error("nfs-ganesha enable falied")
            return False
    else:
        ret, _, _ = tc.run(tc.servers[0], "gluster nfs-ganesha disable "
                           "--mode=script")
        if ret == 0:
            tc.logger.info("nfs-ganesha disable success")
            time.sleep(10)
            for node in tc.servers[0:no_of_servers]:
                ret, _, _ = tc.run(node, "pcs status")
            return True
        else:
            tc.logger.error("nfs-ganesha disable falied")
            return False
コード例 #6
0
def set_nfs_ganesha(option=True, mnode=None):
    '''Enables/Disables NFS-Ganesha Cluster
    Kwargs:
        option (Optional[bool]): If True it enables the nfs-ganesha
            HA Cluster, else disables the nfs-ganesha HA Cluster.
            Default value is True.
        mnode (Optional[str]): Node on which the command has
            to be executed. Default value is tc.servers[0].
    Returns:
        bool: True if successful, False otherwise.
    '''
    if mnode is None:
        mnode = tc.servers[0]
    servers = nodes_from_pool_list()
    no_of_servers = len(servers)
    if option:
        ret, _, _ = tc.run(mnode, "gluster nfs-ganesha enable --mode=script")
        if ret == 0:
            tc.logger.info("nfs-ganesha enable success")
            time.sleep(45)
            ret, _, _ = tc.run(mnode, "pcs status")
            ret = validate_ganesha_ha_status(mnode)
            if ret:
                return True
            else:
                return False
        else:
            tc.logger.error("nfs-ganesha enable falied")
            return False
    else:
        ret, _, _ = tc.run(tc.servers[0], "gluster nfs-ganesha disable "
                           "--mode=script")
        if ret == 0:
            tc.logger.info("nfs-ganesha disable success")
            time.sleep(10)
            for node in tc.servers[0:no_of_servers]:
                ret, _, _ = tc.run(node, "pcs status")
            return True
        else:
            tc.logger.error("nfs-ganesha disable falied")
            return False
コード例 #7
0
ファイル: ganesha.py プロジェクト: hsepeng/glusterfs
def update_ganesha_ha_conf(no_of_servers=None):
    '''Updates the ganesha-ha.conf file, with VIPs and hostnames.
    Kwargs:
        no_of_servers (Optional[int]): The number of nodes on which we have
            to modify the ganesha-ha.conf file. Default it takes
            the number of servers from the pool list.
    Returns:
        bool: True if successfull, False otherwise.
    '''
    if no_of_servers is None:
        servers = nodes_from_pool_list()
        no_of_servers = len(servers)
    else:
        servers = tc.servers[0:no_of_servers]
    server_hostname_dict = get_host_by_name(servers)
    hostnames = server_hostname_dict.values()
    hosts = ','.join(hostnames)
    file_src_path = "/etc/ganesha/ganesha-ha.conf.sample"
    file_dest_path = "/etc/ganesha/ganesha-ha.conf"
    ha_server = tc.run(tc.servers[0], "hostname")
    conn = tc.get_connection(tc.servers[0], "root")
    if conn.modules.os.path.isfile(file_src_path) == True:
        tc.logger.info("%s file available and should be updated as "
                       "ganesha-ha.conf" % file_src_path)
        try:
            conn.modules.shutil.copy(file_src_path, file_dest_path)
            FH = conn.builtin.open(file_dest_path, "r+")
        except IOError as e:
            tc.logger.error(e)
            return False
    lines = FH.readlines()
    FH.seek(0)
    FH.truncate()
    for i in range(len(lines)):
        if re.search("HA_NAME", lines[i]) != None:
            lines[i] = re.sub(r'^HA_NAME.*', "HA_NAME=\"G"+str(time.time()) +
                              "\"", lines[i])
        if re.search("HA_VOL_SERVER", lines[i]) != None:
            lines[i] = re.sub(r'^HA_VOL_SERVER.*', "HA_VOL_SERVER=\"" +
                              ha_server[1].strip()+"\"", lines[i])
        if re.search("HA_CLUSTER_NODES", lines[i]) != None:
            lines[i] = re.sub(r'^HA_CLUSTER_NODES.*', "HA_CLUSTER_NODES=\"" +
                              hosts+"\"", lines[i])
        if re.search("VIP_", lines[i]) != None:
            lines[i] = re.sub(r'.*VIP_.*\n', "", lines[i])
    vips = (tc.global_config["gluster"]["cluster_config"]
            ["nfs_ganesha"]["vips"])
    for i in range(no_of_servers):
        lines += "VIP_%s=\"%s\"\n" % (hostnames[i], vips[i])
    FH.write(''.join(lines))
    # create a local copy of this ha.conf file
    f = open("/tmp/ganesha-ha.conf", "w")
    f.write(''.join(lines))
    f.close()
    FH.close()
    conn.close()
    # copy this ha.conf file to all the other nodes
    for node in tc.servers[1:no_of_servers]:
        ret = tc.upload(node, "/tmp/ganesha-ha.conf", file_dest_path)

    return True
コード例 #8
0
ファイル: volume_ops.py プロジェクト: Say0Never/glusterfs
def create_volume(volname, dist, rep=1, stripe=1, trans='tcp', servers='', \
        snap=True, disp=1, dispd=1, red=1):
    """
        Create the gluster volume specified configuration
        volname and distribute count are mandatory argument
    """
    if servers == '':
        servers = nodes_from_pool_list()
    if not servers:
        servers = tc.servers
    dist = int(dist)
    rep = int(rep)
    stripe = int(stripe)
    disp = int(disp)
    dispd = int(dispd)
    red = int(red)
    dispc = 1

    if disp != 1 and dispd != 1:
        tc.logger.error("volume can't have both disperse and disperse-data")
        return (-1, None, None)
    if disp != 1:
        dispc = int(disp)
    elif dispd != 1:
        dispc = int(dispd) + int(red)

    number_of_bricks = dist * rep * stripe * dispc
    replica = stripec = disperse = disperse_data = redundancy = ''
    brick_root = '/bricks'
    n = 0
    tempn = 0
    bricks_list = ''
    rc = tc.run(servers[0], "gluster volume info | egrep \"^Brick[0-9]+\"", \
            verbose=False)
    for i in range(0, number_of_bricks):
        if not snap:
            bricks_list = "%s %s:%s/%s_brick%d" % \
                (bricks_list, servers[n], brick_root, volname, i)
        else:
            sn = len(re.findall(servers[n], rc[1])) + tempn
            bricks_list = "%s %s:%s/brick%d/%s_brick%d" % \
            (bricks_list, servers[n], brick_root, sn, volname, i)
        if n < len(servers[:]) - 1:
            n = n + 1
        else:
            n = 0
            tempn = tempn + 1

    if rep != 1:
        replica = "replica %d" % rep
    if stripe != 1:
        stripec = "stripe %d" % stripe
    ttype = "transport %s" % trans
    if disp != 1:
        disperse = "disperse %d" % disp
        redundancy = "redundancy %d" % red
    elif dispd != 1:
        disperse_data = "disperse-data %d" % dispd
        redundancy = "redundancy %d" % red

    ret = tc.run(servers[0], "gluster volume create %s %s %s %s %s %s %s %s \
--mode=script" % (volname, replica, stripec, disperse, disperse_data, \
redundancy, ttype, bricks_list))
    return ret
コード例 #9
0
def create_volume(volname, dist, rep=1, stripe=1, trans='tcp', servers='', \
        snap=True, disp=1, dispd=1, red=1):
    """
        Create the gluster volume specified configuration
        volname and distribute count are mandatory argument
    """
    if servers == '':
        servers = nodes_from_pool_list()
    if not servers:
        servers = tc.servers
    dist = int(dist)
    rep = int(rep)
    stripe = int(stripe)
    disp = int(disp)
    dispd = int(dispd)
    red = int(red)
    dispc = 1

    if disp != 1 and dispd != 1:
        tc.logger.error("volume can't have both disperse and disperse-data")
        return (-1, None, None)
    if disp != 1:
        dispc = int(disp)
    elif dispd != 1:
        dispc = int(dispd) + int(red)

    number_of_bricks = dist * rep * stripe * dispc
    replica = stripec = disperse = disperse_data = redundancy = ''
    brick_root = '/bricks'
    n = 0
    tempn = 0
    bricks_list = ''
    rc = tc.run(servers[0], "gluster volume info | egrep \"^Brick[0-9]+\"", \
            verbose=False)
    for i in range(0, number_of_bricks):
        if not snap:
            bricks_list = "%s %s:%s/%s_brick%d" % \
                (bricks_list, servers[n], brick_root, volname, i)
        else:
            sn = len(re.findall(servers[n], rc[1])) + tempn
            bricks_list = "%s %s:%s/brick%d/%s_brick%d" % \
            (bricks_list, servers[n], brick_root, sn, volname, i)
        if n < len(servers[:]) - 1:
            n = n + 1
        else:
            n = 0
            tempn = tempn + 1

    if rep != 1:
        replica = "replica %d" % rep
    if stripe != 1:
        stripec = "stripe %d" % stripe
    ttype = "transport %s" % trans
    if disp != 1:
        disperse = "disperse %d" % disp
        redundancy = "redundancy %d" % red
    elif dispd != 1:
        disperse_data = "disperse-data %d" % dispd
        redundancy = "redundancy %d" % red

    ret = tc.run(servers[0], "gluster volume create %s %s %s %s %s %s %s %s \
--mode=script"               % (volname, replica, stripec, disperse, disperse_data, \
redundancy, ttype, bricks_list))
    return ret
コード例 #10
0
def update_ganesha_ha_conf(no_of_servers=None):
    '''Updates the ganesha-ha.conf file, with VIPs and hostnames.
    Kwargs:
        no_of_servers (Optional[int]): The number of nodes on which we have
            to modify the ganesha-ha.conf file. Default it takes
            the number of servers from the pool list.
    Returns:
        bool: True if successfull, False otherwise.
    '''
    if no_of_servers is None:
        servers = nodes_from_pool_list()
        no_of_servers = len(servers)
    else:
        servers = tc.servers[0:no_of_servers]
    server_hostname_dict = get_host_by_name(servers)
    hostnames = server_hostname_dict.values()
    hosts = ','.join(hostnames)
    file_src_path = "/etc/ganesha/ganesha-ha.conf.sample"
    file_dest_path = "/etc/ganesha/ganesha-ha.conf"
    ha_server = tc.run(tc.servers[0], "hostname")
    conn = tc.get_connection(tc.servers[0], "root")
    if conn.modules.os.path.isfile(file_src_path) == True:
        tc.logger.info("%s file available and should be updated as "
                       "ganesha-ha.conf" % file_src_path)
        try:
            conn.modules.shutil.copy(file_src_path, file_dest_path)
            FH = conn.builtin.open(file_dest_path, "r+")
        except IOError as e:
            tc.logger.error(e)
            return False
    lines = FH.readlines()
    FH.seek(0)
    FH.truncate()
    for i in range(len(lines)):
        if re.search("HA_NAME", lines[i]) != None:
            lines[i] = re.sub(r'^HA_NAME.*',
                              "HA_NAME=\"G" + str(time.time()) + "\"",
                              lines[i])
        if re.search("HA_VOL_SERVER", lines[i]) != None:
            lines[i] = re.sub(r'^HA_VOL_SERVER.*',
                              "HA_VOL_SERVER=\"" + ha_server[1].strip() + "\"",
                              lines[i])
        if re.search("HA_CLUSTER_NODES", lines[i]) != None:
            lines[i] = re.sub(r'^HA_CLUSTER_NODES.*',
                              "HA_CLUSTER_NODES=\"" + hosts + "\"", lines[i])
        if re.search("VIP_", lines[i]) != None:
            lines[i] = re.sub(r'.*VIP_.*\n', "", lines[i])
    vips = (
        tc.global_config["gluster"]["cluster_config"]["nfs_ganesha"]["vips"])
    for i in range(no_of_servers):
        lines += "VIP_%s=\"%s\"\n" % (hostnames[i], vips[i])
    FH.write(''.join(lines))
    # create a local copy of this ha.conf file
    f = open("/tmp/ganesha-ha.conf", "w")
    f.write(''.join(lines))
    f.close()
    FH.close()
    conn.close()
    # copy this ha.conf file to all the other nodes
    for node in tc.servers[1:no_of_servers]:
        ret = tc.upload(node, "/tmp/ganesha-ha.conf", file_dest_path)

    return True
コード例 #11
0
def create_volume(volname, mnode=None, dist=1, rep=1, stripe=1, trans='tcp',
                  servers=None, disp=1, dispd=1, red=1):
    """Create the gluster volume specified configuration
       volname and distribute count are mandatory argument
    Args:
        volname(str): volume name that has to be created

    Kwargs:
        mnode(str): server on which command has to be execeuted,
            defaults to tc.servers[0]
        dist(int): distribute count, defaults to 1
        rep(int): replica count, defaults to 1
        stripe(int): stripe count, defaults to 1
        trans(str): transport type, defaults to tcp
        servers(list): servers on which volume has to be created,
            defaults to number of servers in pool list, if that is None,
            then takes tc.servers
        disp(int): disperse count, defaults to 1
        dispd(int): disperse-data count, defaults to 1
        red(int): rdundancy count, defaults to 1

    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            of command execution.

            The second element 'out' is of type 'str' and is the stdout value
            of the command execution.

            The third element 'err' is of type 'str' and is the stderr value
            of the command execution.

           (-1, '', ''): If not enough bricks are available to create volume.
           (ret, out, err): As returned by volume create command execution.

    Example:
        create_volume(volname)
    """
    if servers is None:
        servers = nodes_from_pool_list()
    if not servers:
        servers = tc.servers[:]
    if mnode is None:
        mnode = tc.servers[0]
    dist = int(dist)
    rep = int(rep)
    stripe = int(stripe)
    disp = int(disp)
    dispd = int(dispd)
    red = int(red)
    dispc = 1

    if disp != 1 and dispd != 1:
        tc.logger.error("volume can't have both disperse and disperse-data")
        return (-1, None, None)
    if disp != 1:
        dispc = int(disp)
    elif dispd != 1:
        dispc = int(dispd) + int(red)

    number_of_bricks = dist * rep * stripe * dispc
    replica = stripec = disperse = disperse_data = redundancy = ''

    from distaflibs.gluster.lib_utils import form_bricks_path
    bricks_path = form_bricks_path(number_of_bricks, servers[:],
                                   mnode, volname)
    if bricks_path is None:
        tc.logger.error("number of bricks required are greater than "
                        "unused bricks")
        return (-1, '', '')

    if rep != 1:
        replica = "replica %d" % rep
    if stripe != 1:
        stripec = "stripe %d" % stripe
    ttype = "transport %s" % trans
    if disp != 1:
        disperse = "disperse %d" % disp
        redundancy = "redundancy %d" % red
    elif dispd != 1:
        disperse_data = "disperse-data %d" % dispd
        redundancy = "redundancy %d" % red

    ret = tc.run(mnode, "gluster volume create %s %s %s %s %s %s %s %s "
                 "--mode=script" % (volname, replica, stripec, disperse,
                                    disperse_data, redundancy, ttype,
                                    bricks_path))

    return ret