- also copy Fedora signing key to reference images
[vserver-reference.git] / system-packages.sh
1 #!/bin/bash
2 #
3 # Download dependencies that would be necessary to build the
4 # pl_netflow and pl_conf root slices from the vserver-reference image.
5 #
6 # Mark Huang <mlhuang@cs.princeton.edu>
7 # Copyright (C) 2004-2006 The Trustees of Princeton University
8 #
9 # $Id: system-packages.sh,v 1.1 2006/04/05 20:32:28 mlhuang Exp $
10 #
11
12 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
13
14 vroot=$PWD/vservers/vserver-reference
15 rpms=$PWD/vservers/system-packages
16 install -d -m 755 $rpms
17
18 # curl can't list file:// URLs
19 list ()
20 {
21     url=$1
22     if [ -e ${url#file://} ] ; then
23         /bin/ls ${url#file://}
24     else
25         curl --fail --silent --max-time 60 $url
26     fi
27 }
28
29 # pl_netflow requires third party MySQL RPMs
30 mysql_mirrors=(
31 file:///usr/share/mirrors/mysql
32 http://boot.planet-lab.org/install-rpms/3rdparty
33 )
34
35 for mirror in "${mysql_mirrors[@]}" ; do
36     baseurl=$mirror/
37     if list $baseurl | grep -q "MySQL-server.*rpm" ; then
38         break
39     fi
40     unset baseurl
41 done
42
43 if [ -z "$baseurl" ] ; then
44     echo "Error: MySQL-server RPM package"
45     echo "       could not be found in any of the following locations:"
46     echo
47     for mirror in ${mysql_mirrors[@]} ; do
48         echo $mirror
49     done
50     echo
51     exit 1
52 fi
53
54 # Add MySQL mirror to yum.conf
55 if ! grep -q mysql $vroot/etc/yum.conf ; then
56     cat >>$vroot/etc/yum.conf <<EOF
57
58 [mysql]
59 name=MySQL
60 baseurl=$baseurl
61 EOF
62 fi
63
64 # Space separated list of required packages
65 pl_netflow="netflow"
66 pl_conf="sidewinder-PlanetLab-SCS sidewinder-common"
67
68 for slice in pl_netflow pl_conf ; do
69     packages=${!slice}
70     dependencies=()
71
72     if yum --help | grep -q shell ; then
73         while read -a words ; do
74             if [ ${#words[*]} -lt 5 ] ; then
75                 continue
76             fi
77
78             # netflow i386 3.1-23.planetlab.2006.04.04 bootstrap 61 k
79             package=${words[0]}
80             arch=${words[1]}
81             version=${words[2]}
82             # Remove the epoch from the version
83             version=${version##*:}
84             repository=${words[3]}
85
86             baseurl=
87             while read line ; do
88                 if [ -z "$line" ] ; then
89                     continue
90                 elif grep -q "^\[$repository\]" <<<$line ; then
91                     baseurl=$repository
92                 elif [ "$baseurl" = "$repository" ] && grep -q "^baseurl=" <<<$line ; then
93                     eval $line
94
95                     # We could parse headers/header.info and/or
96                     # repodata/primary.xml.gz to figure out where the
97                     # package actually is within the repository, but
98                     # it would be too much trouble. Just try
99                     # downloading it from one of the common
100                     # subdirectories.
101                     echo "* $slice: $repository $package-$version.$arch.rpm"
102                     for subdirectory in "" Fedora/RPMS $arch ; do
103                         if curl --fail --silent --max-time 60 $baseurl/$subdirectory/$package-$version.$arch.rpm \
104                             >$rpms/$package-$version.$arch.rpm ; then
105                             break
106                         fi
107                         rm -f $rpms/$package-$version.$arch.rpm
108                     done
109
110                     # Assert that we got it successfully
111                     if [ ! -f $rpms/$package-$version.$arch.rpm ] ; then
112                         echo "Failed to fetch $package-$version.$arch.rpm from $repository" >&2
113                         false
114                     fi
115
116                     dependencies[${#dependencies[*]}]=$package-$version.$arch.rpm
117                     break
118                 fi
119             done <$vroot/etc/yum.conf
120         done < <((yum -c $vroot/etc/yum.conf --installroot=$vroot shell <<EOF
121 install $packages
122 transaction solve
123 transaction list
124 EOF
125             ) | sed -ne '/^Installing:/,/^Transaction Summary/p' 
126         )
127     else
128         # This is pretty fucked up. Turn on verbose debugging and the
129         # --download-only option, which, contrary to what you might
130         # think the option means, downloads the headers, not the
131         # packages themselves. In any case, verbose debugging prints
132         # out the baseURL and path of each RPM that it would download
133         # if --download-only were not specified.
134         baseURL=
135         path=
136         while read line ; do
137             if [ -z "$baseURL" ] ; then
138                 baseURL=$(sed -ne 's/failover: baseURL = \(.*\)/\1/p' <<<$line)
139             elif [ -z "$path" ] ; then
140                 path=$(sed -ne 's/failover: path = \(.*\)/\1/p' <<<$line)
141             else
142                 if [ "${path##*.}" = "rpm" ] ; then
143                     echo "* $slice: $(basename $path)"
144                     curl --fail --silent --max-time 60 $baseURL/$path >$rpms/$(basename $path)
145                     dependencies[${#dependencies[*]}]=$(basename $path)
146                 fi
147                 baseURL=
148                 path=
149             fi
150         done < <(yum -d 3 -c $vroot/etc/yum.conf --installroot=$vroot -y --download-only install $packages 2>&1)
151     fi
152
153     for dependency in "${dependencies[@]}" ; do
154         echo $dependency
155     done >$rpms/$slice.lst
156 done
157
158 # Clean yum cache
159 yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
160     clean all
161
162 # Clean RPM state
163 rm -f $vroot/var/lib/rpm/__db*
164
165 exit 0