epel mirroring
[build.git] / vbuild-fedora-mirror.sh
1 #!/bin/bash
2 # this can help you create/update your fedora mirror
3 # $Id$
4
5 COMMAND=$(basename $0)
6 LOGDIR=/var/log/fedora-mirroring
7 DATE=$(date '+%Y-%m-%d-%H-%M')
8 LOG=${LOGDIR}/${DATE}.log
9
10 dry_run=
11 verbose=
12 log=
13 skip_core=
14 root=/mirror/
15
16
17 us_fedora_url=rsync://mirrors.kernel.org/fedora
18 # change this
19 us_centos_url=rsync://mirrors.rit.edu/centos
20 us_epel_url="us_epel_url-needs-to-be-defined"
21
22 # this one is contaminated with the .~tmp~ thing
23
24 eu_fedora_url=rsync://mirrors.ircam.fr/fedora-linux
25 eu_centos_url=rsync://mirrors.ircam.fr/CentOS
26 eu_epel_url=rsync://mirrors.ircam.fr/fedora-epel
27
28 jp_fedora_url="jp_fedora_url-needs-to-be-defined"
29 jp_centos_url="jp_centos_url-needs-to-be-defined"
30 jp_epel_url="jp_epel_url-needs-to-be-defined"
31
32 default_distroname=f8
33 all_distronames="f8 f9 centos5.2 epel5"
34 default_arch=i386
35 all_archs="i386 x86_64"
36
37 case $(hostname) in 
38     *.fr|*.de|*.uk)
39         fedora_url=$eu_fedora_url ; centos_url=$eu_centos_url ; epel_url=$eu_epel_url ;;
40     *.jp)
41         fedora_url=$jp_fedora_url ; centos_url=$jp_centos_url ; epel_url=$jp_epel_url ;;
42     *)
43         fedora_url=$us_fedora_url ; centos_url=$us_centos_url ; epel_url=$us_epel_url ;;
44 esac
45
46 function mirror_distro_arch () {
47     distroname=$1; shift
48     arch=$1; shift
49
50     distroname=$(echo $distroname | tr '[A-Z]' '[a-z]')
51     case $distroname in
52         fc*[1-6])
53             distroindex=$(echo $distroname | sed -e "s,fc,,g")
54             distro="Fedora Core"
55             rsyncurl=$fedora_url
56             ;;
57         f*[7-9])
58             distroindex=$(echo $distroname | sed -e "s,f,,g")
59             distro="Fedora"
60             rsyncurl=$fedora_url
61             ;;
62         centos[4-5]|centos[4-5].[0-9])
63             distroindex=$(echo $distroname | sed -e "s,centos,,g")
64             distro="CentOS"
65             rsyncurl=$centos_url
66             ;;
67         epel5)
68             distroindex=5
69             distro=epel
70             rsyncurl=$epel_url
71             ;;
72         *)
73             echo "WARNING -- Unknown distribution $distroname -- skipped"
74             return 1
75             ;;
76     esac
77
78     excludelist="debug/ iso/ ppc/ source/"
79     options="--archive --compress --delete --delete-excluded $dry_run $verbose"
80     [ -n "$(rsync --help | grep no-motd)" ] && options="$options --no-motd"
81     for e in $excludelist; do
82         options="$options --exclude $e"
83     done
84
85     echo ">>>>>>>>>>>>>>>>>>>> root=$root distroname=$distroname arch=$arch rsyncurl=$rsyncurl"
86     [ -n "$verbose" ] && echo "rsync options=$options"
87
88     RES=1
89     paths=""
90     case $distro in
91         [Ff]edora*)
92             case $distroindex in
93                 2|4|6)
94                     [ -z "$skip_core" ] && paths="core/$distroindex/$arch/os/"
95                     paths="$paths core/updates/$distroindex/$arch/ extras/$distroindex/$arch/"
96                     RES=0
97                     ;;
98                 7|8|9)
99                     [ -z "$skip_core" ] && paths="releases/$distroindex/Everything/$arch/os/"
100                     paths="$paths updates/$distroindex/$arch/"
101                     # f8 and f9 have the additional newkey repo
102                     case $distroindex in 
103                         8|9) paths="$paths updates/$distroindex/${arch}.newkey/" ;;
104                     esac
105                     RES=0
106                     ;;
107             esac
108             localpath=fedora
109             ;;
110     
111         CentOS*)
112             case $distroindex in
113                 5*)
114                     [ -z "$skip_core" ] && paths="$distroindex/os/$arch/"
115                     paths="$paths $distroindex/updates/$arch/"
116                     RES=0
117                     ;;
118             esac
119             localpath=centos
120             ;;
121
122         epel*)
123             case $distroindex in
124                 5)
125                     paths="$paths $distroindex/$arch/"
126                     RES=0
127                     ;;
128             esac
129             localpath=epel
130             ;;
131
132     esac
133
134     if [ "$RES" = 1 ] ; then
135         echo "DISTRIBUTION $distro $distroindex CURRENTLY UNSUPPORTED - skipped"
136     else
137         for repopath in $paths; do
138             echo "===== $distro -> $distroindex $repopath"
139             [ -z "$dry_run" ] && mkdir -p ${root}/${localpath}/${repopath}
140             command="rsync $options ${rsyncurl}/${repopath} ${root}/${localpath}/${repopath}"
141             echo $command
142             $command
143         done
144     fi
145
146     echo "<<<<<<<<<<<<<<<<<<<< $distroname $arch"
147
148     return $RES 
149 }
150
151 function usage () {
152     echo "Usage: $COMMAND [-n] [-v] [-c] [-r root] [-u|U rsyncurl] [-e|-j] [-f distroname|-F] [-a arch|-A]"
153     echo "Defaults to -r $root -f $default_distroname -a $default_arch"
154     echo "Default urls : $fedora_url $centos_url"
155     echo "Options:"
156     echo " -n : dry run"
157     echo " -v : verbose"
158     echo " -l : turns on autologging in $LOGDIR"
159     echo " -c : skips core repository"
160     echo " -r root (default is $root)"
161     echo " -u rsyncurl for fedora (default is $fedora_url)"
162     echo " -U rsyncurl for centos (default is $centos_url)"
163     echo " -s : uses standard (US) mirrors $us_fedora_url $us_centos_url"
164     echo " -e : uses European mirrors $eu_fedora_url $eu_centos_url"
165     echo " -j : uses Japanese mirrors $jp_fedora_url $jp_centos_url"
166     echo " -f distroname - use vserver convention, e.g. fc6 and f7"
167     echo " -F : for distroname in $all_distronames"
168     echo " -a arch - use yum convention"
169     echo " -A : for arch in $all_archs"
170     exit 1
171 }
172
173 function run () {
174     RES=0
175     for distroname in $distronames ; do 
176         for arch in $archs; do 
177             mirror_distro_arch "$distroname" "$arch" || RES=1
178         done
179     done
180     return $RES
181 }
182
183 function main () {
184     distronames=""
185     archs=""
186     while getopts "nvlcr:u:U:sejf:Fa:Ah" opt ; do
187         case $opt in
188             n) dry_run=--dry-run ;;
189             v) verbose=--verbose ;;
190             l) log=true ;;
191             c) skip_core=true ;;
192             r) root=$OPTARG ;;
193             u) fedora_url=$OPTARG ;;
194             U) centos_url=$OPTARG ;;
195             s) fedora_url=$us_fedora_url ; centos_url=$us_centos_url ;;
196             e) fedora_url=$eu_fedora_url ; centos_url=$eu_centos_url ;;
197             j) fedora_url=$jp_fedora_url ; centos_url=$jp_centos_url ;;
198             f) distronames="$distronames $OPTARG" ;;
199             F) distronames="$distronames $all_distronames" ;;
200             a) archs="$archs $OPTARG" ;;
201             A) archs="$archs $all_archs" ;;
202             h|*) usage ;;
203         esac
204     done
205     [ -z "$distronames" ] && distronames=$default_distroname
206     [ -z "$archs" ] && archs=$default_arch
207
208     # auto log : if specified
209     if [ -n "$log" ] ; then
210         mkdir -p $LOGDIR
211         run &> $LOG
212     else
213         run
214     fi
215     exit $?
216 }
217
218 main "$@"