first attempt at a rework of kexclude -> {node,plc}yumexclude
[build.git] / build.common
1 # -*-Shell-script-*-
2 #
3 # Common functions for build scripts used by various packages
4 # incorporated (e.g., build, myplc, myplc-devel, vserver-reference)
5 #
6 # Marc E. Fiuczynski <mef@cs.princeton.edu>
7 # Copyright (C) 2007 The Trustees of Princeton University
8 # Thierry Parmentelat <thierry.parmentelat@inria.fr> INRIA
9 #
10 # supported distros f8, f10, f12, f14, f16, centos5, sl6
11 #
12 # for locating pkgs.py
13 export PATH=.:$PATH
14
15 # returns 'Fedora' or 'CentOS' for now
16 function pl_getDistro() {
17     if [ -f "/etc/redhat-release" ] ; then
18         distro=$(awk ' { print $1 } ' /etc/redhat-release)
19         case $distro in Scientific*) distro="SL" ; esac
20     else
21         echo "build.common.pl_getDistro-unknown"
22         exit 1
23     fi
24     echo "$distro"
25     return 0
26 }
27
28 # returns something like 8, 10, or 5.3
29 function pl_getRelease() {
30     if [ -f "/etc/redhat-release" ] ; then
31         release=$(awk ' { if ($1=="Fedora" && $2=="Core") print $4 ; if (($1=="Fedora" && $2!="Core")||$1=="CentOS") print $3 ; if ($1=="Scientific") print $4 } ' /etc/redhat-release)
32     else
33         echo "build.common.pl_getRelease-unknown"
34         exit 1
35     fi
36     # keep only the major number
37     echo "$release" | cut -d. -f1
38     return 0
39 }
40
41 # returns stuff for vserver, i.e. something like 'f8' or 'centos5'
42 function pl_getReleaseName () {
43     distro=$1; shift
44     release=$1; shift
45     case $distro in
46         [Ff]edora*)
47             releasename=f$release
48             ;;
49         [Cc]entOS*)
50             old_IFS="$IFS"
51             IFS="."
52             set -- $release
53             IFS="$old_IFS"
54             releasename=centos$1
55             ;;
56         [Ss]L*)
57             releasename=sl$release
58             ;;
59         *)
60             releasename="unknown-name-for-${pl_DISTRO}-please-edit-build.common"
61             echo 1>&2 "build.common: WARNING - releasename not set for distro=$distro" 
62             return 1
63             ;;
64     esac
65     echo "$releasename"
66     return 0
67 }
68
69 # yum exclusions are now defined in yumexclude.pkgs
70 # so they can now depend both on the linux distro and the pl distro
71 function pl_yumexclude () {
72     keyword=$1; shift
73     fcdistro=$1; shift
74     pldistro=$1; shift
75     # search for file "yumexclude.pkgs"
76     yumexclude_file=$(pl_locateDistroFile ../build/ $pldistro "yumexclude.pkgs")
77     # parse it
78     pl_parsePkgs keyword $fcdistro $pldistro $yumexclude_file || \
79         { echo pl_yumexclude failed with fcdistro=$fcdistro and pldistro=$pldistro; return 1 ; }
80 }
81
82 # node side : use the 'nodeyumexclude' keywork in yumexclude.pkgs
83 function pl_nodeyumexclude () { pl_yumexclude 'nodeyumexclude' "$@" ; }
84 function pl_plcyumexclude () { pl_yumexclude 'plcyumexclude' "$@" ; }
85
86
87 # figure out which redhat distro we are using (fedora, centos, redhat)
88 pl_DISTRO=$(pl_getDistro)
89
90 # select basearch of the host devel environment - protected for macos for local tests
91 # try arch for testing stuff on a mac
92 pl_DISTRO_ARCH=$(uname -i 2>/dev/null || arch 2> /dev/null || echo unknownarch)
93
94 # the release number (plain number)
95 pl_DISTRO_RELEASE=$(pl_getRelease)
96
97 # the release name - for vserver build ...
98 pl_DISTRO_NAME=$(pl_getReleaseName $pl_DISTRO $pl_DISTRO_RELEASE)
99
100 # get path to appropriate yumgroups.xml file
101 # Thierry: quick & dirty improvement 
102 # this file is updated by the toplevel build, from the .pkgs files
103 pl_DISTRO_YUMGROUPS="../../../RPMS/yumgroups.xml"
104
105 function pl_process_fedora_options () {
106     # Get options
107     shiftcount=0
108     while getopts "l:r:a:h" opt ; do
109         case $opt in
110             l)
111                 pl_DISTRO_URL=$OPTARG
112                 let shiftcount=$shiftcount+2
113                 ;;
114             r)
115                 pl_DISTRO_RELEASE=$OPTARG
116                 let shiftcount=$shiftcount+2
117                 ;;
118             a)
119                 pl_DISTRO_ARCH=$OPTARG
120                 let shiftcount=$shiftcount+2
121                 ;;
122             h|*)
123                 echo "Usage: $0 [OPTION]..."
124                 echo "  -l url          distro mirror location (default: $pl_DISTRO_URL)"
125                 echo "  -r release      distro release number (default: $pl_DISTRO_RELEASE)"
126                 echo "  -a arch         distro architecture (default: $pl_DISTRO_ARCH)"
127                 echo "where distro can be either fedora, centos, or redhat"
128                 echo "  -h              This message"
129                 exit 1
130                 ;;
131         esac
132     done
133     return $shiftcount
134 }
135
136 ######################################## handling a root image
137 function pl_root_rpm_macros () {
138     cat <<EOF
139 %_install_langs C:en_US:en
140 %_netsharedpath /proc:/dev/pts:/usr/share/info
141 %_excludedocs 1
142 %__file_context_path /dev/null
143 EOF
144 }
145
146 function pl_root_makedevs() {
147     vroot=$1
148     # Clean ${vroot}/dev, but only when ${vroot}!=""
149     [ -n $vroot ] && rm -rf $vroot/dev
150     
151     mkdir -p $vroot/dev
152     mknod -m 666 $vroot/dev/null c 1 3
153     mknod -m 666 $vroot/dev/zero c 1 5
154     mknod -m 666 $vroot/dev/full c 1 7
155     mknod -m 644 $vroot/dev/random c 1 8
156     mknod -m 644 $vroot/dev/urandom c 1 9
157     mknod -m 666 $vroot/dev/tty c 5 0
158     mknod -m 666 $vroot/dev/ptmx c 5 2
159     # For bash command substitution
160     ln -nsf ../proc/self/fd $vroot/dev/fd
161
162     # For df and linuxconf
163     touch $vroot/dev/hdv1
164
165     # For pseudo ttys
166     mkdir -p $vroot/dev/pts
167
168     # (Might have to remove the following for vserver-reference.)
169
170     # for tmpfs mount
171     mkdir -p $vroot/dev/shm
172
173     # For TUN/TAP
174     mkdir -p $vroot/dev/net
175     mknod -m 600 $vroot/dev/net/tun c 10 200
176
177     # For mkinitrd (in case a kernel is being installed)
178     # As well as for loop back mounting within a vserver. 
179     for i in $(seq 0 255) ; do
180         mknod -m 640 $vroot/dev/loop$i b 7 $i
181     done
182 }
183
184 function mkfedora_usage() {
185     echo "Usage: pl_root_mkfedora [OPTION]... basedir pldistro pkgsfile(s)"
186     echo "      -l url          Fedora mirror location."
187     echo "                      Defaults are searched in <pldistro>.mirrors"
188     echo "      -v              Be verbose"
189     echo "      -h              This message"
190     echo " target selection (defaults based on current build vserver)"
191     echo "      -r release      Fedora release number (default: $releasever)"
192     echo "      -a arch         Fedora architecture (default: $basearch)"
193     exit 1
194 }
195
196 function pl_root_mkfedora () {
197
198     echo "* Entering pl_root_mkfedora" "$@"
199
200     if [ $UID -ne 0 ] ; then
201         echo "Error: You must run this script as root."
202         exit 1
203     fi
204
205 # Verbosity
206     verbose=0
207
208 # Release and architecture to install : defaults to current vserver's settings or previously parsed fedora_options
209     releasever=$pl_DISTRO_RELEASE
210     basearch=$pl_DISTRO_ARCH
211
212 # Get options
213     while getopts "vh" opt ; do
214         case $opt in
215             v) verbose=1; set -x ;;
216             h|*) mkfedora_usage ;;
217         esac
218     done
219
220     shift $(($OPTIND - 1))
221     [[ "$#" -lt 3 ]] && mkfedora_usage
222     vroot=$1 ; shift
223     pldistro=$1 ; shift
224     pkgsfile="$@"
225     vroot=$(cd $vroot && pwd -P)
226     [ -d $vroot ] || mkfedora_usage
227
228
229     # parse pkgsfile and add to local vars
230     fcdistro=${pl_DISTRO_NAME}
231     pkgs_packages=$(pl_parsePkgs package $fcdistro $pldistro $pkgsfile) 
232     pkgs_groups=$(pl_parsePkgs group $fcdistro $pldistro $pkgsfile)
233     # what can get trashed to save space
234     pkgs_junk=$(pl_parsePkgs junk $fcdistro $pldistro $pkgsfile)
235     # but not this
236     pkgs_precious=$(pl_parsePkgs precious $fcdistro $pldistro $pkgsfile)
237     # formerly related to mkfedora -k : packages to take from our own build 
238     # and thus need be excluded frem the stock repos
239     SUBST_NODEYUMEXCLUDE=$(pl_nodeyumexclude $fcdistro $pldistro)
240     pkgs_yumexcludes=$(pl_parsePkgs yumexclude $fcdistro $pldistro $pkgsfile | sed -e s,@NODEYUMEXCLUDE@,"$SUBST_NODEYUMEXCLUDE",)
241     # get mirrors if not specified with -l
242     if [ -z "$mirrors" ] ; then
243         mirrorsfile=$(pl_locateDistroFile ../build/ $pldistro "$pldistro.mirrors")
244         mirrors=$(pl_parsePkgs mirror $fcdistro $pldistro $mirrorsfile)
245     fi
246
247     yumexclude_line=""
248     [ -n "$pkgs_yumexcludes" ] && yumexclude_line="exclude=$pkgs_yumexcludes"
249
250     echo "$0: candidate mirrors"
251     for mirror in $mirrors ; do
252         echo "* candidate mirror $mirror"
253     done
254
255     # the repo part of the final yum.conf
256     yum_conf_repos=$vroot/xxxmkfedora-repos.confxxx
257     if ! yumconf_mirrors $yum_conf_repos ../build/ $fcdistro "$yumexclude_line" $mirrors ; then
258         echo xxx -- error ; return 1
259     fi
260     
261     # Do not tolerate errors
262     set -e
263
264     public_gpg_key=$(yumconf_gpgkey $yum_conf_repos)
265
266     ## make rpms ignore installing stuff to special fs entries like /proc
267     # Because of https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=52725
268     # you have to use at least one language beside 'C'
269     # Prevent all locales from being installed in reference image
270     mkdir -p $vroot/etc/rpm
271     pl_root_rpm_macros > $vroot/etc/rpm/macros 
272
273     # Trick rpm and yum, who read the real root /etc/rpm/macros file
274     # rather than the one installed in the reference image, despite what
275     # you might expect the --root and --installroot options to mean. Both
276     # programs always read $HOME/.rpmmacros.
277     export HOME=$vroot/tmp
278     mkdir -p $vroot/tmp
279     pl_root_rpm_macros > $vroot/tmp/.rpmmacros
280
281     function mkfedora_cleanup () {
282         umount -l $vroot/proc
283         umount -l $vroot/dev/shm
284         umount -l $vroot/dev/pts
285     }
286
287     # Clean up before exiting if anything goes wrong
288     trap "mkfedora_cleanup" ERR INT
289
290     # Mount in reference image
291     mount -t devpts none $vroot/dev/pts
292     mount -t tmpfs none $vroot/dev/shm
293     mkdir -p $vroot/proc
294     mount -t proc none $vroot/proc
295     
296     # Create a /var/lib dirs for yum & rpm
297     mkdir -p $vroot/var/lib/yum
298     mkdir -p $vroot/var/lib/rpm
299     mkdir -p $vroot/usr/share/info
300
301     # Create a dummy /etc/fstab in reference image
302     mkdir -p $vroot/etc
303     cat >$vroot/etc/fstab <<EOF
304 # This fake fstab exists only to please df and linuxconf.
305 /dev/hdv1       /       ext2    defaults        1 1
306 EOF
307     cp $vroot/etc/fstab $vroot/etc/mtab
308
309     # Necessary for some scripts
310     mkdir -p $vroot/etc/sysconfig
311     echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
312
313     # Initialize RPM database in reference image
314     mkdir -p $vroot/var/lib/rpm
315     rpm --root $vroot --initdb
316     rpm --root $vroot --import $public_gpg_key
317
318     # Initialize yum in reference image
319     mkdir -p $vroot/var/cache/yum $vroot/var/log
320
321 # yum.conf is for building only - store in different location than /etc/yum.conf
322     yum_conf=$vroot/etc/mkfedora-yum.conf
323     cat > $yum_conf <<EOF
324 [main]
325 cachedir=/var/cache/yum
326 debuglevel=2
327 logfile=/var/log/yum.log
328 pkgpolicy=newest
329 multilib_policy=best
330 distroverpkg=redhat-release
331 tolerant=1
332 exactarch=1
333 retries=20
334 obsoletes=1
335 gpgcheck=0
336 # Prevent yum-2.4 from loading additional repository definitions
337 # (e.g., from /etc/yum.repos.d/)
338 reposdir=/dev/null
339 EOF
340     
341     cat $yum_conf_repos >> $yum_conf
342
343     # If we are being built as part of an automated RPM build, solve the
344     # bootstrap problem by including any just built packages in the yum
345     # configuration. This cooperates with the PlanetLab build system.
346     if [ -n "$RPM_BUILD_DIR" ] ; then
347         RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
348         # If run under sudo, allow user to delete the headers/ and
349         # repodata/ directories.
350         if [ -n "$SUDO_USER" ] ; then
351             chown -R $SUDO_USER $RPM_RPMS_DIR
352         fi
353         cat >> $yum_conf <<EOF
354
355 [building]
356 name=Building - $basearch - $RPM_RPMS_DIR/
357 baseurl=file://$RPM_RPMS_DIR/
358 EOF
359 fi
360
361     echo "========== Dumping $yum_conf"
362     cat $yum_conf
363     echo "========== EndDump $yum_conf"
364
365     yum_options=""
366 #    yum --help | grep verbose &> /dev/null && yum_options="$yum_options --verbose"
367     yum_options="$yum_options -y"
368     yum_options="$yum_options -c $yum_conf"
369     yum_options="$yum_options --installroot=$vroot"
370
371     # glibc must be specified explicitly for the correct arch to be
372     # chosen.
373     echo "* Installing glibc"
374     # ignore yum's return code that is basically undefined
375     yum $yum_options install glibc || :
376
377     # Go, baby, go
378     if [ -n "$pkgs_packages" ] ; then
379         echo "* Installing optional packages" $pkgs_packages
380         # ignore yum's return code that is basically undefined
381         echo "* Install options" $vroot $yum_options 
382         yum $yum_options install $pkgs_packages || :
383         if ! rpm --root $vroot -q $pkgs_packages >/dev/null ; then
384             echo "* Warning: Missing packages"
385             rpm --root $vroot -q $pkgs_packages | grep "not installed"
386         fi
387     fi
388
389     if [ -n "$pkgs_groups" ] ; then
390        ## call yum sequentially to get finer-grained info on dependencies
391         for group_plus in $pkgs_groups ; do
392             group=$(echo $group_plus | sed -e "s,+++, ,g")
393             echo "* Installing optional group $group" 
394             # ignore yum's return code that is basically undefined
395             yum $yum_options groupinstall "$group" || :
396         done
397     fi
398
399     # formerly in bootcd/prep.sh : to optimize footprint
400     if [ -n "$pkgs_junk" ] ; then
401         echo "* Removing unnecessary junk"
402         pushd $vroot
403         # Save precious files
404         [ -n "$pkgs_precious" ] && tar --ignore-failed-read -cpf precious.tar $pkgs_precious
405         # Remove unnecessary junk
406         [ -n "$pkgs_junk" ] && rm -rf $pkgs_junk
407         # Restore precious files
408         [ -n "$pkgs_precious" ] && tar -xpf precious.tar && rm -f precious.tar
409         popd
410     fi
411
412     # Clean yum cache
413     echo "* Cleaning up"
414
415     # NOTE: this hack is for Fedora >= 12.
416     # if kernel-debug is installed, clean it up
417     # we link to our version of kernel/initrd and clean up
418     # kernel-debug manually
419     if rpm --root $vroot --quiet -q kernel-debug ; then
420         echo "* Cleaning up kernel-debug - (workaround for f12)"
421         pushd $vroot/boot/
422         rm -rf kernel-boot kernel-bootsmp initrd-boot initrd-bootsmp
423         ln -s vmlinuz-*${pldistro}* kernel-boot
424         ln -s vmlinuz-*${pldistro}* kernel-bootsmp
425         ln -s initrd-*${pldistro}* initrd-boot
426         ln -s initrd-*${pldistro}* initrd-bootsmp
427         rpm --root $vroot --nodeps -e kernel-debug || :
428         popd
429     fi
430
431     # ignore yum's return code that is basically undefined
432     yum $yum_options clean all || :
433
434     # Clean RPM state
435     rm -f $vroot/var/lib/rpm/__db*
436
437     # Set time zone to UTC
438     if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
439         rm -f $vroot/etc/localtime
440         ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
441     fi
442
443     echo "Dumping current list of rpms in /etc/mkfedora-rpms.txt"
444     chroot $vroot rpm -aq | sort > $vroot/etc/mkfedora-rpms.txt
445
446     # remove trap handler, as we are about to call it directly.
447     trap - ERR INT
448
449     # Clean up
450     mkfedora_cleanup
451
452     return 0
453 }
454
455 function pl_root_tune_image () {
456     root=$1; shift
457
458     # Disable all services in reference image
459     chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
460
461     # FC2 minilogd starts up during shutdown and makes unmounting
462     # impossible. Just get rid of it.
463     rm -f $root/sbin/minilogd
464     ln -nsf /bin/true $root/sbin/minilogd
465
466     # This tells the Boot Manager that it is okay to update
467     # /etc/resolv.conf and /etc/hosts whenever the network configuration
468     # changes. Users are free to delete this file.
469     touch $root/etc/AUTO_UPDATE_NET_FILES
470 }
471
472 # Move specified directories out of a src tree into a dst tree, and
473 # then for each moved directory create a symlink in src to dst.
474 function pl_move_dirs() {
475     root=$1
476     data=$2
477     store=$3
478     shift 3
479
480     mkdir -p $root/data
481     for datadir in "$@" ; do
482         mkdir -p ${data}${datadir}
483         if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
484             (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
485         fi
486         rm -rf ${root}/${datadir}
487         mkdir -p $(dirname ${root}/${datadir})
488         ln -nsf ${store}/${datadir} ${root}/${datadir}
489     done
490 }
491
492 # Construct an image file from given some directory
493 # XXX in the future maybe use livecdtools?
494 function pl_make_image() {
495     root=$1
496     image=$2
497     extraspace=$3
498
499     # Leave about 100 MB free space and allow for about 20% inode overhead
500     bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
501     bs=4096
502     blocks=$(($bytes / $bs))
503     dd bs=$bs count=$blocks if=/dev/zero of=$image
504     mkfs.ext3 -b $bs -j -F $image
505
506     # Temporarily mount it
507     tmp=$(mktemp -d tmp.XXXXXX)
508     mount -o loop $image $tmp
509     trap "umount $tmp; rmdir $tmp" ERR INT
510
511     # Move files to it
512     (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
513
514     # Unmount it
515     umount $tmp
516     rmdir $tmp
517     trap - ERR INT
518 }
519
520 # Fix permissions on tmp directories
521 function pl_fixtmp_permissions() {
522     root=$1
523     chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
524 }
525
526 function pl_fixdirs() {
527     root=$1
528     datadirs=$2
529     for datadir in datadirs ; do
530         if [ -h ${root}/${datadir} ] ; then
531             rm -f ${root}/${datadir}
532             mkdir -p ${root}/${datadir}
533         fi
534     done
535 }
536
537 ########## .pkgs format
538 # Usage: pl_parsePkgs keyword [-a arch] fcdistro pldistro pkgs-file[..s]
539 # pkgs.py should be found in PATH, like this file build.common
540 function pl_parsePkgs () {
541     target_arch=$pl_DISTRO_ARCH
542     keyword=$1;shift
543     [ "$1" == "-a" ] && { shift; target_arch="$1"; shift; }
544     fcdistro=$1; shift
545     pldistro=$1; shift
546
547     echo 1>&2 "pl_parsePkgs: using -a $target_arch -f $fcdistro -d $pldistro $keyword $@"
548     pkgs.py -a $target_arch -f $fcdistro -d $pldistro $keyword "$@" 
549 }
550 # usage: pl_getPackages [-a arch] fcdistro pldistro pkg-file[..s]
551 function pl_getPackages() { pl_parsePkgs package "$@" ; }
552 function pl_getGroups() { pl_parsePkgs group "$@" ; }
553
554 ##############################
555 ### temporary - only for checking the new python version
556 # for a given keyword like 'package' :
557 # we support fcdistro-dependant format, for tokens (pkgname) without whitespace
558 # you can e.g. use
559 # package: pkg1 .. pkgn 
560 # package+f8: pkg1 .. pkgn
561 # package-f8: pkg1 .. pkgn
562
563 # values can contain @arch@, @fcdistro@ or @pldistro@ that are replaced with the current values
564 #
565 # the reason for the -a option is for when we build the build vserver itself; in this case
566 # pl_DISTRO_ARCH is the one we obtain from the root context, and that's wrong
567 # specify -sa arch AFTER keyword, so as to keep pl_getPackages and pl_getGroups simple
568 #
569 function pl_parsePkgs_old () {
570
571     target_arch=$pl_DISTRO_ARCH
572     keyword=$1;shift
573     [ "$1" == "-a" ] && { shift; target_arch="$1"; shift; }
574     fcdistro=$1; shift
575     pldistro=$1; shift
576     # remaining arguments are paths to the pkgs files
577
578     # grab regular descriptions
579     all=$(grep -v '^#' "$@" | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,")
580     # grab additions
581     add=$(grep -v '^#' "$@" | grep --regexp="^${keyword}+${fcdistro}:" | sed -e "s,${keyword}+${fcdistro}:,,")
582     # grab exclusions
583     sub=$(grep -v '^#' "$@" | grep --regexp="^${keyword}-${fcdistro}:" | sed -e "s,${keyword}-${fcdistro}:,,")
584
585     for rpm in $all $add; do
586         for exclude in $sub; do
587             [ "$rpm" = "$exclude" ] && continue 2
588         done
589         echo "${rpm} " | sed -e "s,@arch@,${target_arch},g" -e "s,@fcdistro@,$fcdistro,g" -e "s,@pldistro@,$pldistro,g"
590     done
591     return 0
592 }
593 ##############################
594
595 # locates a pldistro-dependant file
596 # tries first in build/<pldistro>/, then in build/planetlab/
597 function pl_locateDistroFile () {
598     builddir=$1; shift
599     pldistro=$1; shift
600     pkgsfile=$1; shift
601
602     pkgspath=""
603     # if config dir is missing but a .svnpath or a .gitpath exists, use it to extract the config dir
604     configdir="$builddir/config.${pldistro}"
605     if [ ! -d $configdir ] ; then
606         if [ -f "${configdir}.svnpath" -o -f "${configdir}.gitpath" ] ; then
607             echo 1>&2 "Invoking make to extract remote config.${pldistro}"
608             make 1>&2 --silent --no-print-directory -C $builddir stage1=true config.${pldistro}
609         fi
610     fi
611     # locate it
612     paths="$builddir/config.$pldistro/$pkgsfile $builddir/config.planetlab/$pkgsfile"
613     for path in $paths; do
614         if [ -f $path ] ; then
615             pkgspath=$path
616             break
617         fi
618     done
619     if [ -z "$pkgspath" ] ; then
620         echo 1>&2 "pl_locateDistroFile - in $(pwd) : cannot locate $pkgsfile in $builddir"
621         echo 1>&2 "candidates were $paths"
622         echo "not-found-by-pl_locateDistroFile"
623         return 1
624     else
625         echo 1>&2 "pl_locateDistroFile: using $pkgspath"
626         echo $pkgspath
627         return 0
628     fi
629 }
630
631 function yumgroups_from_pkgs () {
632     builddir=$1; shift
633     pldistro=$1; shift
634     fcdistro=$1; shift
635     pkgsnames=$@
636
637     sedargs="-e s,@FCDISTRO@,$fcdistro,g"
638
639    cat <<__header
640 <?xml version="1.0"?>
641 <!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
642 <comps>
643 __header
644
645     for pkgsname in $pkgsnames; do
646         pkgsfile=$(pl_locateDistroFile $builddir $pldistro $pkgsname)
647         packages=$(pl_getPackages $fcdistro $pldistro $pkgsfile)
648
649         groupname=$(pkgs.py groupname $pkgsfile | sed $sedargs)
650         groupdesc=$(pkgs.py groupdesc $pkgsfile | sed $sedargs)
651
652         if [ -z "$groupname" -o -z "$groupdesc" ] ; then
653             echo "Cannot find groupname: and groupdesc: in $pkgsfile -- skipped" 1>&2
654             continue
655         fi
656         
657         cat << __group_header
658   <group>
659     <id>$(echo $groupname|tr A-Z a-z)</id>
660     <name>$groupname</name>
661     <description>$groupdesc</description>
662     <uservisible>true</uservisible>
663     <packagelist>
664 __group_header
665         for package in $packages; do 
666             echo "<packagereq type=\"mandatory\">$package</packagereq>"
667         done
668         cat << __group_footer
669     </packagelist>
670   </group>
671 __group_footer
672     done
673 cat <<__footer
674 </comps>
675 __footer
676 }
677
678
679 function build_fetch () {
680     curl --fail --silent --max-time 60 --output /dev/null "$1" 
681 }
682
683 # tries to compute a valid yum.conf for that pldistro from the template in mirroring/
684 # returns 0 and writes <dest_yumconf> on success
685 # returns 1 on failure, in which case <dest_yumconf> is deleted
686 function yumconf_mirrors () {
687     dest_yumconf=$1; shift
688     builddir=$1; shift
689     fcdistro=$1; shift
690     yumexclude_line="$1" ; shift
691     mirrors="$@"
692
693     template=$builddir/mirroring/$fcdistro/yum.repos.d/building.repo.in
694     
695     if [ ! -f $template ] ; then
696         echo "yumconf_mirrors: cannot locate template $template"
697         rm -f $dest_yumconf
698         return 1
699     fi
700
701     for mirror in $mirrors; do
702         if yumconf_mirror $dest_yumconf $template "$yumexclude_line" $mirror; then
703             return 0
704         fi
705     done
706     echo 'yumconf_mirrors in build.common : ran out of mirrors -- BAILING OUT'
707     rm -f $dest_yumconf
708     return 1
709 }
710
711 # computes a yum.conf from the template, and checks that all baseurl defined in there are valid repos
712 # returns 0 on success and 1 on failure
713 function yumconf_mirror () {
714     dest_yumconf=$1; shift
715     template=$1; shift
716     yumexclude_line="$1" ; shift
717     mirror=$1; shift
718
719     sed -e "s,@MIRRORURL@,$mirror,g" \
720         -e "/baseurl=/i\\
721 $yumexclude_line" $template > $dest_yumconf
722     
723     # capture all lines defining baseurl
724     baseurl_defs=$(grep '^baseurl=' $dest_yumconf)
725     if [ -z "$baseurl_defs" ] ; then
726         return 1
727     fi
728
729     for baseurl_def in $baseurl_defs; do
730         baseurl=$(echo $baseurl_def | sed \
731             -e s,baseurl=,, \
732             -e 's,$basearch,'"$pl_DISTRO_ARCH",g)
733         repomd=$baseurl/repodata/repomd.xml
734
735         echo "* Trying to fetch $repomd"
736         if ! build_fetch $repomd ; then
737             echo "* Failed to fetch $repomd"
738             return 1
739         fi
740     done
741     echo "* Selected mirror $mirror"
742     return 0
743 }
744
745 # from a yum.conf as generated above, computes the gpgkey urls
746 function yumconf_gpgkey () {
747     dest_yumconf=$1; shift
748
749     values=$(grep -h '^gpgkey=' $dest_yumconf | sed -e s,gpgkey=,, | sed -e 's,$basearch,'"$pl_DISTRO_ARCH",g | sed -e 's, ,\n,g' | sort | uniq | xargs)
750     [ -n "$values" ] || return 1
751     echo $values
752     return 0
753 }
754
755 # patches a yum conf to insert an exclude line in each declared repo
756 function yumconf_exclude () {
757     repo=$1; shift
758     yumexclude_line="$1" ; shift
759     
760     sed -i -e "/#baseurl=.*$/i\\
761 $yumexclude_line" $repo
762 }