to be able to create different flavours of vservers (exp. f8 on f12 nodes)
[nodeimage.git] / plc.d / packages
1 #!/bin/bash
2 # $Id$
3 # $URL$
4 #
5 # priority: 1200
6 #
7 # Update node package repository metadata and sign packages
8 #
9 # Mark Huang <mlhuang@cs.princeton.edu>
10 # Copyright (C) 2006 The Trustees of Princeton University
11 #
12
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
16
17 # Be verbose
18 set -x
19
20 case "$1" in
21     start)
22         if [ "$PLC_BOOT_ENABLED" != "1" ] ; then
23             exit 0
24         fi
25
26         MESSAGE=$"Signing and indexing node packages"
27         dialog "$MESSAGE"
28
29         shopt -s nullglob
30
31         shift
32         if [[ -z "$@" ]] ; then
33             # use all subdirs in install-rpms by default
34             repositories=/var/www/html/install-rpms/*
35         else
36             # else use argv
37             repositories="$@"
38         fi
39
40         ### availability of repo indexing tools
41         # old one - might be needed for old-style nodes
42         type -p yum-arch > /dev/null && have_yum_arch="true"
43         # new one
44         type -p createrepo > /dev/null && have_createrepo="true"
45
46
47         ### copy vserver-PLDISTRO* and vserver-systemslices-PLDISTRO*
48         ### pacakges to each repository to be able to create different
49         ### flavours of vservers on nodes
50         for repository1 in $repositories; do
51             for repository2 in $repositories; do
52                 if [[ $(basename $repository1) == ${PLC_FLAVOUR_NODE_PLDISTRO}* ]]  \
53                     && [[ $(basename $repository2) == ${PLC_FLAVOUR_NODE_PLDISTRO}* ]] \
54                     && [[ $repository1 != $repository2 ]] ; then
55                     cp -a $repository1/vserver{,-systemslices}-${PLC_FLAVOUR_NODE_PLDISTRO}* $repository2
56                 fi
57             done
58         done
59
60
61         for repository in $repositories ; do
62             # the rpms that need signing
63             new_rpms=
64             # and the corresponding stamps
65             new_stamps=
66             # is there a need to refresh yum metadata
67             need_yum_arch=
68             need_createrepo=
69
70             # right after installation, no package is present
71             # but we still need to create index 
72             [ -n "$have_yum_arch" -a ! -f $repository/headers/header.info ] && need_yum_arch=true
73             [ -n "$have_createrepo" -a ! -f $repository/repodata/repomd.xml ] && need_createrepo=true
74             
75             for package in $(find $repository/ -name '*.rpm') ; do
76                 stamp=$repository/signed-stamps/$(basename $package).signed
77                 # If package is newer than signature stamp
78                 if [ $package -nt $stamp ] ; then
79                     new_rpms="$new_rpms $package"
80                     new_stamps="$new_stamps $stamp"
81                 fi
82                 # Or than yum-arch headers
83                 [ -n "$have_yum_arch" ] && [ $package -nt $repository/headers/header.info ] && need_yum_arch=true
84                 # Or than createrepo database
85                 [ -n "$have_createrepo" ] && [ $package -nt $repository/repodata/repomd.xml ] && need_createrepo=true
86             done
87
88             if [ -n "$new_rpms" ] ; then
89                 # Create a stamp once the package gets signed
90                 mkdir $repository/signed-stamps 2> /dev/null
91
92                 # Sign RPMS. setsid detaches rpm from the terminal,
93                 # allowing the (hopefully blank) GPG password to be
94                 # entered from stdin instead of /dev/tty.
95                 echo | setsid rpm \
96                     --define "_signature gpg" \
97                     --define "_gpg_path /etc/planetlab" \
98                     --define "_gpg_name $PLC_MAIL_SUPPORT_ADDRESS" \
99                     --resign $new_rpms && touch $new_stamps
100                 check
101             fi
102
103             # Update repository index / yum metadata. 
104
105             if [ -n "$need_yum_arch" ] ; then
106                 # yum-arch sometimes leaves behind
107                 # .oldheaders and .olddata directories accidentally.
108                 rm -rf $repository/{.oldheaders,.olddata}
109                 yum-arch $repository 
110                 check
111             fi
112
113             if [ -n "$need_createrepo" ] ; then
114                 if [ -f $repository/yumgroups.xml ] ; then
115                     createrepo --quiet -g yumgroups.xml $repository 
116                 else
117                     createrepo --quiet $repository
118                 fi
119                 check
120             fi
121         done
122
123         result "$MESSAGE"
124         ;;
125     clean)
126         shift
127         if [[ -z "$@" ]] ; then
128             # use all subdirs in install-rpms by default
129             repositories=/var/www/html/install-rpms/*
130         else
131             # else use argv
132             repositories=$@
133         fi
134
135         for repository in $repositories ; do
136             rm -rf $repository/signed-stamps
137             rm -rf $repository/repodata
138             rm -rf $repository/headers
139         done
140         ;;
141     *)
142         echo "Usage: $0 start|clean [repo ..]"
143         ;;
144 esac
145
146 exit $ERRORS