summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/quota_ops.py
blob: 5a7f50fcbec823eecfc3482fb92ba6ef7fed74d2 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/usr/bin/env python
#  Copyright (C) 2015-2016  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: Library for gluster quota operations.
"""

from glusto.core import Glusto as g

try:
    import xml.etree.cElementTree as etree
except ImportError:
    import xml.etree.ElementTree as etree


def enable_quota(mnode, volname):
    """Enables quota on given volume

    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume 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:
        enable_quota("abc.xyz.com", testvol)
    """

    cmd = "gluster volume quota %s enable" % volname
    return g.run(mnode, cmd)


def disable_quota(mnode, volname):
    """Disables quota on given volume

    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume 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:
        disable_quota("abc.xyz.com", testvol)
    """

    cmd = "gluster volume quota %s disable --mode=script" % volname
    return g.run(mnode, cmd)


def is_quota_enabled(mnode, volname):
    """Checks if quota is enabled on given volume

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

    Returns:
        bool: True, if quota is enabled
            False, if quota is disabled

    Example:
        is_quota_enabled(mnode, testvol)
    """

    from glustolibs.gluster.volume_ops import get_volume_options

    output = get_volume_options(mnode, volname, "features.quota")
    if output is None:
        return False

    g.log.info("Quota Status in volume %s %s"
               % (volname, output["features.quota"]))
    if output["features.quota"] != 'on':
        return False

    return True


def quota_list(mnode, volname, path=None):
    """Executes quota list command for given volume

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

    Kwargs:
        path (str): Quota path

    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:
        quota_list(mnode, testvol)
    """

    if not path:
        path = ''

    cmd = "gluster volume quota %s list %s" % (volname, path)
    ret = g.run(mnode, cmd)
    return ret


def set_quota_limit_usage(mnode, volname, path='/', limit='100GB',
                          soft_limit=''):
    """Sets limit-usage on the path of the specified volume to
        specified limit

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

    Kwargs:
        path (str): path to which quota limit usage is set.
            Defaults to /.
        limit (str): quota limit usage. defaults to 100GB
        soft_limit (str): quota soft limit to be set

    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.

    Examples:
        >>> set_quota_limit_usage("abc.com", "testvol")

    """

    cmd = ("gluster volume quota %s limit-usage %s %s %s --mode=script"
           % (volname, path, limit, soft_limit))
    return g.run(mnode, cmd)


def get_quota_list(mnode, volname, path=None):
    """Parse the output of 'gluster quota list' command.

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

    Kwargs:
        path (str): Quota path

    Returns:
        NoneType: None if command execution fails, parse errors.
        dict: dict on success.

    Examples:
        >>> get_quota_list('abc.lab.eng.xyz.com', "testvol")
        {'/': {'used_space': '0', 'hl_exceeded': 'No', 'soft_limit_percent':
        '60%', 'avail_space': '2147483648', 'soft_limit_value': '1288490188',
        'sl_exceeded': 'No', 'hard_limit': '2147483648'}}
    """
    if not path:
        path = ''

    cmd = "gluster volume quota %s list %s --xml" % (volname, path)
    ret, out, _ = g.run(mnode, cmd)
    if ret != 0:
        g.log.error("Failed to execute 'quota list' on node %s. "
                    "Hence failed to get the quota list.", mnode)
        return None

    try:
        root = etree.XML(out)
    except etree.ParseError:
        g.log.error("Failed to parse the gluster quota list xml output.")
        return None

    quotalist = {}
    for path in root.findall("volQuota/limit"):
        for elem in path.getchildren():
            if elem.tag == "path":
                path = elem.text
                quotalist[path] = {}
            else:
                quotalist[path][elem.tag] = elem.text
    return quotalist


def set_quota_limit_objects(mnode, volname, path='/', limit='10',
                            soft_limit=''):
    """Sets limit-objects on the path of the specified volume to
        specified limit

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

    Kwargs:
        path (str): path to which quota limit usage is set.
            Defaults to /.
        limit (str): quota limit objects. defaults to 10.
        soft_limit (str): quota soft limit to be set

    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.

    Examples:
        >>> set_quota_limit_objects("abc.com", "testvol")

    """

    cmd = ("gluster volume quota %s limit-objects %s %s %s --mode=script"
           % (volname, path, limit, soft_limit))
    return g.run(mnode, cmd)


def quota_list_objects(mnode, volname, path=None):
    """Executes quota list command for given volume

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

    Kwargs:
        path (str): Quota path

    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:
        quota_list_objects("abc.com", testvol)

    """

    if not path:
        path = ''

    cmd = "gluster volume quota %s list-objects %s" % (volname, path)
    ret = g.run(mnode, cmd)
    return ret


def get_quota_list_objects(mnode, volname, path=None):
    """Parse the output of 'gluster quota list-objects' command.

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

    Kwargs:
        path (str): Quota path

    Returns:
        NoneType: None if command execution fails, parse errors.
        dict: dict of dict on success.

    Examples:
        >>> get_quota_list_objects('abc.lab.eng.xyz.com', "testvol")
        {'/': {'available': '7', 'hl_exceeded': 'No', 'soft_limit_percent':
        '80%', 'soft_limit_value': '8', 'dir_count': '3', 'sl_exceeded':
        'No', 'file_count': '0', 'hard_limit': '10'}}
    """

    if not path:
        path = ''

    cmd = "gluster volume quota %s list-objects %s --xml" % (volname, path)
    ret, out, _ = g.run(mnode, cmd)
    if ret != 0:
        g.log.error("Failed to execute 'quota list' on node %s. "
                    "Hence failed to get the quota list.", mnode)
        return None

    try:
        root = etree.XML(out)
    except etree.ParseError:
        g.log.error("Failed to parse the gluster quota list xml output.")
        return None

    quotalist = {}
    for path in root.findall("volQuota/limit"):
        for elem in path.getchildren():
            if elem.tag == "path":
                path = elem.text
                quotalist[path] = {}
            else:
                quotalist[path][elem.tag] = elem.text
    return quotalist


def set_quota_alert_time(mnode, volname, time):
    """Sets quota alert time

    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name
        time (str): quota alert time in seconds

    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.

    Examples:
        >>> set_quota_alert_time("abc.com", "testvol", <alert time>)

    """

    cmd = ("gluster volume quota %s alert-time %s --mode=script"
           % (volname, time))
    return g.run(mnode, cmd)


def set_quota_soft_timeout(mnode, volname, timeout):
    """Sets quota soft timeout

    Args:
        mnode (str): Node on which command has to be executed.
        volname (str): volume name
        timeout (str): quota soft limit timeout value

    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.

    Examples:
        >>> set_quota_soft_timeout("abc.com", "testvol", <timeout-value>)

    """

    cmd = ("gluster volume quota %s soft-timeout %s --mode=script"
           % (volname, timeout))
    return g.run(mnode, cmd)


def set_quota_hard_timeout(mnode, volname, timeout):
    """Sets quota hard timeout

    Args:
        mnode (str): Node on which command has to be executed.
        volname (str): volume name
        timeout (str): quota hard limit timeout value

    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.

    Examples:
        >>> set_quota_hard_timeout("abc.com", "testvol", <timeout-value>)

    """

    cmd = ("gluster volume quota %s hard-timeout %s --mode=script"
           % (volname, timeout))
    return g.run(mnode, cmd)


def set_quota_default_soft_limit(mnode, volname, timeout):
    """Sets quota default soft limit

    Args:
        mnode (str): Node on which command has to be executed.
        volname (str): volume name
        timeout (str): quota soft limit timeout value

    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.

    Examples:
        >>> set_quota_default_soft_limit("abc.com", "testvol",
                                         <timeout-value>)

    """

    cmd = ("gluster volume quota %s default-soft-limit %s --mode=script"
           % (volname, timeout))
    return g.run(mnode, cmd)


def remove_quota(mnode, volname, path):
    """Removes quota for the given path

    Args:
        mnode (str): Node on which command has to be executed.
        volname (str): volume name
        path (str): path to which quota limit usage is set.
            Defaults to /.

    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.

    Examples:
        >>> remove_quota("abc.com", "testvol", <path>)

    """

    cmd = "gluster volume quota %s remove %s --mode=script" % (volname, path)
    return g.run(mnode, cmd)


def remove_quota_objects(mnode, volname, path):
    """Removes quota objects for the given path

    Args:
        mnode (str): Node on which command has to be executed.
        volname (str): volume name
        path (str): path to which quota limit usage is set.
            Defaults to /.

    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.

    Examples:
        >>> remove_quota_objects("abc.com", "testvol", <path>)

    """

    cmd = ("gluster volume quota %s remove-objects %s --mode=script"
           % (volname, path))
    return g.run(mnode, cmd)