diff options
Diffstat (limited to 'libs/utils/hostutils.py')
-rw-r--r-- | libs/utils/hostutils.py | 74 |
1 files changed, 48 insertions, 26 deletions
diff --git a/libs/utils/hostutils.py b/libs/utils/hostutils.py index 68bb1bf..9992bb6 100644 --- a/libs/utils/hostutils.py +++ b/libs/utils/hostutils.py @@ -12,18 +12,23 @@ Supported Wrappers: 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): """ """ - base_command = "cd " + logger = GlobalObj.getLoggerObj() + base_command = "cd" cm = GlobalObj.getConnectionsManagerObj() host_connection = cm.getConnection(hostkey) if not host_connection: - print "SSH Connection Not established to host '%s' " % hostkey + logger.error("SSH Connection Not established to host '%s' " % hostkey) return 1 - command = base_command + dirpath - print "%s : %s" % (hostkey, command) + + 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']) @@ -33,20 +38,22 @@ def cd(hostkey, dirpath): def rmdir(hostkey, dirpath): """ """ - base_command = "rm -rf " + logger = GlobalObj.getLoggerObj() + 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" + logger.error("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 + logger.error("SSH Connection Not established to host '%s' " + % hostkey) return 1 - command = base_command + dirpath - print "%s : %s" % (hostkey, command) + + 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']) @@ -56,20 +63,23 @@ def rmdir(hostkey, dirpath): def mkdir(hostkey, dirpath): """ """ - base_command = "mkdir -p " + logger = GlobalObj.getLoggerObj() + 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" + logger.error("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 + logger.error("SSH Connection Not established to host '%s' " + % hostkey) return 1 - command = base_command + dirpath - print "%s : %s" % (hostkey, command) + + 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']) @@ -79,18 +89,25 @@ def mkdir(hostkey, dirpath): def mkfs(hostkey, device, fstype=None): """ """ - base_command = "mkfs " + logger = GlobalObj.getLoggerObj() + base_command = "mkfs" cm = GlobalObj.getConnectionsManagerObj() host_connection = cm.getConnection(hostkey) + command = [base_command] + options = [] + if not host_connection: - print "SSH Connection Not established to host '%s' " % hostkey + logger.error("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) + + 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']) @@ -100,17 +117,19 @@ def mkfs(hostkey, device, fstype=None): def find_mountpoints(hostkey, device): """ """ + logger = GlobalObj.getLoggerObj() 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 + logger.error("SSH connection to host '%s' has not been established" + % hostkey) return 1 mountpoints = [] command = base_command + device - print "%s : %s" % (hostkey, command) + logger.debug('%s: Executing Command: %s' % (hostkey, command)) output = host_connection.executecommand(command) if not output["exitstatus"]: for data in output["stdoutdata"]: @@ -121,14 +140,16 @@ def find_mountpoints(hostkey, device): def execute_command(hostkey, command, commandInput=None): """ """ + logger = GlobalObj.getLoggerObj() cm = GlobalObj.getConnectionsManagerObj() host_connection = cm.getConnection(hostkey) if not host_connection: - print "SSH Connection Not established to host '%s' " % hostkey + logger.error("SSH Connection Not established to host '%s' " + % hostkey) return 1 new_command = _substitute_value_for_variables(hostkey, command) - print "%s : %s" % (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']) @@ -139,6 +160,7 @@ def execute_command(hostkey, command, commandInput=None): 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 = [] @@ -166,7 +188,7 @@ def _substitute_value_for_variables(hostkey, command): host = function(hostkey) if not host: - print "No Host to execute the command\n" + logger.error("No Host to execute the command\n") return 1 for variable in variables: |