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