File: build-recipe-docker

package info (click to toggle)
obs-build 20180831-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 2,396 kB
  • sloc: perl: 10,030; sh: 3,142; ansic: 284; makefile: 151; python: 35
file content (179 lines) | stat: -rw-r--r-- 6,541 bytes parent folder | download
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
#################################################################
#
# Docker specific functions.
#
################################################################
#
# 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"
    test "$DO_INIT_TOPDIR" != false && rm -Rf "$BUILD_ROOT/$TOPDIR"
    mkdir -p "$BUILD_ROOT$TOPDIR/SOURCES"
    if test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir ; then 
        mv "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
    else
        if test -z "$LINKSOURCES" ; then 
            cp -dLR "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
        else
            cp -lR "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
        fi
        if test "$?" != 0 ; then 
            cleanup_and_exit 1 "source copy failed"
        fi
    fi  
}

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

    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 test "$BUILDENGINE" = "podman"; then
        DOCKER_TOOL="podman"
        if ! $BUILD_DIR/startdockerd --root "$BUILD_ROOT" --webserver-only "$TOPDIR/SOURCES/repos" ; then
             cleanup_and_exit 1
        fi
    else
        DOCKER_TOOL="docker"
        if ! $BUILD_DIR/startdockerd --root "$BUILD_ROOT" --webserver "$TOPDIR/SOURCES/repos" ; then
             cleanup_and_exit 1
        fi
        DOCKERD_STARTED=true
    fi

    cp $BUILD_DIR/obs-docker-support "$BUILD_ROOT/$TOPDIR/SOURCES/.obs-docker-support"
    chmod 755 "$BUILD_ROOT/$TOPDIR/SOURCES/.obs-docker-support"

    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_TOOL load --input $TOPDIR/SOURCES/$base_image_path
    else
        echo "Filesystem image found"
        desired_tag=$(grep "^\s*FROM" "$RECIPEFILE" | cut -d" " -f2)
        chroot $BUILD_ROOT $DOCKER_TOOL import $TOPDIR/SOURCES/$base_image_path "$desired_tag"
    fi

    # Prepare the package repository
    if test -x $BUILD_ROOT/usr/bin/createrepo ; then
	chroot $BUILD_ROOT createrepo "$TOPDIR/SOURCES/repos" >/dev/null
    fi
    if test -x $BUILD_ROOT/usr/bin/dpkg-scanpackages ; then
	chroot $BUILD_ROOT bash -c "cd $TOPDIR/SOURCES/repos && dpkg-scanpackages -m . | gzip > Packages.gz"
    fi

    # Find tags
    FIRSTTAG=
    ALLTAGS=
    for t in $(perl -I$BUILD_DIR -MBuild::Docker -e Build::Docker::showtags "$BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE") ; do
	test -n "$FIRSTTAG" || FIRSTTAG="$t"
        ALLTAGS="$ALLTAGS $t"
    done
    if test -z "$FIRSTTAG" -a -f TAG ; then
	for t in $(egrep -v '^#' TAG) ; do
	    test -n "$FIRSTTAG" || FIRSTTAG="$t"
	    ALLTAGS="$ALLTAGS $t"
	done
    fi
    if test -z "$FIRSTTAG" ; then
        cleanup_and_exit 1 "please specify a tag for the container"
    fi
    ALLTAGS="${ALLTAGS# }"

    tagargs=()
    for t in $ALLTAGS; do
	tagargs[${#tagargs[@]}]='-t'
	tagargs[${#tagargs[@]}]="$t"
    done

    # patch in obs-docker-support helper
    sed -i '/^[ 	]*[fF][rR][oO][mM]/a COPY .obs-docker-support /usr/local/sbin/obs-docker-support\nRUN obs-docker-support --install' "$BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE"
    echo >> $BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE
    if test -n "$(sed -ne '/^[ 	]*[uU][sS][eE][rR][ 	]/p' "$BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE")" ; then
    	sed -i '1,/^[ 	]*[uU][sS][eE][rR][ 	]/{
/^[ 	]*[uU][sS][eE][rR][ 	]/i RUN obs-docker-support --uninstall
}' "$BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE"
    else
	echo 'RUN obs-docker-support --uninstall' >> "$BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE"
    fi
    test -n "$DISTURL" && echo "LABEL org.openbuildservice.disturl=$DISTURL" >> "$BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE"

    # now do the build
    echo "Building image $ALLTAGS"
    if ! chroot $BUILD_ROOT $DOCKER_TOOL build --network=host "${tagargs[@]}" -f "$TOPDIR/SOURCES/$RECIPEFILE" $TOPDIR/SOURCES/ ; then
        cleanup_and_exit 1 "Docker build command failed"
    fi

    # Save the resulting image to a tarball. Use tag for generating the file name.
    echo "Saving image $FIRSTTAG"
    mkdir -p $BUILD_ROOT$TOPDIR/DOCKER
    FILENAME="$FIRSTTAG"
    FILENAME="${FILENAME//[\/:]/-}"
    if ! chroot $BUILD_ROOT $DOCKER_TOOL save --output "$TOPDIR/DOCKER/$FILENAME.tar" "$FIRSTTAG" ; then
        cleanup_and_exit 1 "Docker save command failed"
    fi
    
    # Create containerinfo
    disturlarg=()
    test -n "$DISTURL" && disturlarg=("--disturl" "$DISTURL")
    perl -I$BUILD_DIR -MBuild::Docker -e Build::Docker::showcontainerinfo -- "${disturlarg[@]}" "$BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE" "$FILENAME.tar" "$ALLTAGS" containers/annotation> "$BUILD_ROOT$TOPDIR/DOCKER/$FILENAME.containerinfo"

    # We're done. Clean up.
    recipe_cleanup_docker
    BUILD_SUCCEEDED=true
}

recipe_resultdirs_docker() {
    echo DOCKER
}

recipe_cleanup_docker() {
    if test -n "$DOCKERD_STARTED" ; then
	test -n "$TOPDIR" && rm -f "$BUILD_ROOT/$TOPDIR/SOURCES/.obs-docker-support"
	DOCKERD_STARTED=
	$BUILD_DIR/startdockerd --root "$BUILD_ROOT" --kill
    fi
}

# Local Variables:
# mode: Shell-script
# End: