aboutsummaryrefslogtreecommitdiffstats
path: root/build/f_isoroot/f_repobuild/select_ubuntu_repo.sh
blob: bfaec74f1841d3e29638ab7d67faa50288e0c670 (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
#!/bin/bash
##############################################################################
# Copyright (c) 2015 Ericsson AB and others.
# mskalski@mirantis.com
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
RSYNC="rsync -4 --contimeout 5 --no-motd --list-only"

# try to choose close ubuntu mirror which support rsync protocol
# https://bugs.launchpad.net/fuel/+bug/1459252

# A minor modificiation of Michal Skalski's original Makefile version
# to only consider repos where no repo updates are in progress (as
# that may have us hanging quite a while otherwise). If no suitable
# local mirror can be found after four attempts, the default archive
# is returned instead.

return_url=0

while [ "$1" != "" ]; do
    case $1 in
        -u | --url )   shift
                       return_url=1
                       ;;
    # Shift all the parameters down by one
    esac
    shift
done


cnt=0
while [ $cnt -lt 4 ]
do
    for url in $(curl -s http://mirrors.ubuntu.com/mirrors.txt)
    do
        host=$(echo $url | cut -d'/' -f3)
        if $RSYNC "${host}::ubuntu/." &> /dev/null
        then
            if ! $RSYNC "${host}::ubuntu/Archive-Update-in-Progress*" &> /dev/null
            then
                if [ "$return_url" = "1" ]; then
                    echo "$url"
                    exit 0
                else
                    echo "$host"
                    exit 0
                fi
            fi
        fi
    done
    cnt=$[cnt + 1]
    sleep 15
done

if [ "$return_url" = "1" ]; then
    echo "http://archive.ubuntu.com/ubuntu/"
else
    echo "archive.ubuntu.com"
fi