summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/snap_ops.py
blob: 0fba7771b9740a2e402b4866ad86011c41829433 (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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
#!/usr/bin/env python
#  Copyright (C) 2015-2020  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 snapshot operations.
"""

from glusto.core import Glusto as g
from glustolibs.gluster.volume_ops import volume_start, volume_stop

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


def snap_create(mnode, volname, snapname, timestamp=False,
                description='', force=False):
    """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
        force (bool): If this option is set to True, then snap
            create will get execute with force option. If it is set to False,
            then snap create 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
            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)

    """

    if description != '':
        description = "description '%s'" % description

    tstamp = ''
    if not timestamp:
        tstamp = "no-timestamp"

    frce = ''
    if force:
        frce = 'force'

    cmd = ("gluster snapshot create %s %s %s %s %s"
           % (snapname, volname, tstamp, description, frce))
    return g.run(mnode, cmd)


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)

    """
    cmd = "gluster snapshot clone %s %s --mode=script" % (clonename, snapname)
    return g.run(mnode, cmd)


def snap_restore(mnode, snapname):
    """Executes snap restore cli for the given snapshot

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

    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_restore(mnode, testsnap)

    """

    cmd = "gluster snapshot restore %s --mode=script" % snapname
    return g.run(mnode, cmd)


def snap_restore_complete(mnode, volname, snapname):
    """stops the volume restore the snapshot and starts the volume

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

    Returns:
        bool: True on success, False on failure

    Example:
        snap_restore_complete(mnode, testvol, testsnap)

    """

    # Stopping volume before snap restore
    ret = volume_stop(mnode, volname)
    if not ret:
        g.log.error("Failed to stop volume %s before restoring snapshot "
                    "%s in node %s" % (volname, snapname, mnode))
        return False
    ret, _, _ = snap_restore(mnode, snapname)
    if ret != 0:
        g.log.error("snapshot restore cli execution failed")
        return False

    # Starting volume after snap restore
    ret = volume_start(mnode, volname)
    if not ret:
        g.log.error("Failed to start volume %s after restoring snapshot "
                    "%s in node %s" % (volname, snapname, mnode))
        return False
    return True


def snap_status(mnode, snapname="", volname=""):
    """Runs 'gluster snapshot status' on specific node

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

    Kwargs:
        snapname (str): snapshot name
        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:
        snap_status("abc.com")

    """

    if snapname != "" and volname != "":
        g.log.error("Incorrect cmd. snap status cli accepts either "
                    "snapname or volname")
        return (-1, None, None)

    if volname != '':
        volname = "volume %s" % volname

    cmd = "gluster snapshot status %s %s" % (snapname, volname)
    return g.run(mnode, cmd)


def get_snap_status(mnode):
    """Parse the output of 'gluster snapshot status' command.

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

    Returns:
        NoneType: None if command execution fails, parse errors.
        list: list of dict on success. Each snap status will be
            in dict format

    Examples:
        >>> get_snap_status('abc.lab.eng.xyz.com')
        [{'volCount': '1', 'volume': {'brick': [{'path': '10.70.47.11:
        testvol_brick0', 'pid': '26747', 'lvUsage': '3.52', 'volumeGroup':
        'RHS_vg0', 'lvSize': '9.95g'}, {'path': '10.70.47.16:/testvol_brick1',
        'pid': '25497', 'lvUsage': '3.52', 'volumeGroup': 'RHS_vg0',
        'lvSize': '9.95g'}], 'brickCount': '2'}, 'name': 'snap2', 'uuid':
        '56a39a92-c339-47cc-a8b2-9e54bb2a6324'}, {'volCount': '1', 'volume':
        {'brick': [{'path': '10.70.47.11:testvol_next_brick0', 'pid': '26719',
        'lvUsage': '4.93', 'volumeGroup': 'RHS_vg1', 'lvSize': '9.95g'}],
        'brickCount': '1'}, 'name': 'next_snap1',
        'uuid': 'dcf0cd31-c0db-47ad-92ec-f72af2d7b385'}]
    """

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

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

    snap_status_list = []
    for snap in root.findall("snapStatus/snapshots/snapshot"):
        snap_status = {}
        for element in snap.getchildren():
            if element.tag == "volume":
                status = {}
                status["brick"] = []
                for elmt in element.getchildren():
                    if elmt.tag == "brick":
                        brick_info = {}
                        for el in elmt.getchildren():
                            brick_info[el.tag] = el.text
                        status["brick"].append(brick_info)
                    else:
                        status[elmt.tag] = elmt.text

                snap_status[element.tag] = status
            else:
                snap_status[element.tag] = element.text
        snap_status_list.append(snap_status)
    return snap_status_list


