* nix-copy-closure: start only one SSH connection to the server, or

recycle an already existing connection (using  OpenSSH's connection
  sharing feature).
This commit is contained in:
Eelco Dolstra 2010-02-03 15:34:52 +00:00
parent 4d8a85b8f5
commit bc1e478db1
3 changed files with 49 additions and 5 deletions

View file

@ -15,6 +15,7 @@ install-exec-local: readmanifest.pm download-using-manifests.pl copy-from-other-
$(INSTALL) -d $(DESTDIR)$(libexecdir)/nix $(INSTALL) -d $(DESTDIR)$(libexecdir)/nix
$(INSTALL_DATA) readmanifest.pm $(DESTDIR)$(libexecdir)/nix $(INSTALL_DATA) readmanifest.pm $(DESTDIR)$(libexecdir)/nix
$(INSTALL_DATA) readconfig.pm $(DESTDIR)$(libexecdir)/nix $(INSTALL_DATA) readconfig.pm $(DESTDIR)$(libexecdir)/nix
$(INSTALL_DATA) ssh.pm $(DESTDIR)$(libexecdir)/nix
$(INSTALL_PROGRAM) find-runtime-roots.pl $(DESTDIR)$(libexecdir)/nix $(INSTALL_PROGRAM) find-runtime-roots.pl $(DESTDIR)$(libexecdir)/nix
$(INSTALL_PROGRAM) generate-patches.pl $(DESTDIR)$(libexecdir)/nix $(INSTALL_PROGRAM) generate-patches.pl $(DESTDIR)$(libexecdir)/nix
$(INSTALL_PROGRAM) build-remote.pl $(DESTDIR)$(libexecdir)/nix $(INSTALL_PROGRAM) build-remote.pl $(DESTDIR)$(libexecdir)/nix
@ -31,6 +32,7 @@ EXTRA_DIST = nix-collect-garbage.in \
nix-channel.in \ nix-channel.in \
readmanifest.pm.in \ readmanifest.pm.in \
readconfig.pm.in \ readconfig.pm.in \
ssh.pm \
nix-build.in \ nix-build.in \
download-using-manifests.pl.in \ download-using-manifests.pl.in \
copy-from-other-stores.pl.in \ copy-from-other-stores.pl.in \

View file

@ -1,4 +1,6 @@
#! @perl@ -w #! @perl@ -w -I@libexecdir@/nix
use ssh;
my $binDir = $ENV{"NIX_BIN_DIR"} || "@bindir@"; my $binDir = $ENV{"NIX_BIN_DIR"} || "@bindir@";
@ -14,7 +16,6 @@ EOF
# Get the target host. # Get the target host.
my $sshHost; my $sshHost;
my @sshOpts = split ' ', ($ENV{"NIX_SSHOPTS"} or "");
my $sign = 0; my $sign = 0;
@ -52,6 +53,9 @@ while (@ARGV) {
} }
openSSHConnection $sshHost;
if ($toMode) { # Copy TO the remote machine. if ($toMode) { # Copy TO the remote machine.
my @allStorePaths; my @allStorePaths;
@ -73,7 +77,6 @@ if ($toMode) { # Copy TO the remote machine.
my @missing = (); my @missing = ();
while (<READ>) { while (<READ>) {
chomp; chomp;
print STDERR "target machine needs $_\n";
push @missing, $_; push @missing, $_;
} }
close READ or die; close READ or die;
@ -81,6 +84,8 @@ if ($toMode) { # Copy TO the remote machine.
# Export the store paths and import them on the remote machine. # Export the store paths and import them on the remote machine.
if (scalar @missing > 0) { if (scalar @missing > 0) {
print STDERR "copying these missing paths:\n";
print STDERR " $_\n" foreach @missing;
my $extraOpts = ""; my $extraOpts = "";
$extraOpts .= "--sign" if $sign == 1; $extraOpts .= "--sign" if $sign == 1;
system("nix-store --export $extraOpts @missing | $compressor | ssh @sshOpts $sshHost '$decompressor | nix-store --import'") == 0 system("nix-store --export $extraOpts @missing | $compressor | ssh @sshOpts $sshHost '$decompressor | nix-store --import'") == 0
@ -114,7 +119,6 @@ else { # Copy FROM the remote machine.
my @missing = (); my @missing = ();
while (<READ>) { while (<READ>) {
chomp; chomp;
print STDERR "local machine needs $_\n";
push @missing, $_; push @missing, $_;
} }
close READ or die; close READ or die;
@ -122,10 +126,12 @@ else { # Copy FROM the remote machine.
# Export the store paths on the remote machine and import them on locally. # Export the store paths on the remote machine and import them on locally.
if (scalar @missing > 0) { if (scalar @missing > 0) {
print STDERR "copying these missing paths:\n";
print STDERR " $_\n" foreach @missing;
my $extraOpts = ""; my $extraOpts = "";
$extraOpts .= "--sign" if $sign == 1; $extraOpts .= "--sign" if $sign == 1;
system("ssh @sshOpts $sshHost 'nix-store --export $extraOpts @missing | $compressor' | $decompressor | @bindir@/nix-store --import") == 0 system("ssh @sshOpts $sshHost 'nix-store --export $extraOpts @missing | $compressor' | $decompressor | @bindir@/nix-store --import") == 0
or die "copying store paths to remote machine `$sshHost' failed: $?"; or die "copying store paths from remote machine `$sshHost' failed: $?";
} }
} }

36
scripts/ssh.pm Normal file
View file

@ -0,0 +1,36 @@
use strict;
use File::Temp qw(tempdir);
our @sshOpts = split ' ', ($ENV{"NIX_SSHOPTS"} or "");
my $sshStarted = 0;
my $sshHost;
# Open a master SSH connection to `host', unless there already is a
# running master connection (as determined by `-O check').
sub openSSHConnection {
my ($host) = @_;
die if $sshStarted;
$sshHost = $host;
return if system("ssh $sshHost @sshOpts -O check 2> /dev/null") == 0;
my $tmpDir = tempdir("nix-ssh.XXXXXX", CLEANUP => 1, TMPDIR => 1)
or die "cannot create a temporary directory";
push @sshOpts, "-S", "$tmpDir/control";
system("ssh $sshHost @sshOpts -M -N -f") == 0
or die "unable to start SSH: $?";
$sshStarted = 1;
}
# Tell the master SSH client to exit.
sub closeSSHConnection {
if ($sshStarted) {
system("ssh $sshHost @sshOpts -O exit 2> /dev/null") == 0
or warn "unable to stop SSH master: $?";
}
}
END { closeSSHConnection; }
return 1;