CentOS 5 no longer supported
[git-tools-moved-to-github.git] / update-archive / update-yum-archive.sh
1 #!/bin/sh
2 if [ "$(id -u)" != "0" -o $HOME != "/root" ]; then
3   echo "This script must be run as root (not sudo) to perform package singning." 1>&2
4   exit 1
5 fi
6
7 if test ! -f ~/.rpmmacros; then
8   echo "$0: Make sure ~/.rpmmacros file exsits and contains macros from git-tools/update-archive/root-rpmmacros"
9   exit 1
10 fi
11
12 if test ! -x /usr/bin/createrepo; then
13   echo "$0: /usr/bin/createrepo missing. Install createrepo"
14   exit 1
15 fi
16
17 if test ! -x /usr/bin/gpg; then
18   echo "$0: /usr/bin/gpg missing. Install gpg"
19   exit 1
20 fi
21
22 if test ! -x /usr/bin/expect; then
23   echo "$0: /usr/bin/expect missing. Install expect"
24   exit 1
25 fi
26
27 now=`date +%s`
28 # sign if newer than signage (86400=24 hrs)
29 signage=8000
30 #check if root has rpm macros
31 confdir=`pwd`
32 architectures="i386 x86_64"
33 dirs_to_upd=""
34 for vdist in centos/6 centos/7; do
35   dist=`basename $vdist`
36   vendor=`dirname $vdist`
37   ftpdir=/home/ftp/pub/yum/$vendor/$dist
38   if test ! -d $ftpdir; then
39     echo "$ftpdir does not exist"
40     exit 1
41   fi
42   cd $ftpdir    
43   sections=""
44   for section in main restricted; do
45     # Prepare pool
46     d=$section
47     if test ! -d $d; then mkdir -p $d; fi
48     if test ! -d $d/SRPMS; then mkdir -p $d/SRPMS; fi
49     # Remove invalid symlinks (SRPMS, i386, x86_64,..)
50     for l in $d/SRPMS/*; do
51       if test -L "$l"; then
52         if test ! -f "$l"; then
53           rm $l
54         fi
55       fi
56     done
57     for arch in $architectures; do
58       dirs_to_upd="${dirs_to_upd} ${ftpdir}/${d}/${arch}"
59       for l in $d/$arch/Packages/*; do
60         if test -L "$l"; then
61           if test ! -f "$l"; then
62             rm $l
63           fi
64         fi
65       done
66     done
67     # Make symlinks from the regular FTP archive
68     for pdir in /home/ftp/pub/*; do
69       use=false
70       if test -f $pdir/.htaccess -a $section != "main"; then
71         use=true
72       fi
73       if test ! -f $pdir/.htaccess -a $section = "main"; then
74         use=true
75       fi
76       if $use; then
77         distdir=$pdir/redhat/$vendor/$dist
78         if test -d $distdir; then
79           if test -d $distdir/SRPMS; then
80             for f in $distdir/SRPMS/*.src.rpm; do
81               if test -f $f; then
82                 ln -sf $f $d/SRPMS/
83               fi
84             done
85           fi
86           for arch in $architectures; do
87             mkdir -p $d/${arch}/Packages
88             for f in $distdir/RPMS/${arch}/*.rpm $distdir/RPMS/noarch/*.rpm; do
89               b=`basename $f`
90               if test -f $f; then
91                 mustsign=false
92                 age=`stat --printf='%Y' $f`
93                 if expr $now - $age \< $signage >/dev/null; then
94                   mustsign=true
95                 fi
96                 if test ! -f $d/${arch}/Packages/$b; then
97                   mustsign=true
98                 fi
99                 if $mustsign; then
100                   ln -sf $f $d/${arch}/Packages/
101                   if rpm -K $f|grep -q 'GPG' >/dev/null; then
102                     echo "Already signed $f"
103                   else
104                     expect << __EOF
105                     spawn rpm --addsign $f
106 expect -exact "Enter pass phrase: "
107 send -- "\r"
108 expect eof
109 __EOF
110                     chgrp staff $f
111                     chmod g+w $f
112                   fi
113                 fi
114               fi
115             done
116           done
117         fi
118       fi
119     done
120   done
121 done
122 echo "Updating repository meta:"
123 for rdir in ${dirs_to_upd}; do
124   echo "Section ${rdir}"
125   if test -d $rdir && ls -A $rdir; then
126         # Get rid of createrepo warnings
127         # https://bugs.launchpad.net/ubuntu/+source/createrepo/+bug/530015
128         createrepo -q --update ${rdir} 2>&1 |
129                 grep -v DeprecationWarning:|grep -v '  import'
130         if test -f ${rdir}/repodata/repomd.xml.asc; then
131                 rm ${rdir}/repodata/repomd.xml.asc
132         fi
133         gpg --detach-sign -u "Index Data" --armor ${rdir}/repodata/repomd.xml
134   else
135         echo "Empty."
136   fi
137 done
138 # Local Variables:
139 # mode:shell-script
140 # sh-indentation: 2
141 # sh-basic-offset: 8
142 # End: