Skip to content

Commit

Permalink
[libc++] SSH: Create a tarball of dependencies and scp that instead
Browse files Browse the repository at this point in the history
The benefit of doing this is that we can now handle directories that
contain symlinks and other arbitrary things, such as the static_test_env
required by filesystem tests.

As a fly-by fix, we also accumulate several commands to perform over SSH
and execute them at once instead of SSHing several times. This should be
faster on average.
  • Loading branch information
ldionne committed Apr 1, 2020
1 parent 8ba8a4a commit 92e563b
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions libcxx/utils/ssh.py
Expand Up @@ -18,6 +18,8 @@
import posixpath
import subprocess
import sys
import tarfile
import tempfile


def main():
Expand All @@ -35,7 +37,7 @@ def main():
commandLine = remaining[1:] # Skip the '--'

ssh = lambda command: ['ssh', '-oBatchMode=yes', args.host, command]
scp = lambda src, dst: ['scp', '-oBatchMode=yes', '-p', '-r', src, '{}:{}'.format(args.host, dst)]
scp = lambda src, dst: ['scp', '-oBatchMode=yes', src, '{}:{}'.format(args.host, dst)]

# Create a temporary directory where the test will be run.
tmp = subprocess.check_output(ssh('mktemp -d /tmp/libcxx.XXXXXXXXXX'), universal_newlines=True).strip()
Expand All @@ -48,54 +50,58 @@ def main():
# for there to be no such executable, for example in the case of a .sh.cpp
# test.
isTestExe = lambda exe: exe.endswith('.tmp.exe') and os.path.exists(exe)
testExeOnRemote = lambda exe: posixpath.join(tmp, os.path.basename(exe))
pathOnRemote = lambda file: posixpath.join(tmp, os.path.basename(file))

try:
# Do any necessary codesigning of test-executables found in the command line.
if args.codesign_identity:
for exe in filter(isTestExe, commandLine):
rc = subprocess.call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={})
if rc != 0:
sys.stderr.write('Failed to codesign: {}'.format(exe))
return rc

# Ensure the test dependencies exist and scp them to the temporary directory.
# Test dependencies can be either files or directories, so the `scp` command
# needs to use `-r`.
for dep in args.dependencies:
if not os.path.exists(dep):
sys.stderr.write('Missing file or directory {} marked as a dependency of a test'.format(dep))
return 1
rc = subprocess.call(scp(dep, tmp))
if rc != 0:
sys.stderr.write('Failed to copy dependency "{}" to remote host'.format(dep))
return rc
subprocess.check_call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={})

# Ensure the test dependencies exist, tar them up and copy the tarball
# over to the remote host.
with tempfile.NamedTemporaryFile(suffix='.tar') as tmpTar:
with tarfile.open(fileobj=tmpTar, mode='w') as tarball:
for dep in args.dependencies:
if not os.path.exists(dep):
sys.stderr.write('Missing file or directory "{}" marked as a dependency of a test'.format(dep))
return 1
tarball.add(dep, arcname=os.path.basename(dep))

remoteTarball = pathOnRemote(tmpTar.name)
tmpTar.flush()
subprocess.check_call(scp(tmpTar.name, remoteTarball))

# Untar the dependencies in the temporary directory and remove the tarball.
remoteCommands = [
'tar -xf {} -C {}'.format(remoteTarball, tmp),
'rm {}'.format(remoteTarball)
]

# Make sure all test-executables in the remote command line have 'execute'
# permissions on the remote host. The host that compiled the test-executable
# might not have a notion of 'executable' permissions.
for exe in map(testExeOnRemote, filter(isTestExe, commandLine)):
rc = subprocess.call(ssh('chmod +x {}'.format(exe)))
if rc != 0:
sys.stderr.write('Failed to chmod +x test-executable "{}" on the remote host'.format(exe))
return rc
for exe in map(pathOnRemote, filter(isTestExe, commandLine)):
remoteCommands.append('chmod +x {}'.format(exe))

# Execute the command through SSH in the temporary directory, with the
# correct environment. We tweak the command line to run it on the remote
# host by transforming the path of test-executables to their path in the
# temporary directory, where we know they have been copied when we handled
# test dependencies above.
commands = [
remoteCommands += [
'cd {}'.format(tmp),
'export {}'.format(' '.join(args.env)),
' '.join(testExeOnRemote(x) if isTestExe(x) else x for x in commandLine)
' '.join(pathOnRemote(x) if isTestExe(x) else x for x in commandLine)
]
rc = subprocess.call(ssh(' && '.join(commands)))

# Finally, SSH to the remote host and execute all the commands.
rc = subprocess.call(ssh(' && '.join(remoteCommands)))
return rc

finally:
# Make sure the temporary directory is removed when we're done.
subprocess.call(ssh('rm -r {}'.format(tmp)))
subprocess.check_call(ssh('rm -r {}'.format(tmp)))


if __name__ == '__main__':
Expand Down

0 comments on commit 92e563b

Please sign in to comment.