summaryrefslogtreecommitdiffstats
path: root/libs/utils/serverutils.py
blob: 10fe830c7fc0bd41cf51b781bfe2965308d97f9f (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
"""serverutils module
"""
import re
import hostutils
from atfglobals import GlobalObj
import atfutils

def md5sum_of_brick(brickkey):
    """
    Parameter: brick (tye: string)
    Returns: output of arequal-checksum command execution(type:dict)
        Key : Value of the Output
        exitstatus: exit status of the arequal-checksum command on brick
        stdoutdata: stdout data of arequal-checksum command execution
        stderrdata: stderr data of arequal-checksum command execution
    """
    output = {}
    output["exitstatus"] = None
    output["stdoutdata"] = None
    output["stderrdata"] = None
    logger = GlobalObj.getLoggerObj()
    env = GlobalObj.getTestenvObj()

    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

    else:
        serverkey = re.split("\.", raw_brick_obj.hostname, maxsplit=1)[0]

    brick_obj = env.getBrick(brickkey)
    if not brick_obj:
        logger.error("InValid Brick. %s not defined in TestEnvironment"
                 % brickkey)
        output["exitstatus"] = 1
        return output
    else:
        brick_path = brick_obj.path

    output = hostutils.md5sum(serverkey, brick_path)
    return output

def md5sum_of_bricks(bricks):
    """
    Description:
        Calculate md5sum of bricks using arequal-checksum

    Parameters: bricks (type: List)

    Returns: md5sums of all the bricks (type: dict)
        Keys: bricks
        Value: ouput (type:dict)
            exitstatus: exit status of the arequal-checksum command on brick
            stdoutdata: stdout data of arequal-checksum command execution
            stderrdata: stderr data of arequal-checksum command execution
    """
    md5sums = {}
    for brickkey in bricks:
        output = md5sum_of_brick(brickkey)
        md5sums[brickkey] = output

    return md5sums

def get_gfid_on_brick(brickkey, filename="."):
    """
    """
    output = {}
    output["exitstatus"] = None
    output["stdoutdata"] = None
    output["stderrdata"] = None
    base_command = "getfattr -n 'trusted.gfid' -e hex"
    command = ' '.join([base_command, filename])
    output = execute_on_brick(brickkey, command)
    if atfutils.assert_success(output['exitstatus']):
        atfutils.print_stdout(output['stdoutdata'])
        atfutils.print_stderr(output['stderrdata'])

    elif output['stdoutdata'] is None or (not output['stdoutdata']):
        output['stdoutdata'] = ""

    else:
        output['stdoutdata'] = str(output['stdoutdata'])

    return output

def get_gfid_on_bricks(bricks, filename="."):
    """
    """
    gfid_on_bricks = {}
    for brickkey in bricks:
        output = get_gfid_on_brick(brickkey, filename)
        gfid_on_bricks[brickkey] = output

    return gfid_on_bricks

def execute_on_brick(brickkey, command, commandInput=None):
    """
    """
    output = {}
    output["exitstatus"] = None
    output["stdoutdata"] = None
    output["stderrdata"] = None

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

    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
    else:
        serverkey = re.split("\.", raw_brick_obj.hostname, maxsplit=1)[0]

    brick_obj = env.getBrick(brickkey)
    if not brick_obj:
        logger.error("InValid Brick. %s not defined in TestEnvironment"
                     % brickkey)
        output["exitstatus"] = 1
        return output
    else:
        exportdirpath = brick_obj.path

    command = "cd " + exportdirpath + ";" + command
    output = hostutils.execute_command(serverkey, command, commandInput)
    return output

__all__ = ['execute_on_brick',
           'md5sum_of_bricks']