From 73edc40a119bef47a15d0048503281e05a0ee48b Mon Sep 17 00:00:00 2001 From: Erich Eckner Date: Fri, 15 Jun 2018 11:34:11 +0200 Subject: bin/check-mirrors new --- bin/check-mirrors | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100755 bin/check-mirrors (limited to 'bin/check-mirrors') diff --git a/bin/check-mirrors b/bin/check-mirrors new file mode 100755 index 0000000..5940d25 --- /dev/null +++ b/bin/check-mirrors @@ -0,0 +1,174 @@ +#!/bin/sh + +# shellcheck source=../lib/load-configuration +. "${0%/*}/../lib/load-configuration" + +if [ $# -eq 0 ]; then + + country_codes=$( + curl -Ss 'https://git.archlinux32.org/archlinux32/releng/raw/branch/master/flag-names' + ) + + tmp_file=$( + mktemp "tmp.check-mirrors.XXXXXXXXXX" --tmpdir + ) + trap 'rm "${tmp_file}"' EXIT + + git -C "${repo_paths__archlinux32}" archive HEAD -- 'core/pacman-mirrorlist/mirrorlist' | \ + sed -n ' + 1,/^$/d + /^## [^A-Z]/d + s/^##/Country/ + s/^#Server = /Server / + T + p + ' | \ + while read -r what rest; do + case "${what}" in + 'Country') + country="${rest}" + country_code=$( + printf '%s\n' "${country_codes}" | \ + grep '^\S\+ '"$(str_to_regex "${country}")"'$' | \ + cut -d' ' -f1 + ) + continue + ;; + 'Server') + printf '%s %s %s\n' \ + "$(printf '%s' "${country}" | base64 -w0)" \ + "$(printf '%s' "${country_code}" | base64 -w0)" \ + "$(printf '%s' "${rest}" | base64 -w0)" + ;; + esac + done | \ + parallel -j100 "$0" "{}" > \ + "${tmp_file}" + + # shellcheck disable=SC2016 + { + printf 'LOAD DATA LOCAL INFILE "%s" INTO TABLE `mirror_statuses` (' \ + "${tmp_file}" + printf '`%s`,' \ + 'protocol' \ + 'url' \ + 'country' \ + 'country_code' \ + 'last_sync' \ + 'start' \ + 'stop' \ + 'isos' \ + 'ipv4' \ + 'ipv6' \ + 'active' | \ + sed 's/,$//' + printf ');\n' + } | \ + mysql_run_query + +elif [ $# -eq 1 ]; then + # check a single mirror + url="$1" + country=$( + printf '%s' "${url%% *}" | \ + base64 -d + ) + url="${url#* }" + country_code=$( + printf '%s' "${url%% *}" | \ + base64 -d + ) + url=$( + # shellcheck disable=SC2016 + printf '%s' "${url#* }" | \ + base64 -d | \ + sed 's,/\$arch/\$repo$,,' + ) + start=$( + date '+%s.%N' + ) + success=1 + has_ipv4=1 + if last_sync=$( + curl -4 -s "${url}/lastsync" + ); then + if printf '%s' "${last_sync}" | \ + tr '\n' 'X' | \ + grep -q '[^0-9]'; then + has_ipv4=0 + fi + else + has_ipv4=0 + fi + has_ipv6=1 + if cnt=$( + curl -6 -s "${url}/lastsync" + ); then + if printf '%s' "${cnt}" | \ + tr '\n' 'X' | \ + grep -q '[^0-9]'; then + has_ipv6=0 + else + last_sync="${cnt}" + fi + else + has_ipv6=0 + fi + if [ ${has_ipv6} -eq 1 ]; then + ip_flag='-6' + elif [ ${has_ipv4} -eq 1 ]; then + ip_flag='-4' + else + success=0 + fi + if [ ${success} -eq 1 ]; then + has_isos=1 + cnt=$( + curl -s "${ip_flag}" "${url}/archisos/" + ) || has_isos=0 + for suffix in 'i686' 'dual'; do + if ! printf '%s\n' "${cnt}" | \ + grep -qF "$(date '+archlinux-%Y.%m.01-'"${suffix}"'.iso')"; then + has_isos=0 + break + fi + done + else + last_sync='0' + fi + stop=$( + date '+%s.%N' + ) + # columns to output: + # "protocol" => "rsync", + # "url" => "rsync://mirror.aarnet.edu.au/archlinux/", + # "country" => "Australia", + # "country_code" => "AU", + # "last_sync" => "1529051726", + # "start" => "1529051726.369490757", + # "stop" => "1529051726.369490757", + # "isos" => true, + # "ipv4" => true, + # "ipv6" => true, + # "active" => true, + printf '%s\t' \ + "${url%%:*}" \ + "${url}" \ + "${country}" \ + "${country_code}" \ + "${last_sync}" \ + "${start}" \ + "${stop}" \ + "${has_isos}" \ + "${has_ipv4}" \ + "${has_ipv6}" \ + "${success}" | \ + sed 's/\t$/\n/' +else + >&2 echo 'usage:' + >&2 echo ' check-mirrors' + >&2 echo ' check all mirrors and update database' + # shellcheck disable=SC2016 + >&2 echo ' check-mirrors $country $country_code $url' + >&2 echo ' check the given mirror and output values to stdout' +fi -- cgit v1.2.3