Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/bin/check-mirrors
blob: 9d15f28a11e4e9eb5a75c807d5b9e59a1aa68085 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/sh

# shellcheck source=../lib/load-configuration
. "${0%/*}/../lib/load-configuration"

if [ $# -eq 0 ]; then

  git -C "${releng_directory}" remote update

  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' | \
    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}"

  # 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
    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'
fi