def get_snap_status_by_snapname(mnode, snapname):
    """Parse the output of 'gluster snapshot status' command
        for the given snapshot.

    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.

    Examples:
        >>> get_snap_status_by_snapname('abc.lab.eng.xyz.com',
                                        'snap1')
        {'volCount': '1', 'volume': {'brick': [{'path': '10.70.47.11:
        testvol_brick0', 'pid': '26747', 'lvUsage': '3.52', 'volumeGroup':
        'RHS_vg0', 'lvSize': '9.95g'}, {'path': '10.70.47.16:/testvol_brick1',
        'pid': '25497', 'lvUsage': '3.52', 'volumeGroup': 'RHS_vg0',
        'lvSize': '9.95g'}], 'brickCount': '2'}, 'name': 'snap2', 'uuid':
        '56a39a92-c339-47cc-a8b2-9e54bb2a6324'}
    """

    snap_status_list = get_snap_status(mnode)
    if not snap_status_list:
        g.log.error("Failed to parse snap status in "
                    "get_snap_status_by_snapname()")
        return None

    for snap_status in snap_status_list:
        if "name" in snap_status:
            if snap_status["name"] == snapname:
                return snap_status
    g.log.error("The snap %s not found" % (snapname))
    return None


def snap_status_by_volname(mnode, volname):
    """Parse the output of 'gluster snapshot status' command
        for the given volume.

    Args:
        mnode (str): Node on which command has to be executed.
        volname (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.
    """
    cmd = "gluster snapshot status volume %s" % volname
    return g.run(mnode, cmd)


def snap_info(mnode, snapname="", volname=""):
    """Runs 'gluster snapshot info' on specific node

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

    Kwargs:
        snapname (str): snapshot name
        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:
        snap_info("abc.com")

    """

    if snapname != "" and volname != "":
        g.log.error("Incorrect cmd. snap info cli accepts either "
                    "snapname or volname")
        return (-1, None, None)

    if volname != '':
        volname = "volume %s" % volname

    cmd = "gluster snapshot info %s %s" % (snapname, volname)
    return g.run(mnode, cmd)


def get_snap_info(mnode):
    """Parse the output of 'gluster snapshot info' command.

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

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

    Examples:
        >>> get_snap_info('abc.lab.eng.xyz.com')
        [{'description': 'This is snap2', 'uuid':
        '56a39a92-c339-47cc-a8b2-9e54bb2a6324', 'volCount': '1',
        'snapVolume': {'status': 'Stopped', 'name':
        'df1882d3f86d48738e69f298096f3810'}, 'createTime':
        '2016-04-07 12:01:21', 'name': 'snap2'}, {'description': None,
        'uuid': 'a322d93a-2732-447d-ab88-b943fa402fd2', 'volCount': '1',
        'snapVolume': {'status': 'Stopped', 'name':
        '2c790e6132e447e79168d9708d4abfe7'}, 'createTime':
        '2016-04-07 13:59:43', 'name': 'snap1'}]
    """

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

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

    snap_info_list = []
    for snap in root.findall("snapInfo/snapshots/snapshot"):
        snap_info = {}
        for element in snap.getchildren():
            if element.tag == "snapVolume":
                info = {}
                for elmt in element.getchildren():
                    if elmt.tag == "originVolume":
                        info["originVolume"] = {}
                        for el in elmt.getchildren():
                            info[elmt.tag][el.tag] = el.text
                    else:
                        info[elmt.tag] = elmt.text
                snap_info[element.tag] = info
            else:
                snap_info[element.tag] = element.text
        snap_info_list.append(snap_info)
    return snap_info_list


def get_snap_info_by_snapname(mnode, snapname):
    """Parse the output of 'gluster snapshot info' command
        for the given snapshot.

    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.

    Examples:
        >>> get_snap_info_by_snapname('abc.lab.eng.xyz.com', 'snap1')
        {'description': 'This is snap2', 'uuid':
        '56a39a92-c339-47cc-a8b2-9e54bb2a6324', 'volCount': '1',
        'snapVolume': {'status': 'Stopped', 'name':
        'df1882d3f86d48738e69f298096f3810'}
    """

    snap_info_list = get_snap_info(mnode)
    if not snap_info_list:
        g.log.error("Failed to parse snap info in "
                    "get_snap_info_by_snapname()")
        return None

    for snap_info in snap_info_list:
        if "name" in snap_info:
            if snap_info["name"] == snapname:
                return snap_info
    g.log.error("The snap %s not found" % (snapname))
    return None


