diff options
Diffstat (limited to 'SharedModules/Utils/hostutils.py')
-rw-r--r-- | SharedModules/Utils/hostutils.py | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/SharedModules/Utils/hostutils.py b/SharedModules/Utils/hostutils.py new file mode 100644 index 0000000..68bb1bf --- /dev/null +++ b/SharedModules/Utils/hostutils.py @@ -0,0 +1,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'] |