-
Notifications
You must be signed in to change notification settings - Fork 5
/
gen_dockerfiles
executable file
·207 lines (187 loc) · 7.36 KB
/
gen_dockerfiles
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
#!/bin/bash
#emacs: -*- mode: shell-script; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
#ex: set sts=4 ts=4 sw=4 noet:
#
#
# COPYRIGHT: Yaroslav Halchenko 2014
#
# LICENSE: MIT
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
set -eu
ae_verbose=3
print_verbose()
{
level=$1; shift
if [ "$ae_verbose" -ge "$level" ]; then
# use stderr for printing within functions stdout of which might be used
echo -n "I: " >&2
i=1; while [ $i -lt "$level" ]; do echo -ne " ">&2; i=$(($i+1)); done
echo -e "$*" >&2
fi
}
error()
{
code=$1; shift
echo -e "E: $*" >&2
exit "$code"
}
ND_CFG=neurodebian/neurodebian.cfg
ND_CFG_TOOL=neurodebian/tools/nd_querycfg
ND_STACKBREW=stackbrew/library/neurodebian
# remove previously generated files
git rm -fr dockerfiles/*
all_flavors="free non-free"
all_releases=$($ND_CFG_TOOL -f $ND_CFG -F" " "release files" | grep -v '^data ' | awk '{print $1}')
release_tags=
print_verbose 1 "Generating dockerfiles"
for release in $all_releases; do
for flavor in $all_flavors; do
[ "$release" = 'data' ] && continue || : # skip data
print_verbose 2 "release: $release"
# figure out either it is ubuntu or debian
release_desc=$($ND_CFG_TOOL -f $ND_CFG "release names" "$release")
if [[ "$release_desc" =~ ^Debian ]]; then
base=debian
elif [[ "$release_desc" =~ ^Ubuntu ]]; then
base=ubuntu
else
error "Unknown release $release"
fi
if ! grep -q "\\b$release\\b" stackbrew/library/$base; then
print_verbose 2 "Skipping $release since absent in the $base"
continue
fi
release_suf=
if [ "$flavor" = 'non-free' ]; then
release_suf="-$flavor"
# to enable additional suites on both debian and ubuntu
sed_cmd="RUN [ -e /etc/apt/sources.list.d/debian.sources ] && srcs=/etc/apt/sources.list.d/debian.sources || srcs=/etc/apt/sources.list; sed -i -e 's,main *$,main contrib non-free,g' /etc/apt/sources.list.d/neurodebian.sources.list"
case $base in
debian) sed_cmd+=" \$srcs";; # the same substitution
ubuntu)
# some (recent, from 17.04/zesty?) ubuntu images might have multiverse already listed, so they are
# "non-free" by default. For consistency we would still keep "original", possibly
# free and -non-free flavors which might differ only in having NeuroDebian repo
# enabled for contrib and non-free
sed_cmd+="; grep -q 'deb .* multiverse$' \$srcs || sed -i -e 's,universe *$,universe multiverse,g' \$srcs";;
*) echo "Unknown base $base" >&2; exit 1;;
esac
else
sed_cmd=
fi
release_tag=$release$release_suf
# Generate dockerfile
release_tags+=" ${release}_$release_suf"
mkdir -p "dockerfiles/$release_tag"
cat >| "dockerfiles/$release_tag/Dockerfile" <<EOF
# Build #: +1
FROM $base:$release
# https://bugs.debian.org/830696 (apt uses gpgv by default in newer releases, rather than gpg)
RUN set -x \\
&& apt-get update \\
&& { \\
which gpg \\
|| apt-get install -y --no-install-recommends gnupg \\
; } \\
# Ubuntu includes "gnupg" (not "gnupg2", but still 2.x), but not dirmngr, and gnupg 2.x requires dirmngr
# so, if we're not running gnupg 1.x, explicitly install dirmngr too
&& { \\
gpg --version | grep -q '^gpg (GnuPG) 1\\.' \\
|| apt-get install -y --no-install-recommends dirmngr \\
; } \\
&& rm -rf /var/lib/apt/lists/*
# apt-key is a bit finicky during "docker build" with gnupg 2.x, so install the repo key the same way debian-archive-keyring does (/etc/apt/trusted.gpg.d)
# this makes "apt-key list" output prettier too!
RUN set -x \\
&& export GNUPGHOME="\$(mktemp -d)" \\
&& gpg --batch --keyserver keyserver.ubuntu.com --recv-keys DD95CC430502E37EF840ACEEA5D32F012649A5A9 \\
&& gpg --batch --export DD95CC430502E37EF840ACEEA5D32F012649A5A9 > /etc/apt/trusted.gpg.d/neurodebian.gpg \\
&& rm -rf "\$GNUPGHOME" \\
&& apt-key list | grep neurodebian
RUN { \\
echo 'deb http://neuro.debian.net/debian $release main'; \\
echo 'deb http://neuro.debian.net/debian data main'; \\
echo '#deb-src http://neuro.debian.net/debian-devel $release main'; \\
} > /etc/apt/sources.list.d/neurodebian.sources.list
# Minimalistic package to assist with freezing the APT configuration
# which would be coming from neurodebian repo.
# Also install and enable eatmydata to be used for all apt-get calls
# to speed up docker builds.
RUN set -x \\
&& apt-get update \\
&& apt-get install -y --no-install-recommends neurodebian-freeze eatmydata \\
&& ln -s /usr/bin/eatmydata /usr/local/bin/apt-get \\
&& rm -rf /var/lib/apt/lists/*
$sed_cmd
EOF
done
done
git add dockerfiles
if git status dockerfiles | grep -q 'nothing to commit'; then
print_verbose 1 "No changes to dockerfiles - not committing anything"
else
print_verbose 1 "Committing to GIT dockerfiles"
git commit -m "Re-generated dockerfiles" dockerfiles
fi
current_treeish=$(git rev-parse 'HEAD^{commit}')
debian_latest=$(awk -F '[ ,]+' '/latest/ { print $2; }' stackbrew/library/debian)
print_verbose 1 "Populating ${ND_STACKBREW} for $release_tags"
echo "Maintainers: Yaroslav Halchenko <[email protected]> (@yarikoptic)" >| ${ND_STACKBREW}
echo "GitRepo: https://github.com/neurodebian/dockerfiles.git" >> ${ND_STACKBREW}
echo "GitCommit: $current_treeish" >> ${ND_STACKBREW}
for release_rec in $release_tags; do
# NOTE: there might be a better way
release="${release_rec%_*}"
release_suf="${release_rec##*_}"
release_tag="$release$release_suf"
release_tags_all="$release_tag"
architectures="amd64, arm64v8" # will add i386 if Debian
bp_release=$($ND_CFG_TOOL -f $ND_CFG -F" " "release backport ids" "${release}" | sed -e 's,.*=,,g')
if echo "$bp_release" | grep -q '\.'; then
: # ubuntu release since has . in it. nothing to do
else
architectures+=", i386"
fi
if [ "$bp_release" != "" ]; then
release_tags_all+=", $bp_release$release_suf"
fi
# Let's use Debian's stable as our stable!
if [ "$release" = "$debian_latest" ]; then
if [ "$release_suf" != "" ]; then
# "non-free"
release_tags_all+=", ${release_suf#-}"
else
release_tags_all+=", latest"
fi
fi
cat << EOF >> ${ND_STACKBREW}
Tags: $release_tags_all
Architectures: $architectures
Directory: dockerfiles/$release_tag
EOF
done
GIT_DIR=stackbrew/.git git add library/neurodebian
if GIT_DIR=stackbrew/.git git status 2>&1 | grep -q 'nothing to commit'; then
print_verbose 1 "No changes to stackbrew configuration - not committing anything"
else
print_verbose 1 "Committing to GIT stackbrew"
( cd stackbrew; echo git commit -m "Update NeuroDebian" library/neurodebian; )
fi