forked from openSUSE/obs-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obs-docker-support
executable file
·268 lines (256 loc) · 6.36 KB
/
obs-docker-support
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
#!/bin/bash
################################################################
#
# Enable docker build support in container.
#
# Author: Marco Strigl
#
################################################################
#
# 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
#
################################################################
LOCAL_REPOS_D="/etc/repos_obs_dockersupport.d/"
LOCAL_APTREPOS_D="/etc/aptrepos_obs_dockersupport.d/"
DATA_DIR=
zypper() {
cmd=
# try to find the command
globalopts=()
while test -n "$1"; do
case $1 in
-*)
globalopts[${#globalopts[@]}]="$1"
shift
;;
*)
cmd=$1
shift
break
;;
esac
done
case $cmd in
in|install|rm|remove|up|update|if|info)
exec /usr/bin/zypper -D $LOCAL_REPOS_D "${globalopts[@]}" "$cmd" "$@"
;;
ar|addrepo)
exec /usr/bin/zypper "${globalopts[@]}" "$cmd" -C "$@"
;;
ref|refresh)
echo "skipping zypper refresh"
exit 0
;;
*)
exec /usr/bin/zypper "${globalopts[@]}" "$cmd" "$@"
;;
esac
}
obs_pkg_mgr() {
case "$1" in
install|remove)
shift
exec /usr/bin/zypper -D $LOCAL_REPOS_D --no-gpg-checks -n in "$@"
;;
add_repo)
shift
exec /usr/bin/zypper ar -C "$@"
;;
*)
echo "Usage: obs_pkg_mgr (install|add_repo) args" >&2
exit 1
;;
esac
}
apt_get() {
cmd=
# try to find the command
globalopts=()
while test -n "$1"; do
case $1 in
-*)
globalopts[${#globalopts[@]}]="$1"
shift
;;
*)
cmd=$1
shift
break
;;
esac
done
case $cmd in
update)
exit 0
;;
install|upgrade)
exec /usr/bin/apt-get -o Dir::Etc::SourceList=$LOCAL_APTREPOS_D/obssource -o Dir::Etc::SourceParts=$LOCAL_APTREPOS_D --allow-unauthenticated "${globalopts[@]}" "$cmd" "$@"
;;
*)
exec /usr/bin/apt-get "${globalopts[@]}" "$cmd" "$@"
;;
esac
}
dnf() {
# try to find command
globalopts=()
while test -n "$1"; do
case $1 in
-*)
globalopts[${#globalopts[@]}]="$1"
shift
;;
*)
cmd=$1
shift
break
;;
esac
done
case $cmd in
in|install|up|update)
exec /usr/bin/dnf --setopt=reposdir=$LOCAL_REPOS_D "${globalopts[@]}" "$cmd" "$@"
;;
*)
exec /usr/bin/dnf "${globalopts[@]}" "$cmd" "$@"
;;
esac
}
yum() {
# try to find command
globalopts=()
while test -n "$1"; do
case $1 in
-*)
globalopts[${#globalopts[@]}]="$1"
shift
;;
*)
cmd=$1
shift
break
;;
esac
done
case $cmd in
in|install|up|update)
exec /usr/bin/yum --setopt=reposdir=$LOCAL_REPOS_D "${globalopts[@]}" "$cmd" "$@"
;;
*)
exec /usr/bin/yum "${globalopts[@]}" "$cmd" "$@"
;;
esac
}
upload_with_perl='
use Socket;
$/ = undef;
my $data = <STDIN>;
my ($size, $ans) = length($data);
socket(S, PF_INET, SOCK_STREAM, getprotobyname("tcp")) || die;
connect(S, sockaddr_in(80, inet_aton("127.0.0.1"))) || die("connect: $!\n");
select(S);
$| = 1;
print S "PUT $ARGV[0] HTTP/1.1\r\nContent-Length: ".length($data)."\r\n\r\n$data";
read(S, $ans, 1024);
die($ans) unless $ans =~ / 200 /;
'
upload_packages() {
local n=$1
local ans
if test -x /usr/bin/rpm -o -x /bin/rpm ; then
rpm -qa --qf '%{NAME}|%{EPOCH}|%{VERSION}|%{RELEASE}|%{ARCH}|%{DISTURL}|%{LICENSE}\n' > /tmp/packages
if test -n "$DATA_DIR" ; then
cp /tmp/packages "$DATA_DIR/UPLOAD/$n"
elif test -x /usr/bin/curl ; then
curl "http://localhost:80/$n" -T /tmp/packages > /dev/null
elif test -x /usr/bin/perl ; then
perl -e "$upload_with_perl" "/$n" < /tmp/packages
else
(echo -n -e "PUT /$n HTTP/1.1\r\nContent-Length: $(wc -c < /tmp/packages)\r\n\r\n" | cat - /tmp/packages && read -t 600 ans ; case "$ans" in *\ 200\ *) ;; *) echo "$ans" >&2 ;; esac) <>/dev/tcp/localhost/80 1>&0
fi
rm -f /tmp/packages
fi
}
obs_docker_support() {
case "$1" in
--install|-i)
data_url=http://localhost:80
test -n "$DATA_DIR" && data_url="file:$DATA_DIR"
test -e /usr/bin/zypper && ln -s obs-docker-support /usr/local/sbin/zypper
test -e /usr/bin/yum && ln -s obs-docker-support /usr/local/sbin/yum
test -e /usr/bin/dnf && ln -s obs-docker-support /usr/local/sbin/dnf
test -e /usr/bin/apt-get && ln -s obs-docker-support /usr/local/sbin/apt-get
ln -s obs-docker-support /usr/local/sbin/obs_pkg_mgr
if test -e /usr/bin/zypper -o -e /usr/bin/yum -o -e /usr/bin/dnf ; then
mkdir -p "$LOCAL_REPOS_D"
cat >$LOCAL_REPOS_D/obs_repository.repo <<EOF
[obs_repository]
name=obs_repository
enabled=1
autorefresh=0
baseurl=$data_url
type=rpm-md
gpgcheck=0
EOF
test -x /usr/bin/zypper && /usr/bin/zypper -D $LOCAL_REPOS_D ref
fi
if test -e /usr/bin/apt-get ; then
mkdir -p "$LOCAL_APTREPOS_D"
echo "deb $data_url ./" > $LOCAL_APTREPOS_D/obssource
test -e /var/lib/apt && mv /var/lib/apt /var/lib/apt.obssave
/usr/bin/apt-get -o Dir::Etc::SourceList=$LOCAL_APTREPOS_D/obssource -o Dir::Etc::SourceParts=$LOCAL_APTREPOS_D update
fi
upload_packages basepackages
;;
--uninstall|-u)
rm -rf "$LOCAL_REPOS_D"
rm -rf "$LOCAL_APTREPOS_D"
rm -f /usr/local/sbin/zypper
rm -f /usr/local/sbin/yum
rm -f /usr/local/sbin/dnf
rm -f /usr/local/sbin/apt-get
rm -f /usr/local/sbin/obs_pkg_mgr
if test -e /var/lib/apt.obssave ; then
rm -rf /var/lib/apt
mv /var/lib/apt.obssave /var/lib/apt
fi
upload_packages packages
rm -f /usr/local/sbin/obs-docker-support
;;
esac
}
if test `id -u` != "0"; then
echo "obs-docker-support: not executed as root user!"
exit 1
fi
case ${0##*/} in
obs-docker-support)
obs_docker_support "$@"
;;
obs_pkg_mgr)
obs_pkg_mgr "$@"
;;
zypper)
zypper "$@"
;;
apt-get)
apt_get "$@"
;;
*)
echo "obs-docker-support: unsupported mode ${0##*/}" >&2
exit 1
esac