Merge "Turn kvm-plugin back on"
[fuel.git] / build / f_isoroot / f_repobuild / select_ubuntu_repo.sh
1 #!/bin/bash
2
3 BLACKLIST="http://mirrors.se.eu.kernel.org/ubuntu/"
4 #BLACKLIST+=" http://foo.bar"
5
6 cleanup() {
7     rm -f $TMPFILE
8 }
9
10 debugmsg() {
11     test -n "$DEBUG" && echo "$@" >&2
12 }
13
14
15 # Check if url is blacklisted in this script
16 blacklisted () {
17   for blackurl in $BLACKLIST
18   do
19     if [ "$1" == "$blackurl" ]; then
20       return 0
21     fi
22   done
23   return 1
24 }
25
26
27 # Check mirror's integrity
28 check_mirror () {
29     mirror=$1
30     status=0
31     for packdir in dists/trusty-updates/main/binary-amd64 \
32         dists/trusty-updates/restricted/binary-amd64 \
33         dists/trusty-updates/universe/binary-amd64 \
34         dists/trusty-updates/multiverse/binary-amd64 \
35         dists/trusty-security/main/binary-amd64 \
36         dists/trusty-security/restricted/binary-amd64 \
37         dists/trusty-security/universe/binary-amd64 \
38         dists/trusty-security/multiverse/binary-amd64 \
39         dists/trusty-proposed/main/binary-amd64 \
40         dists/trusty-proposed/restricted/binary-amd64 \
41         dists/trusty-proposed/universe/binary-amd64 \
42         dists/trusty-proposed/multiverse/binary-amd64 \
43         dists/trusty/main/binary-amd64 \
44         dists/trusty/restricted/binary-amd64 \
45         dists/trusty/universe/binary-amd64 \
46         dists/trusty/multiverse/binary-amd64 \
47         dists/trusty-backports/main/binary-amd64 \
48         dists/trusty-backports/restricted/binary-amd64 \
49         dists/trusty-backports/universe/binary-amd64 \
50         dists/trusty-backports/multiverse/binary-amd64
51     do
52         for packfile in Release Packages.gz
53         do
54             if [ $status -ne 1 ]; then
55                 curl --output /dev/null --silent --head --fail \
56                     $mirror/$packdir/$packfile
57                 if [ $? -ne 0 ]; then
58                     debugmsg "$mirror: Faulty (at least missing $packdir/$packfile)"
59                     status=1
60                 fi
61             fi
62         done
63     done
64     return $status
65 }
66
67 if [ "$1" == "-d" ]; then
68     DEBUG=1
69 fi
70
71 # Hardcode for testing purposes
72 DEBUG=1
73
74 TMPFILE=$(mktemp /tmp/mirrorsXXXXX)A
75 trap cleanup exit
76
77 # Generate a list of mirrors considered as "up"
78 curl -s  https://launchpad.net/ubuntu/+archivemirrors | \
79     grep -P -B8 "statusUP|statusSIX" | \
80     grep -o -P "(f|ht)tp.*\""  | \
81     sed 's/"$//' | sort | uniq > $TMPFILE
82
83 # Iterate over "close" mirror, check that they are considered up
84 # and sane.
85 for url in $(curl -s http://mirrors.ubuntu.com/mirrors.txt)
86 do
87     if ! grep -q $url $TMPFILE; then
88         debugmsg "$url Faulty (detected by Ubuntu)"
89     elif blacklisted $url; then
90         debugmsg "$url blacklisted"
91     elif [ -z $BESTURL ]; then
92         if grep -q $url $TMPFILE && check_mirror $url; then
93             debugmsg "$url: OK (setting as primary URL)"
94             BESTURL=$url
95             test -z "$DEBUG" && break
96         fi
97     else
98         grep -q $url $TMPFILE && check_mirror $url && debugmsg "$url: OK"
99     fi
100 done
101
102 echo "$BESTURL"