Enable use of Fuel library commit ids for mirror
[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 return_url=0
22
23 while [ "$1" != "" ]; do
24     case $1 in
25         -u | --url )   shift
26                        return_url=1
27                        ;;
28     # Shift all the parameters down by one
29     esac
30     shift
31 done
32
33
34 cnt=0
35 while [ $cnt -lt 4 ]
36 do
37     for url in $(curl -s http://mirrors.ubuntu.com/mirrors.txt)
38     do
39         host=$(echo $url | cut -d'/' -f3)
40         if $RSYNC "${host}::ubuntu/." &> /dev/null
41         then
42             if ! $RSYNC "${host}::ubuntu/Archive-Update-in-Progress*" &> /dev/null
43             then
44                 if [ "$return_url" = "1" ]; then
45                     echo "$url"
46                     exit 0
47                 else
48                     echo "$host"
49                     exit 0
50                 fi
51             fi
52         fi
53     done
54     cnt=$[cnt + 1]
55     sleep 15
56 done
57
58 if [ "$return_url" = "1" ]; then
59     echo "http://archive.ubuntu.com/ubuntu/"
60 else
61     echo "archive.ubuntu.com"
62 fi