"""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 import pdb system_dirs = re.compile('(/bin|/boot|/dev|/etc|/lib|/mnt|/net|/opt|/root|/sbin|/usr|/var|/sys)\/?$') def cd(hostkey, dirpath): """ """ logger = GlobalObj.getLoggerObj() base_command = "cd" cm = GlobalObj.getConnectionsManagerObj() host_connection = cm.getConnection(hostkey) if not host_connection: logger.error("SSH Connection Not established to host '%s' " % hostkey) return 1 command = ' '.join([base_command, dirpath]) logger.debug('%s: Executing Command: %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): """ """ logger = GlobalObj.getLoggerObj() base_command = "rm -rf" cm = GlobalObj.getConnectionsManagerObj() if system_dirs.match(dirpath): logger.error("System Directiories cannot be deleted") return 1 else: host_connection = cm.getConnection(hostkey) if not host_connection: logger.error("SSH Connection Not established to host '%s' " % hostkey) return 1 command = ' '.join([base_command, dirpath]) logger.debug('%s: Executing Command: %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): """ """ logger = GlobalObj.getLoggerObj() base_command = "mkdir -p" cm = GlobalObj.getConnectionsManagerObj() if system_dirs.match(dirpath): logger.error("System Directiories cannot be created") return 1 else: host_connection = cm.getConnection(hostkey) if not host_connection: logger.error("SSH Connection Not established to host '%s' " % hostkey) return 1 command = ' '.join([base_command, dirpath]) logger.debug('%s: Executing Command: %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): """ """ logger = GlobalObj.getLoggerObj() base_command = "mkfs" cm = GlobalObj.getConnectionsManagerObj() host_connection = cm.getConnection(hostkey) command = [base_command] options = [] if not host_connection: logger.error("SSH Connection Not established to host '%s' " % hostkey) return 1 if fstype is None: fstype = "xfs" options.extend(["-t", fstype, "-f", device]) command.extend(options) command = ' '.join(command) logger.debug('%s: Executing Command: %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): """ """ logger = GlobalObj.getLoggerObj() base_command = "mount | grep " cm = GlobalObj.getConnectionsManagerObj() host_connection = cm.getConnection(hostkey) if not host_connection: logger.error("SSH connection to host '%s' has not been established" % hostkey) return 1 mountpoints = [] command = base_command + device logger.debug('%s: Executing Command: %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): """ """ logger = GlobalObj.getLoggerObj() cm = GlobalObj.getConnectionsManagerObj() host_connection = cm.getConnection(hostkey) if not host_connection: logger.error("SSH Connection Not established to host '%s' " % hostkey) return 1 new_command = _substitute_value_for_variables(hostkey, command) logger.debug('%s: Executing Command: %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): """ """ logger = GlobalObj.getLoggerObj() 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: logger.error("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']