Skip to content

Commit

Permalink
Merge pull request #1323 from adisbladis/scp-to-rsync
Browse files Browse the repository at this point in the history
Use rsync instead of scp for file copying
  • Loading branch information
grahamc committed May 1, 2020
2 parents 92abb11 + a07bdf6 commit d3c127c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions default.nix
Expand Up @@ -18,6 +18,7 @@ in pkgs.poetry2nix.mkPoetryApplication {

propagatedBuildInputs = [
pkgs.openssh
pkgs.rsync
];

nativeBuildInputs = [
Expand Down
28 changes: 18 additions & 10 deletions nixops/backends/__init__.py
Expand Up @@ -419,27 +419,35 @@ def copy_closure_to(self, path):
env=env,
)

def get_scp_name(self):
def _get_scp_name(self) -> str:
ssh_name = self.get_ssh_name()
# ipv6 addresses have to be wrapped in brackets for scp
if ":" in ssh_name:
return "[%s]" % (ssh_name)
return ssh_name

def upload_file(self, source, target, recursive=False):
def _fmt_rsync_command(self, *args: str, recursive: bool = False) -> List[str]:
master = self.ssh.get_master()
cmdline = ["scp"] + self.get_ssh_flags(True) + master.opts

ssh_cmdline: List[str] = ["ssh"] + self.get_ssh_flags() + master.opts
cmdline = ["rsync", "-e", nixops.util.shlex_join(ssh_cmdline)]
if recursive:
cmdline += ["-r"]
cmdline += [source, "root@" + self.get_scp_name() + ":" + target]

cmdline.extend(args)

return cmdline

def upload_file(self, source: str, target: str, recursive: bool = False):
cmdline = self._fmt_rsync_command(
source, "root@" + self._get_scp_name() + ":" + target, recursive=recursive,
)
return self._logged_exec(cmdline)

def download_file(self, source, target, recursive=False):
master = self.ssh.get_master()
cmdline = ["scp"] + self.get_ssh_flags(True) + master.opts
if recursive:
cmdline += ["-r"]
cmdline += ["root@" + self.get_scp_name() + ":" + source, target]
def download_file(self, source: str, target: str, recursive: bool = False):
cmdline = self._fmt_rsync_command(
"root@" + self._get_scp_name() + ":" + source, target, recursive=recursive,
)
return self._logged_exec(cmdline)

def get_console_output(self):
Expand Down
7 changes: 7 additions & 0 deletions nixops/util.py
Expand Up @@ -18,6 +18,7 @@
import re
import typeguard
import inspect
import shlex
from typing import (
Callable,
List,
Expand All @@ -33,6 +34,7 @@
Hashable,
TypeVar,
Generic,
Iterable,
)

# the following ansi_ imports are for backwards compatability. They
Expand All @@ -43,6 +45,11 @@
from io import StringIO


def shlex_join(split_command: Iterable[str]) -> str:
"""Backport of shlex.join from python 3.8"""
return " ".join(shlex.quote(arg) for arg in split_command)


devnull = open(os.devnull, "r+")


Expand Down
1 change: 1 addition & 0 deletions shell.nix
Expand Up @@ -13,6 +13,7 @@ in pkgs.mkShell {
})
pkgs.openssh
pkgs.poetry
pkgs.rsync # Included by default on NixOS
];

shellHook = ''
Expand Down

0 comments on commit d3c127c

Please sign in to comment.