not only print actions, actually perform them
[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; rm $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                     ln -s $vsrpm $link
111                 fi
112             done
113         done
114
115         ##########
116         # now that the symlinks are OK, we can tweak yumgroups
117         for repository in $repositories; do
118             hack_yumgroups $repository
119         done
120
121         ########## sign plain packages
122         for repository in $repositories ; do
123             # the rpms that need signing
124             new_rpms=
125             # and the corresponding stamps
126             new_stamps=
127             # is there a need to refresh yum metadata
128             # a safe approach would be to always run createrepo
129             # however this is painfully slow with multi-flavour installed
130             need_createrepo= 
131             # however if we run this script like
132             # /etc/plc.d/packages force
133             # then we force a createrepo
134             [ "$mode" == force ] && need_createrepo=true
135
136             # right after installation, no package is present
137             # but we still need to create index 
138             [ ! -f $repository/repodata/repomd.xml ] && need_createrepo=true
139
140             # it's not helpful to sign symlinks that will get signed on their own
141             for package in $(find $repository/ -name '*.rpm' -a \! -type l) ; do
142                 stamp=$repository/signed-stamps/$(basename $package).signed
143                 # If package is newer than signature stamp
144                 if [ $package -nt $stamp ] ; then
145                     new_rpms="$new_rpms $package"
146                     new_stamps="$new_stamps $stamp"
147                 fi
148                 # Or than createrepo database
149                 [ $package -nt $repository/repodata/repomd.xml ] && need_createrepo=true
150             done
151
152             if [ -n "$new_rpms" ] ; then
153                 # Create a stamp once the package gets signed
154                 mkdir $repository/signed-stamps 2> /dev/null
155
156                 # Sign RPMS. setsid detaches rpm from the terminal,
157                 # allowing the (hopefully blank) GPG password to be
158                 # entered from stdin instead of /dev/tty.
159                 echo | setsid rpm \
160                     --define "_signature gpg" \
161                     --define "_gpg_path /etc/planetlab" \
162                     --define "_gpg_name $PLC_MAIL_SUPPORT_ADDRESS" \
163                     --resign $new_rpms && touch $new_stamps
164 #               check
165             fi
166
167             # Update repository index / yum metadata. 
168
169             if [ -n "$need_createrepo" ] ; then
170                 if [ -f $repository/yumgroups.xml ] ; then
171                     createrepo --quiet -g yumgroups.xml $repository 
172                     check
173                 else
174                     createrepo --quiet $repository
175                     check
176                 fi
177             fi
178         done
179
180         result "$MESSAGE"
181         ;;
182     clean)
183         shift
184         if [[ -z "$@" ]] ; then
185             # use all subdirs in install-rpms by default
186             repositories=/var/www/html/install-rpms/*
187         else
188             # else use argv
189             repositories=$@
190         fi
191
192         for repository in $repositories ; do
193             rm -rf $repository/signed-stamps
194             rm -rf $repository/repodata
195             rm -rf $repository/headers
196             find $repository -type l | xargs rm
197         done
198         ;;
199     *)
200         echo "Usage: $0 start|force|clean [repo ..]"
201         ;;
202 esac
203
204 exit $ERRORS