summaryrefslogtreecommitdiffstats
path: root/libs/utils/glusterutils.py
blob: 8264f3ef6d64331bd09833a921a112ac7db63df5 (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
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
"""glusterutils module contains wrappers for gluster commands.
*) glusterd_start
*) glusterd_start_allservers
*) glusterd_stop
*) glusterd_stop_allservers
*) glusterd_restart
*) glusterd_remove_dir
*) glusterd_remove_dir_allservers
*) glusterd_remove_logs_allservers
*) volume_delete
*) volume_create
*) volume_start
*) volume_stop
*) volume_addbrick
*) volume_replacebrick
*) volume_set
*) volume_reset
*) peer_probe
*) create_brick
*) mount_exportdir
*) umount_exportdir
"""

import re
import os
import atfutils
import hostutils
from atfglobals import GlobalObj

def glusterd_start(serverkey, force=False):
    """
    """
    logger = GlobalObj.getLoggerObj()
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    commands_to_execute = ["which glusterd", "ps -e | grep glusterd"]
    gluster_version = env.getServer(serverkey).glusterversion
    host_connection = cm.getConnection(serverkey)
    if not host_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    """ Check if gluster is already running. If already Running and force=True,
    restart glusterd process"""
    command = commands_to_execute.pop()
    output = host_connection.executecommand(command)
    return_status = atfutils.assert_success(output['exitstatus'])
    if not return_status:
        if force:
            return_status = glusterd_restart(serverkey)
            return return_status
        else:
            return return_status

    command = commands_to_execute.pop()
    output = host_connection.executecommand(command)
    if output["exitstatus"]:
        logger.error("Unable to start glusterd")
        return_status = atfutils.assert_success(output['exitstatus'])
        return return_status
    else:
        if output["stdoutdata"]:
            gluster_path = None
            gluster_path = output["stdoutdata"][0].strip("\n")
        else:
            logger.error("Unable to find gluster path")
            return_status = atfutils.assert_success(output['exitstatus'])
            return return_status

        if gluster_path:
            command = gluster_path + " --version"
            output = host_connection.executecommand(command)
            if not output["stdoutdata"] == None:
                if re.search(gluster_version, str(output["stdoutdata"])):
                    logger.debug('%s: Executing Command: %s'
                                 %(serverkey, gluster_path))
                    output = host_connection.executecommand(gluster_path)
                    return_status = atfutils.assert_success(output['exitstatus'])
                    atfutils.print_stdout(output['stdoutdata'])
                    atfutils.print_stderr(output['stderrdata'])
                    return return_status
                else:
                    logger.error("Unable to start glusterd")
                    return 1
        else:
            logger.error("Unable to start glusterd")
            return 1

def glusterd_start_allservers(force=False):
    """
    """
    env = GlobalObj.getTestenvObj()
    all_servers = env.getServers()
    for serverkey in all_servers.keys():
        return_status = glusterd_start(serverkey)
        if return_status:
            return return_status

    return 0

def glusterd_stop(serverkey):
    """
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "kill -KILL "
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    host_connection = cm.getConnection(serverkey)
    if not host_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    gluster_pid_list = []
    output = host_connection.executecommand("pidof glusterd")
    return_status = atfutils.assert_failure(output['exitstatus'])
    if not return_status:
        return return_status

    else:
        if output["stdoutdata"]:
            for output in output["stdoutdata"]:
                gluster_pid_list.append(output)

            for pid in gluster_pid_list:
                command = base_command + pid
                logger.debug('%s: Executing Command: %s' % (serverkey, command))
                output = host_connection.executecommand(command)
                return_status = atfutils.assert_success(output['exitstatus'])
                atfutils.print_stdout(output['stdoutdata'])
                atfutils.print_stderr(output['stderrdata'])
                if return_status:
                    break

        return return_status

def glusterd_stop_allservers():
    """
    """
    env = GlobalObj.getTestenvObj()
    all_servers = env.getServers()
    for serverkey in all_servers.keys():
        return_status = glusterd_stop(serverkey)
        if return_status:
            return return_status

    return 0

def glusterd_restart(serverkey):
    """
    """
    return_status = glusterd_stop(serverkey)
    if return_status:
        return return_status
    else:
        return_status = glusterd_start(serverkey)
        return return_status

def glusterd_remove_dir(serverkey):
    """
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "rm -rf"
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    glusterd_dir = GlobalObj.glusterd_dir

    server_obj = env.getServer(serverkey)
    if not server_obj:
        logger.error("Invalid Host. %s not defined in TestEnvironment"
                     % serverkey)
        return 1

    server_connection = cm.getConnection(serverkey)
    if not server_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    command = ' '.join([base_command, glusterd_dir])
    logger.debug('%s: Executing Command: %s' % (serverkey, command))
    output = server_connection.executecommand(command)
    return_status = atfutils.assert_success(output['exitstatus'])
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return 0

def glusterd_remove_dir_allservers():
    """
    """
    env = GlobalObj.getTestenvObj()
    all_servers = env.getServers()
    for serverkey in all_servers.keys():
        return_status = glusterd_remove_dir(serverkey)

    return 0


def glusterd_remove_logs(serverkey):
    """
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "rm -rf"
    log_paths = GlobalObj.glusterd_log_paths
    absolute_path_list = []
    prefix_path = ''
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()

    server_obj = env.getServer(serverkey)
    if not server_obj:
        logger.error("Invalid Host. %s not defined in TestEnvironment"
                     % serverkey)
        return 1

    server_connection = cm.getConnection(serverkey)
    if not server_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    if server_obj.installpath:
        prefix_path = server_obj.installpath

    for path in log_paths:
        absolute_path_list.append(prefix_path + path)

    for path in absolute_path_list:
        command = ' '.join([base_command, path])
        logger.debug('%s: Executing Command: %s' % (serverkey, command))
        output = server_connection.executecommand(command)
        return_status = atfutils.assert_success(output['exitstatus'])
        atfutils.print_stdout(output['stdoutdata'])
        atfutils.print_stderr(output['stderrdata'])

    return 0

def glusterd_remove_logs_allservers():
    """
    """
    env = GlobalObj.getTestenvObj()
    all_servers = env.getServers()
    for serverkey in all_servers.keys():
        return_status = glusterd_remove_logs(serverkey)

    return 0

def volume_delete(serverkey):
    """
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "gluster volume delete "
    command = [base_command]
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    active_volume = env.getActiveVolume()
    if not active_volume:
        logger.error("Invalid Volume.ActiveVolume not defined" +
                     "for the TestEnvironment")
        return 1
    command.extend([active_volume.volumename])
    command = ' '.join(command)
    host_connection = cm.getConnection(serverkey)
    if not host_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    logger.debug('%s: Executing Command: %s' % (serverkey, command))
    output = host_connection.executecommand(command, commandInput="y\n")
    return_status = atfutils.assert_success(output['exitstatus'])
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def volume_create(serverkey):
    """
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "gluster volume create"
    command = [base_command]
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    active_volume = env.getActiveVolume()
    if not active_volume:
        logger.error("ActiveVolume not defined for the TestEnvironment")
        return 1
    command.extend([active_volume.volumename])

    if active_volume.replica:
        command.extend(["replica", active_volume.replica])

    if active_volume.stripe:
        command.extend(["stripe", active_volume.stripe])

    if active_volume.transporttype:
        command.extend(["transport", active_volume.transporttype])

    command = ' '.join(command)
    for brick_obj in active_volume.bricks:
        brick_value = brick_obj.hostname + ":" + brick_obj.path
        command = ' '.join([command, brick_value])

    host_connection = cm.getConnection(serverkey)
    if not host_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    logger.debug('%s: Executing Command: %s' % (serverkey, command))
    output = host_connection.executecommand(command, commandInput="y\n")
    return_status = atfutils.assert_success(output['exitstatus'])
    if return_status:
        if str(output["stdoutdata"]).rfind("already exists"):
            return_status = 0
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def volume_start(serverkey, force=False):
    """
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "gluster volume start"
    command = [base_command]
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    active_volume = env.getActiveVolume()
    if not active_volume:
        logger.error("ActiveVolume not defined for the TestEnvironment")
        return 1
    command.extend([active_volume.volumename])

    if force:
        command.extend(["force"])

    command=' '.join(command)
    host_connection = cm.getConnection(serverkey)
    if not host_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    logger.debug('%s: Executing Command: %s' % (serverkey, command))
    output = host_connection.executecommand(command, commandInput="y\n")
    return_status = atfutils.assert_success(output['exitstatus'])
    if return_status:
        if str(output["stdoutdata"]).rfind("already started"):
            return_status = 0
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def volume_stop(serverkey, force=False):
    """
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "gluster volume stop"
    command = [base_command]
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    active_volume = env.getActiveVolume()
    if not active_volume:
        logger.error("ActiveVolume not defined for the TestEnvironment")
        return 1
    command.extend([active_volume.volumename])

    if force:
        command.extend(["force"])

    command = ' '.join(command)
    host_connection = cm.getConnection(serverkey)
    if not host_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    logger.debug('%s: Executing Command: %s' % (serverkey, command))
    output = host_connection.executecommand(command, commandInput="y\n")
    return_status = atfutils.assert_success(output['exitstatus'])
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def volume_addbrick(serverkey, *bricks, **arguments):
    """
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "gluster volume add-brick"
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    active_volume = env.getActiveVolume()
    command = [base_command]

    if not active_volume:
        logger.error("ActiveVolume not defined for the TestEnvironment")
        return 1
    volumename = active_volume.volumename

    command.extend([volumename])

    """
    arguments can have key brick_type
    brick_type=[<stripe|replica> <COUNT>]
    """

    if arguments.has_key('brick_type'):
        command.extend([arguments['brick_type']])

    for brick in bricks:
        brick_obj = env.getBrick(brick)
        if not brick_obj:
            logger.error("Invalid Brick. Brick Not defined in TestEnvironment")
            return 1
        brick_value = brick_obj.hostname + ":" + brick_obj.path
        command.extend([brick_value])

    command = ' '.join(command)
    host_connection = cm.getConnection(serverkey)
    if not host_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    logger.debug('%s: Executing Command: %s' % (serverkey, command))
    output = host_connection.executecommand(command, commandInput="y\n")
    return_status = atfutils.assert_success(output['exitstatus'])
    if not return_status:
        if env.addBricksToVolume(*bricks):
            return 1
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def volume_removebrick(serverkey, *bricks, **arguments):
    """
    *bricks : list of bricks to be removed
    **arguments(optional): It takes key:value pair
                we are using optional keys
                brick_type and operation
    brick_type: [replica <COUNT>]
    operation:{start|pause|abort|status|commit|force}
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "gluster volume remove-brick"
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    active_volume = env.getActiveVolume()
    command = [base_command]

    if not active_volume:
        logger.error("ActiveVolume not defined in the TestEnvironment")
        return 1
    command.extend([active_volume.volumename])
    """
    brick_type can have only [replica <COUNT>]
    """
    if arguments.has_key('brick_type'):
        command.extend([arguments['brick_type']])

    for brick in bricks:
        brick_obj = env.getBrick(brick)
        if not brick_obj:
            logger.error("Invalid Brick. Brick not defined in TestEnviroment")
            return 1
        brick_value = brick_obj.hostname +':'+ brick_obj.path
        command.extend([brick_value])
    """
    operation can have {start|pause|abort|status|commit|force}
    which is optional.
    """

    if arguments.has_key('operation'):
        command.extend([arguments['operation']])

    command = ' '.join(command)
    host_connection = cm.getConnection(serverkey)
    if not host_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    logger.debug('%s:Executing Command: %s'% (serverkey, command))
    output = host_connection.executecommand(command, commandInput="y\n")
    return_status = atfutils.assert_success(output['exitstatus'])
    if not return_status:
        if env.removeBricksFromVolume(*bricks):
            return 1
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def volume_replacebrick(serverkey, replace_brick, to_brick, operation):
    """
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "gluster volume replace-brick"
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    command = [base_command]

    active_volume = env.getActiveVolume()
    if not active_volume:
        logger.error("ActiveVolume not defined for the TestEnvironment")
        return 1
    volumename = active_volume.volumename
    replace_brick_obj = env.getBrick(replace_brick)
    to_brick_obj = env.getBrick(to_brick)
    """
    checking if objects are none
    """
    if not (replace_brick_obj and to_brick_obj):
        logger.error("Invalid Brick. Brick Not defined in TestEnvironment")
        return 1

    replace_brick_value = replace_brick_obj.hostname+':'+replace_brick_obj.path
    to_brick_value = to_brick_obj.hostname+':'+to_brick_obj.path


    command.extend([volumename, replace_brick_value, to_brick_value, operation])
    command = ' '.join(command)
    host_connection = cm.getConnection(serverkey)
    if not host_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    logger.debug('%s: Executing Command: %s' % (serverkey, command))
    output = host_connection.executecommand(command, commandInput="y\n")
    return_status = atfutils.assert_success(output['exitstatus'])
    if (not return_status) and (operation == "commit"):
        if env.replaceBrickInVolume(brick, newbrick):
            return 1
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def volume_set(serverkey, key, value):
    """
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "gluster volume set"
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    command = [base_command]

    active_volume = env.getActiveVolume()
    if not active_volume:
        logger.error("ActiveVolume not defined for the TestEnvironment")
        return 1
    volumename = active_volume.volumename

    command.extend([volumename, key, value])
    command = ' '.join(command)

    host_connection = cm.getConnection(serverkey)
    if not host_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    logger.debug('%s: Executing Command: %s' % (serverkey, command))
    output = host_connection.executecommand(command, commandInput="y\n")
    return_status = atfutils.assert_success(output['exitstatus'])
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def volume_log_rotate(serverkey, brick):
    """
    brick is compulsory parameter
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "gluster volume log rotate"
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    command = [base_command]

    active_volume = env.getActiveVolume()
    if not active_volume:
        logger.error("ActiveVolume not defined for the Testenvironment")
        return 1
    volumename = active_volume.volumename

    brick_obj = env.getBrick(brick)
    if not brick_obj:
        logger.error("Invalid Brick. Brick Not defined in TestEnvironment")
        return 1

    brick_value = brick_obj.hostname + ":" + brick_obj.path

    command.extend([volumename, brick_value])

    command = ' '.join(command)

    host_connection = cm.getConnection(serverkey)
    if not host_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    logger.debug('%s: Executing Command: %s' %(serverkey, command))
    output = host_connection.executecommand(command)
    return_status = atfutils.assert_success(output['exitstatus'])
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def volume_reset(serverkey):
    """
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "gluster volume reset "
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    active_volume = env.getActiveVolume()
    if not active_volume:
        logger.error("ActiveVolume not defined for the TestEnvironment")
        return 1
    volumename = active_volume.volumename
    command = base_command + volumename
    host_connection = cm.getConnection(serverkey)
    if not host_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    logger.debug('%s: Executing Command: %s' % (serverkey, command))
    output = host_connection.executecommand(command, commandInput="y\n")
    return_status = atfutils.assert_success(output['exitstatus'])
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def volume_profile(serverkey, operation):
    """
    operation:{start|info|stop}
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "gluster volume profile"
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    command = [base_command]

    active_volume = env.getActiveVolume()
    if not active_volume:
        logger.error("ActiveVolume not defined for the TestEnvironment")
        return 1
    volumename = active_volume.volumename

    command.extend([volumename, operation])
    command = ' '.join(command)

    host_connection = cm.getConnection(serverkey)
    if not host_connection:
        logger.error("SSH connection to the Host '%s' has not been established"
                     & serverkey)
        return 1

    logger.debug('%s: Execute Command: %s' %(serverkey, command))
    output = host_connection.executecommand(command)
    return_status = atfutils.assert_success(output['exitstatus'])
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def volume_quota(serverkey, operation, **arguments):
    """
    arguments can have two values
    path: path can be '/'
        ex: path='/'
    value: value can be in GB or MB
        ex: value=1GB
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "gluster volume quota"
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    command = [base_command]
    active_volume = env.getActiveVolume()
    if not active_volume:
        logger.error("ActiveVolume not defined for the Testenvironment")
        return 1
    volumename = active_volume.volumename

    command.extend([volumename, operation])

    if arguments.has_key('path'):
        command.extend([arguments['path']])

    if arguments.has_key('value'):
        command.extend([arguments['value']])

    command = ' '.join(command)

    host_connection = cm.getConnection(serverkey)
    if not host_connection:
        logger.error("SSH connection to the host '%s' has not been established"
                     % serverkey)
        return 1

    logger.debug('%s: Executing Command: %s' % (serverkey, command))
    output = host_connection.executecommand(command)
    return_status = atfutils.assert_success(output['exitstatus'])
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def volume_top(serverkey, operation, **arguments):
    """
    operation:{[open|read|write|opendir|readdir] |[read-perf|write-perf
                bs <size> count <count>]}
    arguments(optional): Takes maximum two parameters
    brick : brick1, brick2 etc
    list-cnt: can take any number

    """
    logger = GlobalObj.getLoggerObj()
    base_command = "gluster volume top"
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    command = [base_command]

    active_volume = env.getActiveVolume()
    if not active_volume:
        logger.error("ActiveVolume not defines for the TestEnvironment")
        return 1
    volumename = active_volume.volumename

    command.extend([volumename, operation])

    if arguments.has_key('brick'):
        brick_obj = env.getBrick(arguments['brick'])
        if not brick_obj:
            logger.error("Invalid Brick. Brick Not defined in TestEnvironment")
            return 1
        brick_value = brick_obj.hostname+':'+brick_obj.path
        command.extend(['brick',brick_value])

    if arguments.has_key('list_cnt'):
        command.extend(['list-cnt', arguments['list_cnt']])

    command = ' '.join(command)

    host_connection = cm.getConnection(serverkey)
    if not host_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     %serverkey)
        return 1

    logger.debug("%s: Executing command: %s" %(serverkey, command))
    output = host_connection.executecommand(command)
    return_status = atfutils.assert_success(output['exitstatus'])
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def volume_stop_brick(brickkey, signal="SIGTERM"):
    """
    Description: Stop a brick
    Arguments: brickkey. (The brick to be stopped. Ex:- "brick1")
    """
    output = {}
    output['exitstatus'] = None
    output['stdoutdata'] = None
    output['stderrdata'] = None

    base_command = "kill -s %s $(cat %s)"
    glusterd_vol_dir = "/etc/glusterd/vols"
    brick_pid_dir = "run"
    pid_file_extension = ".pid"

    logger = GlobalObj.getLoggerObj()
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()

    """Get the active volume
    """
    active_volume = atfutils.get_active_volume()
    if active_volume is None:
        output['exitstatus'] = 1
        return output
    volumename = active_volume.volumename

    """ Get the serverkey on which it has to be executed
    """
    raw_brick_obj = env.getRawBrick(brickkey)
    if not raw_brick_obj:
        logger.error("InValid Brick. %s not defined in TestEnvironment"
                     % brickkey)
        output['exitstatus'] = 1
        return output
    serverkey = re.split("\.", raw_brick_obj.hostname, maxsplit=1)[0]
    server_connection = cm.getConnection(serverkey)
    if not server_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        output['exitstatus'] = 1
        return output

    """ Get the brickobj
    """
    brick_obj = env.getBrick(brickkey)
    if not brick_obj:
        logger.error("Invalid Brick. Brick Not defined in TestEnvironment")
        output['exitstatus'] = 1
        return output

    """ Absolute path for the file containing the pid of brick process
    """
    hostname = brick_obj.hostname
    path = brick_obj.path.replace("/", "-")
    pid_filename = hostname + path + pid_file_extension
    pid_file_abspath = os.path.join(glusterd_vol_dir, volumename,
                            brick_pid_dir, pid_filename)

    command = base_command % (signal, pid_file_abspath)
    output = server_connection.executecommand(command, commandInput="y\n")
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return output

def peer_probe(fromserverkey):
    """
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "gluster peer probe "
    all_servers = {}
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    all_servers = env.getServers()
    all_servers.pop(fromserverkey)
    host_connection = cm.getConnection(fromserverkey)
    if not host_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    for key in all_servers.keys():
        if not key is fromserverkey:
            server_obj = all_servers[key]
            """
            One hostname is being taken at a time while executing peer probe
            """
            command = ' '.join([base_command, server_obj.hostname])
            logger.debug('%s: Executing Command: %s' % (fromserverkey, command))
            output = host_connection.executecommand(command)
            return_status = atfutils.assert_success(output['exitstatus'])
            atfutils.print_stdout(output['stdoutdata'])
            atfutils.print_stderr(output['stderrdata'])

            if return_status:
                return 1

    return 0

def create_brick(brickkey):
    """
    """
    logger = GlobalObj.getLoggerObj()
    return_status = 1
    env = GlobalObj.getTestenvObj()
    brick_obj = env.getRawBrick(brickkey)

    hostname_value = brick_obj.hostname
    serverkey = re.split("\.", hostname_value, maxsplit=1)[0]

    exportdir = brick_obj.path
    device = fstype = None
    """If the exportdir is not a mount point of a device:
    1) Remove the existing exportdir
    2) Create new exportdir"""

    if re.match("^\/", exportdir):
        dirpath = exportdir
    else:
        export_obj = env.getExportdir(exportdir)
        dirpath = export_obj.dir
        device = export_obj.device
        fstype = export_obj.fstype
        options = export_obj.options

    logger.debug('%s: Executing Command: %s'% (serverkey, 'create_brick'))
    if device:
        if umount_device(serverkey, device):
            return return_status
        if hostutils.mkfs(serverkey, device, fstype):
            return return_status
        if mount_exportdir(serverkey, device, fstype, options, dirpath):
            return return_status
        return 0

    else:
        if hostutils.rmdir(serverkey, dirpath):
            return return_status
        if hostutils.mkdir(serverkey, dirpath):
            return return_status

        return 0

def umount_device(serverkey, device):
    """
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "umount "
    cm = GlobalObj.getConnectionsManagerObj()

    server_connection = cm.getConnection(serverkey)
    if not server_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    mountpoints = hostutils.find_mountpoints(serverkey, device)

    for mountpoint in mountpoints:
        command = base_command + mountpoint
        logger.debug('%s: Executing Command: %s' % (serverkey, command))
        output = server_connection.executecommand(command)
        return_status = atfutils.assert_success(output['exitstatus'])
        atfutils.print_stdout(output['stdoutdata'])
        atfutils.print_stderr(output['stderrdata'])
        if return_status:
            stdoutdata = str(output["stdoutdata"])
            if ((stdoutdata.rfind("not found")) or (stdoutdata.rfind("not mount"))):
                return_status = 0

            else:
                return return_status

    return 0

def mount_exportdir(serverkey, device, fstype, options, dirpath):
    """
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "mount "
    cm = GlobalObj.getConnectionsManagerObj()
    command = [base_command]

    server_connection = cm.getConnection(serverkey)
    if not server_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % serverkey)
        return 1

    if fstype is None:
        fstype = "xfs"
    command.extend(["-t", fstype])

    if options:
        command.extend([options])

    command.extend([device, dirpath])
    command = ' '.join(command)

    logger.debug('%s: Executing Command: %s' % (serverkey, command))
    output = server_connection.executecommand(command)
    return_status = atfutils.assert_success(output['exitstatus'])
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def create_brick_allservers():
    """
    """
    env = GlobalObj.getTestenvObj()
    brick_keys = env.getBrickKeys()
    for brickkey in brick_keys:
        return_status = create_brick(brickkey)
        if return_status:
            return return_status

    return 0


__all__ = ['glusterd_start',
           'glusterd_start_allservers',
           'glusterd_stop',
           'glusterd_stop_allservers',
           'glusterd_restart',
           'glusterd_remove_dir',
           'glusterd_remove_dir_allservers',
           'glusterd_remove_logs_allservers',
           'volume_delete',
           'volume_create',
           'volume_start',
           'volume_stop',
           'volume_addbrick',
           'volume_replacebrick',
           'volume_set',
           'volume_reset',
           'peer_probe',
           'create_brick',
           'create_brick_allservers',
           'mount_exportdir',
           'umount_device']