mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[remote-run] Add support for custom ssh_config options with -o (#18814)
This has precedent in 'sftp', which also forwards anything passed with -o through to ssh.
This commit is contained in:
27
test/remote-run/custom-options.test-sh
Normal file
27
test/remote-run/custom-options.test-sh
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
RUN: %utils/remote-run -n --remote-dir /xyz-REMOTE -o FIRST_OPT -o SECOND_OPT --output-prefix %t some_user@some_host:12345 cp %t/nested/input %t/nested/output 2>&1 >/dev/null | %FileCheck %s
|
||||||
|
|
||||||
|
CHECK: /usr/bin/ssh -n
|
||||||
|
CHECK-DAG: -p 12345
|
||||||
|
CHECK-DAG: -o FIRST_OPT -o SECOND_OPT
|
||||||
|
CHECK-SAME: some_user@some_host -- '/usr/bin/env' '/bin/mkdir' '-p' '{{.+}}-REMOTE/output/nested'
|
||||||
|
|
||||||
|
CHECK-NEXT: /usr/bin/sftp
|
||||||
|
CHECK-DAG: -P 12345
|
||||||
|
CHECK-DAG: -o FIRST_OPT -o SECOND_OPT
|
||||||
|
CHECK-SAME: some_user@some_host
|
||||||
|
CHECK-DAG: -put '{{.+}}/nested/output' '/xyz-REMOTE/output/nested/output'
|
||||||
|
CHECK-DAG: -put '{{.+}}/nested/input' '/xyz-REMOTE/output/nested/input'
|
||||||
|
|
||||||
|
CHECK: /usr/bin/ssh -n
|
||||||
|
CHECK-DAG: -p 12345
|
||||||
|
CHECK-DAG: -o FIRST_OPT -o SECOND_OPT
|
||||||
|
CHECK-SAME: some_user@some_host -- '/usr/bin/env' 'cp'
|
||||||
|
|
||||||
|
CHECK-NEXT: {{^}}/bin/mkdir -p {{.+}}/nested
|
||||||
|
|
||||||
|
CHECK-NEXT: /usr/bin/sftp
|
||||||
|
CHECK-DAG: -P 12345
|
||||||
|
CHECK-DAG: -o FIRST_OPT -o SECOND_OPT
|
||||||
|
CHECK-SAME: some_user@some_host
|
||||||
|
CHECK-DAG: -get '/xyz-REMOTE/output/nested/output' '{{.+}}/nested/output'
|
||||||
|
CHECK-DAG: -get '/xyz-REMOTE/output/nested/input' '{{.+}}/nested/input'
|
||||||
@@ -93,19 +93,26 @@ class CommandRunner(object):
|
|||||||
sys.exit(sftp_proc.returncode)
|
sys.exit(sftp_proc.returncode)
|
||||||
|
|
||||||
class RemoteCommandRunner(CommandRunner):
|
class RemoteCommandRunner(CommandRunner):
|
||||||
def __init__(self, host, identity_path):
|
def __init__(self, host, identity_path, ssh_options):
|
||||||
if ':' in host:
|
if ':' in host:
|
||||||
(self.remote_host, self.port) = host.rsplit(':', 1)
|
(self.remote_host, self.port) = host.rsplit(':', 1)
|
||||||
else:
|
else:
|
||||||
self.remote_host = host
|
self.remote_host = host
|
||||||
self.port = None
|
self.port = None
|
||||||
self.identity_path = identity_path
|
self.identity_path = identity_path
|
||||||
|
self.ssh_options = ssh_options
|
||||||
|
|
||||||
def common_options(self, port_flag):
|
def common_options(self, port_flag):
|
||||||
port_option = [port_flag, self.port] if self.port else []
|
port_option = [port_flag, self.port] if self.port else []
|
||||||
identity_option = (
|
identity_option = (
|
||||||
['-i', self.identity_path] if self.identity_path else [])
|
['-i', self.identity_path] if self.identity_path else [])
|
||||||
return port_option + identity_option
|
# Interleave '-o' with each custom option.
|
||||||
|
# From https://stackoverflow.com/a/8168526,
|
||||||
|
# with explanatory help from
|
||||||
|
# https://spapas.github.io/2016/04/27/python-nested-list-comprehensions/
|
||||||
|
extra_options = [arg for option in self.ssh_options
|
||||||
|
for arg in ["-o", option]]
|
||||||
|
return port_option + identity_option + extra_options
|
||||||
|
|
||||||
def remote_invocation(self, command):
|
def remote_invocation(self, command):
|
||||||
return (['/usr/bin/ssh', '-n'] +
|
return (['/usr/bin/ssh', '-n'] +
|
||||||
@@ -164,6 +171,9 @@ def main():
|
|||||||
|
|
||||||
parser.add_argument('-i', '--identity', dest='identity', metavar='FILE',
|
parser.add_argument('-i', '--identity', dest='identity', metavar='FILE',
|
||||||
help='an SSH identity file (private key) to use')
|
help='an SSH identity file (private key) to use')
|
||||||
|
parser.add_argument('-o', '--ssh-option', action='append', default=[],
|
||||||
|
dest='ssh_options', metavar='OPTION',
|
||||||
|
help='extra SSH config options (man ssh_config)')
|
||||||
parser.add_argument('--debug-as-local', metavar='/PATH/TO/SFTP-SERVER',
|
parser.add_argument('--debug-as-local', metavar='/PATH/TO/SFTP-SERVER',
|
||||||
help='run commands locally instead of over SSH, for '
|
help='run commands locally instead of over SSH, for '
|
||||||
'debugging purposes. The "host" argument is '
|
'debugging purposes. The "host" argument is '
|
||||||
@@ -181,7 +191,7 @@ def main():
|
|||||||
args.command.insert(0, args.host)
|
args.command.insert(0, args.host)
|
||||||
del args.host
|
del args.host
|
||||||
else:
|
else:
|
||||||
runner = RemoteCommandRunner(args.host, args.identity)
|
runner = RemoteCommandRunner(args.host, args.identity, args.ssh_options)
|
||||||
runner.dry_run = args.dry_run
|
runner.dry_run = args.dry_run
|
||||||
runner.verbose = args.verbose or args.dry_run
|
runner.verbose = args.verbose or args.dry_run
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user