-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmageia-docker
executable file
·278 lines (239 loc) · 8.61 KB
/
mageia-docker
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
#!/bin/bash
#
# Generates automatically docker images for Mageia distributions
#
# Based on work done by Fedora and Juan Luis Baptiste
# at https://github.com/juanluisbaptiste/docker-brew-mageia
APPNAME=$(basename $0)
# Workdirs
if [ _"$HOME" = _"" ]; then
echo "Please define HOME to a sensible value"
exit -1
fi
# Get env var from the user home dir
if [ -r $HOME/.$APPNAME ]; then
. $HOME/.$APPNAME
fi
GITDIRRO=${GITDIRRO:=$HOME/tmp/ro-$APPNAME}
GITURLRO=${GITURL:=https://github.com/bcornec}
TMPDM=${WORKDIR:=$HOME/tmp/tmp-$APPNAME}
# By default we host ourselves
MGAMODE=${MGAMODE:="hosted"}
SCPURL=${SCPURL:=localhost:/tmp}
BIGURL=${BIGURL:=http://localhost/$APPNAME}
MGAARCH=${MGAARCH:=x86_64}
# Mirror to consider
#MGAMIRROR=${MGAMIRROR:=http://distrib-coffee.ipsl.jussieu.fr/pub/linux/Mageia/distrib}
#ftp://ftp.free.fr/mirrors/mageia.org/distrib/6/armv7hl
# What is our current date
DATE=`date "+%Y-%m-%d"`
usage() {
echo >&2 "Syntax: $APPNAME [-v Mageia-Version][-m Mageia-Mirror-URL][-w /path/to/workdir]"
exit 1
}
# Mageia version to consider
# By default the running version if on Mageia
# else generating for cauldron
# Other value could be a number or cauldron
if [ -r /etc/mageia-release ]; then
MGAVER=`pbdistrocheck | grep Ver: | cut -f2`
rpm -q docker >& /dev/null
if [ $? -ne 0 ]; then
# Install deps in case they're missing
sudo urpmi docker
fi
else
MGAVER=cauldron
fi
# Mirror to consider, by default mirrorlist
MGAMIRROR="https://mirrors.mageia.org/api/mageia.$MGAVER.$MGAARCH.list"
# Get env var from the user home dir
if [ -r $HOME/.$APPNAME ]; then
. $HOME/.$APPNAME
fi
opt=$(getopt --options 'w:,v:,m:,h,f' --longoptions 'workdir:,version:,mirror:,help,force' --name $APPNAME -- "$@")
eval set -- "$opt"
unset opt
FORCE="false"
while true; do
case "$1" in
-v|--version) MGAVER="$2" ; shift 2 ;;
-m|--mirror) MGAMIRROR="$2" ; shift 2 ;;
-f|--force) FORCE="true" ; shift ;;
-w|--workdir) WORKDIR="$2" ; shift 2 ;;
-h|--help) usage ;;
--) shift ; break ;;
esac
done
TMPDM=${WORKDIR:=$TMPDM}
if [ _"$MGAVER" = _"" ]; then
echo "Please specify a version with -v"
exit -1
fi
echo "Creating Mageia $MGAVER ($MGAARCH) container on Docker Hub"
cd $GITDIRRO 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
echo "$GITDIRRO doesn't exist, creating it"
mkdir -p $GITDIRRO
cd $GITDIRRO
git clone $GITURLRO/$APPNAME .
if [ $? -ne 0 ]; then
echo "Unable to clone $GITURLRO"
exit -1
fi
fi
git checkout -f master 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
echo "Unable to checkout branch master"
exit -1
fi
git branch -D $MGAVER-$MGAARCH 2>/dev/null 1>/dev/null
git checkout -f --orphan $MGAVER-$MGAARCH
if [ $? -ne 0 ]; then
echo "Unable to create branch $MGAVER-$MGAARCH"
exit -1
fi
TARGETDIR="$TMPDM/$MGAVER/$MGAARCH"
echo "Cleanup $TARGETDIR first for idempotence"
echo "Press ^C if that's not what you want"
sleep 5
sudo rm -rf $TARGETDIR
echo "Cleanup `pwd` then for git setup"
echo "Press ^C if that's not what you want"
sleep 5
git rm --cached -r . 2>/dev/null 1>/dev/null
echo "Creating the chroot with latest Mageia $MGAVER content from $MGAMIRROR/$MGAVER/$MGAARCH"
sudo /usr/sbin/urpmi.addmedia --distrib --urpmi-root "$TARGETDIR" $MGAMIRROR/$MGAVER/$MGAARCH 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
echo "Unable to prepare chroot"
exit -1
fi
# We would like to remove kernel, nmap and upper deps, systemd which is not possible
sudo LANG=C LANGUAGE=C /usr/sbin/urpmi -q --auto --no-recommends --urpmi-root "$TARGETDIR" --media release,updates --skip grub2,grub2-common basesystem-minimal urpmi 2>/dev/null 1>/dev/null
# for armv7hl add --allow-force --force --force-key
if [ $? -ne 0 ]; then
echo "Unable to prepare install dependencies in chroot"
exit -1
fi
#
if [ -d "$TARGETDIR/etc/sysconfig" ]; then
# allow networking init scripts inside the container to work without extra steps
echo 'NETWORKING=yes' > /tmp/network$$
sudo install -m 0644 /tmp/network$$ "$TARGETDIR"/etc/sysconfig/network
rm -f /tmp/network$$
fi
#
# Docker mounts tmpfs at /dev and procfs at /proc so we can remove them
sudo rm -rf "$TARGETDIR/dev" "$TARGETDIR/proc"
sudo install -m 0755 -d "$TARGETDIR/dev" "$TARGETDIR/proc"
# Fix permissions for RPM system
list=`sudo ls "$TARGETDIR"/var/lib/rpm/ | grep -E '^[A-Zi]'`
for f in $list; do
sudo chmod 644 "$TARGETDIR"/var/lib/rpm/$f
done
# make sure /etc/resolv.conf has something useful in it
cat > /tmp/resolv.conf.$$ << EOF
nameserver 8.8.8.8
nameserver 8.8.4.4
EOF
sudo install -m 0644 /tmp/resolv.conf.$$ "$TARGETDIR/etc/resolv.conf"
rm -f /tmp/resolv.conf.$$
# Needs root access for dev files e.g.
echo "Capturing that chroot content into mageia-$MGAVER-$MGAARCH-$DATE.tar.xz"
sudo rm -f mageia-$MGAVER-$MGAARCH-$DATE.tar.xz
sudo tar -C $TARGETDIR -cJf mageia-$MGAVER-$MGAARCH-$DATE.tar.xz .
sudo chmod 644 mageia-$MGAVER-$MGAARCH-$DATE.tar.xz
sudo chroot $TARGETDIR rpm -qa | sort -u > /tmp/rpm-qa-$MGAARCH.$$
# Cleanup
sudo rm -rf $TARGETDIR
if [ -r $HOME/rpm-qa-$MGAVER-$MGAARCH ]; then
diff -q $HOME/rpm-qa-$MGAVER-$MGAARCH /tmp/rpm-qa-$MGAARCH.$$
if [ $? -eq 0 ]; then
echo "The list of RPMs packages didn't change so no need to build a new Docker image"
if [ $FORCE = "false" ]; then
exit 1
fi
fi
fi
mv -f /tmp/rpm-qa-$MGAARCH.$$ $HOME/rpm-qa-$MGAVER-$MGAARCH
cp $HOME/rpm-qa-$MGAVER-$MGAARCH rpm-qa-$MGAVER-$MGAARCH
echo "Creating the appropriate Docker file"
cat > Dockerfile << EOF
FROM scratch
MAINTAINER bcornec@mageia.org
WORKDIR /
ADD mageia-$MGAVER-$MGAARCH-$DATE.tar.xz /
LABEL name="Mageia $MGAVER Base Image Arch: $MGAARCH" \
vendor="Mageia" \
license="GPLv2" \
build-date="$DATE"
CMD /bin/bash
EOF
echo "Generating the docker image mageiaofficial:$MGAVER-$MGAARCH from it"
docker build -t mageiaofficial:$MGAVER-$MGAARCH . 2>&1 >/dev/null
docker images mageiaofficial:$MGAVER-$MGAARCH
#docker tag mageiaofficial:$MGAVER-$MGAARCH bcornec/mageia:$MGAVER-$MGAARCH
#docker login -u $MGAAC -p $MGAPWD
#docker push bcornec/mageia:$MGAVER-$MGAARCH
if [ _"$MGAMODE" = _"hosted" ] || [ _"$MGAMODE" = _"local" ]; then
# We host ourselves somewhere on $BIGURL
perl -pi -e "s|ADD mageia-$MGAVER-$MGAARCH-$DATE.tar.xz|ADD $BIGURL/$MGAVER/$MGAARCH/mageia-$DATE.tar.xz|" Dockerfile
SSHDIR=`echo $SCPURL | cut -d: -f2`
if [ _"$MGAMODE" = _"hosted" ]; then
SSHHOST=`echo $SCPURL | cut -d: -f1`
echo "Transferring the big file onto $SCPURL"
ssh $SSHHOST "mkdir -p $SSHDIR/$MGAVER/$MGAARCH ; rm -f $SSHDIR/$MGAVER/$MGAARCH/mageia-$DATE.tar.xz"
scp -p mageia-$MGAVER-$MGAARCH-$DATE.tar.xz $SCPURL/$MGAVER/$MGAARCH/mageia-$DATE.tar.xz
else
if [ _"$KEEP" != _"0" ]; then
echo "Transferring the big file onto $SCPURL"
mkdir -p $SSHDIR/$MGAVER/$MGAARCH
sudo mv mageia-$MGAVER-$MGAARCH-$DATE.tar.xz $SSHDIR/$MGAVER/$MGAARCH/mageia-$DATE.tar.xz
fi
fi
else
# We host on github
# Automatic build in Docker Hub does not support Git LFS yet
# Cf: https://github.com/docker/hub-feedback/issues/500
perl -pi -e "s|ADD mageia-$MGAVER-$MGAARCH-$DATE.tar.xz|ADD $GITURLRO/$APPNAME/$MGAVER/$MGAARCH/mageia-$DATE.tar.xz|" Dockerfile
# This requires git-lfs for managing large files
sudo urpmi -q git-lfs 1>/dev/null
git lfs track "*.tar.xz"
git add .gitattributes
git add mageia-$MGAVER-$MGAARCH-$DATE.tar.xz
# Remove old cached files
git lfs prune
fi
if [ -f mageia-$MGAVER-$DATE.tar.xz ]; then
sudo rm -f mageia-$MGAVER-$MGAARCH-$DATE.tar.xz
fi
echo "Commiting into our git repo"
git add Dockerfile $APPNAME LICENSE README.md run-mageia rpm-qa-$MGAVER-$MGAARCH
git commit -m "update Mageia $MGAVER ($MGAARCH) - $DATE"
CID=`git show | head -1 | cut -d' ' -f2`
echo "New commit ID for latest commit is $CID"
if [ _"$MGAMODE" != _"hosted" ]; then
# Seems we need to do it twice for everything to be pushed with LFS
git push -f --set-upstream origin $MGAVER-$MGAARCH
fi
git push -f --set-upstream origin $MGAVER-$MGAARCH
git checkout -f master
# Backup rpm list for next run
cp $TMPDM/rpm-qa-$MGAARCH rpm-qa-$MGAVER-$MGAARCH
cp $TMPDM/rpm-qa-$MGAARCH $HOME/rpm-qa-$MGAVER-$MGAARCH
git add rpm-qa-$MGAVER-$MGAARCH
git commit -m "update Mageia $MGAVER-$MGAARCH RPM list"
git push
# Doesn't seem to trigger a build with the webhook so forcing it
#curl -H "Content-Type: application/json" --data '{"source_type": "Branch", "source_name": "$MGAVER-$MGAARCH"}' -X POST https://registry.hub.docker.com/u/bcornec/mageia-docker/trigger/$DCKTRIG/
# This removes everything udner WORKDIR for me
#rm -rf $TMPDM
if [ _"$MGAMODE" = _"local" ]; then
exit 0
fi
exit 0
git remote update
perl -pi -e 'undef $/; s|(/'$MGAVER'\nGitCommit:) [a-f0-9]+|$1 '$CID'|' mageia
git add mageia
git commit -m "update Mageia commit ID to $CID"
git push origin