cosmetic
[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 #################### 
20
21 # (*) sometimes we have yum issuing errors like 'package does not match intended content'
22 #  this means that the sha256 checksum of the downloaded pkg does not match 
23 #    the sha256 checksum advertised in filelists.xml
24 #  if you did run 'yum clean all' on the node, 
25 #    this means that the repodata/ dir. on the server side is out of date 
26 #  forcing a createrepo should solve this 
27
28 # (*) also sometimes the node complains that a package is not signed
29 #
30 # so there quite obviously are some corner cases that are not well handled
31 # hopefully the following subforms may be helpful to recover in these cases
32
33 # /etc/plc.d/packages clean
34 #   performs cleanup of the yum repositories and signed-stamps
35 #   thus forcing the next 'start' command to re-sign and re-index everything
36 # /etc/plc.d/packages superclean
37 #   like 'clean', plus this will reinstall the noderepo rpms that you have, 
38 #   that is to say restart from unsigned rpms
39 # /etc/plc.d/packages start
40 #   this is the usual form, it should be smart enough to only sign the packages that need to, 
41 #   and to rerun createrepo when useful
42 # /etc/plc.d/packages force
43 #   same as 'start' but createrepo is run unconditionnally
44
45 #################### 
46
47
48 # helper for hacking yumgroups
49 # if we've installed for several flavours
50 # we create cross links in install-rpms like this one
51 # ./onelab-f8-i386/vserver-onelab-f12-i386-5.0-6.2011.02.03.i686.rpm 
52 #   -> /var/www/html/install-rpms/onelab-f12-i386/vserver-onelab-f12-i386-5.0-6.2011.02.03.i686.rpm
53 #
54 # but this won't make it to the nodes until they are insered in yumgroups.xml in the PlanetLab group
55
56
57 function hack_yumgroups () {
58     repo=$1; shift
59
60     pushd $repo >& /dev/null
61     pwd
62     vsrpms=$(find . -name 'vserver*rpm')
63     echo found vsrpms $vsrpms
64     if [ ! -f yumgroups.xml ] ; then
65         echo "hack_yumgroups: could not find yumgroups in $(pwd)" 
66     elif [ -z "$vsrpms" ] ; then
67         echo "No need to hack yumgroups, no foreign vserver package found"
68     else
69         cp yumgroups.xml yumgroups.xml.hacking
70         # remove references to package vserver-
71         grep -v '>vserver-' yumgroups.xml.hacking > yumgroups.xml
72         # build a list of lines with corresponding rpm names
73         insert=""
74         for vsrpm in $vsrpms; do
75             rpmname=$(rpm -q --qf '%{name}' -p $vsrpm)
76             echo found file $vsrpm with name $rpmname
77             insert="$insert<packagereq type=\"mandatory\">$rpmname</packagereq>"
78         done
79         echo 'inserting' $insert
80         # insert in yumgroups at the right place -- first packages in the PlanetLab group
81         ed yumgroups.xml <<EOF
82 1
83 /name>PlanetLab<
84 /packagelist
85 +
86 i
87 $insert
88 .
89 w
90 q
91 EOF
92     fi
93     popd >& /dev/null
94 }
95
96 ####################
97 case "$1" in
98     start|force)
99         if [ "$PLC_BOOT_ENABLED" != "1" ] ; then
100             exit 0
101         fi
102
103         MESSAGE=$"Signing and indexing node packages"
104         dialog "$MESSAGE"
105
106         shopt -s nullglob
107
108         mode=$1; shift
109
110         if [[ -z "$@" ]] ; then
111             # use all subdirs in install-rpms by default
112             repositories=/var/www/html/install-rpms/*
113         else
114             # else use argv
115             repositories="$@"
116         fi
117
118         ##########
119         # deal with the vserver packages
120         # symlink all instances of plain 'vserver-*rpm' in all repos
121         # and cleanup old links 
122         vsrpms=$(find $repositories -name 'vserver*rpm' -a -type f)
123         vslinks=$(find $repositories -name 'vserver*rpm' -a -type l)
124
125         for vslink in $vslinks; do
126             [ ! -e $vslink ] && { echo removing old $vslink; rm $vslink; }
127         done
128
129         for repo in $repositories; do
130             for vsrpm in $vsrpms; do
131             # if in the repo we're dealing with, ignore
132                 if [ "$(echo $vsrpm | sed -e s,^$repo,,)" != $vsrpm ] ; then
133                     continue
134                 fi
135                 b=$(basename $vsrpm)
136                 link=$repo/$b
137                 if [ ! -e $link ] ; then
138                     echo "creating symlink $link towards $vsrpm"
139                     ln -s $vsrpm $link
140                 fi
141             done
142         done
143
144         ##########
145         # now that the symlinks are OK, we can tweak yumgroups
146         for repository in $repositories; do
147             hack_yumgroups $repository
148         done
149
150         ########## sign plain packages
151         for repository in $repositories ; do
152             # the rpms that need signing
153             new_rpms=
154             # and the corresponding stamps
155             new_stamps=
156             # is there a need to refresh yum metadata
157             # a safe approach would be to always run createrepo
158             # however this is painfully slow with multi-flavour installed
159             need_createrepo= 
160             # however if we run this script like
161             # /etc/plc.d/packages force
162             # then we force a createrepo
163             [ "$mode" == force ] && need_createrepo=true
164
165             # right after installation, no package is present
166             # but we still need to create index 
167             [ ! -f $repository/repodata/repomd.xml ] && need_createrepo=true
168
169             # it's not helpful to sign symlinks that will get signed on their own
170             for package in $(find $repository/ -name '*.rpm' -a \! -type l) ; do
171                 stamp=$repository/signed-stamps/$(basename $package).signed
172                 # If package is newer than signature stamp
173                 if [ $package -nt $stamp ] ; then
174                     new_rpms="$new_rpms $package"
175                     new_stamps="$new_stamps $stamp"
176                 fi
177                 # Or than createrepo database
178                 [ $package -nt $repository/repodata/repomd.xml ] && need_createrepo=true
179                 [ $package -nt $repository/repodata/filelists.xml.gz ] && need_createrepo=true
180             done
181
182             if [ -n "$new_rpms" ] ; then
183                 # Create a stamp once the package gets signed
184                 mkdir $repository/signed-stamps 2> /dev/null
185
186                 # Sign RPMS. setsid detaches rpm from the terminal,
187                 # allowing the (hopefully blank) GPG password to be
188                 # entered from stdin instead of /dev/tty.
189                 echo | setsid rpm \
190                     --define "_signature gpg" \
191                     --define "_gpg_path /etc/planetlab" \
192                     --define "_gpg_name $PLC_MAIL_SUPPORT_ADDRESS" \
193                     --resign $new_rpms && touch $new_stamps
194 #               check
195                 # if we sign at least one package then we need to reindex
196                 need_createrepo=true
197             fi
198
199             # Update repository index / yum metadata. 
200
201             if [ -n "$need_createrepo" ] ; then
202                 if [ -f $repository/yumgroups.xml ] ; then
203                     createrepo --quiet -g yumgroups.xml $repository 
204                     check
205                 else
206                     createrepo --quiet $repository
207                     check
208                 fi
209             fi
210         done
211
212         result "$MESSAGE"
213         ;;
214     clean)
215         shift
216         if [[ -z "$@" ]] ; then
217             # use all subdirs in install-rpms by default
218             repositories=/var/www/html/install-rpms/*
219         else
220             # else use argv
221             repositories=$@
222         fi
223
224         for repository in $repositories ; do
225             rm -rf $repository/signed-stamps
226             rm -rf $repository/repodata
227             find $repository -type l | xargs rm
228         done
229         ;;
230     # (use this only if you have noderepos installed)
231     superclean)
232         shift
233         find /var/www/html/install-rpms -name signed-stamps | xargs rm -rf
234         find /var/www/html/install-rpms -name repodata | xargs rm -rf
235         find /var/www/html/install-rpms -type l | xargs rm
236         rpm -aq | grep noderepo | xargs yum -y reinstall
237         ;;
238     *)
239         echo "Usage: $0 start|force|clean [repo ..]"
240         ;;
241 esac
242
243 exit $ERRORS