diff options
author | Jonathan Abrahams <jonathan@mongodb.com> | 2018-03-26 11:25:04 -0400 |
---|---|---|
committer | Jonathan Abrahams <jonathan@mongodb.com> | 2018-03-26 13:04:25 -0400 |
commit | 36148ad8bbdb94162b2926f4700d935ee4dc5994 (patch) | |
tree | 1d893c4ca0b0afa407f7724c7942dfbf643560af /buildscripts/remote_operations.py | |
parent | d62d631f0ca40c5199fdfae2980080ca0cc982b5 (diff) | |
download | mongo-36148ad8bbdb94162b2926f4700d935ee4dc5994.tar.gz |
SERVER-23312 Format Python files with yapf
Diffstat (limited to 'buildscripts/remote_operations.py')
-rwxr-xr-x | buildscripts/remote_operations.py | 257 |
1 files changed, 94 insertions, 163 deletions
diff --git a/buildscripts/remote_operations.py b/buildscripts/remote_operations.py index 18e29fdf0db..b75af20a049 100755 --- a/buildscripts/remote_operations.py +++ b/buildscripts/remote_operations.py @@ -1,5 +1,4 @@ #!/usr/bin/env python - """Remote access utilities, via ssh & scp.""" from __future__ import print_function @@ -21,8 +20,7 @@ if os.name == "posix" and sys.version_info[0] == 2: import warnings warnings.warn(("Falling back to using the subprocess module because subprocess32 isn't" " available. When using the subprocess module, a child process may trigger" - " an invalid free(). See SERVER-22219 for more details."), - RuntimeWarning) + " an invalid free(). See SERVER-22219 for more details."), RuntimeWarning) import subprocess else: import subprocess @@ -52,26 +50,15 @@ def posix_path(path): path = path[1:-1] drive, new_path = os.path.splitdrive(path) if drive: - new_path = posixpath.join( - "/cygdrive", - drive.split(":")[0], - *re.split("/|\\\\", new_path)) + new_path = posixpath.join("/cygdrive", drive.split(":")[0], *re.split("/|\\\\", new_path)) return "{quote}{path}{quote}".format(quote=path_quote, path=new_path) class RemoteOperations(object): """Class to support remote operations.""" - def __init__(self, - user_host, - ssh_connection_options=None, - ssh_options=None, - scp_options=None, - retries=0, - retry_sleep=0, - debug=False, - shell_binary="/bin/bash", - use_shell=False): + def __init__(self, user_host, ssh_connection_options=None, ssh_options=None, scp_options=None, + retries=0, retry_sleep=0, debug=False, shell_binary="/bin/bash", use_shell=False): self.user_host = user_host self.ssh_connection_options = ssh_connection_options if ssh_connection_options else "" @@ -92,17 +79,15 @@ class RemoteOperations(object): if not self.use_shell: cmd = shlex.split(cmd) # Use a common pipe for stdout & stderr for logging. - process = subprocess.Popen(cmd, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=self.use_shell) buff_stdout, _ = process.communicate() return process.poll(), buff_stdout def _remote_access(self): """ This will check if a remote session is possible. """ - cmd = "ssh {} {} {} date".format( - self.ssh_connection_options, self.ssh_options, self.user_host) + cmd = "ssh {} {} {} date".format(self.ssh_connection_options, self.ssh_options, + self.user_host) attempt_num = 0 buff = "" while True: @@ -159,13 +144,9 @@ class RemoteOperations(object): operation_param = "{}".format(operation_param.replace("'", r"\'")) operation_param = "{}".format(operation_param.replace("\"", r"\"")) dollar = "$" - cmd = "ssh {} {} {} {} -c \"{}'{}'\"".format( - self.ssh_connection_options, - self.ssh_options, - self.user_host, - self.shell_binary, - dollar, - operation_param) + cmd = "ssh {} {} {} {} -c \"{}'{}'\"".format(self.ssh_connection_options, + self.ssh_options, self.user_host, + self.shell_binary, dollar, operation_param) cmds.append(cmd) elif operation_type == "copy_to": @@ -182,16 +163,15 @@ class RemoteOperations(object): elif operation_type == "copy_from": operation_dir = operation_dir if operation_dir else "." if not os.path.isdir(operation_dir): - raise ValueError( - "Local directory '{}' does not exist.".format(operation_dir)) + raise ValueError("Local directory '{}' does not exist.".format(operation_dir)) # We support multiple files being copied from the remote host # by invoking scp for each file specified. # Note - this is a method which scp does not support directly. for copy_file in operation_param: copy_file = posix_path(copy_file) - cmd = "scp -r {} {} {}:".format( - self.ssh_connection_options, self.scp_options, self.user_host) + cmd = "scp -r {} {} {}:".format(self.ssh_connection_options, self.scp_options, + self.user_host) # Quote (on Posix), and escape the file if there are spaces. # Note - we do not support other non-ASCII characters in a file name. quote = "\"" if not _IS_WINDOWS else "" @@ -202,9 +182,8 @@ class RemoteOperations(object): cmds.append(cmd) else: - raise ValueError( - "Invalid operation '{}' specified, choose from {}.".format( - operation_type, _OPERATIONS)) + raise ValueError("Invalid operation '{}' specified, choose from {}.".format( + operation_type, _OPERATIONS)) final_ret = 0 buff = "" @@ -217,24 +196,18 @@ class RemoteOperations(object): def shell(self, operation_param, operation_dir=None): """ Helper for remote shell operations. """ - return self.operation( - operation_type="shell", - operation_param=operation_param, - operation_dir=operation_dir) + return self.operation(operation_type="shell", operation_param=operation_param, + operation_dir=operation_dir) def copy_to(self, operation_param, operation_dir=None): """ Helper for remote copy_to operations. """ - return self.operation( - operation_type="copy_to", - operation_param=operation_param, - operation_dir=operation_dir) + return self.operation(operation_type="copy_to", operation_param=operation_param, + operation_dir=operation_dir) def copy_from(self, operation_param, operation_dir=None): """ Helper for remote copy_from operations. """ - return self.operation( - operation_type="copy_from", - operation_param=operation_param, - operation_dir=operation_dir) + return self.operation(operation_type="copy_from", operation_param=operation_param, + operation_dir=operation_dir) def main(): @@ -245,114 +218,77 @@ def main(): shell_options = optparse.OptionGroup(parser, "Shell options") copy_options = optparse.OptionGroup(parser, "Copy options") - parser.add_option("--userHost", - dest="user_host", - default=None, - help="User and remote host to execute commands on [REQUIRED]." - " Examples, 'user@1.2.3.4' or 'user@myhost.com'.") - - parser.add_option("--operation", - dest="operation", - default="shell", - choices=_OPERATIONS, - help="Remote operation to perform, choose one of '{}'," - " defaults to '%default'.".format(", ".join(_OPERATIONS))) - - control_options.add_option("--sshConnectionOptions", - dest="ssh_connection_options", - default=None, - action="append", - help="SSH connection options which are common to ssh and scp." - " More than one option can be specified either" - " in one quoted string or by specifying" - " this option more than once. Example options:" - " '-i $HOME/.ssh/access.pem -o ConnectTimeout=10" - " -o ConnectionAttempts=10'") - - control_options.add_option("--sshOptions", - dest="ssh_options", - default=None, - action="append", - help="SSH specific options." - " More than one option can be specified either" - " in one quoted string or by specifying" - " this option more than once. Example options:" - " '-t' or '-T'") - - control_options.add_option("--scpOptions", - dest="scp_options", - default=None, - action="append", - help="SCP specific options." - " More than one option can be specified either" - " in one quoted string or by specifying" - " this option more than once. Example options:" - " '-l 5000'") - - control_options.add_option("--retries", - dest="retries", - type=int, - default=0, - help="Number of retries to attempt for operation," - " defaults to '%default'.") - - control_options.add_option("--retrySleep", - dest="retry_sleep", - type=int, - default=10, - help="Number of seconds to wait between retries," - " defaults to '%default'.") - - control_options.add_option("--debug", - dest="debug", - action="store_true", - default=False, + parser.add_option("--userHost", dest="user_host", default=None, + help=("User and remote host to execute commands on [REQUIRED]." + " Examples, 'user@1.2.3.4' or 'user@myhost.com'.")) + + parser.add_option("--operation", dest="operation", default="shell", choices=_OPERATIONS, + help=("Remote operation to perform, choose one of '{}'," + " defaults to '%default'.".format(", ".join(_OPERATIONS)))) + + control_options.add_option("--sshConnectionOptions", dest="ssh_connection_options", + default=None, action="append", + help=("SSH connection options which are common to ssh and scp." + " More than one option can be specified either" + " in one quoted string or by specifying" + " this option more than once. Example options:" + " '-i $HOME/.ssh/access.pem -o ConnectTimeout=10" + " -o ConnectionAttempts=10'")) + + control_options.add_option("--sshOptions", dest="ssh_options", default=None, action="append", + help=("SSH specific options." + " More than one option can be specified either" + " in one quoted string or by specifying" + " this option more than once. Example options:" + " '-t' or '-T'")) + + control_options.add_option("--scpOptions", dest="scp_options", default=None, action="append", + help=("SCP specific options." + " More than one option can be specified either" + " in one quoted string or by specifying" + " this option more than once. Example options:" + " '-l 5000'")) + + control_options.add_option("--retries", dest="retries", type=int, default=0, + help=("Number of retries to attempt for operation," + " defaults to '%default'.")) + + control_options.add_option("--retrySleep", dest="retry_sleep", type=int, default=10, + help=("Number of seconds to wait between retries," + " defaults to '%default'.")) + + control_options.add_option("--debug", dest="debug", action="store_true", default=False, help="Provides debug output.") - control_options.add_option("--verbose", - dest="verbose", - action="store_true", - default=False, + control_options.add_option("--verbose", dest="verbose", action="store_true", default=False, help="Print exit status and output at end.") - shell_options.add_option("--commands", - dest="remote_commands", - default=None, - action="append", - help="Commands to excute on the remote host. The" - " commands must be separated by a ';' and can either" - " be specifed in a quoted string or by specifying" - " this option more than once. A ';' will be added" - " between commands when this option is specifed" - " more than once.") - - shell_options.add_option("--commandDir", - dest="command_dir", - default=None, - help="Working directory on remote to execute commands" - " form. Defaults to remote login directory.") - - copy_options.add_option("--file", - dest="files", - default=None, - action="append", - help="The file to copy to/from remote host. To" - " support spaces in the file, each file must be" - " specified using this option more than once.") - - copy_options.add_option("--remoteDir", - dest="remote_dir", - default=None, - help="Remote directory to copy to, only applies when" - " operation is 'copy_to'. Defaults to the login" - " directory on the remote host.") - - copy_options.add_option("--localDir", - dest="local_dir", - default=".", - help="Local directory to copy to, only applies when" - " operation is 'copy_from'. Defaults to the" - " current directory, '%default'.") + shell_options.add_option("--commands", dest="remote_commands", default=None, action="append", + help=("Commands to excute on the remote host. The" + " commands must be separated by a ';' and can either" + " be specifed in a quoted string or by specifying" + " this option more than once. A ';' will be added" + " between commands when this option is specifed" + " more than once.")) + + shell_options.add_option("--commandDir", dest="command_dir", default=None, + help=("Working directory on remote to execute commands" + " form. Defaults to remote login directory.")) + + copy_options.add_option("--file", dest="files", default=None, action="append", + help=("The file to copy to/from remote host. To" + " support spaces in the file, each file must be" + " specified using this option more than once.")) + + copy_options.add_option("--remoteDir", dest="remote_dir", default=None, + help=("Remote directory to copy to, only applies when" + " operation is 'copy_to'. Defaults to the login" + " directory on the remote host.")) + + copy_options.add_option("--localDir", dest="local_dir", default=".", + help=("Local directory to copy to, only applies when" + " operation is 'copy_from'. Defaults to the" + " current directory, '%default'.")) parser.add_option_group(control_options) parser.add_option_group(shell_options) @@ -367,15 +303,14 @@ def main(): if options.operation == "shell": if not getattr(options, "remote_commands", None): parser.print_help() - parser.error("Missing required '{}' option '{}'".format( - options.operation, "--commands")) + parser.error("Missing required '{}' option '{}'".format(options.operation, + "--commands")) operation_param = ";".join(options.remote_commands) operation_dir = options.command_dir else: if not getattr(options, "files", None): parser.print_help() - parser.error("Missing required '{}' option '{}'".format( - options.operation, "--file")) + parser.error("Missing required '{}' option '{}'".format(options.operation, "--file")) operation_param = options.files if options.operation == "copy_to": operation_dir = options.remote_dir @@ -398,13 +333,9 @@ def main(): scp_options = " ".join(options.scp_options) remote_op = RemoteOperations( - user_host=options.user_host, - ssh_connection_options=ssh_connection_options, - ssh_options=ssh_options, - scp_options=scp_options, - retries=options.retries, - retry_sleep=options.retry_sleep, - debug=options.debug) + user_host=options.user_host, ssh_connection_options=ssh_connection_options, + ssh_options=ssh_options, scp_options=scp_options, retries=options.retries, + retry_sleep=options.retry_sleep, debug=options.debug) ret_code, buffer = remote_op.operation(options.operation, operation_param, operation_dir) if options.verbose: print("Return code: {} for command {}".format(ret_code, sys.argv)) |