summaryrefslogtreecommitdiffstats
path: root/SharedModules/Utils/hostutils.py
diff options
context:
space:
mode:
authorShwetha-H-Panduranga <shwetha@gluster.com>2011-12-06 14:25:20 +0530
committerShwetha-H-Panduranga <shwetha@gluster.com>2011-12-06 14:25:20 +0530
commit129e39fe6878f28ca203c690fab382b7289b334c (patch)
tree4b825dc642cb6eb9a060e54bf8d69288fbee4904 /SharedModules/Utils/hostutils.py
parent18445ae1a94366c955cc7626fb8ec749dedcf73e (diff)
Removing all automation files from the repo
Diffstat (limited to 'SharedModules/Utils/hostutils.py')
-rw-r--r--SharedModules/Utils/hostutils.py193
1 files changed, 0 insertions, 193 deletions
diff --git a/SharedModules/Utils/hostutils.py b/SharedModules/Utils/hostutils.py
deleted file mode 100644
index 68bb1bf..0000000
--- a/SharedModules/Utils/hostutils.py
+++ /dev/null
@@ -1,193 +0,0 @@
-"""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']