60f3c842da8192415f7b6e4a320a7372a6d63aa2
[bootstrapfs.git] / plc.d / packages
1 #!/bin/bash
2 #
3 # priority: 1200
4 #
5 # Update node package repository metadata and sign packages
6 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
10 #
11
12 # Source function library and configuration
13 . /etc/plc.d/functions
14 . /etc/planetlab/plc_config
15
16 # Be verbose
17 set -x
18
19 #################### hack yumgroups
20 # if we've installed for several flavours
21 # we create cross links in install-rpms like this one
22 # ./onelab-f8-i386/vserver-onelab-f12-i386-5.0-6.2011.02.03.i686.rpm 
23 #   -> /var/www/html/install-rpms/onelab-f12-i386/vserver-onelab-f12-i386-5.0-6.2011.02.03.i686.rpm
24 #
25 # but this won't make it to the nodes until they are insered in yumgroups.xml in the PlanetLab group
26
27
28 function hack_yumgroups () {
29     repo=$1; shift
30
31     pushd $repo >& /dev/null
32     pwd
33     vsrpms=$(find . -name 'vserver*rpm')
34     echo found vsrpms $vsrpms
35     if [ ! -f yumgroups.xml ] ; then
36         echo "hack_yumgroups: could not find yumgroups in $(pwd)" 
37     elif [ -z "$vsrpms" ] ; then
38         echo "No need to hack yumgroups, no foreign vserver package found"
39     else
40         cp yumgroups.xml yumgroups.xml.hacking
41         # remove references to package vserver-
42         grep -v '>vserver-' yumgroups.xml.hacking > yumgroups.xml
43         # build a list of lines with corresponding rpm names
44         insert=""
45         for vsrpm in $vsrpms; do
46             rpmname=$(rpm -q --qf '%{name}' -p $vsrpm)
47             echo found file $vsrpm with name $rpmname
48             insert="$insert<packagereq type=\"mandatory\">$rpmname</packagereq>"
49         done
50         echo 'inserting' $insert
51         # insert in yumgroups at the right place -- first packages in the PlanetLab group
52         ed yumgroups.xml <<EOF
53 1
54 /name>PlanetLab<
55 /packagelist
56 +
57 i
58 $insert
59 .
60 w
61 q
62 EOF
63     fi
64     popd >& /dev/null
65 }
66
67 ####################
68 case "$1" in
69     start|force)
70         if [ "$PLC_BOOT_ENABLED" != "1" ] ; then
71             exit 0
72         fi
73
74         MESSAGE=$"Signing and indexing node packages"
75         dialog "$MESSAGE"
76
77         shopt -s nullglob
78
79         mode=$1; shift
80
81         if [[ -z "$@" ]] ; then
82             # use all subdirs in install-rpms by default
83             repositories=/var/www/html/install-rpms/*
84         else
85             # else use argv
86             repositories="$@"
87         fi
88
89         ##########
90         # deal with the vserver packages
91         # symlink all instances of plain 'vserver-*rpm' in all repos
92         # and cleanup old links 
93         vsrpms=$(find $repositories -name 'vserver*rpm' -a -type f)
94         vslinks=$(find $repositories -name 'vserver*rpm' -a -type l)
95
96         for vslink in $vslinks; do
97             [ ! -e $vslink ] && echo removing old $vslink
98         done
99
100         for repo in $repositories; do
101             for vsrpm in $vsrpms; do
102             # if in the repo we're dealing with, ignore
103                 if [ "$(echo $vsrpm | sed -e s,^$repo,,)" != $vsrpm ] ; then
104                     continue
105                 fi
106                 b=$(basename $vsrpm)
107                 link=$repo/$b
108                 if [ ! -e $link ] ; then
109                     echo "creating symlink $link towards $vsrpm"
110                 fi
111             done
112         done
113
114         ##########
115         # now that the symlinks are OK, we can tweak yumgroups
116         for repository in $repositories; do
117             hack_yumgroups $repository
118         done
119
120         ########## sign plain packages
121         for repository in $repositories ; do
122             # the rpms that need signing
123             new_rpms=
124             # and the corresponding stamps
125             new_stamps=
126             # is there a need to refresh yum metadata
127             # a safe approach would be to always run createrepo
128             # however this is painfully slow with multi-flavour installed
129             need_createrepo= 
130             # however if we run this script like
131             # /etc/plc.d/packages force
132             # then we force a createrepo
133             [ "$mode" == force ] && need_createrepo=true
134
135             # right after installation, no package is present
136             # but we still need to create index 
137             [ ! -f $repository/repodata/repomd.xml ] && need_createrepo=true
138
139             # it's not helpful to sign symlinks that will get signed on their own
140             for package in $(find $repository/ -name '*.rpm' -a \! -type l) ; do
141                 stamp=$repository/signed-stamps/$(basename $package).signed
142                 # If package is newer than signature stamp
143                 if [ $package -nt $stamp ] ; then
144                     new_rpms="$new_rpms $package"
145                     new_stamps="$new_stamps $stamp"
146                 fi
147                 # Or than createrepo database
148                 [ $package -nt $repository/repodata/repomd.xml ] && need_createrepo=true
149             done
150
151             if [ -n "$new_rpms" ] ; then
152                 # Create a stamp once the package gets signed
153                 mkdir $repository/signed-stamps 2> /dev/null
154
155                 # Sign RPMS. setsid detaches rpm from the terminal,
156                 # allowing the (hopefully blank) GPG password to be
157                 # entered from stdin instead of /dev/tty.
158                 echo | setsid rpm \
159                     --define "_signature gpg" \
160                     --define "_gpg_path /etc/planetlab" \
161                     --define "_gpg_name $PLC_MAIL_SUPPORT_ADDRESS" \
162                     --resign $new_rpms && touch $new_stamps
163                 check
164             fi
165
166             # Update repository index / yum metadata. 
167
168             if [ -n "$need_createrepo" ] ; then
169                 if [ -f $repository/yumgroups.xml ] ; then
170                     createrepo --quiet -g yumgroups.xml $repository 
171                 else
172                     createrepo --quiet $repository
173                 fi
174                 check
175             fi
176         done
177
178         result "$MESSAGE"
179         ;;
180     clean)
181         shift
182         if [[ -z "$@" ]] ; then
183             # use all subdirs in install-rpms by default
184             repositories=/var/www/html/install-rpms/*
185         else
186             # else use argv
187             repositories=$@
188         fi
189
190         for repository in $repositories ; do
191             rm -rf $repository/signed-stamps
192             rm -rf $repository/repodata
193             rm -rf $repository/headers
194             find $repository -type l | xargs rm
195         done
196         ;;
197     *)
198         echo "Usage: $0 start|force|clean [repo ..]"
199         ;;
200 esac
201
202 exit $ERRORS