diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2010-02-03T15·34+0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2010-02-03T15·34+0000 |
commit | bc1e478db160059753c4bf4cb28dd50437a76b27 (patch) | |
tree | cb3f5cb95831cae721800cffd37bce5fde37cbbd /scripts/ssh.pm | |
parent | 4d8a85b8f56625616c993d0bace0a4e856e7f2c5 (diff) |
* nix-copy-closure: start only one SSH connection to the server, or
recycle an already existing connection (using OpenSSH's connection sharing feature).
Diffstat (limited to 'scripts/ssh.pm')
-rw-r--r-- | scripts/ssh.pm | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/scripts/ssh.pm b/scripts/ssh.pm new file mode 100644 index 000000000000..0295cef33b0a --- /dev/null +++ b/scripts/ssh.pm @@ -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; |