def get_snap_info_by_volname(mnode, volname):
    """Parse the output of 'gluster snapshot info' command
        for the given volume.

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

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

    Examples:
        >>> get_snap_info_by_volname('abc.lab.eng.xyz.com',
                                     'testvol')
        {'originVolume': {'snapCount': '1', 'name': 'testvol',
        'snapRemaining': '255'}, 'count': '1', 'snapshots':
        [{'description': 'This is next snap1', 'uuid':
        'dcf0cd31-c0db-47ad-92ec-f72af2d7b385', 'volCount': '1',
        'snapVolume': {'status': 'Stopped', 'name':
        '49c290d6e8b74205adb3cce1206b5bc5'}, 'createTime':
        '2016-04-07 12:03:11', 'name': 'next_snap1'}]}
    """

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

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

    snap_vol_info = {}

    for snap in root.findall("snapInfo"):
        for element in snap.getchildren():
            if element.tag == "originVolume":
                info = {}
                for elmt in element.getchildren():
                    info[elmt.tag] = elmt.text
                snap_vol_info[element.tag] = info
            else:
                snap_vol_info[element.tag] = element.text

    snap_info_list = []
    for snap in root.findall("snapInfo/snapshots/snapshot"):
        snap_info = {}
        for element in snap.getchildren():
            if element.tag == "snapVolume":
                info = {}
                for elmt in element.getchildren():
                    if elmt.tag == "originVolume":
                        info["originVolume"] = {}
                        for el in elmt.getchildren():
                            info[elmt.tag][el.tag] = el.text
                    else:
                        info[elmt.tag] = elmt.text
                snap_info[element.tag] = info
            else:
                snap_info[element.tag] = element.text
        snap_info_list.append(snap_info)
    snap_vol_info["snapshots"] = snap_info_list
    return snap_vol_info


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.

    Example:
        snap_list("abc.com")

    """

    cmd = "gluster snapshot list"
    return g.run(mnode, cmd)


def get_snap_list(mnode, volname=""):
    """Parse the output of 'gluster snapshot list' command.
    If a volname is provided then the output will be specific
    to that volume.

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

    Kwargs:
        volname (str): volume name

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

    Examples:
        >>> get_snap_list('abc.lab.eng.xyz.com')
        ['snap1', 'snap2']
    """

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

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

    snap_list = []
    for snap in root.findall("snapList/snapshot"):
        snap_list.append(snap.text)

    return snap_list


def snap_config(mnode, volname=None):
    """Runs 'gluster snapshot config' on specific node

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

    Kwargs:
        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:
        snap_config(mnode)

    """

    if volname is None:
        volname = ""

    cmd = "gluster snapshot config %s" % volname
    return g.run(mnode, cmd)


def get_snap_config(mnode, volname=None):
    """Parse the output of 'gluster snapshot config' command.

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

    Kwargs:
        volname (str): volume name

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

    Examples:
        >>> get_snap_config('abc.com')
        {'volumeConfig': [{'softLimit': '230', 'effectiveHardLimit': '256',
        'name': 'testvol', 'hardLimit': '256'}, {'softLimit': '230',
        'effectiveHardLimit': '256', 'name': 'testvol_next',
        'hardLimit': '256'}], 'systemConfig': {'softLimit': '90%',
        'activateOnCreate': 'disable', 'hardLimit': '256',
        'autoDelete': 'disable'}}
    """

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

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

    snap_config = {}
    for config in root.findall("snapConfig/systemConfig"):
        sys_config = {}
        for element in config.getchildren():
            sys_config[element.tag] = element.text
    snap_config["systemConfig"] = sys_config

    volume_config = []
    for config in root.findall("snapConfig/volumeConfig/volume"):
        vol_config = {}
        for element in config.getchildren():
            vol_config[element.tag] = element.text

        if volname is not None:
            if volname == vol_config["name"]:
                volume_config.append(vol_config)
        else:
            volume_config.append(vol_config)

    snap_config["volumeConfig"] = volume_config
    return snap_config


def set_snap_config(mnode, option, volname=None):
    """Sets given snap config on the given node

    Args:
        mnode (str): Node on which cmd has to be executed.
        option (dict): dict of single snap config option

    Kwargs:
        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:
        >>>option={'snap-max-hard-limit':'200'}
        set_snap_config(mnode, option)

    """

    if volname is None:
        volname = ""

    cmd = ("gluster snapshot config %s %s %s --mode=script"
           % (volname, list(option.keys())[0], list(option.values())[0]))
    return g.run(mnode, cmd)


def snap_delete(mnode, snapname):
    """Deletes the given snapshot

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

    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_delete(mnode, testsnap)

    """

    cmd = "gluster snapshot delete %s --mode=script" % snapname
    return g.run(mnode, cmd)


def snap_delete_by_volumename(mnode, volname):
    """Deletes the given snapshot

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

    """

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


def snap_delete_all(mnode):
    """Deletes all the snapshot in the cluster

    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.

    Example:
        snap_delete_all("abc.com")

    """
    cmd = "gluster snapshot delete all --mode=script"
    return g.run(mnode, cmd)


def snap_activate(mnode, snapname, force=False):
    """Activates the given snapshot

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

    Kwargs:
        force (bool): If this option is set to True, then snap
            activate will get execute with force option. If it is set to False,
            then snap activate 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
            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_activate("abc.com", testsnap)

    """

    frce = ''
    if force:
        frce = 'force'

    cmd = "gluster snapshot activate %s %s --mode=script" % (snapname, frce)
    return g.run(mnode, cmd)


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 cloned

    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)

    """

    cmd = "gluster snapshot deactivate %s --mode=script" % snapname
    return g.run(mnode, cmd)


def terminate_snapd_on_node(mnode):
    """Terminate snapd on the specified node

    Args:
        mnode(str):node on which commands 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.
    """
    cmd = "ps aux| grep -m1 snapd | awk '{print $2}'"
    _, out, _ = g.run(mnode, cmd)
    if out is None:
        g.log.error("Failed to get the snapd PID using command %s", cmd)
        return None
    cmd = "kill -9 %s" % out
    return g.run(mnode, cmd)