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