[kvm-plugin] Update the kernel version
[fuel.git] / build / f_isoroot / f_repobuild / select_ubuntu_repo.sh
index bfaec74..c8c86db 100755 (executable)
 #!/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
 
+UBUNTU_DISTRO="xenial"
+BLACKLIST="http://mirrors.se.eu.kernel.org/ubuntu/"
+#BLACKLIST+=" http://foo.bar"
 
-cnt=0
-while [ $cnt -lt 4 ]
-do
-    for url in $(curl -s http://mirrors.ubuntu.com/mirrors.txt)
+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
-        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
+        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
-        fi
+        done
     done
-    cnt=$[cnt + 1]
-    sleep 15
-done
+    return $status
+}
 
-if [ "$return_url" = "1" ]; then
-    echo "http://archive.ubuntu.com/ubuntu/"
-else
-    echo "archive.ubuntu.com"
+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"