Skip to content

Commit

Permalink
Write docker containerinfo with a perl function
Browse files Browse the repository at this point in the history
This allows us to:
- read the new repos from the Dockerfile
- add the old repos from the container annotation
  • Loading branch information
mlschroe committed Jun 28, 2017
1 parent 490d272 commit 0f2bc07
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
57 changes: 49 additions & 8 deletions Build/Docker.pm
Expand Up @@ -30,22 +30,32 @@

package Build::Docker;

use Build::SimpleXML;

use strict;

sub slurp {
my ($fn) = @_;
local *F;
return undef unless open(F, '<', $fn);
local $/ = undef; # Perl slurp mode
my $content = <F>;
close F;
return $content;
}

sub parse {
my ($cf, $fn) = @_;

# Perl slurp mode
local $/=undef;
open DOCKERFILE, $fn or die "Couldn't open Dockerfile";
my $dockerfile_data = <DOCKERFILE>;
close DOCKERFILE;
my $dockerfile_data = slurp($fn);
return { 'error' => 'could not open Dockerfile' } unless defined $dockerfile_data;

# Remove all whitespace from end of lines to make parsing easier
$dockerfile_data =~ s/[^\S\n\r]+$//gm;

my @deps;
my @repos;
my @repo_urls;
my $pkg_string;

# Match lines that start with "RUN" up to where the "RUN" command ends. It
Expand Down Expand Up @@ -75,18 +85,19 @@ sub parse {
# "obs://Virtualization:containers/openSUSE_Leap_42.2/"
while ($run_command =~ /obs_pkg_mgr\s+add_repo\s+(.+?)\s+/g) {
my $repo_url = $1;
print "Converting repo to obs format: $repo_url\n";
unshift @repo_urls, $repo_url;
if ($Build::Kiwi::urlmapper) {
my $repo_prp = $Build::Kiwi::urlmapper->($repo_url);
return {'error' => "cannot map '$repo_url' to obs"} unless $repo_prp;
my ($projid, $repoid) = split('/', $repo_prp, 2);
push @repos, {'project' => $projid, 'repository' => $repoid};
unshift @repos, {'project' => $projid, 'repository' => $repoid};
} else {
print "Converting repo to obs format: $repo_url\n";
# this is just for testing purposes...
$repo_url =~ s/^\/+$//;
$repo_url =~ s/:\//:/g;
my @repo_url = split('/', $repo_url);
push @repos, {'project' => $repo_url[-2], 'repository' => $repo_url[-1]} if @repo_url >= 2;
unshift @repos, {'project' => $repo_url[-2], 'repository' => $repo_url[-1]} if @repo_url >= 2;
}
}
}
Expand All @@ -100,8 +111,38 @@ sub parse {
$ret->{'name'} = 'docker';
$ret->{'deps'} = \@deps;
$ret->{'path'} = \@repos;
$ret->{'repo_urls'} = \@repo_urls;

return $ret;
}

sub showcontainerinfo {
my ($fn, $image, $taglist, $annotationfile) = @ARGV;
local $Build::Kiwi::urlmapper = sub { return $_[0] };
my $d = parse({}, $fn);
die("$d->{'error'}\n") if $d->{'error'};
$image =~ s/.*\/// if defined $image;
my @tags = split(' ', $taglist);
@tags = map {"\"$_\""} @tags;
my @repos = map {"{ \"url\": \"$_\" }"} @{$d->{'repo_urls'} || []};
if ($annotationfile) {
my $annotation = slurp($annotationfile);
$annotation = Build::SimpleXML::parse($annotation) if $annotation;
$annotation = $annotation && ref($annotation) eq 'HASH' ? $annotation->{'annotation'} : undef;
$annotation = $annotation && ref($annotation) eq 'ARRAY' ? $annotation->[0] : undef;
my $annorepos = $annotation && ref($annotation) eq 'HASH' ? $annotation->{'repo'} : undef;
$annorepos = undef unless $annorepos && ref($annorepos) eq 'ARRAY';
for my $annorepo (@{$annorepos || []}) {
next unless $annorepo && ref($annorepo) eq 'HASH' && $annorepo->{'url'};
push @repos, "{ \"url\": \"$annorepo->{'url'}\" }";
}
}
print "{\n";
print " \"name\": \"$d->{'name'}\"";
print ",\n \"tags\": [ ".join(', ', @tags)." ]" if @tags;
print ",\n \"repos\": [ ".join(', ', @repos)." ]" if @repos;
print ",\n \"file\": \"$image\"" if defined $image;
print "\n}\n";
}

1;
15 changes: 6 additions & 9 deletions build-recipe-docker
Expand Up @@ -152,14 +152,16 @@ recipe_build_docker() {
REPO_URL=$(ifconfig docker0 | sed -n -e 's@.*inet addr:\([^ /]*\).*@\1@p')
fi
if test -z "$REPO_URL" ; then
echo "network setup error"
echo "network setup error, no docker0 interface?"
cleanup_build_processes
BUILD_SUCCEEDED=false
return
fi
REPO_URL="http://$REPO_URL:8080"

# Create the needed file to publish the generated image
IMAGE_NAME=
TAG=
if [ -f "TAG" ] && [ $(cat TAG | grep ":") ]; then
IMAGE_NAME=$(cat TAG | cut -d":" -f1)
TAG=$(cat TAG | cut -d":" -f2)
Expand All @@ -186,20 +188,15 @@ recipe_build_docker() {

# Save the resulting image to a tarball.
mkdir -p $BUILD_ROOT$TOPDIR/DOCKER
if ! chroot $BUILD_ROOT docker save --output $TOPDIR/DOCKER/$FILENAME.tar "$IMAGE_NAME:$TAG" ; then
if ! chroot $BUILD_ROOT docker save --output "$TOPDIR/DOCKER/$FILENAME.tar" "$IMAGE_NAME:$TAG" ; then
echo "Docker save command failed"
cleanup_build_processes
BUILD_SUCCEEDED=false
return
fi

cat > $BUILD_ROOT$TOPDIR/DOCKER/$FILENAME.containerinfo <<containerinfo
{
"name": "$FILENAME",
"tags": [ "$IMAGE_NAME:$TAG" ],
"file": "$FILENAME.tar"
}
containerinfo
# Create containerinfo
perl -I$BUILD_DIR -MBuild::Docker -e Build::Docker::showcontainerinfo "$BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE" "$FILENAME.tar" "$IMAGE_NAME:$TAG" containers/annotation> "$BUILD_ROOT$TOPDIR/DOCKER/$FILENAME.containerinfo"

cleanup_build_processes
BUILD_SUCCEEDED=true
Expand Down

0 comments on commit 0f2bc07

Please sign in to comment.