- create new package vserver-system-packages that contain the packages
[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$
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             if [ -f $rpms/$package-$version.$arch.rpm ] ; then
87                 break
88             fi
89
90             baseurl=
91             while read line ; do
92                 if grep -q "^\[$repository\]" <<<$line ; then
93                     baseurl=$repository
94                 elif [ "$baseurl" = "$repository" ] && grep -q "^baseurl=" <<<$line ; then
95                     eval $line
96
97                     # We could parse headers/header.info and/or
98                     # repodata/primary.xml.gz to figure out where the
99                     # package actually is within the repository, but
100                     # it would be too much trouble. Just try
101                     # downloading it from one of the common
102                     # subdirectories.
103                     echo "* $slice: $repository $package-$version.$arch.rpm"
104                     for subdirectory in "" Fedora/RPMS $arch ; do
105                         if curl --fail --silent --max-time 60 $baseurl/$subdirectory/$package-$version.$arch.rpm \
106                             >$rpms/$package-$version.$arch.rpm ; then
107                             break
108                         fi
109                         rm -f $rpms/$package-$version.$arch.rpm
110                     done
111
112                     # Assert that we got it successfully
113                     if [ ! -f $rpms/$package-$version.$arch.rpm ] ; then
114                         echo "Failed to fetch $package-$version.$arch.rpm from $repository" >&2
115                         false
116                     fi
117
118                     dependencies[${#dependencies[*]}]=$package-$version.$arch.rpm
119                     break
120                 fi
121             done <$vroot/etc/yum.conf
122         done < <(yum -c $vroot/etc/yum.conf --installroot=$vroot shell <<EOF
123 install $packages
124 transaction solve
125 transaction list
126 EOF
127         )
128     else
129         # This is pretty fucked up. Turn on verbose debugging and the
130         # --download-only option, which, contrary to what you might
131         # think the option means, downloads the headers, not the
132         # packages themselves. In any case, verbose debugging prints
133         # out the baseURL and path of each RPM that it would download
134         # if --download-only were not specified.
135         baseURL=
136         path=
137         while read line ; do
138             if [ -z "$baseURL" ] ; then
139                 baseURL=$(sed -ne 's/failover: baseURL = \(.*\)/\1/p' <<<$line)
140             elif [ -z "$path" ] ; then
141                 path=$(sed -ne 's/failover: path = \(.*\)/\1/p' <<<$line)
142             else
143                 if [ "${path##*.}" = "rpm" ] ; then
144                     echo "* $slice: $(basename $path)"
145                     curl --fail --silent --max-time 60 $baseURL/$path >$rpms/$(basename $path)
146                     dependencies[${#dependencies[*]}]=$(basename $path)
147                 fi
148                 baseURL=
149                 path=
150             fi
151         done < <(yum -d 3 -c $vroot/etc/yum.conf --installroot=$vroot -y --download-only install $packages 2>&1)
152     fi
153
154     for dependency in "${dependencies[@]}" ; do
155         echo $dependency
156     done >$rpms/$slice.lst
157 done
158
159 # Clean yum cache
160 yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
161     clean all
162
163 # Clean RPM state
164 rm -f $vroot/var/lib/rpm/__db*
165
166 exit 0