540f7ebce1e49a343be7ace797da49826648f8e6
[fuel.git] / build / f_isoroot / f_repobuild / select_ubuntu_repo.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2015 Ericsson AB and others.
4 # mskalski@mirantis.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 RSYNC="rsync -4 --contimeout 5 --no-motd --list-only"
11
12 # try to choose close ubuntu mirror which support rsync protocol
13 # https://bugs.launchpad.net/fuel/+bug/1459252
14
15 # A minor modificiation of Michal Skalski's original Makefile version
16 # to only consider repos where no repo updates are in progress (as
17 # that may have us hanging quite a while otherwise). If no suitable
18 # local mirror can be found after four attempts, the default archive
19 # is returned instead.
20
21 # Some Ubuntu mirrors seem less reliable for this type of mirroring -
22 # as they are discoved they can be added to the blacklist below in order
23 # for them not to be considered.
24 BLACKLIST="mirrors.se.eu.kernel.org"
25
26 return_url=0
27
28 while [ "$1" != "" ]; do
29     case $1 in
30         -u | --url )   shift
31                        return_url=1
32                        ;;
33     # Shift all the parameters down by one
34     esac
35     shift
36 done
37
38 cnt=0
39 while [ $cnt -lt 4 ]
40 do
41     for url in $(curl -s http://mirrors.ubuntu.com/mirrors.txt)
42     do
43         host=$(echo $url | cut -d'/' -f3)
44         echo ${BLACKLIST} | grep -q ${host} && continue
45         if $RSYNC "${host}::ubuntu/." &> /dev/null
46         then
47             if ! $RSYNC "${host}::ubuntu/Archive-Update-in-Progress*" &> /dev/null
48             then
49                 if [ "$return_url" = "1" ]; then
50                     echo "$url"
51                     exit 0
52                 else
53                     echo "$host"
54                     exit 0
55                 fi
56             fi
57         fi
58     done
59     cnt=$[cnt + 1]
60     sleep 15
61 done
62
63 if [ "$return_url" = "1" ]; then
64     echo "http://archive.ubuntu.com/ubuntu/"
65 else
66     echo "archive.ubuntu.com"
67 fi