Пример #1
0
    def send_rsync(self,
                   host_path: str,
                   remote_path: str,
                   params: List[str] = None) -> None:
        """Send a local file or folder to a remote instance with rsync.

        Parameters
        ----------
        host_path : str
            The local filename or folder
        remote_path : str
            The remote filename or folder to use
        params : List[str], optional
            Extra parameters to be passed to rsync.
            For example, ["--filter=':- .gitignore'"]

        Raises
        ------
        RemoteFileTransferError
            In case sending the file fails.

        """
        if not os.path.exists(host_path):
            raise errors.RemoteFileTransferError(
                f"{host_path} does not exist.")

        _from = host_path
        if os.path.isdir(host_path) and not host_path.endswith(os.sep):
            _from = f"{host_path}{os.sep}"

        _to = f"{self.username}@{self.host if self.use_public else self.private_host}:{remote_path}"

        rsync_params = ""
        if params:
            rsync_params = " ".join(params)
        cmd = (
            f'rsync {rsync_params} -ae "ssh -i {self.key} -o StrictHostKeyChecking=no" '
            f'{_from} {_to}')
        try:
            subprocess.check_call(cmd,
                                  stdout=subprocess.DEVNULL,
                                  stderr=subprocess.DEVNULL,
                                  shell=True)
            logger.debug(f"rsync {host_path} -> {remote_path} successful")
        except subprocess.CalledProcessError as e:
            raise errors.RemoteFileTransferError(e)
Пример #2
0
    def send_rsync(self, host_path: str, remote_path: str) -> None:
        """Send a local file or folder to a remote instance with rsync.

        Parameters
        ----------
        host_path : str
            The local filename or folder
        remote_path : str
            The remote filename or folder to use

        Raises
        ------
        RemoteFileTransferError
            In case sending the file fails.

        """
        if not os.path.exists(host_path):
            raise errors.RemoteFileTransferError(
                f"{host_path} does not exist.")

        _from = host_path
        if os.path.isdir(host_path) and not host_path.endswith(os.sep):
            _from = f"{host_path}{os.sep}"

        _to = f"{self.username}@{self.host if self.use_public else self.private_host}:{remote_path}"

        cmd = [
            "rsync", "-ae", f"ssh -i {self.key} -o StrictHostKeyChecking=no",
            _from, _to
        ]
        try:
            subprocess.check_call(cmd,
                                  stdout=subprocess.DEVNULL,
                                  stderr=subprocess.DEVNULL)
            logger.debug(f"rsync {host_path} -> {remote_path} successful")
        except subprocess.CalledProcessError as e:
            raise errors.RemoteFileTransferError(e)