Skip to content

Commit

Permalink
Merge pull request #14217 from mlschroe/master
Browse files Browse the repository at this point in the history
[backend] BSPublisher: remove duplicated code
  • Loading branch information
mlschroe committed Apr 24, 2023
2 parents 6a5ae55 + cb64b38 commit a73a5f0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 48 deletions.
33 changes: 5 additions & 28 deletions src/backend/BSPublisher/Container.pm
Original file line number Diff line number Diff line change
Expand Up @@ -300,43 +300,20 @@ sub upload_all_containers {
return \%container_repositories;
}

sub construct_container_tar {
my ($containerinfo, $doopen) = @_;
my $manifest = $containerinfo->{'tar_manifest'};
my $mtime = $containerinfo->{'tar_mtime'};
my $blobids = $containerinfo->{'tar_blobids'};
my $blobdir = $containerinfo->{'blobdir'};
return (undef, undef) unless $mtime && $manifest && $blobids && $blobdir;
my @tar;
for my $blobid (@$blobids) {
my $file = "$blobdir/_blob.$blobid";
if ($doopen) {
my $fd;
open($fd, '<', $file) || die("$file: $!\n");
$file = $fd;
}
die("missing blobid $blobid\n") unless -e $file;
push @tar, {'name' => $blobid, 'file' => $file, 'mtime' => $mtime, 'offset' => 0, 'size' => (-s _), 'blobid' => $blobid};
}
push @tar, {'name' => 'manifest.json', 'data' => $manifest, 'mtime' => $mtime, 'size' => length($manifest)};
return (\@tar, $mtime);
}

sub reconstruct_container {
my ($containerinfo, $dst, $dstfinal) = @_;
my ($tar, $mtime) = construct_container_tar($containerinfo);
my ($tar, $mtime) = BSPublisher::Containerinfo::construct_container_tar($containerinfo);
BSTar::writetarfile($dst, $dstfinal, $tar, 'mtime' => $mtime) if $tar;
}

sub create_container_dist_info {
my ($containerinfo, $oci, $platforms) = @_;
my $file = $containerinfo->{'publishfile'};
my $tar;
my ($tar, $mtime, $layer_compression);
if (!defined($file)) {
die("need a blobdir to reconstruct containers\n") unless $containerinfo->{'blobdir'};
($tar) = construct_container_tar($containerinfo, 1);
($tar, $mtime, $layer_compression) = BSPublisher::Containerinfo::construct_container_tar($containerinfo, 1);
} elsif (($containerinfo->{'type'} || '') eq 'helm') {
($tar) = BSContar::container_from_helm($file, $containerinfo->{'config_json'}, $containerinfo->{'tags'});
($tar, $mtime, $layer_compression) = BSContar::container_from_helm($file, $containerinfo->{'config_json'}, $containerinfo->{'tags'});
} elsif ($file =~ /\.tar$/) {
my $tarfd;
open($tarfd, '<', $file) || die("$file: $!\n");
Expand Down Expand Up @@ -373,7 +350,7 @@ sub create_container_dist_info {
};
my @layer_data;
die("container has no layers\n") unless @{$manifest->{'Layers'} || []};
my @layer_comp = @{$containerinfo->{'layer_compression'} || []};
my @layer_comp = @{$layer_compression || []};
for my $layer_file (@{$manifest->{'Layers'}}) {
my $layer_ent = $tar{$layer_file};
die("file $layer_file not included in tar\n") unless $layer_ent;
Expand Down
23 changes: 23 additions & 0 deletions src/backend/BSPublisher/Containerinfo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@

package BSPublisher::Containerinfo;

sub construct_container_tar {
my ($containerinfo, $doopen) = @_;
my $blobdir = $containerinfo->{'blobdir'};
die("need a blobdir to reconstruct containers\n") unless $blobdir;
my $manifest = $containerinfo->{'tar_manifest'};
my $mtime = $containerinfo->{'tar_mtime'};
my $blobids = $containerinfo->{'tar_blobids'};
die("containerinfo is incomplete\n") unless $mtime && $manifest && $blobids;
my @tar;
for my $blobid (@$blobids) {
my $file = "$blobdir/_blob.$blobid";
if ($doopen) {
my $fd;
open($fd, '<', $file) || die("$file: $!\n");
$file = $fd;
}
die("missing blobid $blobid\n") unless -e $file;
push @tar, {'name' => $blobid, 'file' => $file, 'mtime' => $mtime, 'offset' => 0, 'size' => (-s $_), 'blobid' => $blobid};
}
push @tar, {'name' => 'manifest.json', 'data' => $manifest, 'mtime' => $mtime, 'size' => length($manifest)};
return (\@tar, $mtime, $containerinfo->{'layer_compression'});
}

sub create_packagelist {
my ($containerinfo) = @_;
my @bins;
Expand Down
21 changes: 1 addition & 20 deletions src/backend/BSPublisher/Registry.pm
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,6 @@ sub push_manifestinfo {
writestr("$dir/.$mani_id.$$", "$dir/$mani_id", $info_json);
}


sub construct_container_tar {
my ($containerinfo) = @_;
my $blobdir = $containerinfo->{'blobdir'};
die("need a blobdir to reconstruct containers\n") unless $blobdir;
my $manifest = $containerinfo->{'tar_manifest'};
my $mtime = $containerinfo->{'tar_mtime'};
my $blobids = $containerinfo->{'tar_blobids'};
die("containerinfo is incomplete\n") unless $mtime && $manifest && $blobids;
my @tar;
for my $blobid (@$blobids) {
my $file;
open($file, '<', "$blobdir/_blob.$blobid") || die("$blobdir/_blob.$blobid: $!");
push @tar, {'name' => $blobid, 'file' => $file, 'mtime' => $mtime, 'offset' => 0, 'size' => (-s $file), 'blobid' => $blobid};
}
push @tar, {'name' => 'manifest.json', 'data' => $manifest, 'mtime' => $mtime, 'size' => length($manifest)};
return (\@tar, $mtime, $containerinfo->{'layer_compression'});
}

sub gen_timestampkey {
print "local notary: generating timestamp keypair\n";
my @keyargs = ('rsa@2048', '800'); # expire time does not matter...
Expand Down Expand Up @@ -664,7 +645,7 @@ sub push_containers {
($tar, $mtime, $layer_compression) = BSContar::normalize_container($tarfd, 1);
}
} else {
($tar, $mtime, $layer_compression) = construct_container_tar($containerinfo);
($tar, $mtime, $layer_compression) = BSPublisher::Containerinfo::construct_container_tar($containerinfo, 1);
}
my %tar = map {$_->{'name'} => $_} @$tar;

Expand Down

0 comments on commit a73a5f0

Please sign in to comment.