Пример #1
0
def get_volume_options(mnode, volname, option=None):
    """Gets the option values for the given volume.
    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name
    Kwargs:
        option (str): volume option to get status.
                    If not given, the function returns all the options for
                    the given volume
    Returns:
        dict: value for the given volume option in dict format, on success
        NoneType: on failure
    Example:
        get_volume_options(mnode, "testvol")
    """
    if not option:
        _, get_vol_options, err = RestClient(mnode).handle_request(
            "GET", "/v1/volumes/%s/options" % volname, httplib.OK, None)
    else:
        _, get_vol_options, err = RestClient(mnode).handle_request(
            "GET", "/v1/volumes/%s/options/%s" % (volname, option),
            httplib.OK, None)
    if not err:
        get_vol_options = json.loads(get_vol_options)
        return get_vol_options
    return None
Пример #2
0
def volume_start(mnode, volname, force=False):
    """Starts the gluster volume
    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name
    Kwargs:
        force (bool): If this option is set to True, then start volume
            will get executed with force option. If it is set to False,
            then start volume will get executed without force option
    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            The second element 'out' is of type 'str' and is the output of
            the operation
            The third element 'err|status' code on failure.
            Otherwise None.
    Example:
        volume_start("w.x.y.z", "testvol")
    """
    data = {
            "force-start-bricks": force
           }
    return RestClient(mnode).handle_request(
            "POST", "/v1/volumes/%s/start" % volname,
            httplib.OK, data)
Пример #3
0
def snap_deactivate(mnode, snapname):
    """Deactivates the given snapshot

    Args:
        mnode (str): Node on which cmd has to be executed.
        snapname (str): snapshot name to be deactivated

    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.

    Example:
        snap_deactivate("abc.com", testsnap)

    """
    return RestClient(mnode).handle_request('POST',
                                            "/v1/snapshots/%s/deactivate"
                                            % snapname, httplib.OK, None)
Пример #4
0
def snap_create(mnode, volname, snapname, timestamp=False, description=None):
    """Creates snapshot for the given volume.

    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name
        snapname (str): snapshot name

    Kwargs:
        timestamp (bool): If this option is set to True, then
            timestamps will get appended to the snapname. If this option
            is set to False, then timestamps will not be appended to snapname.
        description (str): description for snapshot creation

    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.

    Example:
        snap_create("abc.com", testvol, testsnap)

    """
    data = {"snapname": snapname, "volname": volname,
            "description": description, "timestamp": timestamp}
    return RestClient(mnode).handle_request("POST", "/v1/snapshots", httplib.CREATED, data)
Пример #5
0
def peer_detach(mnode, server):
    """ Detach the specified server.

    Args:
        mnode (str): Node on which command has to be executed.
        server (str): Server to be peer detached.

    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.
    """

    server_id = get_peer_id(mnode, server)
    ret, out, err = RestClient(mnode).handle_request(
        'DELETE', "/v1/peers/%s" % server_id, httplib.NO_CONTENT, None)
    if ret != httplib.NO_CONTENT:
        returncode = 1
        g.log.error("Failed to peer detach the node '%s'.", server)
    else:
        returncode = 0

    return (returncode, out, err)
Пример #6
0
def volume_reset(mnode, volname, force=False,
                 options=None, all_volumes=False):
    """Resets the gluster volume
    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name
    Kwargs:
        force (bool): If this option is set to True, then reset volume
            will get executed with force option. If it is set to False,
            then reset volume will get executed without force option.
        options (dict): volume options
        all_volumes (bool)
    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            The second element 'out' is of type 'str' and is the output of
            the operation
            The third element 'err|status' code on failure.
            Otherwise None.
    Example:
        volume_reset("w.x.y.z", "testvol")`
    """
    if not 'options':
        options = {}
    data = {
            "options": options,
            "force": force,
            "all": all_volumes,
            }
    return RestClient(mnode).handle_request(
            "DELETE", "/v1/volumes/%s/options" % volname,
            httplib.OK, data)
Пример #7
0
def snap_clone(mnode, snapname, clonename):
    """Clones the given snapshot

    Args:
        mnode (str): Node on which cmd has to be executed.
        snapname (str): snapshot name to be cloned
        clonename (str): clone name

    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.

    Example:
        snap_clone("abc.com", testsnap, clone1)

    """
    data = {"clonename": clonename}
    return RestClient(mnode).handle_request('POST', "/v1/snapshots/%s/clone"
                                            % snapname, httplib.CREATED, data)
Пример #8
0
def snap_info(mnode, snapname):
    """Gets the snap info by snapname

    Args:
        mnode (str): Node on which command has to be executed.
        snapname (str): snapshot name

    Returns:
        NoneType: None if command execution fails, parse errors.
        dict: on success.
    """
    return RestClient(mnode).handle_request('GET', "/v1/snapshots/%s"
                                            % snapname, httplib.OK, None)
