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