2010-11-29 16:14:43 +00:00
|
|
|
# This script mirrors a remote Nix channel in the local filesystem.
|
|
|
|
# It downloads the remote manifest, then any NAR files that are not
|
|
|
|
# already available in the target directory. If $ENABLE_PATCHES is
|
|
|
|
# set, it also generates patches between the NAR files in the old
|
|
|
|
# version of the manifest and the new version. Because this script
|
|
|
|
# can take a long time to finish, it uses a lock to guard against
|
|
|
|
# concurrent updates, allowing it to be run periodically from a cron
|
|
|
|
# job.
|
|
|
|
|
2009-04-16 09:25:42 +00:00
|
|
|
use strict;
|
2010-12-06 00:53:36 +00:00
|
|
|
use NixManifest;
|
2010-11-29 16:14:43 +00:00
|
|
|
use GeneratePatches;
|
2009-04-16 09:25:42 +00:00
|
|
|
use File::Basename;
|
|
|
|
use File::stat;
|
2010-11-29 16:14:43 +00:00
|
|
|
use File::Temp qw/tempfile tempdir/;
|
2010-01-26 09:38:13 +00:00
|
|
|
use Fcntl ':flock';
|
2010-11-29 16:14:43 +00:00
|
|
|
use POSIX qw(strftime);
|
|
|
|
|
2009-04-16 09:25:42 +00:00
|
|
|
|
2010-06-23 14:07:47 +00:00
|
|
|
if (scalar @ARGV != 6 && scalar @ARGV != 7) {
|
|
|
|
print STDERR "Syntax: perl mirror-channel.pl <src-channel-url> <dst-channel-dir> <nar-dir> <nar-url> <patches-dir> <patches-url> [<nix-exprs-url>]\n";
|
2009-04-16 09:25:42 +00:00
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
2010-01-26 09:17:50 +00:00
|
|
|
my $curl = "curl --location --silent --show-error --fail";
|
|
|
|
|
2009-04-16 09:25:42 +00:00
|
|
|
my $srcChannelURL = $ARGV[0];
|
2010-06-23 14:07:47 +00:00
|
|
|
my $dstChannelPath = $ARGV[1];
|
|
|
|
my $narPath = $ARGV[2];
|
|
|
|
my $narURL = $ARGV[3];
|
|
|
|
my $patchesPath = $ARGV[4];
|
|
|
|
my $patchesURL = $ARGV[5];
|
|
|
|
my $nixexprsURL = $ARGV[6] || "$srcChannelURL/nixexprs.tar.bz2";
|
2010-11-29 16:14:43 +00:00
|
|
|
my $enablePatches = defined $ENV{'ENABLE_PATCHES'} && -e "$dstChannelPath/MANIFEST";
|
2009-04-16 09:25:42 +00:00
|
|
|
|
|
|
|
die "$dstChannelPath doesn't exist\n" unless -d $dstChannelPath;
|
2010-06-23 14:07:47 +00:00
|
|
|
die "$narPath doesn't exist\n" unless -d $narPath;
|
|
|
|
die "$patchesPath doesn't exist\n" unless -d $patchesPath;
|
2009-04-16 09:25:42 +00:00
|
|
|
|
2010-11-30 13:05:32 +00:00
|
|
|
my $manifestPath = "$dstChannelPath/MANIFEST";
|
|
|
|
|
2010-11-29 16:14:43 +00:00
|
|
|
my $tmpDir = tempdir("nix-mirror-XXXXXXX", TMPDIR => 1, CLEANUP => 1);
|
|
|
|
|
|
|
|
|
2010-01-26 09:38:13 +00:00
|
|
|
open LOCK, ">$dstChannelPath/.lock" or die;
|
|
|
|
flock LOCK, LOCK_EX;
|
|
|
|
|
2010-11-29 16:14:43 +00:00
|
|
|
print STDERR "started mirroring at ", strftime("%a %b %e %H:%M:%S %Y", localtime), "\n";
|
|
|
|
|
2010-08-27 09:01:45 +00:00
|
|
|
|
2010-11-30 13:05:32 +00:00
|
|
|
# Backup the old manifest once per day.
|
|
|
|
my $backupPath = strftime("$dstChannelPath/MANIFEST.backup-%Y%m%d", gmtime);
|
|
|
|
if (-f $manifestPath && ! -f $backupPath) {
|
|
|
|
system "cp $manifestPath $backupPath";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-27 09:01:45 +00:00
|
|
|
# Read the old manifest, if available.
|
|
|
|
my %narFilesOld;
|
|
|
|
my %localPathsOld;
|
|
|
|
my %patchesOld;
|
|
|
|
|
2010-11-30 13:05:32 +00:00
|
|
|
readManifest($manifestPath, \%narFilesOld, \%localPathsOld, \%patchesOld)
|
|
|
|
if -f $manifestPath;
|
2010-08-27 09:01:45 +00:00
|
|
|
|
|
|
|
my %knownURLs;
|
|
|
|
while (my ($storePath, $files) = each %narFilesOld) {
|
|
|
|
$knownURLs{$_->{url}} = $_ foreach @{$files};
|
|
|
|
}
|
|
|
|
|
2010-11-29 16:14:43 +00:00
|
|
|
|
2010-08-27 09:01:45 +00:00
|
|
|
# Fetch the new manifest.
|
2010-11-29 16:14:43 +00:00
|
|
|
my $srcManifest = "$tmpDir/MANIFEST.src";
|
|
|
|
system("$curl '$srcChannelURL/MANIFEST' > $srcManifest") == 0 or die;
|
2009-04-16 09:25:42 +00:00
|
|
|
|
|
|
|
|
2010-11-29 16:14:43 +00:00
|
|
|
# Read the manifest.
|
|
|
|
my (%narFiles, %localPaths, %patches);
|
|
|
|
readManifest($srcManifest, \%narFiles, \%localPaths, \%patches);
|
2009-04-16 09:25:42 +00:00
|
|
|
|
|
|
|
%localPaths = ();
|
|
|
|
%patches = (); # not supported yet
|
|
|
|
|
|
|
|
my $size = scalar (keys %narFiles);
|
|
|
|
print "$size store paths in manifest\n";
|
|
|
|
|
2010-11-29 16:14:43 +00:00
|
|
|
|
2010-01-26 09:51:05 +00:00
|
|
|
# Protect against Hydra problems that leave the channel empty.
|
|
|
|
die "cowardly refusing to mirror an empty channel" if $size == 0;
|
|
|
|
|
2010-11-29 16:14:43 +00:00
|
|
|
|
2009-04-16 09:25:42 +00:00
|
|
|
# Download every file that we don't already have, and update every URL
|
|
|
|
# to point to the mirror. Also fill in the size and hash fields in
|
|
|
|
# the manifest in order to be compatible with Nix < 0.13.
|
|
|
|
|
|
|
|
while (my ($storePath, $files) = each %narFiles) {
|
|
|
|
foreach my $file (@{$files}) {
|
2009-10-16 11:33:48 +00:00
|
|
|
my $narHash = $file->{narHash};
|
2009-04-16 09:25:42 +00:00
|
|
|
my $srcURL = $file->{url};
|
2009-10-16 11:33:48 +00:00
|
|
|
my $dstName = $narHash;
|
|
|
|
$dstName =~ s/:/_/; # `:' in filenames might cause problems
|
2010-06-23 14:07:47 +00:00
|
|
|
my $dstFile = "$narPath/$dstName";
|
|
|
|
my $dstURL = "$narURL/$dstName";
|
2009-04-16 09:25:42 +00:00
|
|
|
|
|
|
|
$file->{url} = $dstURL;
|
|
|
|
if (! -e $dstFile) {
|
|
|
|
print "downloading $srcURL\n";
|
2010-06-23 14:07:47 +00:00
|
|
|
my $dstFileTmp = "$narPath/.tmp.$$.nar.$dstName";
|
2010-07-28 08:25:22 +00:00
|
|
|
system("$curl '$srcURL' > $dstFileTmp") == 0 or die "failed to download `$srcURL'";
|
2010-12-06 00:53:36 +00:00
|
|
|
|
|
|
|
# Verify whether the downloaded file is a bzipped NAR file
|
|
|
|
# that matches the NAR hash given in the manifest.
|
|
|
|
system("bunzip2 < $dstFileTmp > $tmpDir/out") == 0 or die "downloaded file is not a bzip2 file!";
|
|
|
|
my $hash = `nix-hash --type sha256 --flat $tmpDir/out`;
|
|
|
|
chomp $hash;
|
|
|
|
die "hash mismatch in downloaded file `$srcURL'" if "sha256:$hash" ne $file->{narHash};
|
|
|
|
|
2009-04-16 09:25:42 +00:00
|
|
|
rename($dstFileTmp, $dstFile) or die "cannot rename $dstFileTmp";
|
|
|
|
}
|
2010-08-27 09:01:45 +00:00
|
|
|
|
|
|
|
my $old = $knownURLs{$dstURL};
|
|
|
|
|
|
|
|
if (defined $old) {
|
|
|
|
$file->{size} = $old->{size};
|
|
|
|
$file->{hash} = $old->{hash};
|
2009-04-16 09:25:42 +00:00
|
|
|
} else {
|
2010-08-27 09:01:45 +00:00
|
|
|
$file->{size} = stat($dstFile)->size or die "cannot get size of $dstFile";
|
|
|
|
|
|
|
|
my $hashFile = "$narPath/.hash.$dstName";
|
|
|
|
my $hash;
|
|
|
|
if (-e $hashFile) {
|
|
|
|
open HASH, "<$hashFile" or die;
|
|
|
|
$hash = <HASH>;
|
|
|
|
close HASH;
|
|
|
|
} else {
|
|
|
|
$hash = `nix-hash --flat --type sha256 --base32 '$dstFile'` or die;
|
|
|
|
chomp $hash;
|
|
|
|
open HASH, ">$hashFile" or die;
|
|
|
|
print HASH $hash;
|
|
|
|
close HASH;
|
|
|
|
}
|
|
|
|
$file->{hash} = "sha256:$hash";
|
2009-04-16 09:25:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-08 09:09:28 +00:00
|
|
|
|
2010-11-29 16:14:43 +00:00
|
|
|
# Read all the old patches and propagate the useful ones. We use the
|
|
|
|
# file "all-patches" to keep track of all patches that have been
|
|
|
|
# generated in the past, so that patches are not lost if (for
|
|
|
|
# instance) a package temporarily disappears from the source channel,
|
|
|
|
# or if multiple instances of this script are running concurrently.
|
|
|
|
my (%dummy1, %dummy2, %allPatches);
|
|
|
|
|
|
|
|
sub readAllPatches {
|
|
|
|
readManifest("$patchesPath/all-patches", \%dummy1, \%dummy2, \%allPatches)
|
|
|
|
if -f "$patchesPath/all-patches";
|
2010-07-08 09:09:28 +00:00
|
|
|
}
|
2009-04-17 13:48:30 +00:00
|
|
|
|
2010-11-29 16:14:43 +00:00
|
|
|
readAllPatches;
|
|
|
|
|
|
|
|
propagatePatches \%allPatches, \%narFiles, \%patches;
|
|
|
|
propagatePatches \%patchesOld, \%narFiles, \%patches; # not really needed
|
|
|
|
|
|
|
|
|
|
|
|
# Make the temporary manifest available.
|
|
|
|
writeManifest("$dstChannelPath/MANIFEST.tmp", \%narFiles, \%patches);
|
|
|
|
|
2010-11-30 13:05:32 +00:00
|
|
|
rename("$dstChannelPath/MANIFEST.tmp", "$manifestPath") or die;
|
|
|
|
rename("$dstChannelPath/MANIFEST.tmp.bz2", "$manifestPath.bz2") or die;
|
2010-08-05 20:17:17 +00:00
|
|
|
|
2010-11-29 16:14:43 +00:00
|
|
|
|
|
|
|
# Mirror nixexprs.tar.bz2. This should really be done atomically with updating the manifest.
|
2009-04-17 13:48:30 +00:00
|
|
|
my $tmpFile = "$dstChannelPath/.tmp.$$.nixexprs.tar.bz2";
|
2010-06-23 14:07:47 +00:00
|
|
|
system("$curl '$nixexprsURL' > $tmpFile") == 0 or die "cannot download `$nixexprsURL'";
|
2009-04-17 13:48:30 +00:00
|
|
|
rename($tmpFile, "$dstChannelPath/nixexprs.tar.bz2") or die "cannot rename $tmpFile";
|
2010-06-23 14:07:47 +00:00
|
|
|
|
2010-11-29 16:14:43 +00:00
|
|
|
|
|
|
|
# Release the lock on the manifest to allow the manifest to be updated
|
|
|
|
# by other runs of this script while we're generating patches.
|
|
|
|
flock LOCK, LOCK_UN;
|
|
|
|
|
|
|
|
|
|
|
|
if ($enablePatches) {
|
|
|
|
|
|
|
|
# Generate patches asynchronously. This can take a long time.
|
|
|
|
generatePatches(\%narFilesOld, \%narFiles, \%allPatches, \%patches,
|
|
|
|
$narPath, $patchesPath, $patchesURL, $tmpDir);
|
|
|
|
|
|
|
|
# Lock all-patches.
|
|
|
|
open PLOCK, ">$patchesPath/all-patches.lock" or die;
|
|
|
|
flock PLOCK, LOCK_EX;
|
|
|
|
|
|
|
|
# Update the list of all patches. We need to reread all-patches
|
|
|
|
# and merge in our new patches because the file may have changed
|
|
|
|
# in the meantime.
|
|
|
|
readAllPatches;
|
|
|
|
copyPatches \%patches, \%allPatches;
|
|
|
|
writeManifest("$patchesPath/all-patches", {}, \%allPatches, 0);
|
|
|
|
|
|
|
|
# Reacquire the manifest lock.
|
|
|
|
flock LOCK, LOCK_EX;
|
|
|
|
|
|
|
|
# Rewrite the manifest. We have to reread it and propagate all
|
|
|
|
# patches because it may have changed in the meantime.
|
2010-11-30 13:05:32 +00:00
|
|
|
readManifest($manifestPath, \%narFiles, \%localPaths, \%patches);
|
2010-11-29 16:14:43 +00:00
|
|
|
|
|
|
|
propagatePatches \%allPatches, \%narFiles, \%patches;
|
|
|
|
|
2010-11-30 13:05:32 +00:00
|
|
|
writeManifest($manifestPath, \%narFiles, \%patches);
|
2010-11-29 16:14:43 +00:00
|
|
|
}
|