Skip to content

Commit

Permalink
Implement --no-repo-refresh support for registries
Browse files Browse the repository at this point in the history
  • Loading branch information
mlschroe committed Oct 8, 2024
1 parent 5a6604e commit d6f6adc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
26 changes: 22 additions & 4 deletions PBuild/RemoteRegistry.pm
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ sub queryremotecontainer {
my $registrydomain = $registry;
$registrydomain =~ s/^[^\/]+\/\///;
$registrydomain =~ s/\/.*//;
$repotag .= ":latest" unless $repotag =~ /:[^\/:]+$/;
die unless $repotag =~ /^(.*):([^\/:]+)$/;
my ($repository, $tag) = ($1, $2);
my $refname = "$repository:$tag";
Expand Down Expand Up @@ -177,14 +176,33 @@ sub queryremotecontainer {
# get data from a registry for a set of containers
#
sub fetchrepo {
my ($bconf, $arch, $repodir, $url, $repotags) = @_;
my ($bconf, $arch, $repodir, $url, $repotags, $opts) = @_;
my $oldtags = {};
if ($opts->{'single'} || $opts->{'no-repo-refresh'}) {
my $oldmetadata = (-s "$repodir/_metadata") ? PBuild::Util::retrieve("$repodir/_metadata", 1) : undef;
$oldtags = $oldmetadata->{'tags'} if $oldmetadata && $oldmetadata->{'tags'};
}
my $ua;
my %tags;
%tags = %$oldtags if $oldtags && $opts->{'single'}; # repotags is incomplete
my @bins;
my $ua = Build::Download::create_ua();
for my $repotag (@{$repotags || []}) {
my $bin = queryremotecontainer($ua, $arch, $repodir, $url, $repotag);
my $rt = $repotag;
$rt .= ":latest" unless $rt =~ /:[^\/:]+$/;
my $bin;
if ($opts->{'no-repo-refresh'} && exists $oldtags->{$rt}) {
$bin = $oldtags->{$rt};
} else {
$ua ||= Build::Download::create_ua();
$bin = queryremotecontainer($ua, $arch, $repodir, $url, $rt);
}
push @bins, $bin if $bin;
$tags{$rt} = $bin;
}
my $metadata = { 'tags' => \%tags };
my $meta = { 'repodir' => $repodir, 'url' => $url };
PBuild::Util::mkdir_p($repodir);
PBuild::Util::store("$repodir/._metadata.$$", "$repodir/_metadata", $metadata);
return (\@bins, $meta);
}

Expand Down
4 changes: 2 additions & 2 deletions PBuild/RepoMgr.pm
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ sub addremoterepo {
# Add a remote registry to the manager
#
sub addremoteregistry {
my ($repos, $bconf, $myarch, $builddir, $registry, $tags) = @_;
my ($repos, $bconf, $myarch, $builddir, $registry, $tags, $opts) = @_;
my $repourl = $registry;
$repourl = "https://$repourl" unless $repourl =~ /^[^\/]+\/\//;
my $id = Digest::MD5::md5_hex("$myarch/$repourl");
return $repos->{$id} if $repos->{$id};
my $repodir = "$builddir/.pbuild/_base/$id";
my ($bins, $meta) = PBuild::RemoteRegistry::fetchrepo($bconf, $myarch, $repodir, $repourl, $tags);
my ($bins, $meta) = PBuild::RemoteRegistry::fetchrepo($bconf, $myarch, $repodir, $repourl, $tags, $opts);
$_->{'repoid'} = $id for @$bins;
my $repo = { 'dir' => $repodir, 'bins' => $bins, 'meta' => $meta, 'url' => $repourl, 'arch' => $myarch, 'type' => 'registry', 'repoid' => $id };
$repos->{$id} = $repo;
Expand Down
6 changes: 3 additions & 3 deletions pbuild
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ push @hostrepos, $repomgr->addlocalrepo($bconf_host, $hostarch, $builddir, \%pkg
print "fetching metadata of ".PBuild::Util::plural(scalar(@{$opts->{'repo'}}) + ($cross ? scalar(@{$opts->{'hostrepo'}}) : 0), 'remote repo')."\n";
for my $repourl (@{$opts->{'repo'}}) {
if ($repourl =~ /^registry@(.+)/) {
push @repos, $repomgr->addremoteregistry($bconf, $myarch, $builddir, $1, \@containertags);
push @repos, $repomgr->addremoteregistry($bconf, $myarch, $builddir, $1, \@containertags, $opts);
} else {
push @repos, $repomgr->addremoterepo($bconf, $myarch, $builddir, $repourl, $buildtype, $opts);
}
Expand All @@ -416,9 +416,9 @@ if ($cross) {
}

if (@{$opts->{'registry'} || []} && @containertags) {
print "fetching remote registry metadata\n";
print "fetching remote registry metadata of ".PBuild::Util::plural(scalar(@containertags), 'tag')."\n";
for my $registry (@{$opts->{'registry'} || []}) {
push @repos, $repomgr->addremoteregistry($bconf, $myarch, $builddir, $registry, \@containertags);
push @repos, $repomgr->addremoteregistry($bconf, $myarch, $builddir, $registry, \@containertags, $opts);
}
}

Expand Down

0 comments on commit d6f6adc

Please sign in to comment.