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