forked from openSUSE/obs-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-recipe-docker
145 lines (120 loc) · 4.96 KB
/
build-recipe-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
#################################################################
#
# Docker specific functions.
#
# Author: TODO
#
################################################################
#
# 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
#
################################################################
DOCKERD_STARTED=
recipe_setup_docker() {
TOPDIR="/usr/src/packages"
mkdir -p "$BUILD_ROOT$TOPDIR/SOURCES"
mkdir -p "$BUILD_ROOT$TOPDIR/SOURCES/packages"
echo "Copying packages"
for package in $(find repos -name \*.rpm); do
cp $package "$BUILD_ROOT$TOPDIR/SOURCES/packages/"
done
# Exclude the "repos" directory which was handled above
find . -maxdepth 1 ! -name "repos" ! -name '.' -exec cp -rp -t $BUILD_ROOT$TOPDIR/SOURCES/ {} \+
}
recipe_prepare_docker() {
:
}
# Variables:
# $BUILD_ROOT is the chroot
# $TOPDIR/SOURCES includes the docker sources
# $TOPDIR/$DOCKERIMAGE_ROOT where docker will be called
# $RECIPEFILE the name of the Dockerfile
recipe_build_docker() {
touch $BUILD_ROOT/etc/resolv.conf
chmod 755 $BUILD_ROOT/$TOPDIR/SOURCES/obs_pkg_mgr
base_image_path=$(find containers -regextype egrep -regex ".*\.(tgz|tar|tar\.xz|tar\.gz)$" -print -quit)
test -f "$base_image_path" || cleanup_and_exit 1 "base image not found"
if ! $BUILD_DIR/startdockerd --root "$BUILD_ROOT" --webserver "$TOPDIR/SOURCES/packages" ; then
cleanup_and_exit 1
fi
DOCKERD_STARTED=true
echo "Loading base image"
if test -L "$base_image_path" ; then
# copy into build root
cp -L "$base_image_path" "$base_image_path.lnk"
mv "$base_image_path.lnk" "$base_image_path"
fi
# Inspect the content of the image to decide if this is a layered image
# or a filesystem one. We need to know if we will "docker load" it or
# "docker import" it.
if tar -tf $base_image_path | grep "^manifest.json" -q; then
echo "Layered image found"
chroot $BUILD_ROOT docker load --input $TOPDIR/SOURCES/$base_image_path
else
echo "Filesystem image found"
desired_tag=$(grep "^\s*FROM" Dockerfile | cut -d" " -f2)
chroot $BUILD_ROOT docker import $TOPDIR/SOURCES/$base_image_path "$desired_tag"
fi
# Start the local zypper repository
# TODO [TBD] Should we inject the OBS_REPOSITORY_URL in the Dockerfile as described in
# the following link or is this a user's responsibility?
# https://github.com/SUSE/cloudfoundry/blob/NativeDockerInOBS_research_results/NativeDockerInOBS.md
# TODO [TBD] What about SLES? Are dependencies fetched automatically from osc/OBS?
# What about registration?
# Prepare the zypper repository
# TODO: Handle other distribution repositories as well (deb, arch etc)
chroot $BUILD_ROOT createrepo "$TOPDIR/SOURCES/packages" >/dev/null
# Create the needed file to publish the generated image
IMAGE_NAME=
TAG=
if [ -f "TAG" ] && [ $(cat TAG | grep ":") ]; then
IMAGE_NAME=$(cat TAG | cut -d":" -f1)
TAG=$(cat TAG | cut -d":" -f2)
fi
if [ -z "$IMAGE_NAME" ] || [ -z "$TAG" ]; then
cleanup_and_exit 1 "TAG file missing of contents invalid (use image_name:tag format)"
fi
# Use tag to generate the filename for the image
FILENAME="$IMAGE_NAME:$TAG"
FILENAME="${FILENAME//[\/:]/-}"
echo "Building image"
if ! chroot $BUILD_ROOT docker build --network=host -t "$IMAGE_NAME:$TAG" --build-arg OBS_REPOSITORY_URL=http://localhost:80 $TOPDIR/SOURCES/ ; then
cleanup_and_exit 1 "Docker build command failed"
fi
# Save the resulting image to a tarball.
echo "Saving image"
mkdir -p $BUILD_ROOT$TOPDIR/DOCKER
if ! chroot $BUILD_ROOT docker save --output "$TOPDIR/DOCKER/$FILENAME.tar" "$IMAGE_NAME:$TAG" ; then
cleanup_and_exit 1 "Docker save command failed"
fi
# Create containerinfo
perl -I$BUILD_DIR -MBuild::Docker -e Build::Docker::showcontainerinfo "$BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE" "$FILENAME.tar" "$IMAGE_NAME:$TAG" containers/annotation> "$BUILD_ROOT$TOPDIR/DOCKER/$FILENAME.containerinfo"
recipe_cleanup_docker
BUILD_SUCCEEDED=true
}
recipe_resultdirs_docker() {
echo DOCKER
}
recipe_cleanup_docker() {
if test -n "$DOCKERD_STARTED" ; then
DOCKERD_STARTED=
$BUILD_DIR/startdockerd --root "$BUILD_ROOT" --kill
fi
}
# Local Variables:
# mode: Shell-script
# End: