xref: /llvm-project/libcxx/utils/ssh.py (revision 1fc5010d6b70bb5c2330595230ca5c5fe07bcad0)
1#===----------------------------------------------------------------------===##
2#
3# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4# See https://llvm.org/LICENSE.txt for license information.
5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6#
7#===----------------------------------------------------------------------===##
8
9"""
10Runs an executable on a remote host.
11
12This is meant to be used as an executor when running the C++ Standard Library
13conformance test suite.
14"""
15
16import argparse
17import os
18import posixpath
19import subprocess
20import sys
21import tarfile
22import tempfile
23
24
25def main():
26    parser = argparse.ArgumentParser()
27    parser.add_argument('--host', type=str, required=True)
28    parser.add_argument('--execdir', type=str, required=True)
29    parser.add_argument('--codesign_identity', type=str, required=False, default=None)
30    parser.add_argument('--env', type=str, nargs='*', required=False, default=dict())
31    (args, remaining) = parser.parse_known_args(sys.argv[1:])
32
33    if len(remaining) < 2:
34        sys.stderr.write('Missing actual commands to run')
35        return 1
36
37    commandLine = remaining[1:] # Skip the '--'
38
39    ssh = lambda command: ['ssh', '-oBatchMode=yes', args.host, command]
40    scp = lambda src, dst: ['scp', '-q', '-oBatchMode=yes', src, '{}:{}'.format(args.host, dst)]
41
42    # Create a temporary directory where the test will be run.
43    # That is effectively the value of %T on the remote host.
44    tmp = subprocess.check_output(ssh('mktemp -d /tmp/libcxx.XXXXXXXXXX'), universal_newlines=True).strip()
45
46    # HACK:
47    # If an argument is a file that ends in `.tmp.exe`, assume it is the name
48    # of an executable generated by a test file. We call these test-executables
49    # below. This allows us to do custom processing like codesigning test-executables
50    # and changing their path when running on the remote host. It's also possible
51    # for there to be no such executable, for example in the case of a .sh.cpp
52    # test.
53    isTestExe = lambda exe: exe.endswith('.tmp.exe') and os.path.exists(exe)
54    pathOnRemote = lambda file: posixpath.join(tmp, os.path.basename(file))
55
56    try:
57        # Do any necessary codesigning of test-executables found in the command line.
58        if args.codesign_identity:
59            for exe in filter(isTestExe, commandLine):
60                subprocess.check_call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={})
61
62        # tar up the execution directory (which contains everything that's needed
63        # to run the test), and copy the tarball over to the remote host.
64        try:
65            tmpTar = tempfile.NamedTemporaryFile(suffix='.tar', delete=False)
66            with tarfile.open(fileobj=tmpTar, mode='w') as tarball:
67                tarball.add(args.execdir, arcname=os.path.basename(args.execdir))
68
69            # Make sure we close the file before we scp it, because accessing
70            # the temporary file while still open doesn't work on Windows.
71            tmpTar.close()
72            remoteTarball = pathOnRemote(tmpTar.name)
73            subprocess.check_call(scp(tmpTar.name, remoteTarball))
74        finally:
75            # Make sure we close the file in case an exception happens before
76            # we've closed it above -- otherwise close() is idempotent.
77            tmpTar.close()
78            os.remove(tmpTar.name)
79
80        # Untar the dependencies in the temporary directory and remove the tarball.
81        remoteCommands = [
82            'tar -xf {} -C {} --strip-components 1'.format(remoteTarball, tmp),
83            'rm {}'.format(remoteTarball)
84        ]
85
86        # Make sure all test-executables in the remote command line have 'execute'
87        # permissions on the remote host. The host that compiled the test-executable
88        # might not have a notion of 'executable' permissions.
89        for exe in map(pathOnRemote, filter(isTestExe, commandLine)):
90            remoteCommands.append('chmod +x {}'.format(exe))
91
92        # Execute the command through SSH in the temporary directory, with the
93        # correct environment. We tweak the command line to run it on the remote
94        # host by transforming the path of test-executables to their path in the
95        # temporary directory on the remote host.
96        commandLine = (pathOnRemote(x) if isTestExe(x) else x for x in commandLine)
97        remoteCommands.append('cd {}'.format(tmp))
98        if args.env:
99            remoteCommands.append('export {}'.format(' '.join(args.env)))
100        remoteCommands.append(subprocess.list2cmdline(commandLine))
101
102        # Finally, SSH to the remote host and execute all the commands.
103        rc = subprocess.call(ssh(' && '.join(remoteCommands)))
104        return rc
105
106    finally:
107        # Make sure the temporary directory is removed when we're done.
108        subprocess.check_call(ssh('rm -r {}'.format(tmp)))
109
110
111if __name__ == '__main__':
112    exit(main())
113