forked from openSUSE/obs-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patchdockerfile
executable file
·70 lines (64 loc) · 2.42 KB
/
patchdockerfile
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
#!/usr/bin/perl -w
################################################################
#
# Copyright (c) 2020 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
#
################################################################
use strict;
use warnings;
my $install_docker_support = "COPY .obs-docker-support /usr/local/sbin/obs-docker-support\n" .
"RUN obs-docker-support --install\n";
my $uninstall_docker_support = "RUN obs-docker-support --uninstall\n";
my @sections = ( [] );
while (<STDIN>) {
$_ .= "\n" unless /\n$/s;
push @sections, [] if /^FROM\s/i;
push @{$sections[-1]}, $_;
}
# write out header unmodified
print join('', @{shift @sections});
for my $s (@sections) {
# cannot use docker support for scratch images
if (@$s && $s->[0] =~ /^\s*[fF][rR][oO][mM]\s+(--.*\s+)?scratch[\s\n]/) {
print join('', @$s);
next;
}
# do not install docker support if no RUN is used
# (we always do it for the last stage to get the basepackage list)
if ($s != $sections[-1] && !grep {/^\s*RUN/i} @$s) {
print join('', @$s);
next;
}
# analyze the section (first line is always FROM)
my ($install_idx, $uninstall_idx, $from_scratch);
my $idx = 0;
for my $line (@$s) {
if ($line =~ /^[uU][sS][eE][rR]\s+root[\s\n]/) {
$install_idx = $idx unless defined $install_idx;
$uninstall_idx = undef;
} elsif ($line =~ /^[uU][sS][eE][rR]\s+/) {
$install_idx = 0 unless defined $install_idx;
$uninstall_idx = $idx unless defined $uninstall_idx;
}
$idx++;
}
# patch in docker support commands
$s->[$install_idx || 0] .= $install_docker_support;
substr($s->[$uninstall_idx], 0, 0, $uninstall_docker_support) if defined $uninstall_idx;
$s->[-1] .= $uninstall_docker_support unless defined $uninstall_idx;
print join('', @$s);
}