aboutsummaryrefslogtreecommitdiffstats
path: root/build/f_isoroot/f_repobuild/select_ubuntu_repo.sh
blob: c8c86db53d82789ca3d7fbc2fc59852116f88f20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash

UBUNTU_DISTRO="xenial"
BLACKLIST="http://mirrors.se.eu.kernel.org/ubuntu/"
#BLACKLIST+=" http://foo.bar"

cleanup() {
    rm -f $TMPFILE
}

debugmsg() {
    test -n "$DEBUG" && echo "$@" >&2
}


# Check if url is blacklisted in this script
blacklisted () {
  for blackurl in $BLACKLIST
  do
    if [ "$1" == "$blackurl" ]; then
      return 0
    fi
  done
  return 1
}


# Check mirror's integrity
check_mirror () {
    mirror=$1
    status=0
    for packdir in dists/${UBUNTU_DISTRO}-updates/main/binary-amd64 \
        dists/${UBUNTU_DISTRO}-updates/restricted/binary-amd64 \
        dists/${UBUNTU_DISTRO}-updates/universe/binary-amd64 \
        dists/${UBUNTU_DISTRO}-updates/multiverse/binary-amd64 \
        dists/${UBUNTU_DISTRO}-security/main/binary-amd64 \
        dists/${UBUNTU_DISTRO}-security/restricted/binary-amd64 \
        dists/${UBUNTU_DISTRO}-security/universe/binary-amd64 \
        dists/${UBUNTU_DISTRO}-security/multiverse/binary-amd64 \
        dists/${UBUNTU_DISTRO}-proposed/main/binary-amd64 \
        dists/${UBUNTU_DISTRO}-proposed/restricted/binary-amd64 \
        dists/${UBUNTU_DISTRO}-proposed/universe/binary-amd64 \
        dists/${UBUNTU_DISTRO}-proposed/multiverse/binary-amd64 \
        dists/${UBUNTU_DISTRO}/main/binary-amd64 \
        dists/${UBUNTU_DISTRO}/restricted/binary-amd64 \
        dists/${UBUNTU_DISTRO}/universe/binary-amd64 \
        dists/${UBUNTU_DISTRO}/multiverse/binary-amd64 \
        dists/${UBUNTU_DISTRO}-backports/main/binary-amd64 \
        dists/${UBUNTU_DISTRO}-backports/restricted/binary-amd64 \
        dists/${UBUNTU_DISTRO}-backports/universe/binary-amd64 \
        dists/${UBUNTU_DISTRO}-backports/multiverse/binary-amd64
    do
        for packfile in Release Packages.gz
        do
            if [ $status -ne 1 ]; then
                curl --output /dev/null --silent --head --fail \
                    $mirror/$packdir/$packfile
                if [ $? -ne 0 ]; then
                    debugmsg "$mirror: Faulty (at least missing $packdir/$packfile)"
                    status=1
                fi
            fi
        done
    done
    return $status
}

if [ "$1" == "-d" ]; then
    DEBUG=1
fi

# Hardcode for testing purposes
# DEBUG=1

TMPFILE=$(mktemp /tmp/mirrorsXXXXX)A
trap cleanup exit

# Generate a list of mirrors considered as "up"
curl -s  https://launchpad.net/ubuntu/+archivemirrors | \
    grep -P -B8 "statusUP|statusONE|statusSIX" | \
    grep -o -P "(f|ht)tp.*\""  | \
    sed 's/"$//' | sort | uniq > $TMPFILE

# Iterate over "close" mirror, check that they are considered up
# and sane.
for url in $(curl -s http://mirrors.ubuntu.com/mirrors.txt)
do
    if ! grep -q $url $TMPFILE; then
        debugmsg "$url Faulty (detected by Ubuntu)"
    elif blacklisted $url; then
        debugmsg "$url blacklisted"
    elif [ -z $BESTURL ]; then
        if grep -q $url $TMPFILE && check_mirror $url; then
            debugmsg "$url: OK (setting as primary URL)"
            BESTURL=$url
            test -z "$DEBUG" && break
        fi
    else
        grep -q $url $TMPFILE && check_mirror $url && debugmsg "$url: OK"
    fi
done

echo "$BESTURL"