Пример #9
0
def add_brick(mnode,
              volname,
              bricks_list,
              force=False,
              replica_count=0,
              arbiter_count=0):
    """Add Bricks specified in the bricks_list to the volume.
    Args:
        mnode (str): None on which the commands are executed.
        volname (str): Name of the volume
        bricks_list (list): List of bricks to be added
    Kwargs:
        force (bool): If this option is set to True, then add brick command
            will get executed with force option. If it is set to False,
            then add brick command will get executed without force option
        **kwargs
            The keys, values in kwargs are:
                - replica_count : (int)
                - arbiter_count : (int)
    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            The second element 'out' is of type 'str' and is the output of
            the operation
            The third element 'err' is of type 'str' and is the status
            error msg of operation else returns None.
    Example:
        add_brick(mnode, volname, bricks_list)
    """
    if len(bricks_list) <= 0:
        raise GlusterApiInvalidInputs("Bricks cannot be empty")

    req_bricks = validate_brick(bricks_list)
    if req_bricks is None:
        raise GlusterApiInvalidInputs("Invalid Brick details, bricks "
                                      "should be in form of "
                                      "<peerid>:<path>")
    # To create a brick dir
    create_brick_dir = {"create-brick-dir": True}

    data = {
        "ReplicaCount": replica_count,
        "Bricks": req_bricks,
        "Force": force,
        "Flags": create_brick_dir
    }

    return RestClient(mnode).handle_request("POST",
                                            "/v1/volumes/%s/expand" % volname,
                                            httplib.OK, data)
Пример #10
0
def volume_delete(mnode, volname, xfail=False):
    """Deletes the gluster volume if given volume exists in
       gluster and deletes the directories in the bricks
       associated with the given volume
    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name
    Kwargs:
        xfail (bool): expect to fail (non existent volume, etc.)
    Returns:
        bool: True, if volume is deleted
              False, otherwise
    Example:
        volume_delete("w.x.y.z", "testvol")
    """
    hosts = []
    paths = []
    volinfo = get_volume_info(mnode, volname, xfail)
    if not volinfo:
        if xfail:
            g.log.info(
                "Volume {} does not exist in {}"
                .format(volname, mnode)
            )
            return True
        else:
            g.log.error(
                "Unexpected: volume {} does not exist in {}"
                .format(volname, mnode))
            return False

    _, _, err = RestClient(mnode).handle_request(
            "DELETE", "/v1/volumes/%s" % volname,
            httplib.NO_CONTENT, None)
    if err:
        if xfail:
            g.log.info("Volume delete is expected to fail")
            return True

        g.log.error("Volume delete failed")
        return False

    # remove all brick directories
    for j in volinfo['subvols']:
        for i in j['bricks']:
            g.run(i['host'], "rm -rf %s" % i['path'])

    return True
Пример #11
0
def volume_list(mnode):
    """List the gluster volume
    Args:
        mnode (str): Node on which cmd has to be executed.
    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            The second element 'out' is of type 'str' and is the output of
            the operation
            The third element 'err|status' code on failure.
            Otherwise None.
    Example:
        volume_list("w.x.y.z")
    """
    return RestClient(mnode).handle_request(
            "GET", "/v1/volumes", httplib.OK, None)
Пример #12
0
def snap_list(mnode):
    """Lists the snapshots

    Args:
        mnode (str): Node on which cmd has to be executed.

    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.
    """
    return RestClient(mnode).handle_request('GET', "/v1/snapshots", httplib.OK, None)
Пример #13
0
def pool_list(mnode):
    """Runs 'gluster pool list' command on the specified node.

    Args:
        mnode (str): Node on which command has to be executed.

    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.
    """
    return RestClient(mnode).handle_request('GET', "/v1/peers", httplib.OK,
                                            None)
Пример #14
0
def volume_brick_status(mnode, volname):
    """Get gluster volume brick status
    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name
    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            The second element 'out' is of type 'str' and is the output of
            the operation
            The third element 'err|status' code on failure.
            Otherwise None.
    Example:
        volume_status("w.x.y.z","testvol")
    """
    return RestClient(mnode).handle_request(
            "GET", "/v1/volumes/%s/bricks" % volname,
            httplib.OK, None)
Пример #15
0
def snap_status(mnode, snapname):
    """Get the snap status by snapname

    Args:
        mnode (str): Node on which command has to be executed.
        snapname (str): snapshot name

    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.

    """
    return RestClient(mnode).handle_request('GET', "/v1/snapshots/%s/status"
                                            % snapname, httplib.OK, None)
Пример #16
0
def peer_probe(mnode, server):
    """Probe the specified server.

    Args:
        mnode (str): Node on which command has to be executed.
        server (str): Server to be peer probed.

    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 error message
            and error code of the the command execution.
    """

    data = {"addresses": [server]}
    return RestClient(mnode).handle_request('POST', "/v1/peers",
                                            httplib.CREATED, data)
