pkgs.py accepts new option -u to avoid sorting - used when loading <distro>.mirrors
[build.git] / build.common
1 # -*-Shell-script-*-
2 #
3 # Common functions for build scripts used by various packages
4 # incorporated (e.g., build, bootcd, nodeimage, sliceimage)
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 distro shortname, 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 - something like 'f8' or 'sl6'
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     # 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 vm. 
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 VM context)"
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 vm 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     # locate builddir by looking for pkgs.py
245     builddir=$(dirname $(type -p pkgs.py))
246     SUBST_NODEYUMEXCLUDE=$(pl_nodeyumexclude $fcdistro $pldistro $builddir)
247     pkgs_yumexclude=$(pl_parsePkgs yumexclude $fcdistro $pldistro $pkgsfile | sed -e s,@NODEYUMEXCLUDE@,"$SUBST_NODEYUMEXCLUDE",)
248     # get mirrors if not specified with -l
249     if [ -z "$mirrors" ] ; then
250         mirrorsfile=$(pl_locateDistroFile ../build/ $pldistro "$pldistro.mirrors")
251         # do not sort mirrors, keep order from input
252         mirrors=$(pl_parsePkgs -u mirror $fcdistro $pldistro $mirrorsfile)
253     fi
254
255     yumexclude_line=""
256     [ -n "$pkgs_yumexclude" ] && yumexclude_line="exclude=$pkgs_yumexclude"
257
258     echo "$0: candidate mirrors"
259     for mirror in $mirrors ; do
260         echo "* candidate mirror $mirror"
261     done
262
263     # the repo part of the final yum.conf
264     yum_conf_repos=$vroot/xxxmkfedora-repos.confxxx
265     if ! yumconf_mirrors $yum_conf_repos ../build/ $fcdistro "$yumexclude_line" $mirrors ; then
266         echo xxx -- error ; return 1
267     fi
268     
269     # Do not tolerate errors
270     set -e
271
272     public_gpg_key=$(yumconf_gpgkey $yum_conf_repos)
273
274     ## make rpms ignore installing stuff to special fs entries like /proc
275     # Because of https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=52725
276     # you have to use at least one language beside 'C'
277     # Prevent all locales from being installed in reference image
278     mkdir -p $vroot/etc/rpm
279     pl_root_rpm_macros > $vroot/etc/rpm/macros 
280
281     # Trick rpm and yum, who read the real root /etc/rpm/macros file
282     # rather than the one installed in the reference image, despite what
283     # you might expect the --root and --installroot options to mean. Both
284     # programs always read $HOME/.rpmmacros.
285     export HOME=$vroot/tmp
286     mkdir -p $vroot/tmp
287     pl_root_rpm_macros > $vroot/tmp/.rpmmacros
288
289     function mkfedora_cleanup () {
290         umount -l $vroot/proc
291         umount -l $vroot/dev/shm
292         umount -l $vroot/dev/pts
293     }
294
295     # Clean up before exiting if anything goes wrong
296     trap "mkfedora_cleanup" ERR INT
297
298     # Mount in reference image
299     mount -t devpts none $vroot/dev/pts
300     mount -t tmpfs none $vroot/dev/shm
301     mkdir -p $vroot/proc
302     mount -t proc none $vroot/proc
303     
304     # Create a /var/lib dirs for yum & rpm
305     mkdir -p $vroot/var/lib/yum
306     mkdir -p $vroot/var/lib/rpm
307     mkdir -p $vroot/usr/share/info
308
309     # Create a dummy /etc/fstab in reference image
310     mkdir -p $vroot/etc
311     cat >$vroot/etc/fstab <<EOF
312 # This fake fstab exists only to please df and linuxconf.
313 /dev/hdv1       /       ext2    defaults        1 1
314 EOF
315     cp $vroot/etc/fstab $vroot/etc/mtab
316
317     # Necessary for some scripts
318     mkdir -p $vroot/etc/sysconfig
319     echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
320
321     # Initialize RPM database in reference image
322     mkdir -p $vroot/var/lib/rpm
323     rpm --root $vroot --initdb
324     rpm --root $vroot --import $public_gpg_key
325
326     # Initialize yum in reference image
327     mkdir -p $vroot/var/cache/yum $vroot/var/log
328
329 # yum.conf is for building only - store in different location than /etc/yum.conf
330     yum_conf=$vroot/etc/mkfedora-yum.conf
331     cat > $yum_conf <<EOF
332 [main]
333 cachedir=/var/cache/yum
334 debuglevel=2
335 logfile=/var/log/yum.log
336 pkgpolicy=newest
337 multilib_policy=best
338 distroverpkg=redhat-release
339 tolerant=1
340 exactarch=1
341 retries=20
342 obsoletes=1
343 gpgcheck=0
344 # Prevent yum-2.4 from loading additional repository definitions
345 # (e.g., from /etc/yum.repos.d/)
346 reposdir=/dev/null
347 EOF
348     
349     cat $yum_conf_repos >> $yum_conf
350
351     # If we are being built as part of an automated RPM build, solve the
352     # bootstrap problem by including any just built packages in the yum
353     # configuration. This cooperates with the PlanetLab build system.
354     if [ -n "$RPM_BUILD_DIR" ] ; then
355         RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
356         # If run under sudo, allow user to delete the headers/ and
357         # repodata/ directories.
358         if [ -n "$SUDO_USER" ] ; then
359             chown -R $SUDO_USER $RPM_RPMS_DIR
360         fi
361         cat >> $yum_conf <<EOF
362
363 [building]
364 name=Building - $basearch - $RPM_RPMS_DIR/
365 baseurl=file://$RPM_RPMS_DIR/
366 EOF
367 fi
368
369     echo "========== Dumping $yum_conf"
370     cat $yum_conf
371     echo "========== EndDump $yum_conf"
372
373     yum_options=""
374 #    yum --help | grep verbose &> /dev/null && yum_options="$yum_options --verbose"
375     yum_options="$yum_options -y"
376     yum_options="$yum_options -c $yum_conf"
377     yum_options="$yum_options --installroot=$vroot"
378
379     # glibc must be specified explicitly for the correct arch to be
380     # chosen.
381     echo "* Installing glibc"
382     # ignore yum's return code that is basically undefined
383     yum $yum_options install glibc || :
384
385     # Go, baby, go
386     if [ -n "$pkgs_packages" ] ; then
387         echo "* Installing optional packages" $pkgs_packages
388         # ignore yum's return code that is basically undefined
389         echo "* Install options" $vroot $yum_options 
390         yum $yum_options install $pkgs_packages || :
391         if ! rpm --root $vroot -q $pkgs_packages >/dev/null ; then
392             echo "* Warning: Missing packages"
393             rpm --root $vroot -q $pkgs_packages | grep "not installed"
394         fi
395     fi
396
397     if [ -n "$pkgs_groups" ] ; then
398        ## call yum sequentially to get finer-grained info on dependencies
399         for group_plus in $pkgs_groups ; do
400             group=$(echo $group_plus | sed -e "s,+++, ,g")
401             echo "* Installing optional group $group" 
402             # ignore yum's return code that is basically undefined
403             yum $yum_options groupinstall "$group" || :
404         done
405     fi
406
407     # formerly in bootcd/prep.sh : to optimize footprint
408     if [ -n "$pkgs_junk" ] ; then
409         echo "* Removing unnecessary junk"
410         pushd $vroot
411         # Save precious files
412         [ -n "$pkgs_precious" ] && tar --ignore-failed-read -cpf precious.tar $pkgs_precious
413         # Remove unnecessary junk
414         [ -n "$pkgs_junk" ] && rm -rf $pkgs_junk
415         # Restore precious files
416         [ -n "$pkgs_precious" ] && tar -xpf precious.tar && rm -f precious.tar
417         popd
418     fi
419
420     # Clean yum cache
421     echo "* Cleaning up"
422
423     # NOTE: this hack is for Fedora >= 12.
424     # if kernel-debug is installed, clean it up
425     # we link to our version of kernel/initrd and clean up
426     # kernel-debug manually
427     if rpm --root $vroot --quiet -q kernel-debug ; then
428         echo "* Cleaning up kernel-debug - (workaround for f12)"
429         pushd $vroot/boot/
430         rm -rf kernel-boot kernel-bootsmp initrd-boot initrd-bootsmp
431         ln -s vmlinuz-*${pldistro}* kernel-boot
432         ln -s vmlinuz-*${pldistro}* kernel-bootsmp
433         ln -s initrd-*${pldistro}* initrd-boot
434         ln -s initrd-*${pldistro}* initrd-bootsmp
435         rpm --root $vroot --nodeps -e kernel-debug || :
436         popd
437     fi
438
439     # ignore yum's return code that is basically undefined
440     yum $yum_options clean all || :
441
442     # Clean RPM state
443     rm -f $vroot/var/lib/rpm/__db*
444
445     # Set time zone to UTC
446     if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
447         rm -f $vroot/etc/localtime
448         ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
449     fi
450
451     echo "Dumping current list of rpms in /etc/mkfedora-rpms.txt"
452     chroot $vroot rpm -aq | sort > $vroot/etc/mkfedora-rpms.txt
453
454     # remove trap handler, as we are about to call it directly.
455     trap - ERR INT
456
457     # Clean up
458     mkfedora_cleanup
459
460     return 0
461 }
462
463 function pl_root_tune_image () {
464     root=$1; shift
465
466     # Disable all services in reference image
467     chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
468
469     # FC2 minilogd starts up during shutdown and makes unmounting
470     # impossible. Just get rid of it.
471     rm -f $root/sbin/minilogd
472     ln -nsf /bin/true $root/sbin/minilogd
473
474     # This tells the Boot Manager that it is okay to update
475     # /etc/resolv.conf and /etc/hosts whenever the network configuration
476     # changes. Users are free to delete this file.
477     touch $root/etc/AUTO_UPDATE_NET_FILES
478 }
479
480 # Move specified directories out of a src tree into a dst tree, and
481 # then for each moved directory create a symlink in src to dst.
482 function pl_move_dirs() {
483     root=$1
484     data=$2
485     store=$3
486     shift 3
487
488     mkdir -p $root/data
489     for datadir in "$@" ; do
490         mkdir -p ${data}${datadir}
491         if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
492             (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
493         fi
494         rm -rf ${root}/${datadir}
495         mkdir -p $(dirname ${root}/${datadir})
496         ln -nsf ${store}/${datadir} ${root}/${datadir}
497     done
498 }
499
500 # Construct an image file from given some directory
501 # XXX in the future maybe use livecdtools?
502 function pl_make_image() {
503     root=$1
504     image=$2
505     extraspace=$3
506
507     # Leave about 100 MB free space and allow for about 20% inode overhead
508     bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
509     bs=4096
510     blocks=$(($bytes / $bs))
511     dd bs=$bs count=$blocks if=/dev/zero of=$image
512     mkfs.ext3 -b $bs -j -F $image
513
514     # Temporarily mount it
515     tmp=$(mktemp -d tmp.XXXXXX)
516     mount -o loop $image $tmp
517     trap "umount $tmp; rmdir $tmp" ERR INT
518
519     # Move files to it
520     (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
521
522     # Unmount it
523     umount $tmp
524     rmdir $tmp
525     trap - ERR INT
526 }
527
528 # Fix permissions on tmp directories
529 function pl_fixtmp_permissions() {
530     root=$1
531     chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
532 }
533
534 function pl_fixdirs() {
535     root=$1
536     datadirs=$2
537     for datadir in datadirs ; do
538         if [ -h ${root}/${datadir} ] ; then
539             rm -f ${root}/${datadir}
540             mkdir -p ${root}/${datadir}
541         fi
542     done
543 }
544
545 ########## .pkgs format
546 # Usage: pl_parsePkgs keyword [-a arch] fcdistro pldistro pkgs-file[..s]
547 # pkgs.py should be found in PATH, like this file build.common
548 function pl_parsePkgs () {
549     target_arch=$pl_DISTRO_ARCH
550     keyword=$1;shift
551     [ "$1" == "-a" ] && { shift; target_arch="$1"; shift; }
552     fcdistro=$1; shift
553     pldistro=$1; shift
554
555     echo 1>&2 "pl_parsePkgs: using -a $target_arch -f $fcdistro -d $pldistro $keyword $@"
556     pkgs.py -a $target_arch -f $fcdistro -d $pldistro $keyword "$@" 
557 }
558 # usage: pl_getPackages [-a arch] fcdistro pldistro pkg-file[..s]
559 function pl_getPackages() { pl_parsePkgs package "$@" ; }
560 function pl_getGroups() { pl_parsePkgs group "$@" ; }
561
562 ##############################
563 ### temporary - only for checking the new python version
564 # for a given keyword like 'package' :
565 # we support fcdistro-dependant format, for tokens (pkgname) without whitespace
566 # you can e.g. use
567 # package: pkg1 .. pkgn 
568 # package+f8: pkg1 .. pkgn
569 # package-f8: pkg1 .. pkgn
570
571 # values can contain @arch@, @fcdistro@ or @pldistro@ that are replaced with the current values
572 #
573 # the reason for the -a option is for when we build the build vm itself; in this case
574 # pl_DISTRO_ARCH is the one we obtain from the root context, and that's wrong
575 # specify -sa arch AFTER keyword, so as to keep pl_getPackages and pl_getGroups simple
576 #
577 function pl_parsePkgs_old () {
578
579     target_arch=$pl_DISTRO_ARCH
580     keyword=$1;shift
581     [ "$1" == "-a" ] && { shift; target_arch="$1"; shift; }
582     fcdistro=$1; shift
583     pldistro=$1; shift
584     # remaining arguments are paths to the pkgs files
585
586     # grab regular descriptions
587     all=$(grep -v '^#' "$@" | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,")
588     # grab additions
589     add=$(grep -v '^#' "$@" | grep --regexp="^${keyword}+${fcdistro}:" | sed -e "s,${keyword}+${fcdistro}:,,")
590     # grab exclusions
591     sub=$(grep -v '^#' "$@" | grep --regexp="^${keyword}-${fcdistro}:" | sed -e "s,${keyword}-${fcdistro}:,,")
592
593     for rpm in $all $add; do
594         for exclude in $sub; do
595             [ "$rpm" = "$exclude" ] && continue 2
596         done
597         echo "${rpm} " | sed -e "s,@arch@,${target_arch},g" -e "s,@fcdistro@,$fcdistro,g" -e "s,@pldistro@,$pldistro,g"
598     done
599     return 0
600 }
601 ##############################
602
603 # locates a pldistro-dependant file
604 # tries first in build/<pldistro>/, then in build/planetlab/
605 function pl_locateDistroFile () {
606     builddir=$1; shift
607     pldistro=$1; shift
608     pkgsfile=$1; shift
609
610     pkgspath=""
611     # if config dir is missing but a .svnpath or a .gitpath exists, use it to extract the config dir
612     configdir="$builddir/config.${pldistro}"
613     if [ ! -d $configdir ] ; then
614         if [ -f "${configdir}.svnpath" -o -f "${configdir}.gitpath" ] ; then
615             echo 1>&2 "Invoking make to extract remote config.${pldistro}"
616             make 1>&2 --silent --no-print-directory -C $builddir stage1=true config.${pldistro}
617         fi
618     fi
619     # locate it
620     paths="$builddir/config.$pldistro/$pkgsfile $builddir/config.planetlab/$pkgsfile"
621     for path in $paths; do
622         if [ -f $path ] ; then
623             pkgspath=$path
624             break
625         fi
626     done
627     if [ -z "$pkgspath" ] ; then
628         echo 1>&2 "pl_locateDistroFile - in $(pwd) : cannot locate $pkgsfile in $builddir"
629         echo 1>&2 "candidates were $paths"
630         echo "not-found-by-pl_locateDistroFile"
631         return 1
632     else
633         echo 1>&2 "pl_locateDistroFile: using $pkgspath"
634         echo $pkgspath
635         return 0
636     fi
637 }
638
639 function yumgroups_from_pkgs () {
640     builddir=$1; shift
641     pldistro=$1; shift
642     fcdistro=$1; shift
643     pkgsnames=$@
644
645     sedargs="-e s,@FCDISTRO@,$fcdistro,g"
646
647    cat <<__header
648 <?xml version="1.0"?>
649 <!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
650 <comps>
651 __header
652
653     for pkgsname in $pkgsnames; do
654         pkgsfile=$(pl_locateDistroFile $builddir $pldistro $pkgsname)
655         packages=$(pl_getPackages $fcdistro $pldistro $pkgsfile)
656
657         groupname=$(pkgs.py groupname $pkgsfile | sed $sedargs)
658         groupdesc=$(pkgs.py groupdesc $pkgsfile | sed $sedargs)
659
660         if [ -z "$groupname" -o -z "$groupdesc" ] ; then
661             echo "Cannot find groupname: and groupdesc: in $pkgsfile -- skipped" 1>&2
662             continue
663         fi
664         
665         cat << __group_header
666   <group>
667     <id>$(echo $groupname|tr A-Z a-z)</id>
668     <name>$groupname</name>
669     <description>$groupdesc</description>
670     <uservisible>true</uservisible>
671     <packagelist>
672 __group_header
673         for package in $packages; do 
674             echo "<packagereq type=\"mandatory\">$package</packagereq>"
675         done
676         cat << __group_footer
677     </packagelist>
678   </group>
679 __group_footer
680     done
681 cat <<__footer
682 </comps>
683 __footer
684 }
685
686
687 function build_fetch () {
688     curl --fail --silent --max-time 60 --output /dev/null "$1" 
689 }
690
691 # tries to compute a valid yum.conf for that pldistro from the template in mirroring/
692 # returns 0 and writes <dest_yumconf> on success
693 # returns 1 on failure, in which case <dest_yumconf> is deleted
694 function yumconf_mirrors () {
695     dest_yumconf=$1; shift
696     builddir=$1; shift
697     fcdistro=$1; shift
698     yumexclude_line="$1" ; shift
699     mirrors="$@"
700
701     template=$builddir/mirroring/$fcdistro/yum.repos.d/building.repo.in
702     
703     if [ ! -f $template ] ; then
704         echo "yumconf_mirrors: cannot locate template $template"
705         rm -f $dest_yumconf
706         return 1
707     fi
708
709     for mirror in $mirrors; do
710         if yumconf_mirror $dest_yumconf $template "$yumexclude_line" $mirror; then
711             return 0
712         fi
713     done
714     echo 'yumconf_mirrors in build.common : ran out of mirrors -- BAILING OUT'
715     rm -f $dest_yumconf
716     return 1
717 }
718
719 # computes a yum.conf from the template, and checks that all baseurl defined in there are valid repos
720 # returns 0 on success and 1 on failure
721 function yumconf_mirror () {
722     dest_yumconf=$1; shift
723     template=$1; shift
724     yumexclude_line="$1" ; shift
725     mirror=$1; shift
726
727     sed -e "s,@MIRRORURL@,$mirror,g" \
728         -e "/baseurl=/i\\
729 $yumexclude_line" $template > $dest_yumconf
730     
731     # capture all lines defining baseurl
732     baseurl_defs=$(grep '^baseurl=' $dest_yumconf)
733     if [ -z "$baseurl_defs" ] ; then
734         return 1
735     fi
736
737     for baseurl_def in $baseurl_defs; do
738         baseurl=$(echo $baseurl_def | sed \
739             -e s,baseurl=,, \
740             -e 's,$basearch,'"$pl_DISTRO_ARCH",g)
741         repomd=$baseurl/repodata/repomd.xml
742
743         echo "* Trying to fetch $repomd"
744         if ! build_fetch $repomd ; then
745             echo "* Failed to fetch $repomd"
746             return 1
747         fi
748     done
749     echo "* Selected mirror $mirror"
750     return 0
751 }
752
753 # from a yum.conf as generated above, computes the gpgkey urls
754 function yumconf_gpgkey () {
755     dest_yumconf=$1; shift
756
757     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)
758     [ -n "$values" ] || return 1
759     echo $values
760     return 0
761 }
762
763 # patches a yum conf to insert an exclude line in each declared repo
764 function yumconf_exclude () {
765     repo=$1; shift
766     yumexclude_line="$1" ; shift
767     
768     sed -i -e "/#baseurl=.*$/i\\
769 $yumexclude_line" $repo
770 }