|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import random |
| 5 | +import string |
| 6 | +import time |
| 7 | +import shlex |
| 8 | +from contextlib import contextmanager |
| 9 | + |
| 10 | +import attr |
| 11 | + |
| 12 | +from .ssh import sshmanager |
| 13 | +from .timeout import Timeout |
| 14 | +from ..driver.exception import ExecutionError |
| 15 | +from ..resource.common import Resource, NetworkResource |
| 16 | + |
| 17 | + |
| 18 | +DEFAULT_SFTP_SERVER = "podman run --rm -i --mount type=bind,source={path},target={path} labgrid/sftp-server:latest usr/lib/ssh/sftp-server -e {readonly}" |
| 19 | +DEFAULT_RO_OPT = "-R" |
| 20 | + |
| 21 | + |
| 22 | +@attr.s |
| 23 | +class SSHFsExport: |
| 24 | + """ |
| 25 | + Exports a local directory to a remote device using "reverse" SSH FS |
| 26 | + """ |
| 27 | + |
| 28 | + local_path = attr.ib( |
| 29 | + validator=attr.validators.instance_of(str), |
| 30 | + converter=lambda x: os.path.abspath(str(x)), |
| 31 | + ) |
| 32 | + resource = attr.ib( |
| 33 | + validator=attr.validators.instance_of(Resource), |
| 34 | + ) |
| 35 | + readonly = attr.ib( |
| 36 | + default=True, |
| 37 | + validator=attr.validators.instance_of(bool), |
| 38 | + ) |
| 39 | + |
| 40 | + def __attrs_post_init__(self): |
| 41 | + if not os.path.isdir(self.local_path): |
| 42 | + raise FileNotFoundError(f"Local directory {self.local_path} not found") |
| 43 | + self.logger = logging.getLogger(f"{self}") |
| 44 | + |
| 45 | + @contextmanager |
| 46 | + def export(self, remote_path=None): |
| 47 | + host = self.resource.host |
| 48 | + conn = sshmanager.open(host) |
| 49 | + |
| 50 | + # If no remote path was specified, mount on a temporary directory |
| 51 | + if remote_path is None: |
| 52 | + tmpname = "".join(random.choices(string.ascii_lowercase, k=10)) |
| 53 | + remote_path = f"/tmp/labgrid-sshfs/{tmpname}/" |
| 54 | + conn.run_check(f"mkdir -p {remote_path}") |
| 55 | + |
| 56 | + env = self.resource.target.env |
| 57 | + |
| 58 | + if env is None: |
| 59 | + sftp_server_opt = DEFAULT_SFTP_SERVER |
| 60 | + ro_opt = DEFAULT_RO_OPT |
| 61 | + else: |
| 62 | + sftp_server_opt = config.get_option("sftp_server", DEFAULT_SFTP_SERVER) |
| 63 | + ro_opt = config.env.get_option("sftp_server_readonly_opt", DEFAULT_RO_OPT) |
| 64 | + |
| 65 | + sftp_command = shlex.split( |
| 66 | + sftp_server_opt.format( |
| 67 | + path=self.local_path, |
| 68 | + uid=str(os.getuid()), |
| 69 | + gid=str(os.getgid()), |
| 70 | + readonly=ro_opt if self.readonly else "", |
| 71 | + ) |
| 72 | + ) |
| 73 | + |
| 74 | + sshfs_command = conn.get_prefix() + [ |
| 75 | + "sshfs", |
| 76 | + "-o", |
| 77 | + "slave", |
| 78 | + "-o", |
| 79 | + "idmap=user", |
| 80 | + f":{self.local_path}", |
| 81 | + remote_path, |
| 82 | + ] |
| 83 | + |
| 84 | + self.logger.info( |
| 85 | + "Running %s <-> %s", " ".join(sftp_command), " ".join(sshfs_command) |
| 86 | + ) |
| 87 | + |
| 88 | + # Reverse sshfs requires that sftp-server running locally and sshfs |
| 89 | + # running remotely each have their stdout connected to the others |
| 90 | + # stdin. Connecting sftp-server stdout to sshfs stdin is done using |
| 91 | + # Popen pipes, but in order to connect sshfs stdout to sftp-stdin, an |
| 92 | + # external pipe is needed. |
| 93 | + (rfd, wfd) = os.pipe2(os.O_CLOEXEC) |
| 94 | + try: |
| 95 | + with subprocess.Popen( |
| 96 | + sftp_command, |
| 97 | + stdout=subprocess.PIPE, |
| 98 | + stdin=rfd, |
| 99 | + ) as sftp_server, subprocess.Popen( |
| 100 | + sshfs_command, |
| 101 | + stdout=wfd, |
| 102 | + stdin=sftp_server.stdout, |
| 103 | + ) as sshfs: |
| 104 | + # Close all file descriptor open in this process. This way, if |
| 105 | + # either process exits, the other will get the EPIPE error and |
| 106 | + # exit also. If this process doesn't close its copy of the |
| 107 | + # descriptors, that won't happen |
| 108 | + sftp_server.stdout.close() |
| 109 | + |
| 110 | + os.close(rfd) |
| 111 | + rfd = -1 |
| 112 | + |
| 113 | + os.close(wfd) |
| 114 | + wfd = -1 |
| 115 | + |
| 116 | + # Wait until the mount point appears remotely |
| 117 | + t = Timeout(30.0) |
| 118 | + |
| 119 | + while not t.expired: |
| 120 | + (_, _, exitcode) = conn.run(f"mountpoint --quiet {remote_path}") |
| 121 | + |
| 122 | + if exitcode == 0: |
| 123 | + break |
| 124 | + |
| 125 | + if sshfs.poll() is not None: |
| 126 | + raise ExecutionError( |
| 127 | + "sshfs process exited with {sshfs.returncode}" |
| 128 | + ) |
| 129 | + |
| 130 | + if sftp_server.poll() is not None: |
| 131 | + raise ExecutionError( |
| 132 | + "sftp process exited with {sftp_server.returncode}" |
| 133 | + ) |
| 134 | + |
| 135 | + time.sleep(1) |
| 136 | + |
| 137 | + if t.expired: |
| 138 | + raise TimeoutError("Timeout waiting for SSH fs to mount") |
| 139 | + |
| 140 | + try: |
| 141 | + yield remote_path |
| 142 | + finally: |
| 143 | + sshfs.terminate() |
| 144 | + sftp_server.terminate() |
| 145 | + |
| 146 | + finally: |
| 147 | + # Cleanup if not done already |
| 148 | + if rfd >= 0: |
| 149 | + os.close(rfd) |
| 150 | + |
| 151 | + if wfd >= 0: |
| 152 | + os.close(wfd) |
0 commit comments