summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/quota_libs.py
blob: 7462b69c39af5205af00e3b9ee770979e0268876 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#  Copyright (C) 2018  Red Hat, Inc. <http://www.redhat.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY :or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

""" Description: Module for gluster quota related helper functions. """


from glusto.core import Glusto as g
from glustolibs.gluster.quota_ops import (quota_fetch_list)


def quota_validate(mnode, volname, path, **kwargs):
    """ Validate if the hard limit, soft limit, usage match the expected values.
        If any of the arguments are None, they are not verified.

    Args:
        mnode (str)             : Node on which command has to be executed.
        volname (str)           : volume name.
        path (str)              : Path to be verified.
        kwargs
        hard_limit(int)         : hard limit is verified with this value.
        soft_limit_percent(int) : soft limit (in %) is verified with this value
        used_space(int)         : if set, usage as displayed in quota list is
                                  verified with expected value.
        avail_space(int)         : if set, usage as displayed in quota list is
                                  verified with expected value.
        sl_exceeded(bool)       : expected value of soft limit flag.
        hl_exceeded(bool)       : expected value of hard limit flag.

    """

    if kwargs is None:
        g.log.error("No arguments given for validation")
        return False

    quotalist = quota_fetch_list(mnode, volname, path)

    if path not in quotalist:
        g.log.error("Path not found (script issue) path: %s", path)
        return False
    else:
        listinfo = quotalist[path]

    ret = True
    for key, value in kwargs.items():
        if key and listinfo[key] != value:
            g.log.error("%s = %s does not match with expected value %s",
                        key, str(listinfo[key]), str(value))
            ret = False

    return ret


def quota_fetch_daemon_pid(nodes):
    """
    Checks if quota daemon process is running and
    return the process id's in dictionary format

    Args:
        nodes ( str|list ) : Node/Nodes of the cluster

    Returns:
        tuple : Tuple containing two elements (ret, quotad_pids).
        The first element 'ret' is of type 'bool', True if and only if
        quotad is running on all the nodes in the list and each
        node contains only one instance of quotad running.
        False otherwise.

        The second element 'quotad_pids' is of type dictonary and it
        contains the 'nodes' as the key and 'quotad PID' as the value.

        If there is NO quota daemon running on few nodes, the first element
        will be 'False' and the nodes which do not have a quota daemon running
        will have a value as '-1'.

        If there are MORE THAN ONE quota daemon for a node, the first element
        will be 'False' and the value for that node will be '-1'.

        Example:
            quota_fetch_daemon_pid(["node1", "node2"])
            (False, {'node2': ['8012'], 'node1': [-1]})

            Here 'node1' doesn't have quota daemon running. Hence, value
            of 'node1' is '-1'.
    """
    quotad_pids = {}
    _rc = True
    if not isinstance(nodes, list):
        nodes = [nodes]
    cmd = r"pgrep -f quotad | grep -v ^$$\$"
    g.log.info("Executing cmd: %s on node %s" % (cmd, nodes))
    results = g.run_parallel(nodes, cmd)
    for node in results:
        ret, out, err = results[node]
        if ret == 0:
            if len(out.strip().split("\n")) == 1:
                if not out.strip():
                    g.log.info("NO Quota daemon process found "
                               "on node %s" % node)
                    _rc = False
                    quotad_pids[node] = [-1]
                else:
                    g.log.info("Single Quota Daemon process with "
                               "pid %s found on %s",
                               out.strip().split("\n"), node)
                    quotad_pids[node] = (out.strip().split("\n"))
            else:
                g.log.info("More than One Quota daemon process "
                           "found on node %s" % node)
                _rc = False
                quotad_pids[node] = [-1]
        else:
            g.log.info("Not able to get Quota daemon process "
                       "from node %s" % node)
            _rc = False
            quotad_pids[node] = [-1]

    return _rc, quotad_pids