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