Пример #17
0
def set_volume_options(mnode, volname, options,
                       advance=True, experimental=False,
                       deprecated=False):
    """Sets the option values for the given volume.
    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name
        options (dict): volume options in key
            value format
    Kwargs:
        advance (bool): advance flag to set options. Default set True
        experimental (bool): experimental flag to set options.
                             Default set False.
        deprecated (bool): deprecated flag to set options.
                           Default set False
    Returns:
        bool: True, if the volume option is set
              False, on failure
    Example:
        set_volume_option("w.x.y.z", "testvol", options)
    """
    if not options:
        raise GlusterApiInvalidInputs("cannot set empty options")

    vol_options = {}
    req = {}
    for key in options:
        vol_options[key] = options[key]
    req['options'] = vol_options
    req['allow-advanced-options'] = advance
    req['allow-experimental-options'] = experimental
    req['allow-deprecated-options'] = deprecated
    _, _, err = RestClient(mnode).handle_request(
        "POST", "/v1/volumes/%s/options" % volname,
        httplib.CREATED, req)
    if err:
        return True
    return False
Пример #18
0
def peer_edit(mnode, peerid, zone):
    """ Edits the peer zone
_
    Args:
        mnode (str): Node on which command has to be executed.
        peerid (str): The peerid of the peer.
        Zone (str): The zone details that has to be edited.

    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.
     """

    data = {"metadata": {"zone": zone}}
    return RestClient(mnode).handle_request("POST", "/v1/peers/%s" % peerid,
                                            httplib.CREATED, data)
Пример #19
0
def peer_status(mnode, peer=None):
    """ Fetches the peer status

    Args:
        mnode (str): Node on which command has to be executed.

    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.
     """

    path = "/v1/peers"
    if peer:
        peerid = get_peer_id(mnode, peer)
        path = "%s/%s" % (path, peerid)
    return RestClient(mnode).handle_request('GET', path, httplib.OK, None)
Пример #20
0
def volume_create(mnode, volname, bricks_list, force=False, replica_count=0,
                  arbiter_count=0, transport_type="tcp",
                  options=None, metadata=None):
    """Create the gluster volume with specified configuration
    Args:
        mnode(str): server on which command has to be executed
        volname(str): volume name that has to be created
        bricks_list (list): List of bricks to use for creating volume.
            Example:
                from glustolibs.gluster.lib_utils import form_bricks_list
                bricks_list = form_bricks_list(mnode, volname, num_of_bricks,
                                               servers, servers_info)
        Kwargs:
            force (bool): If this option is set to True, then create volume
                will get executed with force option. If it is set to False,
                then create volume will get executed without force option
            replica_count (int): if volume is replicated type
            arbiter_count (int):if volume is arbiter type
            transport_type : tcp, rdma
            options (dict): volume options
            metadata (dict): volume metadata
    Returns:
        tuple: Tuple containing three elements (ret, out, err).
            The first element 'ret' is of type 'int' and is the return value
            The second element 'out' is of type 'str' and is the output of
            the operation
            The third element 'err|status' code on failure.
            Otherwise None.
           (-1, '', ''): If not enough bricks are available to create volume.
           (ret, out, err): As returned by volume create command execution.
    Example:
        volume_create(mnode, volname, bricks_list)
    """

    if len(bricks_list) <= 0:
            raise GlusterApiInvalidInputs("Bricks cannot be empty")

    req_bricks = validate_brick(bricks_list)
    if not req_bricks:
        raise GlusterApiInvalidInputs("Invalid Brick details, bricks "
                                      "should be in form of "
                                      "<peerid>:<path>")

    if transport_type not in ("tcp", "rdma", "tcp,rdma"):
            raise GlusterApiInvalidInputs("Transport type %s not "
                                          "supported" % transport_type)

    if not options:
        options = {}

    if not metadata:
        metadata = {}

    num_bricks = len(bricks_list)
    sub_volume = []

    if replica_count > 0:
        replica = arbiter_count + replica_count

        if num_bricks % replica != 0:
            raise GlusterApiInvalidInputs(
                    "Invalid number of bricks specified")

        num_subvol = num_bricks / replica
        for i in range(0, num_subvol):
            idx = i * replica
            ida = i * replica + 2
            # If Arbiter is set, set it as Brick Type for 3rd th brick
            if arbiter_count > 0:
                req_bricks[ida]['type'] = 'arbiter'
            subvol_req = {}
            subvol_req['type'] = 'replicate'
            subvol_req['bricks'] = req_bricks[idx:idx + replica]
            subvol_req['replica'] = replica_count
            subvol_req['arbiter'] = arbiter_count
            sub_volume.append(subvol_req)
    else:
        subvol_req = {}
        subvol_req['type'] = 'distrubute'
        subvol_req['bricks'] = req_bricks
        sub_volume.append(subvol_req)

    # To create a brick dir
    create_brick_dir = {"create-brick-dir": True}

    data = {
            "name": volname,
            "subvols": sub_volume,
            "transport": transport_type,
            "options": options,
            "force": force,
            "metadata": metadata,
            "Flags": create_brick_dir
            }

    return RestClient(mnode).handle_request(
            "POST", "/v1/volumes", httplib.CREATED, data)