* Refactoring.
This commit is contained in:
		
							parent
							
								
									f3584ff535
								
							
						
					
					
						commit
						7edd2e2cd2
					
				
					 1 changed files with 55 additions and 37 deletions
				
			
		| 
						 | 
					@ -14,7 +14,7 @@ EOF
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Get the target host.
 | 
					# Get the target host.
 | 
				
			||||||
my $sshHost = shift @ARGV;
 | 
					my $sshHost;
 | 
				
			||||||
my @sshOpts = split ' ', ($ENV{"NIX_SSHOPTS"} or "");
 | 
					my @sshOpts = split ' ', ($ENV{"NIX_SSHOPTS"} or "");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
my $sign = 0;
 | 
					my $sign = 0;
 | 
				
			||||||
| 
						 | 
					@ -22,60 +22,78 @@ my $sign = 0;
 | 
				
			||||||
my $compressor = "cat";
 | 
					my $compressor = "cat";
 | 
				
			||||||
my $decompressor = "cat";
 | 
					my $decompressor = "cat";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					my $toMode = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# !!! Copied from nix-pack-closure, should put this in a module.
 | 
					# !!! Copied from nix-pack-closure, should put this in a module.
 | 
				
			||||||
my %storePathsSeen;
 | 
					 | 
				
			||||||
my @storePaths = ();
 | 
					my @storePaths = ();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
while (@ARGV) {
 | 
					while (@ARGV) {
 | 
				
			||||||
    my $storePath = shift @ARGV;
 | 
					    my $arg = shift @ARGV;
 | 
				
			||||||
    if ($storePath eq "--sign") {
 | 
					    if ($arg eq "--sign") {
 | 
				
			||||||
        $sign = 1;
 | 
					        $sign = 1;
 | 
				
			||||||
        next;
 | 
					        next;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if ($storePath eq "--gzip") {
 | 
					    if ($arg eq "--gzip") {
 | 
				
			||||||
        $compressor = "gzip";
 | 
					        $compressor = "gzip";
 | 
				
			||||||
        $decompressor = "gunzip";
 | 
					        $decompressor = "gunzip";
 | 
				
			||||||
        next;
 | 
					        next;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # $storePath might be a symlink to the store, so resolve it.
 | 
					    if (!defined $sshHost) {
 | 
				
			||||||
    $storePath = (`$binDir/nix-store --query --resolve '$storePath'`
 | 
					        $sshHost = $arg;
 | 
				
			||||||
        or die "cannot resolve `$storePath'");
 | 
					        next;
 | 
				
			||||||
    chomp $storePath;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # Get the closure of this path.
 | 
					 | 
				
			||||||
    my $pid = open(READ,
 | 
					 | 
				
			||||||
        "$binDir/nix-store --query --requisites '$storePath'|") or die;
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    while (<READ>) {
 | 
					 | 
				
			||||||
        chomp;
 | 
					 | 
				
			||||||
        die "bad: $_" unless /^\//;
 | 
					 | 
				
			||||||
        if (!defined $storePathsSeen{$_}) {
 | 
					 | 
				
			||||||
            push @storePaths, $_;
 | 
					 | 
				
			||||||
            $storePathsSeen{$_} = 1;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    close READ or die "nix-store failed: $?";
 | 
					    push @storePaths, $arg;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Ask the remote host which paths are invalid.
 | 
					if ($toMode) { # Copy TO the remote machine.
 | 
				
			||||||
open(READ, "ssh @sshOpts $sshHost nix-store --check-validity --print-invalid @storePaths|");
 | 
					
 | 
				
			||||||
my @missing = ();
 | 
					    my @allStorePaths;
 | 
				
			||||||
while (<READ>) {
 | 
					    my %storePathsSeen;
 | 
				
			||||||
    chomp;
 | 
					
 | 
				
			||||||
    print STDERR "target needs $_\n";
 | 
					    foreach my $storePath (@storePaths) {
 | 
				
			||||||
    push @missing, $_;
 | 
					        # $arg might be a symlink to the store, so resolve it.
 | 
				
			||||||
}
 | 
					        my $storePath2 = (`$binDir/nix-store --query --resolve '$storePath'`
 | 
				
			||||||
close READ or die;
 | 
					            or die "cannot resolve `$storePath'");
 | 
				
			||||||
 | 
					        chomp $storePath2;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Get the closure of this path.
 | 
				
			||||||
 | 
					        my $pid = open(READ,
 | 
				
			||||||
 | 
					            "$binDir/nix-store --query --requisites '$storePath2'|") or die;
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					        while (<READ>) {
 | 
				
			||||||
 | 
					            chomp;
 | 
				
			||||||
 | 
					            die "bad: $_" unless /^\//;
 | 
				
			||||||
 | 
					            if (!defined $storePathsSeen{$_}) {
 | 
				
			||||||
 | 
					                push @allStorePaths, $_;
 | 
				
			||||||
 | 
					                $storePathsSeen{$_} = 1
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        close READ or die "nix-store failed: $?";
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Export the store paths and import them on the remote machine.
 | 
					    # Ask the remote host which paths are invalid.
 | 
				
			||||||
if (scalar @missing > 0) {
 | 
					    open(READ, "ssh @sshOpts $sshHost nix-store --check-validity --print-invalid @allStorePaths|");
 | 
				
			||||||
    my $extraOpts = "";
 | 
					    my @missing = ();
 | 
				
			||||||
    $extraOpts .= "--sign" if $sign == 1;
 | 
					    while (<READ>) {
 | 
				
			||||||
    system("nix-store --export $extraOpts @missing | $compressor | ssh @sshOpts $sshHost '$decompressor | nix-store --import'") == 0
 | 
					        chomp;
 | 
				
			||||||
        or die "copying store paths to remote machine failed: $?";
 | 
					        print STDERR "target needs $_\n";
 | 
				
			||||||
 | 
					        push @missing, $_;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    close READ or die;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Export the store paths and import them on the remote machine.
 | 
				
			||||||
 | 
					    if (scalar @missing > 0) {
 | 
				
			||||||
 | 
					        my $extraOpts = "";
 | 
				
			||||||
 | 
					        $extraOpts .= "--sign" if $sign == 1;
 | 
				
			||||||
 | 
					        system("nix-store --export $extraOpts @missing | $compressor | ssh @sshOpts $sshHost '$decompressor | nix-store --import'") == 0
 | 
				
			||||||
 | 
					            or die "copying store paths to remote machine failed: $?";
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue