Merge "Support BuildFlags: nocumulaterpms" into devel
[tools/build.git] / build-recipe-livebuild
1 #################################################################
2 #
3 # Debian live-build specific functions.
4 #
5 # Author: Jan Blunck <jblunck@infradead.org>
6 #
7 # This file is part of build.
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License version 2 or 3 as
11 # published by the Free Software Foundation.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program (see the file COPYING); if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #
23 #################################################################
24
25 recipe_setup_livebuild() {
26     TOPDIR=/usr/src/packages
27     test "$DO_INIT_TOPDIR" = false || rm -rf "$BUILD_ROOT$TOPDIR"
28     for i in OTHER SOURCES LIVEBUILD_ROOT ; do
29         mkdir -p "$BUILD_ROOT$TOPDIR/$i"
30     done
31     chown -R "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT$TOPDIR"
32     if test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir ; then
33         mv "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
34     else
35         cp -p "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
36     fi
37 }
38
39 recipe_prepare_livebuild() {
40     :
41 }
42
43 createrepo_debian() {
44     local DIR=${1}
45     local ARCH=${2}
46     local DIST=${3}
47
48     if [ -z "${DIR}" -o ! -d ${DIR} -o ${DIR} = ${DIR##${BUILD_ROOT}} ] ; then
49         return
50     fi
51
52     pushd ${DIR} >/dev/null
53
54     # cleanup existing repository files
55     rm -f Packages Packages.gz Release
56     rm -fr dists
57
58     mkdir -p dists/${DIST}
59     # Suite is symlinked to Codename
60     ln -s ${DIST} dists/stable
61
62     # create Packages and Sources files
63     mkdir -p dists/${DIST}/main/binary-${ARCH}
64     mkdir -p dists/${DIST}/main/source
65     cat > ${BUILD_ROOT}/.createrepo_debian.tmp.sh <<-EOF
66         cd /.build.binaries || exit 1
67         dpkg-scanpackages -m . > dists/${DIST}/main/binary-${ARCH}/Packages
68         gzip -c9 < dists/${DIST}/main/binary-${ARCH}/Packages \
69             > dists/${DIST}/main/binary-${ARCH}/Packages.gz
70         dpkg-scansources . > dists/${DIST}/main/source/Sources
71         gzip -c9 dists/${DIST}/main/source/Sources \
72             > dists/${DIST}/main/source/Sources.gz
73         EOF
74     chroot $BUILD_ROOT su -c "sh /.createrepo_debian.tmp.sh" - root
75     local RESULT=$?
76     rm -f $BUILD_ROOT/.createrepo_debian.tmp.sh
77     [ "${RESULT}" != 0 ] && return
78
79     # create Release file
80     pushd dists/${DIST} >/dev/null
81     cat > Release <<-EOF
82         Origin: Debian
83         Label: Debian
84         Suite: stable
85         Version: 7.1
86         Codename: ${DIST}
87         Date: Sat, 15 Jun 2013 10:55:26 UTC
88         Description: Debian repository created by build-recipe-livebuild
89         Components: main
90         EOF
91     echo "SHA256:" >> Release
92     for file in main/binary-${ARCH}/Packages* ; do
93         local SUM=( $(sha256sum ${file}) )
94         local SIZE=$(stat -c '%s' ${file})
95         echo " ${SUM} ${SIZE} ${file}" >> Release
96     done
97     for file in main/source/Sources* ; do
98         local SUM=( $(sha256sum ${file}) )
99         local SIZE=$(stat -c '%s' ${file})
100         echo " ${SUM} ${SIZE} ${file}" >> Release
101     done
102     popd >/dev/null
103
104     # TODO: this is missing the signature with the private key
105
106     popd >/dev/null
107 }
108
109 # This script expects that the $BUILD_ROOT is a Debian installation with
110 # live-build already installed!
111 #
112 # Variables:
113 # $BUILD_ROOT the Debian chroot
114 # $TOPDIR/SOURCES includes the live-build config tarball
115 # $TOPDIR/$LIVEBUILD_ROOT where live-build will be called
116 # $RECIPEFILE the name of the live-build config tarball
117
118 recipe_build_livebuild() {
119     local ARCH=$(chroot $BUILD_ROOT su -c "dpkg-architecture -qDEB_BUILD_ARCH")
120     local DIST=$(chroot $BUILD_ROOT su -c "lsb_release --codename" | awk '{ print $2 }')
121     local LIVEBUILD_ROOT="LIVEBUILD_ROOT"
122
123     [ -z "${ARCH}" -o -z "${DIST}" ] && cleanup_and_exit 1
124
125     test -d $BUILD_ROOT/.build.binaries || cleanup_and_exit 1
126     if test "$DO_INIT" = true -o ! -d "$BUILD_ROOT/.build.binaries/dists" ; then
127         echo "creating repository metadata..."
128         createrepo_debian $BUILD_ROOT/.build.binaries ${ARCH} ${DIST}
129     fi
130
131     # Write our default configuration variables
132     mkdir -p $BUILD_ROOT/etc/live
133     cat > $BUILD_ROOT/etc/live/build.conf <<-EOF
134         LB_DEBIAN_INSTALLER_DISTRIBUTION="${DIST}"
135         LB_DISTRIBUTION="${DIST}"
136         LB_PARENT_DISTRIBUTION="${DIST}"
137         LB_PARENT_DEBIAN_INSTALLER_DISTRIBUTION="${DIST}"
138         LB_PARENT_MIRROR_BOOTSTRAP="file:/.build.binaries/"
139         LB_PARENT_MIRROR_CHROOT="file:/.build.binaries/"
140         LB_PARENT_MIRROR_CHROOT_SECURITY="file:/.build.binaries/"
141         LB_PARENT_MIRROR_BINARY="file:/.build.binaries/"
142         LB_PARENT_MIRROR_BINARY_SECURITY="file:/.build.binaries/"
143         LB_PARENT_MIRROR_DEBIAN_INSTALLER="file:/.build.binaries/"
144         LB_MIRROR_BOOTSTRAP="file:/.build.binaries/"
145         LB_MIRROR_CHROOT="file:/.build.binaries/"
146         LB_MIRROR_CHROOT_SECURITY="file:/.build.binaries/"
147         LB_MIRROR_BINARY="file:/.build.binaries/"
148         LB_MIRROR_BINARY_SECURITY="file:/.build.binaries/"
149         LB_MIRROR_DEBIAN_INSTALLER="file:/.build.binaries/"
150         LB_APT_SECURE="false"
151         LIVE_IMAGE_NAME="${RECIPEFILE%.livebuild}"
152         EOF
153
154     # Expand live-build configuration to $TOPDIR/$LIVEBUILD_ROOT
155     echo "Expanding live-build configuration"
156     tar -xvf $BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE \
157         -C $BUILD_ROOT/$TOPDIR/$LIVEBUILD_ROOT || cleanup_and_exit 1
158
159     # Skip top-level directory if it matches recipe name, ...
160     local files=($BUILD_ROOT/$TOPDIR/$LIVEBUILD_ROOT/*)
161     # ... but ignore some well known names
162     files=(${files[@]%%*/auto})
163     files=(${files[@]%%*/config})
164     files=(${files[@]%%*/local})
165     if [ ${#files[@]} -eq 1 ] && \
166         [ -d $BUILD_ROOT/$TOPDIR/$LIVEBUILD_ROOT/${RECIPEFILE%.livebuild} ]
167     then
168         LIVEBUILD_ROOT="LIVEBUILD_ROOT/${RECIPEFILE%.livebuild}"
169     fi
170
171     # Sanity check to not configure archives inside configuration
172     files=($BUILD_ROOT/$TOPDIR/$LIVEBUILD_ROOT/config/archives/*)
173     if [ ${#files[@]} -gt 0 ]; then
174         echo "E: No configuration in config/archives/* allowed"
175         cleanup_and_exit 1
176     fi
177
178     # TODO: Add the repository public key
179     # cp ... $BUILD_ROOT/$TOPDIR/$LIVEBUILD_ROOT/config/archives/debian.key
180
181     if [ -x $BUILD_ROOT/usr/lib/build/livebuild_pre_run ] ; then
182         echo "Running OBS build livebuild_pre_run hook"
183         chroot $BUILD_ROOT su -c \
184         "export RECIPEFILE=${RECIPEFILE}; /usr/lib/build/livebuild_pre_run" \
185         - root < /dev/null || cleanup_and_exit 1
186     fi
187
188     # TODO: this might move to lb auto/config file
189     if [ -f $BUILD_ROOT/$TOPDIR/SOURCES/livebuild_pre_run ] ; then
190         cp $BUILD_ROOT/$TOPDIR/SOURCES/livebuild_pre_run \
191             $BUILD_ROOT/.build.livebuild_pre_run
192         chmod +x $BUILD_ROOT/.build.livebuild_pre_run
193         echo "Running package livebuild_pre_run hook"
194         chroot $BUILD_ROOT su -c \
195         "export RECIPEFILE=${RECIPEFILE}; /.build.livebuild_pre_run" \
196         - root < /dev/null || cleanup_and_exit 1
197     fi
198
199     chroot $BUILD_ROOT su -c "cd $TOPDIR/$LIVEBUILD_ROOT && lb build" - root \
200         < /dev/null || cleanup_and_exit 1
201
202     # extract build result basenames
203     local build_results=""
204     for i in $BUILD_ROOT/$TOPDIR/$LIVEBUILD_ROOT/* ; do
205         test -f "$i" || continue
206         case "${i##*/}" in
207             *.hybrid.iso)
208                 build_results="${build_results}\n${i%%.hybrid.iso}"
209                 ;;
210             *.iso)
211                 build_results="${build_results}\n${i%%.iso}"
212                 ;;
213             *.img)
214                 build_results="${build_results}\n${i%%.img}"
215                 ;;
216             *.netboot.tar*)
217                 build_results="${build_results}\n${i%%.netboot.tar*}"
218                 ;;
219             *.tar*)
220                 build_results="${build_results}\n${i%%.tar*}"
221                 ;;
222             *)
223                 ;;
224         esac
225     done
226
227     # Fail the build if no build results are found
228     if [ -z "${build_results}" ] ; then
229         echo "No live-build result found"
230         cleanup_and_exit 1
231     fi
232
233     # move created products (and their metadata files) to destination
234     local buildnum="${RELEASE:+-Build${RELEASE}}"
235     for prefix in $(echo -e ${build_results} | sort | uniq) ; do
236         for f in ${prefix}.* ; do
237             mv ${f} \
238                 $BUILD_ROOT/$TOPDIR/OTHER/${prefix##*/}${buildnum}${f#${prefix}}
239             BUILD_SUCCEEDED=true
240         done
241     done
242 }
243
244 recipe_resultdirs_livebuild() {
245     # our results are already in OTHER
246     :
247 }
248
249 # Local Variables:
250 # mode: Shell-script
251 # End: