forked from openSUSE/obs-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Docker.pm
316 lines (298 loc) · 9.46 KB
/
Docker.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
################################################################
#
# Copyright (c) 2017 SUSE Linux Products GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################
package Build::Docker;
use Build::SimpleXML; # to parse the annotation
use Build::SimpleJSON;
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 quote {
my ($str, $q, $vars) = @_;
if ($q ne "'" && $str =~ /\$/) {
$str =~ s/\$([a-zA-Z0-9_]+|\{([^\}]+)\})/join(' ', @{$vars->{$2 || $1} || []})/ge;
}
$str =~ s/([ \t\"\'\$\(\)])/sprintf("%%%02X", ord($1))/ge;
return $str;
}
sub addrepo {
my ($ret, $url, $prio) = @_;
unshift @{$ret->{'imagerepos'}}, { 'url' => $url };
$ret->{'imagerepos'}->[0]->{'priority'} = $prio if defined $prio;
if ($Build::Kiwi::urlmapper) {
my $prp = $Build::Kiwi::urlmapper->($url);
if (!$prp) {
$ret->{'error'} = "cannot map '$url' to obs";
return undef;
}
my ($projid, $repoid) = split('/', $prp, 2);
unshift @{$ret->{'path'}}, {'project' => $projid, 'repository' => $repoid};
$ret->{'path'}->[0]->{'priority'} = $prio if defined $prio;
return 1;
} else {
# this is just for testing purposes...
$url =~ s/^\/+$//;
$url =~ s/:\//:/g;
my @url = split('/', $url);
unshift @{$ret->{'path'}}, {'project' => $url[-2], 'repository' => $url[-1]} if @url >= 2;
$ret->{'path'}->[0]->{'priority'} = $prio if defined $prio;
return 1;
}
}
sub cmd_zypper {
my ($ret, @args) = @_;
# skip global options
shift @args while @args && $args[0] =~ /^-/;
return unless @args;
if ($args[0] eq 'in' || $args[0] eq 'install') {
shift @args;
while (@args && $args[0] =~ /^-/) {
shift @args if $args[0] =~ /^--(?:from|repo|type)$/ || $args[0] =~ /^-[tr]$/;
shift @args;
}
my @deps = grep {/^[a-zA-Z_0-9]/} @args;
s/^([^<=>]+)([<=>]+)/$1 $2 / for @deps;
push @{$ret->{'deps'}}, @deps;
} elsif ($args[0] eq 'ar' || $args[0] eq 'addrepo') {
shift @args;
while (@args && $args[0] =~ /^-/) {
shift @args if $args[0] =~ /^--(?:repo|type)$/ || $args[0] =~ /^-[rt]$/;
shift @args;
}
if (@args) {
my $path = $args[0];
$path =~ s/\/[^\/]*\.repo$//;
addrepo($ret, $path);
}
}
}
sub cmd_obs_pkg_mgr {
my ($ret, @args) = @_;
return unless @args;
if ($args[0] eq 'add_repo') {
shift @args;
addrepo($ret, $args[0]) if @args;
} elsif ($args[0] eq 'install') {
shift @args;
push @{$ret->{'deps'}}, @args;
}
}
sub cmd_dnf {
my ($ret, @args) = @_;
# skip global options
shift @args while @args && $args[0] =~ /^-/;
return unless @args;
if ($args[0] eq 'in' || $args[0] eq 'install') {
shift @args;
while (@args && $args[0] =~ /^-/) {
shift @args;
}
push @{$ret->{'deps'}}, grep {/^[a-zA-Z_0-9]/} @args;
}
}
sub cmd_apt_get {
my ($ret, @args) = @_;
shift @args while @args && $args[0] =~ /^-/;
return unless @args;
if ($args[0] eq 'install') {
shift @args;
push @{$ret->{'deps'}}, grep {/^[a-zA-Z_0-9]/} @args;
}
}
sub parse {
my ($cf, $fn) = @_;
my $basecontainer;
my $unorderedrepos;
my $useobsrepositories;
my $nosquash;
my $dockerfile_data = slurp($fn);
return { 'error' => 'could not open Dockerfile' } unless defined $dockerfile_data;
my @lines = split(/\r?\n/, $dockerfile_data);
my $ret = {
'name' => 'docker',
'deps' => [],
'path' => [],
'imagerepos' => [],
};
while (@lines) {
my $line = shift @lines;
$line =~ s/^\s+//;
if ($line =~ /^#/) {
if ($line =~ /^#!BuildTag:\s*(.*?)$/) {
my @tags = split(' ', $1);
push @{$ret->{'containertags'}}, @tags if @tags;
}
if ($line =~ /^#!BuildVersion:\s*(\S+)\s*$/) {
$ret->{'version'} = $1;
}
if ($line =~ /^#!UnorderedRepos\s*$/) {
$unorderedrepos = 1;
}
if ($line =~ /^#!UseOBSRepositories\s*$/) {
$useobsrepositories = 1;
}
if ($line =~ /^#!NoSquash\s*$/) {
$nosquash = 1;
}
next;
}
# add continuation lines
while (@lines && $line =~ s/\\[ \t]*$//) {
shift @lines while @lines && $lines[0] =~ /^\s*#/;
$line .= shift(@lines) if @lines;
}
$line =~ s/^\s+//;
$line =~ s/\s+$//;
next unless $line;
my ($cmd, @args);
($cmd, $line) = split(' ', $line, 2);
$cmd = uc($cmd);
my $vars = {};
# split line into args
$line =~ s/%/%25/g;
$line =~ s/\\(.)/sprintf("%%%02X", ord($1))/ge;
while ($line =~ /([\"\'])/) {
my $q = $1;
last unless $line =~ s/$q(.*?)$q/quote($1, $q, $vars)/e;
}
if ($line =~ /\$/) {
$line =~ s/\$([a-zA-Z0-9_]+|\{([^\}]+)\})/join(' ', @{$vars->{$2 || $1} || []})/ge;
}
@args = split(/[ \t]+/, $line);
s/%([a-fA-F0-9]{2})/chr(hex($1))/ge for @args;
if ($cmd eq 'FROM') {
if (@args && !$basecontainer && $args[0] ne 'scratch') {
$basecontainer = $args[0];
$basecontainer .= ':latest' unless $basecontainer =~ /:[^:\/]+$/;
}
} elsif ($cmd eq 'RUN') {
$line =~ s/#.*//; # get rid of comments
for my $l (split(/(?:\||\|\||\&|\&\&|;|\)|\()/, $line)) {
$l =~ s/^\s+//;
$l =~ s/\s+$//;
@args = split(/[ \t]+/, $l);
s/%([a-fA-F0-9]{2})/chr(hex($1))/ge for @args;
next unless @args;
my $rcmd = shift @args;
if ($rcmd eq 'zypper') {
cmd_zypper($ret, @args);
} elsif ($rcmd eq 'yum' || $rcmd eq 'dnf') {
cmd_dnf($ret, @args);
} elsif ($rcmd eq 'apt-get') {
cmd_apt_get($ret, @args);
} elsif ($rcmd eq 'obs_pkg_mgr') {
cmd_obs_pkg_mgr($ret, @args);
}
}
}
}
push @{$ret->{'deps'}}, "container:$basecontainer" if $basecontainer;
push @{$ret->{'deps'}}, '--unorderedimagerepos' if $unorderedrepos;
my $version = $ret->{'version'};
my $release = $cf->{'buildrelease'};
for (@{$ret->{'containertags'} || []}) {
s/<VERSION>/$version/g if defined $version;
s/<RELEASE>/$release/g if defined $release;
}
$ret->{'path'} = [ { 'project' => '_obsrepositories', 'repository' => '' } ] if $useobsrepositories;
$ret->{'basecontainer'} = $basecontainer if $basecontainer;
$ret->{'nosquash'} = 1 if $nosquash;
return $ret;
}
sub showcontainerinfo {
my ($disturl, $release);
while (@ARGV) {
if (@ARGV > 2 && $ARGV[0] eq '--disturl') {
(undef, $disturl) = splice(@ARGV, 0, 2);
} elsif (@ARGV > 2 && $ARGV[0] eq '--release') {
(undef, $release) = splice(@ARGV, 0, 2);
} else {
last;
}
}
my ($fn, $image, $taglist, $annotationfile) = @ARGV;
local $Build::Kiwi::urlmapper = sub { return $_[0] };
my $cf = {};
$cf->{'buildrelease'} = $release if defined $release;
my $d = {};
$d = parse($cf, $fn) if $fn;
die("$d->{'error'}\n") if $d->{'error'};
$image =~ s/.*\/// if defined $image;
my @tags = split(' ', $taglist);
for (@tags) {
$_ .= ':latest' unless /:[^:\/]+$/;
if (/:([0-9][^:]*)$/) {
$d->{'version'} = $1 unless defined $d->{'version'};
}
}
my @repos = @{$d->{'imagerepos'} || []};
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'}, '_type' => {'priority' => 'number'} };
$repos[-1]->{'priority'} = $annorepo->{'priority'} if defined $annorepo->{'priority'};
}
}
my $buildtime = time();
my $containerinfo = {
'buildtime' => $buildtime,
'_type' => {'buildtime' => 'number'},
};
$containerinfo->{'tags'} = \@tags if @tags;
$containerinfo->{'repos'} = \@repos if @repos;
$containerinfo->{'file'} = $image if defined $image;
$containerinfo->{'disturl'} = $disturl if defined $disturl;
$containerinfo->{'version'} = $d->{'version'} if defined $d->{'version'};
$containerinfo->{'release'} = $release if defined $release;
print Build::SimpleJSON::unparse($containerinfo)."\n";
}
sub show {
my ($release);
while (@ARGV) {
if (@ARGV > 2 && $ARGV[0] eq '--release') {
(undef, $release) = splice(@ARGV, 0, 2);
} else {
last;
}
}
my ($fn, $field) = @ARGV;
local $Build::Kiwi::urlmapper = sub { return $_[0] };
my $cf = {};
$cf->{'buildrelease'} = $release if defined $release;
my $d = {};
$d = parse($cf, $fn) if $fn;
die("$d->{'error'}\n") if $d->{'error'};
my $x = $d->{$field};
$x = [ $x ] unless ref $x;
print "@$x\n";
}
1;