diff options
Diffstat (limited to 'libs/utils/hostutils.py')
-rw-r--r-- | libs/utils/hostutils.py | 244 |
1 files changed, 110 insertions, 134 deletions
diff --git a/libs/utils/hostutils.py b/libs/utils/hostutils.py index c7ac7f2..3955cc6 100644 --- a/libs/utils/hostutils.py +++ b/libs/utils/hostutils.py @@ -10,141 +10,134 @@ Supported Wrappers: """ import re -from collections import namedtuple +import os import atfutils +from collections import namedtuple from atfglobals import GlobalObj -import os +from atfutils import commands 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['exitstatus']) - 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() + output = atfutils.get_new_output_obj() + 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 + output["exitstatus"] = 1 + return output - command = ' '.join([base_command, dirpath]) - logger.debug('%s: Executing Command: %s' % (hostkey, command)) - output = host_connection.executecommand(command) - return_status = atfutils.assert_success(output['exitstatus']) - atfutils.print_stdout(output['stdoutdata']) - atfutils.print_stderr(output['stderrdata']) - return return_status + command = ' '.join([commands['unlink_dir_force'], dirpath]) + output = execute_command(hostkey, command) + return output def mkdir(hostkey, dirpath): """ """ logger = GlobalObj.getLoggerObj() - base_command = "mkdir -p" - cm = GlobalObj.getConnectionsManagerObj() + output = atfutils.get_new_output_obj() 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 + output["exitstatus"] = 1 + return output - command = ' '.join([base_command, dirpath]) - logger.debug('%s: Executing Command: %s' % (hostkey, command)) - output = host_connection.executecommand(command) - return_status = atfutils.assert_success(output['exitstatus']) - atfutils.print_stdout(output['stdoutdata']) - atfutils.print_stderr(output['stderrdata']) - return return_status + command = ' '.join([commands['mkdir'], dirpath]) + output = execute_command(hostkey, command) + return output -def mkfs(hostkey, device, fstype=None): +def mkfs(hostkey, device, fstype, options=None): """ """ - logger = GlobalObj.getLoggerObj() - base_command = "mkfs" - cm = GlobalObj.getConnectionsManagerObj() - host_connection = cm.getConnection(hostkey) - command = [base_command] - options = [] + command = [commands['mkfs']] - if not host_connection: - logger.error("SSH Connection Not established to host '%s' " % hostkey) - return 1 + # mkfs type + if fstype is None: + fstype = "xfs" + command.extend(["-t", fstype]) + + # mkfs options if specified + if options is not None: + command.extend(["-o", options]) + + # mkfs device + command.extend(["-f", device]) + command = ' '.join(command) + output = execute_command(hostkey, command) + return output +def mount(hostkey, device, fstype, mountpoint, options=None): + """ + """ + command = [commands['mount']] + + # mount type if fstype is None: fstype = "xfs" + command.extend(["-t", fstype]) - options.extend(["-t", fstype, "-f", device]) + # mount options + if options is not None: + command.extend(["-o", options]) - command.extend(options) + # mount device, mountpoint + command.extend([device, mountpoint]) command = ' '.join(command) - logger.debug('%s: Executing Command: %s' % (hostkey, command)) - output = host_connection.executecommand(command) - return_status = atfutils.assert_success(output['exitstatus']) - atfutils.print_stdout(output['stdoutdata']) - atfutils.print_stderr(output['stderrdata']) - return return_status + output = execute_command(hostkey, command) + return output -def md5sum(hostkey, path): +def umount(hostkey, mountpoint): """ """ - output = {} - output["exitstatus"] = None - output["stdoutdata"] = None - output["stderrdata"] = None + command = ' '.join([commands['umount'], mountpoint]) + output = execute_command(hostkey, command) + return output - base_command1 = "rm -rf" - base_command2 = "arequal-checksum" - landfill_dir = os.path.join(path, ".landfill") +def umount_device(hostkey, device): + """ + """ + all_outputs = {} + umount_device_output = atfutils.get_new_output_obj() - cm = GlobalObj.getConnectionsManagerObj() - host_connection = cm.getConnection(hostkey) - if not host_connection: - logger.error("SSH connection to host '%s' has not been established" - % hostkey) - output["exitstatus"] = 1 + output = find_mountpoints(hostkey, device) + assert_success_status = atfutils.assert_success(output["exitstatus"]) + if assert_success_status is not 0: return output + else: + mountpoints = output["stdoutdata"] + + for mountpoint in mountpoints: + output = umount(hostkey, mountpoint) + assert_success_status = atfutils.assert_success(output["exitstatus"]) + if assert_success_status is not 0: + stdoutdata = str(output["stdoutdata"]) + if ((stdoutdata.rfind("not found")) or + (stdoutdata.rfind("not mount"))): + output["exitstatus"] = 0 + all_outputs[mountpoint] = output + + assert_success_status = atfutils.assert_success_of_outputs(all_outputs) + if assert_success_status is not 0: + umount_device_output["exitstatus"] = 1 + umount_device_output["stderrdata"] = "Unable to unmount device %s" % (device) + else: + umount_device_output["exitstatus"] = 0 + umount_device_output["stdoutdata"] = "Successfully able to unmount device %s" % (device) - command1 = ' '.join([base_command1, landfill_dir]) - command2 = ' '.join([base_command2, path]) - command = ';'.join([command1, command2]) - output = host_connection.executecommand(command) - atfutils.print_stdout(output['stdoutdata']) - atfutils.print_stderr(output['stderrdata']) + return umount_device_output +def md5sum(hostkey, path): + """ + """ + base_command = "rm -rf %s ; arequal-checksum %s" + landfill_dir = os.path.join(path, ".landfill") + + command = base_command % (landfill_dir, path) + output = execute_command(hostkey, command) if output['stdoutdata'] is None or (not output['stdoutdata']): output['stdoutdata'] = "" - else: output['stdoutdata'] = str(output['stdoutdata']) @@ -153,34 +146,26 @@ def md5sum(hostkey, path): 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 - + base_command = "mount | grep" 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 + command = ' '.join([base_command, device]) + output = execute_command(hostkey, command) + assert_success_status = atfutils.assert_success(output['exitstatus']) + if assert_success_status is 0: + if output["stdoutdata"]: + for data in output["stdoutdata"]: + mountpoints.append(data.split(" ")[2]) + output["stdoutdata"] = mountpoints + return output def execute_command(hostkey, command, commandInput=None): """ """ - output = {} - output["exitstatus"] = None - output["stdoutdata"] = None - output["stderrdata"] = None logger = GlobalObj.getLoggerObj() + output = atfutils.get_new_output_obj() + new_command = _substitute_value_for_variables(command) + cm = GlobalObj.getConnectionsManagerObj() host_connection = cm.getConnection(hostkey) if not host_connection: @@ -188,16 +173,22 @@ def execute_command(hostkey, command, commandInput=None): % hostkey) output["exitstatus"] = 1 return output - new_command = _substitute_value_for_variables(command) - logger.debug('%s: Executing Command: %s' % (hostkey, command)) + env = GlobalObj.getTestenvObj() + host_obj = env.getHost(hostkey) + if host_obj is None: + logger.error("Invalid Host. %s is not defined in TestEnvironment" % + hostkey) + output["exitstatus"] = 1 + return output + + hostname = host_obj.hostname + logger.debug('%s: Executing Command: %s' % (hostname, 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): """ """ @@ -600,18 +591,3 @@ def gluster_install_rpm(version, *components): return 1 return 0 - - - - - -__all__ = ['cd', - 'rmdir', - 'mkdir', - 'mkfs', - 'md5sum', - 'find_mountpoints', - 'execute_command', - 'gluster_install_tar', - 'gluster_install_git', - 'gluster_install_rpm'] |