diff options
author | Marco Capitani <m.capitani@nextworks.it> | 2017-04-21 15:58:56 +0200 |
---|---|---|
committer | Marco Capitani <m.capitani@nextworks.it> | 2017-04-21 15:58:56 +0200 |
commit | 26e3e22cd6ca6676b7b99624764f8e6f1ae1479d (patch) | |
tree | 65764b5ce423dc88a318e3b7325281bf3a094070 /rumba/ssh_support.py | |
parent | ee2f6e5c21c0ba94048dbf1c86024e3181718fc2 (diff) | |
download | rumba-26e3e22cd6ca6676b7b99624764f8e6f1ae1479d.tar.gz rumba-26e3e22cd6ca6676b7b99624764f8e6f1ae1479d.zip |
rumba: log: Added logging support, migrated from print to logging.
+ Added logging, migrated, default logging configuration is to stdout.
+ Minor PEP8 adjustments all-around
Diffstat (limited to 'rumba/ssh_support.py')
-rw-r--r-- | rumba/ssh_support.py | 51 |
1 files changed, 28 insertions, 23 deletions
diff --git a/rumba/ssh_support.py b/rumba/ssh_support.py index d41eacb..84ff607 100644 --- a/rumba/ssh_support.py +++ b/rumba/ssh_support.py @@ -20,6 +20,11 @@ import os import paramiko +import rumba.log as log + + +logger = log.get_logger(__name__) + def get_ssh_client(): ssh_client = paramiko.SSHClient() @@ -34,12 +39,12 @@ def _print_stream(stream): if o != "": o_array = o.split('\\n') for oi in o_array: - print(oi) + logger.debug(oi) return o def execute_commands(testbed, ssh_config, commands, time_out=3): - ''' + """ Remote execution of a list of shell command on hostname. By default this function will exit (timeout) after 3 seconds. @@ -49,7 +54,7 @@ def execute_commands(testbed, ssh_config, commands, time_out=3): @param time_out: time_out value in seconds, error will be generated if no result received in given number of seconds, the value None can be used when no timeout is needed - ''' + """ ssh_client = get_ssh_client() if ssh_config.proxycommand is not None: @@ -64,10 +69,10 @@ def execute_commands(testbed, ssh_config, commands, time_out=3): sock=proxy) o = "" for command in commands: - print("%s@%s:%s >> %s" % (testbed.username, - ssh_config.hostname, - ssh_config.port, - command)) + logger.debug("%s@%s:%s >> %s" % (testbed.username, + ssh_config.hostname, + ssh_config.port, + command)) envars = '. /etc/profile;' command = envars + ' ' + command stdin, stdout, stderr = ssh_client.exec_command(command) @@ -77,12 +82,12 @@ def execute_commands(testbed, ssh_config, commands, time_out=3): return o except Exception as e: - print(str(e)) + logger.error(str(e)) return def execute_command(testbed, ssh_config, command, time_out=3): - ''' + """ Remote execution of a list of shell command on hostname. By default this function will exit (timeout) after 3 seconds. @@ -94,14 +99,14 @@ def execute_command(testbed, ssh_config, command, time_out=3): be used when no timeout is needed @return: stdout resulting from the command - ''' + """ o = execute_commands(testbed, ssh_config, [command], time_out) if o is not None: return o def copy_file_to_testbed(testbed, ssh_config, text, file_name): - ''' + """ Write a string to a given remote file. Overwrite the complete file if it already exists! @@ -109,7 +114,7 @@ def copy_file_to_testbed(testbed, ssh_config, text, file_name): @param ssh_config: ssh config of the node @param text: string to be written in file @param file_name: file name (including full path) on the host - ''' + """ ssh_client = get_ssh_client() if ssh_config.proxycommand is not None: @@ -131,7 +136,7 @@ def copy_file_to_testbed(testbed, ssh_config, text, file_name): del stdin, stdout err = str(stderr.read()).strip('b\'\"\\n') if err != "": - print(err) + logger.error(err) sftp_client = ssh_client.open_sftp() remote_file = sftp_client.open(file_name, 'w') @@ -140,11 +145,11 @@ def copy_file_to_testbed(testbed, ssh_config, text, file_name): remote_file.close() except Exception as e: - print(str(e)) + logger.error(str(e)) def copy_paths_to_testbed(testbed, ssh_config, paths, destination): - ''' + """ Write a string to a given remote file. Overwrite the complete file if it already exists! @@ -152,7 +157,7 @@ def copy_paths_to_testbed(testbed, ssh_config, paths, destination): @param ssh_config: ssh config of the node @param paths: source paths (local) as an iterable @param destination: destination folder name (remote) - ''' + """ ssh_client = get_ssh_client() if destination is not '' and not destination.endswith('/'): @@ -169,7 +174,7 @@ def copy_paths_to_testbed(testbed, ssh_config, paths, destination): for path in paths: file_name = os.path.basename(path) dest_file = destination + file_name - print("Copying %s to %s@%s:%s path %s" % ( + logger.info("Copying %s to %s@%s:%s path %s" % ( path, testbed.username, ssh_config.hostname, @@ -178,11 +183,11 @@ def copy_paths_to_testbed(testbed, ssh_config, paths, destination): sftp_client.put(path, dest_file) except Exception as e: - print(str(e)) + logger.error(str(e)) def copy_path_to_testbed(testbed, ssh_config, path, destination): - ''' + """ Write a string to a given remote file. Overwrite the complete file if it already exists! @@ -190,19 +195,19 @@ def copy_path_to_testbed(testbed, ssh_config, path, destination): @param ssh_config: ssh config of the node @param path: source path (local) @param destination: destination folder name (remote) - ''' + """ copy_paths_to_testbed(testbed, ssh_config, [path], destination) def setup_vlan(testbed, node, vlan_id, int_name): - ''' + """ Gets the interface (ethx) to link mapping @param testbed: testbed info @param node: the node to create the VLAN on @param vlan_id: the VLAN id @param int_name: the name of the interface - ''' + """ if testbed.username == 'root': def sudo(s): return s @@ -210,7 +215,7 @@ def setup_vlan(testbed, node, vlan_id, int_name): def sudo(s): return 'sudo ' + s - print("Setting up VLAN on node " + node.name) + logger.info("Setting up VLAN on node " + node.name) args = {'ifname': str(int_name), 'vlan': str(vlan_id)} |