Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/bin/check-mirrors
blob: d0b626ce000c4c00cb3208862e670e9d85ad4720 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/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 -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
# 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