refine strategy to spot ip address, keep on calling guest_ipv4
[build.git] / mirroring / mirror.sh
1 #!/bin/bash
2
3 COMMAND=$(basename $0)
4 DIRNAME=$(dirname $0)
5
6 default_url="http://localhost/mirror/"
7 default_distro="f14"
8 all_distros="f12 f14 f16 f18 f20"
9
10 function check_distro () {
11     local distro=$1; shift
12     if [ ! -d $DIRNAME/$distro/yum.repos.d ] ; then
13         echo "Distro $distro not supported - skipped"
14         return 1
15     fi
16     return 0
17 }
18
19 function do_repo () {
20     local distro=$1; shift
21     sedargs="-e s,@MIRRORURL@,$URL,g"
22     [ -n "$GPGOFF" ] && sedargs="$sedargs -e "'s,gpgcheck\W*=\W*1,gpgcheck=0,'
23     sed $sedargs $DIRNAME/$distro/yum.repos.d/building.repo.in
24 }
25
26 function do_init () { 
27     local distro=$1; shift
28     repo=/etc/vservers/.distributions/$distro/yum.repos.d/building.repo
29     dir=/etc/vservers/.distributions/$distro/yum.repos.d/
30     if [ ! -d $dir ] ; then
31         [ -n "$VERBOSE" ] && echo Creating dir $dir
32         mkdir -p $dir
33     fi
34     [ -n "$VERBOSE" ] && echo "Creating $repo"
35     do_repo $distro > $repo
36 }
37
38 function do_diff () {
39     local distro=$1; shift
40     repo=/etc/vservers/.distributions/$distro/yum.repos.d/building.repo
41     if [ ! -f $repo ] ; then
42         echo "Cannot find $repo"
43     else
44         would=/tmp/$COMMAND.$$
45         do_repo $distro > $would
46         echo "==================== DIFF for $distro" '(current <-> would be)'
47         diff $repo $would 
48         rm $would
49     fi
50 }
51
52 function do_display () {
53     local distro=$1; shift
54     dir=/etc/vservers/.distributions/$distro/yum.repos.d/
55     if [ -d $dir ] ; then
56         echo "====================" Contents of $dir
57         ls $dir/*.repo 2> /dev/null | xargs head --verbose --lines=1000
58     else
59         echo "====================" $dir does not exist
60     fi
61 }
62
63 function do_clean () {
64     local distro=$1; shift
65     repo=/etc/vservers/.distributions/$distro/yum.repos.d/building.repo
66     [ -n "$VERBOSE" ] && echo Removing $repo
67     rm $repo
68 }
69
70 function do_superclean () {
71     local distro=$1; shift
72     dir=/etc/vservers/.distributions/$distro/yum.repos.d/
73     [ -n "$VERBOSE" ] && echo Removing all repo files in $dir
74     rm $dir/*.repo
75 }
76
77 function usage () {
78     echo "Usage $COMMAND [options] <command>"
79     echo "  a help to manage the yum.repos.d template in /etc/vservers/.distributions/<distro>"
80     echo "Available commands"
81     echo "  display: shows content (default if <command> is missing)"
82     echo "  diff: shows diff between current and what init would do"
83     echo "  init: creates /etc/vservers/.distributions/<distro>/yum.repos.d/building.repo"
84     echo "  clean: removes building.repo"
85     echo "  superclean: removes yum.repos.d altogether"
86     echo "Options"
87     echo "  -u URL to specify another location"
88     echo "     default is to use mirror root at $default_url"
89     echo "  -f <distro> : defaults to $default_distro"
90     echo "  -a : runs on all distros $all_distros"
91     echo "  -0 : turns off gpgcheck"
92     echo "  -v : verbose"
93     echo "Examples"
94     echo "  $COMMAND -a display "
95     echo "  $COMMAND -a superclean"
96     echo "  $COMMAND -a -u http://mirror.onelab.eu/ init"
97     echo "  $COMMAND -a display"
98     exit 1
99 }
100     
101 DISTROS=""
102 URL=""
103 VERBOSE=""
104 GPGOFF=""
105
106 function main () {
107
108     while getopts "u:f:a0v" opt; do
109         case $opt in
110             u) URL=$OPTARG ;;
111             f) DISTROS="$DISTROS $OPTARG" ;;
112             a) DISTROS="$DISTROS $all_distros" ;;
113             0) GPGOFF=true ;;
114             v) VERBOSE=true ;;
115             *) usage ;;
116         esac
117     done
118
119     shift $(($OPTIND - 1))
120     
121     # no action = display
122     case "$#" in 
123         0)
124             action=display ;;
125         1) 
126             action=$1; shift
127             case $action in
128                 disp*) action=display ;;
129                 init*) action=init ;;
130                 diff*) action=diff ;;
131                 clea*) action=clean ;;
132                 super*) action=superclean ;;
133                 *) usage ;;
134             esac ;;
135         *)
136             usage ;;
137     esac
138
139     [ -z "$URL" ] && URL=$default_url
140     [ -z "$DISTROS" ] && DISTROS="$default_distro"
141
142     # remove trailing slash
143     URL=$(echo $URL | sed -e 's,/$,,')
144
145     for distro in $DISTROS; do
146         [ -n "$VERBOSE" ] && echo ==================== Running $action for $distro
147         check_distro $distro && do_$action $distro
148     done
149
150     exit 0
151 }
152
153 main "$@"