#!/bin/sh # shellcheck source=../lib/load-configuration . "${0%/*}/../lib/load-configuration" if [ $# -eq 0 ]; then # Create a lock file. exec 9> "${check_mirrors_lock_file}" if ! verbose_flock 9; then >&2 echo 'come back (shortly) later - I cannot lock check-mirrors.' exit fi echo 'updating mirrors from git...' >&2 if [ -d "${releng_directory}/.git" ]; then git -C "${releng_directory}" remote update else git -C "${releng_directory}" fetch origin master:master fi || \ true country_codes=$( git -C "${releng_directory}" archive master -- 'flag-names' | \ tar -Ox ) tmp_file=$( mktemp "tmp.check-mirrors.XXXXXXXXXX" --tmpdir ) trap 'rm "${tmp_file}"' EXIT git -C "${repo_paths__archlinux32}" archive HEAD -- 'core/pacman-mirrorlist/mirrorlist' | \ tar -Oxf - | \ 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 -j10 "$0" "{}" > \ "${tmp_file}" echo 'loading mirror data into database...' >&2 # 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' printf '@%s,' \ 'isos' \ 'ipv4' \ 'ipv6' \ 'active' | \ sed 's/,$//' printf ') set ' printf '`%s`=CAST(@%s AS SIGNED),' \ 'isos' 'isos' \ 'ipv4' 'ipv4' \ 'ipv6' 'ipv6' \ 'active' 'active' | \ sed 's/,$//' printf ';\n' } | \ mysql_run_query echo 'done' >&2 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 -k -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 -k -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 -k -s "${ip_flag}" "${url}archisos/" ) || has_isos=0 # doesn't really work: dual doesn't exist anymore and the other test sometimes fails, sometimes succeeds? # let's assume the precense of the 'archisos' directory is enough # for suffix in 'i686' 'dual'; do # # shellcheck disable=SC2059 # if ! printf '%s\n' "${cnt}" | \ # grep -qF "$(printf "$(date '+archlinux-%Y.%m.%%02d-'"${suffix}"'.iso\n')" $(seq 31))" -; then # has_isos=0 # break # fi # done else has_isos=0 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' >&2 echo ' The values must be base64-encoded and passed as a' >&2 echo ' single parameter, separated by single spaces.' fi