summaryrefslogtreecommitdiffstats
path: root/SharedModules/Utils/hostutils.py
blob: 68bb1bf6490206597a29a35b120a2bc7b02b6eb4 (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
"""hostutils module contains wrappers for commands that can be executed on any
host in the test environment

Supported Wrappers:
-------------------
*) rmdir
*) mkdir
*) mkfs
*) execute_command
"""

import re
import atfutils
from atfglobals import GlobalObj

def cd(hostkey, dirpath):
    """
    """
    base_command = "cd "
    cm = GlobalObj.getConnectionsManagerObj()
    host_connection = cm.getConnection(hostkey)
    if not host_connection:
        print "SSH Connection Not established to host '%s' " % hostkey
        return 1
    command = base_command + dirpath
    print "%s : %s" % (hostkey, command)
    output = host_connection.executecommand(command)
    return_status = atfutils.assert_success(**output)
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status   
    
def rmdir(hostkey, dirpath):
    """
    """
    base_command = "rm -rf "
    cm = GlobalObj.getConnectionsManagerObj()
    system_dirs = re.compile('(/bin|/boot|/dev|/etc|/lib|/mnt|/net|/opt|/root|/sbin|/usr|/var|/sys)\/?$')
    if system_dirs.match(dirpath):
        print "System Directiories cannot be deleted"
        return 1
    
    else:
        host_connection = cm.getConnection(hostkey)
        if not host_connection:
            print "SSH Connection Not established to host '%s' " % hostkey
            return 1
        command = base_command + dirpath
        print "%s : %s" % (hostkey, command)
        output = host_connection.executecommand(command)
        return_status = atfutils.assert_success(**output)
        atfutils.print_stdout(output['stdoutdata'])
        atfutils.print_stderr(output['stderrdata'])
        return return_status
        
def mkdir(hostkey, dirpath):
    """
    """
    base_command = "mkdir -p "
    cm = GlobalObj.getConnectionsManagerObj()
    system_dirs = re.compile('(/bin|/boot|/dev|/etc|/lib|/mnt|/net|/opt|/root|/sbin|/usr|/var|/sys)\/?$')
    if system_dirs.match(dirpath):
        print "System Directiories cannot be created"
        return 1

    else:
        host_connection = cm.getConnection(hostkey)
        if not host_connection:
            print "SSH Connection Not established to host '%s' " % hostkey
            return 1
        command = base_command + dirpath
        print "%s : %s" % (hostkey, command)
        output = host_connection.executecommand(command)
        return_status = atfutils.assert_success(**output)
        atfutils.print_stdout(output['stdoutdata'])
        atfutils.print_stderr(output['stderrdata'])
        return return_status

def mkfs(hostkey, device, fstype=None):
    """
    """
    base_command = "mkfs "
    cm = GlobalObj.getConnectionsManagerObj()    
    host_connection = cm.getConnection(hostkey)
    if not host_connection:
        print "SSH Connection Not established to host '%s' " % hostkey
        return 1

    if fstype is None:
        fstype = "xfs"

    command = base_command + " -t " + fstype + " -f " + device
    print "%s : %s" % (hostkey, command)
    output = host_connection.executecommand(command)
    return_status = atfutils.assert_success(**output)
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def find_mountpoints(hostkey, device):
    """
    """
    base_command = "mount | grep " 
    cm = GlobalObj.getConnectionsManagerObj()
    
    host_connection = cm.getConnection(hostkey)
    if not host_connection:
        print "SSH connection to host '%s' has not been established" % hostkey
        return 1

    mountpoints = []
    command = base_command + device
    print "%s : %s" % (hostkey, command)
    output = host_connection.executecommand(command)
    if not output["exitstatus"]:
        for data in output["stdoutdata"]:
            mountpoints.append(data.split(" ")[2])

    return mountpoints

def execute_command(hostkey, command, commandInput=None):
    """
    """
    cm = GlobalObj.getConnectionsManagerObj()
    host_connection = cm.getConnection(hostkey)
    if not host_connection:
        print "SSH Connection Not established to host '%s' " % hostkey
        return 1
    new_command = _substitute_value_for_variables(hostkey, command)

    print "%s : %s" % (hostkey, command)
    output = host_connection.executecommand(new_command, commandInput)
    return_status = atfutils.assert_success(**output)
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status


def _substitute_value_for_variables(hostkey, command):
    """
    """
    pattern_for_variables = re.compile("<[a-z]+\d*>")
    pattern_for_hosts = re.compile('(server|client|master)*')
    variables_to_replace = []
    replace_values = {}
    new_command = command
    Functions_Map = {
        "server" : "getserver",
        "client" : "getclient",
        "master" : "getmaster"
        }
    variables = pattern_for_variables.findall(command)
    host = None

    if not variables: 
        return new_command
    
    else:
        result = pattern_for_hosts.match(hostkey.lower()).group(0)
        if result:
            funcname = Functions_Map[result]
            function = getattr(env, funcname)
            if re.match("master", result):
                host = function()
            else:
                host = function(hostkey)
            
        if not host:
            print "No Host to execute the command\n"
            return 1
        
        for variable in variables:
            if variable not in variables_to_replace:
                variables_to_replace.append(variable.strip("<>"))

        for variable in variables_to_replace:
            value = host.__getattribute__(variable)
            replace_values[variable] = value

        for key in replace_values.keys():
            value = replace_values[key]
            key = "<" + key + ">"
            pattern = re.compile(key)
            new_command = pattern.sub(value, new_command)

        return new_command

__all__ = ['cd',
           'rmdir',
           'mkdir',
           'mkfs',
           'find_mountpoints',
           'execute_command']