"""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 from collections import namedtuple 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(command) logger.debug('%s: Executing Command: %s' % (hostkey, command)) output = host_connection.executecommand(new_command, commandInput) atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) return output def _substitute_value_for_variables(command): """ """ logger = GlobalObj.getLoggerObj() env = GlobalObj.getTestenvObj() new_command = command pattern_for_variables = re.compile("<[a-z]+\d*\.[a-z]*>") name_to_functions_mapping = { "export" : "getExportdir", "server" : "getServer", "brick" : "getBrick", "volume" : "getVolume", "client" : "getClient", "mountdevice" : "getMountDevice", "mount" : "getMount"} name_pattern = re.compile('(export|server|brick|volume|client|mountdevice|mount)*') variable_to_replace_named_tuple = namedtuple("Vars", ["actual_var", "name", "option"]) variables_to_replace = [] variables = pattern_for_variables.findall(command) if not variables: return new_command else: active_volume = env.getActiveVolume() for variable in variables: if variable not in variables_to_replace: name, option = (variable.strip("<>")).split(".") variables_to_replace.append(variable_to_replace_named_tuple(variable, name, option)) for variable in variables_to_replace: actual_var = variable.actual_var name = variable.name option = variable.option matched_obj = name_pattern.match(name.lower()) result = matched_obj.group(0) if result is '': continue else: funcname = name_to_functions_mapping[result] function = getattr(env, funcname) obj_value = function(name) if obj_value is None: continue else: try: option_value = obj_value.__getattribute__(option) except AttributeError: logger.error("Attribute Error: %s object has no attribute '%s'" % (name, option)) continue else: actual_var_pattern = re.compile(actual_var) new_command = actual_var_pattern.sub(option_value, new_command) variables = pattern_for_variables.findall(command) if not variables: new_command = "" return new_command return new_command __all__ = ['cd', 'rmdir', 'mkdir', 'mkfs', 'find_mountpoints', 'execute_command']