From 689f862f24f949361603a808250ae3f7ec9f40f6 Mon Sep 17 00:00:00 2001 From: Shwetha-H-Panduranga Date: Mon, 12 Dec 2011 11:29:47 +0530 Subject: Changes to logger class, Using the logger class in the framework, adding new global values, Changes made to argument parser, testruninfo --- libs/utils/atfutils.py | 11 ++- libs/utils/clientutils.py | 52 ++++++----- libs/utils/glusterutils.py | 210 ++++++++++++++++++++++++++++----------------- libs/utils/hostutils.py | 74 ++++++++++------ libs/utils/managerutils.py | 11 ++- libs/utils/serverutils.py | 7 +- 6 files changed, 233 insertions(+), 132 deletions(-) (limited to 'libs/utils') diff --git a/libs/utils/atfutils.py b/libs/utils/atfutils.py index 980c286..9b91ae2 100644 --- a/libs/utils/atfutils.py +++ b/libs/utils/atfutils.py @@ -30,23 +30,26 @@ def assert_failure(**arguments): def print_stdout(stdoutdata): """ """ + logger = GlobalObj.getLoggerObj() if not stdoutdata == None: for data in stdoutdata: - print data + logger.debug(data) def print_stderr(stderrdata): + logger = GlobalObj.getLoggerObj() if not stderrdata == None: for data in stderrdata: - print data + logger.debug(data) def set_active_volume(volumekey): """ """ + logger = GlobalObj.getLoggerObj() env = GlobalObj.getTestenvObj() return_status = env.setActiveVolume(volumekey) if return_status: - print "Unable to set Active Volume. '%s' Not defined in TestEnvironment"\ - % volumekey + logger.error("Unable to set Active Volume. \ + '%s' Not defined in TestEnvironment" % volumekey ) return return_status diff --git a/libs/utils/clientutils.py b/libs/utils/clientutils.py index ad5d593..33f036e 100644 --- a/libs/utils/clientutils.py +++ b/libs/utils/clientutils.py @@ -12,6 +12,7 @@ Supported Wrappers :- import atfutils import hostutils from atfglobals import GlobalObj +import pdb def umount(mountkey): """unmounts a mountpoint @@ -24,23 +25,26 @@ def umount(mountkey): Success : 0 Failure : 1` """ - base_command = "umount " + logger = GlobalObj.getLoggerObj() + base_command = "umount" env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() mount_obj = env.getMount(mountkey) if not mount_obj: - print "InValid Mount. %s not defined in TestEnvironment" % mountkey + logger.error("InValid Mount. '%s' not defined in TestEnvironment" + % mountkey) return 1 clientkey = mount_obj.client client_connection = cm.getConnection(clientkey) if not client_connection: - print "SSH connection to host '%s' has not been established" % clientkey + logger.error("SSH connection to host '%s' has not been established" + % clientkey) return 1 - command = base_command + mount_obj.dir - print "%s : %s" % (clientkey, command) + command = ' '.join([base_command, mount_obj.dir]) + logger.debug('%s: Executing Command: %s' % (clientkey, command)) output = client_connection.executecommand(command) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) @@ -51,7 +55,7 @@ def umount(mountkey): return_status = 0 else: - print "Unable to umount %s" % mountkey + logger.error("Unable to umount %s" % mountkey) return return_status @@ -91,41 +95,44 @@ def mount(mountkey): Success : 0 Failure : 1` """ - - base_command = command = "mount " + logger = GlobalObj.getLoggerObj() + base_command = "mount" env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() + command = [base_command] + options = [] mount_obj = env.getMount(mountkey) if not mount_obj: - print "InValid Mount. %s not defined in TestEnvironment" % mountkey + logger.error("InValid Mount. %s not defined in TestEnvironment" + % mountkey) return 1 clientkey = mount_obj.client client_connection = cm.getConnection(clientkey) if not client_connection: - print "SSH connection to host '%s' has not been established" % clientkey + logger.error("SSH connection to host '%s' has not been established" + % clientkey) return 1 + return_status = hostutils.mkdir(clientkey, mount_obj.dir) + if return_status: + return return_status + mountdevice_obj = mount_obj.device device = mountdevice_obj.hostname + ":/" + mountdevice_obj.volumename - options = ["-t", mount_obj.type] + options.extend(["-t", mount_obj.type]) if mount_obj.logfile: options.extend(["-o", ("log-file="+mount_obj.logfile), "log-level=INFO"]) - if mount_obj.options: options.extend([mount_obj.option]) - options.extend([device, mount_obj.dir]) - for index, option in enumerate(options): - command = command + option + " " + + command.extend(options) + command = ' '.join(command) - return_status = hostutils.mkdir(clientkey, mount_obj.dir) - if return_status: - return return_status - - print "%s : %s" % (clientkey, command) + logger.debug('%s: Executing Command: %s' % (clientkey, command)) output = client_connection.executecommand(command) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) @@ -156,12 +163,15 @@ def mountall(): def execute_on_mount(mountkey, command, commandInput=None): """ """ + logger = GlobalObj.getLoggerObj() env = GlobalObj.getTestenvObj() mount_obj = env.getMount(mountkey) if not mount_obj: - print "InValid Mount. %s not defined in TestEnvironment" % mountkey + logger.error("InValid Mount. %s not defined in TestEnvironment" + % mountkey) return 1 + clientkey = mount_obj.client mountdir = mount_obj.dir command = "cd " + mountdir + " ;" + command diff --git a/libs/utils/glusterutils.py b/libs/utils/glusterutils.py index 0c15af1..7ab41d0 100644 --- a/libs/utils/glusterutils.py +++ b/libs/utils/glusterutils.py @@ -27,17 +27,18 @@ import atfutils import hostutils from atfglobals import GlobalObj - def glusterd_start(serverkey, force=False): """ """ + logger = GlobalObj.getLoggerObj() env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() commands_to_execute = ["which glusterd", "ps -e | grep glusterd"] gluster_version = env.getServer(serverkey).glusterversion host_connection = cm.getConnection(serverkey) if not host_connection: - print "SSH connection to host '%s' has not been established" % serverkey + logger.error("SSH connection to host '%s' has not been established" + % serverkey) return 1 """ Check if gluster is already running. If already Running and force=True, @@ -55,7 +56,7 @@ def glusterd_start(serverkey, force=False): command = commands_to_execute.pop() output = host_connection.executecommand(command) if output["exitstatus"]: - print "Unable to start glusterd" + logger.error("Unable to start glusterd") return_status = atfutils.assert_success(**output) return return_status else: @@ -63,7 +64,7 @@ def glusterd_start(serverkey, force=False): gluster_path = None gluster_path = output["stdoutdata"][0].strip("\n") else: - print "Unable to find gluster path" + logger.error("Unable to find gluster path") return_status = atfutils.assert_success(**output) return return_status @@ -72,17 +73,18 @@ def glusterd_start(serverkey, force=False): output = host_connection.executecommand(command) if not output["stdoutdata"] == None: if re.search(gluster_version, str(output["stdoutdata"])): - print "%s : %s" % (serverkey, gluster_path) + logger.debug('%s: Executing Command: %s' + %(serverkey, gluster_path)) output = host_connection.executecommand(gluster_path) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) return return_status else: - print "Unable to start glusterd" + logger.error("Unable to start glusterd") return 1 else: - print "Unable to start glusterd" + logger.error("Unable to start glusterd") return 1 def glusterd_start_allservers(force=False): @@ -100,12 +102,14 @@ def glusterd_start_allservers(force=False): def glusterd_stop(serverkey): """ """ + logger = GlobalObj.getLoggerObj() base_command = "kill -KILL " env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() host_connection = cm.getConnection(serverkey) if not host_connection: - print "SSH connection to host '%s' has not been established" % serverkey + logger.error("SSH connection to host '%s' has not been established" + % serverkey) return 1 gluster_pid_list = [] @@ -121,7 +125,7 @@ def glusterd_stop(serverkey): for pid in gluster_pid_list: command = base_command + pid - print "%s : %s" % (serverkey, command) + logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) @@ -156,21 +160,26 @@ def glusterd_restart(serverkey): def glusterd_remove_dir(serverkey): """ """ - command = "rm -rf /etc/glusterd/*" + logger = GlobalObj.getLoggerObj() + base_command = "rm -rf" env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() + glusterd_dir = GlobalObj.glusterd_dir server_obj = env.getServer(serverkey) if not server_obj: - print "Invalid Host. %s not defined in TestEnvironment" % serverkey + logger.error("Invalid Host. %s not defined in TestEnvironment" + % serverkey) return 1 server_connection = cm.getConnection(serverkey) if not server_connection: - print "SSH connection to host '%s' has not been established" % serverkey + logger.error("SSH connection to host '%s' has not been established" + % serverkey) return 1 - - print "%s : %s" % (serverkey, command) + + command = ' '.join([base_command, glusterd_dir]) + logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = server_connection.executecommand(command) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) @@ -191,8 +200,9 @@ def glusterd_remove_dir_allservers(): def glusterd_remove_logs(serverkey): """ """ - base_command = "rm -rf " - log_paths = ["/var/log/glusterfs/*.log", "/var/log/glusterfs/bricks/*"] + logger = GlobalObj.getLoggerObj() + base_command = "rm -rf" + log_paths = GlobalObj.glusterd_log_paths absolute_path_list = [] prefix_path = '' env = GlobalObj.getTestenvObj() @@ -200,12 +210,14 @@ def glusterd_remove_logs(serverkey): server_obj = env.getServer(serverkey) if not server_obj: - print "Invalid Host. %s not defined in TestEnvironment" % serverkey + logger.error("Invalid Host. %s not defined in TestEnvironment" + % serverkey) return 1 server_connection = cm.getConnection(serverkey) if not server_connection: - print "SSH connection to host '%s' has not been established" % serverkey + logger.error("SSH connection to host '%s' has not been established" + % serverkey) return 1 if server_obj.installpath: @@ -215,8 +227,8 @@ def glusterd_remove_logs(serverkey): absolute_path_list.append(prefix_path + path) for path in absolute_path_list: - command = base_command + path - print "%s : %s" % (serverkey, command) + command = ' '.join([base_command, path]) + logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = server_connection.executecommand(command) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) @@ -237,21 +249,24 @@ def glusterd_remove_logs_allservers(): def volume_delete(serverkey): """ """ + logger = GlobalObj.getLoggerObj() base_command = "gluster volume delete " env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() active_volume = env.getActiveVolume() if not active_volume: - print "Invalid Volume.ActiveVolume not defined for the TestEnvironment" + logger.error("Invalid Volume.ActiveVolume not defined" + + "for the TestEnvironment") return 1 volumename = active_volume.volumename command = base_command + volumename host_connection = cm.getConnection(serverkey) if not host_connection: - print "SSH connection to host '%s' has not been established" % serverkey + logger.error("SSH connection to host '%s' has not been established" + % serverkey) return 1 - print "%s : %s" % (serverkey, command) + logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) @@ -261,30 +276,30 @@ def volume_delete(serverkey): def volume_create(serverkey): """ """ + logger = GlobalObj.getLoggerObj() base_command = "gluster volume create " env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() active_volume = env.getActiveVolume() if not active_volume: - print "ActiveVolume not defined for the TestEnvironment" + logger.error("ActiveVolume not defined for the TestEnvironment") return 1 - - command = base_command + \ - active_volume.volumename + " " + \ - active_volume.volumetype + " " + \ - active_volume.count + " " + \ - "transport " + active_volume.transporttype + " " + + command = ' '.join([base_command, active_volume.volumename, + active_volume.volumetype, active_volume.count, + "transport", active_volume.transporttype]) for brick_obj in active_volume.bricks: brick_value = brick_obj.hostname + ":" + brick_obj.path - command = command + brick_value + " " + command = ' '.join([command, brick_value]) host_connection = cm.getConnection(serverkey) if not host_connection: - print "SSH connection to host '%s' has not been established" % serverkey + logger.error("SSH connection to host '%s' has not been established" + % serverkey) return 1 - print "%s : %s" % (serverkey, command) + logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) if return_status: @@ -297,12 +312,13 @@ def volume_create(serverkey): def volume_start(serverkey, force=False): """ """ + logger = GlobalObj.getLoggerObj() base_command = "gluster volume start " env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() active_volume = env.getActiveVolume() if not active_volume: - print "ActiveVolume not defined for the TestEnvironment" + logger.error("ActiveVolume not defined for the TestEnvironment") return 1 volumename = active_volume.volumename command = base_command + volumename @@ -311,10 +327,11 @@ def volume_start(serverkey, force=False): host_connection = cm.getConnection(serverkey) if not host_connection: - print "SSH connection to host '%s' has not been established" % serverkey + logger.error("SSH connection to host '%s' has not been established" + % serverkey) return 1 - print "%s : %s" % (serverkey, command) + logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) if return_status: @@ -327,12 +344,13 @@ def volume_start(serverkey, force=False): def volume_stop(serverkey, force=False): """ """ + logger = GlobalObj.getLoggerObj() base_command = "gluster volume stop " env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() active_volume = env.getActiveVolume() if not active_volume: - print "ActiveVolume not defined for the TestEnvironment" + logger.error("ActiveVolume not defined for the TestEnvironment") return 1 volumename = active_volume.volumename command = base_command + volumename @@ -341,10 +359,11 @@ def volume_stop(serverkey, force=False): host_connection = cm.getConnection(serverkey) if not host_connection: - print "SSH connection to host '%s' has not been established" % serverkey + logger.error("SSH connection to host '%s' has not been established" + % serverkey) return 1 - print "%s : %s" % (serverkey, command) + logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) @@ -354,29 +373,35 @@ def volume_stop(serverkey, force=False): def volume_addbrick(serverkey, *bricks): """ """ - base_command = "gluster volume add-brick " + logger = GlobalObj.getLoggerObj() + base_command = "gluster volume add-brick" env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() active_volume = env.getActiveVolume() + command = [base_command] + if not active_volume: - print "ActiveVolume not defined for the TestEnvironment" + logger.error("ActiveVolume not defined for the TestEnvironment") return 1 - volumename = active_volume.volumenameGlobalObj.getConnectionsManagerObj() - command = base_command + volumename + " " + volumename = active_volume.volumename + + command.extend([volumename]) for brick in bricks: brick_obj = env.getBrick(brick) if not brick_obj: - print "Invalid Brick. Brick Not defined in TestEnvironment" + logger.error("Invalid Brick. Brick Not defined in TestEnvironment") return 1 brick_value = brick_obj.hostname + ":" + brick_obj.path - command = command + brick_value - + command.extend([brick_value]) + + command = ' '.join(command) host_connection = cm.getConnection(serverkey) if not host_connection: - print "SSH connection to host '%s' has not been established" % serverkey + logger.error("SSH connection to host '%s' has not been established" + % serverkey) return 1 - print "%s : %s" % (serverkey, command) + logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) if not return_status: @@ -389,25 +414,33 @@ def volume_addbrick(serverkey, *bricks): def volume_replacebrick(serverkey, replacebrick_key, tobrick_key): """ """ + logger = GlobalObj.getLoggerObj() base_command = "gluster volume replace-brick " env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() + command = [base_command] + active_volume = env.getActiveVolume() if not active_volume: - print "ActiveVolume not defined for the TestEnvironment" + logger.error("ActiveVolume not defined for the TestEnvironment") return 1 volumename = active_volume.volumename - command = base_command + volumename + " " replace_brick = env.getbrick(replacebrick_key) to_brick = env.getbrick(tobrick_key) - command = command + replace_brick + " " + to_brick - + + if not (to_brick and replace_brick): + logger.error("Invalid Brick. Brick Not defined in TestEnvironment") + return 1 + + command.extend([volumename, replace_brick, to_brick]) + command = ' '.join(command) host_connection = cm.getConnection(serverkey) if not host_connection: - print "SSH connection to host '%s' has not been established" % serverkey + logger.error("SSH connection to host '%s' has not been established" + % serverkey) return 1 - print "%s : %s" % (serverkey, command) + logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) if not return_status: @@ -420,21 +453,28 @@ def volume_replacebrick(serverkey, replacebrick_key, tobrick_key): def volume_set(serverkey, key, value): """ """ - base_command = "gluster volume set " + logger = GlobalObj.getLoggerObj() + base_command = "gluster volume set" env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() + command = [base_command] + active_volume = env.getActiveVolume() if not active_volume: - print "ActiveVolume not defined for the TestEnvironment" + logger.error("ActiveVolume not defined for the TestEnvironment") return 1 volumename = active_volume.volumename - command = base_command + volumename + " " + key + " " + value + + command.extend([volumename, key, value]) + command = ' '.join(command) + host_connection = cm.getConnection(serverkey) if not host_connection: - print "SSH connection to host '%s' has not been established" % serverkey + logger.error("SSH connection to host '%s' has not been established" + % serverkey) return 1 - print "%s : %s" % (serverkey, command) + logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) @@ -444,21 +484,23 @@ def volume_set(serverkey, key, value): def volume_reset(serverkey): """ """ + logger = GlobalObj.getLoggerObj() base_command = "gluster volume reset " env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() active_volume = env.getActiveVolume() if not active_volume: - print "ActiveVolume not defined for the TestEnvironment" + logger.error("ActiveVolume not defined for the TestEnvironment") return 1 volumename = active_volume.volumename command = base_command + volumename host_connection = cm.getConnection(serverkey) if not host_connection: - print "SSH connection to host '%s' has not been established" % serverkey + logger.error("SSH connection to host '%s' has not been established" + % serverkey) return 1 - print "%s : %s" % (serverkey, command) + logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) @@ -468,8 +510,9 @@ def volume_reset(serverkey): def peer_probe(fromserverkey): """ """ + logger = GlobalObj.getLoggerObj() base_command = "gluster peer probe " - command = base_command + command = [base_command] all_servers = {} env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() @@ -481,14 +524,16 @@ def peer_probe(fromserverkey): continue else: server_obj = all_servers[key] - command = command + server_obj.hostname + " " - + command.extend([server_obj.hostname]) + + command = ' '.join(command) host_connection = cm.getConnection(fromserverkey) if not host_connection: - print "SSH connection to host '%s' has not been established" % serverkey + logger.error("SSH connection to host '%s' has not been established" + % serverkey) return 1 - print "%s : %s" % (fromserverkey, command) + logger.debug('%s: Executing Command: %s' % (fromserverkey, command)) output = host_connection.executecommand(command) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) @@ -498,6 +543,7 @@ def peer_probe(fromserverkey): def create_brick(brickkey): """ """ + logger = GlobalObj.getLoggerObj() return_status = 1 env = GlobalObj.getTestenvObj() brick_obj = env.getRawBrick(brickkey) @@ -513,20 +559,20 @@ def create_brick(brickkey): if re.match("^\/", exportdir): dirpath = exportdir - command = base_command + dirpath else: export_obj = env.getExportdir(exportdir) dirpath = export_obj.dir device = export_obj.device fstype = export_obj.fstype + options = export_obj.options - print "%s : %s" % (serverkey, 'create_brick') + logger.debug('%s: Executing Command: %s'% (serverkey, 'create_brick')) if device: if umount_device(serverkey, device): return return_status if hostutils.mkfs(serverkey, device, fstype): return return_status - if mount_exportdir(serverkey, device, fstype, dirpath): + if mount_exportdir(serverkey, device, fstype, options, dirpath): return return_status return 0 @@ -541,19 +587,21 @@ def create_brick(brickkey): def umount_device(serverkey, device): """ """ + logger = GlobalObj.getLoggerObj() base_command = "umount " cm = GlobalObj.getConnectionsManagerObj() server_connection = cm.getConnection(serverkey) if not server_connection: - print "SSH connection to host '%s' has not been established" % serverkey + logger.error("SSH connection to host '%s' has not been established" + % serverkey) return 1 mountpoints = hostutils.find_mountpoints(serverkey, device) for mountpoint in mountpoints: command = base_command + mountpoint - print "%s : %s" % (serverkey, command) + logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = server_connection.executecommand(command) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) @@ -568,23 +616,31 @@ def umount_device(serverkey, device): return 0 -def mount_exportdir(serverkey, device, fstype, dirpath): +def mount_exportdir(serverkey, device, fstype, options, dirpath): """ """ + logger = GlobalObj.getLoggerObj() base_command = "mount " cm = GlobalObj.getConnectionsManagerObj() + command = [base_command] server_connection = cm.getConnection(serverkey) if not server_connection: - print "SSH connection to host '%s' has not been established" % serverkey + logger.error("SSH connection to host '%s' has not been established" + % serverkey) return 1 if fstype is None: fstype = "xfs" + command.extend(["-t", fstype]) + + if options: + command.extend([options]) + + command.extend([device, dirpath]) + command = ' '.join(command) - command = base_command + "-t " + fstype + " " + device + " " + dirpath - - print "%s : %s" % (serverkey, command) + logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = server_connection.executecommand(command) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) 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: diff --git a/libs/utils/managerutils.py b/libs/utils/managerutils.py index ca38b3f..6f72e99 100644 --- a/libs/utils/managerutils.py +++ b/libs/utils/managerutils.py @@ -13,13 +13,19 @@ from atfglobals import GlobalObj def ssh_connect(hostkey): """ """ + logger = GlobalObj.getLoggerObj() env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() + if cm is None: + logger.error("Init ConnectionsManager") + return 1 + host_connection = cm.getConnection(hostkey) if not host_connection: host_obj = env.getHost(hostkey) if not host_obj: - print "Invalid Host. %s is not defined in TestEnvironment" % hostkey + logger.error("Invalid Host. %s is not defined in TestEnvironment" + % hostkey) return 1 else: host_connection = ssh.SshConnection() @@ -33,13 +39,14 @@ def ssh_connect(hostkey): cm.addClient(hostkey, host_connection) return 0 else: - print "Connection to %s already exist" % hostkey + logger.debug("Connection to %s already exist" % hostkey) return 0 def ssh_connect_allhosts(): """ """ + GlobalObj.initConnectionsManagerObj() env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() hosts_keys = env.getHostsKeys() diff --git a/libs/utils/serverutils.py b/libs/utils/serverutils.py index 618ee23..78e48b8 100644 --- a/libs/utils/serverutils.py +++ b/libs/utils/serverutils.py @@ -7,17 +7,20 @@ from atfglobals import GlobalObj def execute_on_brick(brickkey, command, commandInput=None): """ """ + logger = GlobalObj.getLoggerObj() env = GlobalObj.getTestenvObj() raw_brick_obj = env.getRawBrick(brickkey) if not raw_brick_obj: - print "InValid Brick. %s not defined in TestEnvironment" % brickkey + logger.error("InValid Brick. %s not defined in TestEnvironment" + % brickkey) return 1 serverkey = re.split("\.", raw_brick_obj.hostname, maxsplit=1)[0] brick_obj = env.getBrick(brickkey) if not brick_obj: - print "InValid Brick. %s not defined in TestEnvironment" % brickkey + logger.error("InValid Brick. %s not defined in TestEnvironment" + % brickkey) return 1 exportdirpath = brick_obj.path -- cgit