summaryrefslogtreecommitdiff
path: root/arch-mirror
blob: c00579d0ce41caf181b1745ea0d8d5e3671b7216 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/bin/bash

if [ "$(whoami)" != 'http' ]; then
  if [ $# -ne 0 ]; then
    >&2 printf 'Too many arguments: %s.\n' "$#"
    >&2 printf 'usage: %s\n' "$0"
    exit 1
  fi
  exec su http -s /bin/bash -c "$0"
fi

mirror_dir='/srv/http/arch'

rsync_options='-rtlHp4 --safe-links --timeout=600 --contimeout=60 --delay-updates --no-motd --partial --exclude .~tmp~'

total_timeout=1200

if stty &>/dev/null; then
  rsync_verbose='-v'
  wget_verbose='-v'
else
  rsync_verbose=''
  wget_verbose='-nv'
fi

retrieve_listing() {
  local url="$1"
  case "${url%%://*}" in
    'rsync')
      timeout ${total_timeout} rsync "${url}" \
      | grep '^d' \
      | awk '{print $5}'
    ;;
    'https'|'http')
      curl -fSsL --max-time ${total_timeout} "${url}" \
      | sed '
        s@^.*<a href="\([^"]\+\)/">\1/</a>.*$@\1@
        t
        d
      '
    ;;
    *)
      >&2 printf 'Unknown protocol: %s\n' "${url%%://*}"
    ;;
  esac
}

retrieve_file() {
  local url="$1"
  local file="$2"
  case "${url%%://*}" in
    'rsync')
      timeout ${total_timeout} rsync -q "${url}" "${file}"
    ;;
    'https'|'http')
      curl -fSsL --max-time ${total_timeout} "${url}" -o "${file}"
    ;;
    *)
      >&2 printf 'Unknown protocol: %s\n' "${url%%://*}"
    ;;
  esac
}

retrieve_content() {
  local url="$1"
  local destination="$2"
  local extra_options
  local timeout

  if [ -z "${destination##*/archlinuxarm/}" ]; then
    extra_options='--exclude os'
  else
    unset extra_options
  fi

  if [ -z "${destination##*/archlinux32/}" ]; then
    timeout='timeout 36000'
  else
    timeout="timeout ${total_timeout}"
  fi

  case "${url%%://*}" in
    'rsync')
      ${timeout} rsync ${rsync_options} ${rsync_verbose} ${extra_options} "${url}" "${destination}" \
      || return $?
    ;;
    'https'|'http')
      ${timeout} wget --mirror --prefer-family=Ipv4 -nH -np ${wget_verbose} -P "${destination}" --cut-dirs $(
        echo "${url#*//}" \
        | tr -d '\n' \
        | tr '/' '\n' \
        | sed 1d \
        | wc -l
      ) "${url}" \
      || return $?
    ;;
    *)
      >&2 printf 'Unknown protocol: %s\n' "${url%%://*}"
    ;;
  esac
}

resolve_complete_mirror() {
  local host="$1"
  local pre="${host%%//*}//"
  host="${host#${pre}}"
  local post="/${host#*/}"
  host="${host%${post}}"

  {
    dig +short "${host}" A \
    | sed '
      /^[0-9]\+\(\.[0-9]\+\)\{3\}$/ !d
    '
    dig +short "${host}" AAAA \
    | sed '
      s/^[0-9a-f:]\+$/[\0]/
      t
      d
    '
  } \
  | awk '{print "'"${pre}"'" $1 "'"${post}"'"}' \
  | while read -r url; do
    retrieve_listing "${url}" \
    | grep -v '^\(merged$\|\.\)' \
    | sed 's@^\(archlinuxfr\|quarry\)$@\0/os@' \
    | awk '{print "/" $1 "/#'"${url}"'" $1 "/"}'
  done
}

fetch_archlinux_mirrors() {
  curl -fSs --max-time ${total_timeout} 'https://www.archlinux.org/mirrors/status/json/' \
  | sed -n '
    s@^[^[]*"urls": \[@@
    T
    s@][^[]*$@@
    T
    p
  ' \
  | sed '
    s/}, {/},\n{/g
  ' \
  | grep -F '"protocol": "rsync"' \
  | grep -F '"isos": true' \
  | grep -vwF arch.eckner.net \
  | grep '"country_code": "\(DE\|CZ\|NL\|CH\|AT\|DK\)"' \
  | {
    date -u +'%FT%TZ CUTCUTCUT' -d@$(($(date +%s)-3600))
    sed '
      s@^.*, "last_sync": "\([^"]\+\)",.*$@\1 \0@
    '
  } \
  | sort -k1r,1 \
  | sed '
    / CUTCUTCUT/,$ d
    s@^\S\+ {"url": "\([^"]\+\)".*$@/archlinux/#\1@
    t
    d
  '
}

fetch_lastsync() {
  local tmp_file=$(mktemp)
  while read -r proximity url; do
    rm -f "${tmp_file}"
    if [ -n "${1}" ]; then
      if ! retrieve_file "${url}${1}" "${tmp_file}" 2>/dev/null; then
        rm -f "${tmp_file}"
      fi
    fi
    for file in 'lastsync' 'lastupdate'; do
      if [ -s "${tmp_file}" ]; then
        break
      fi
      for dir in '' 'os/'; do
        if [ -s "${tmp_file}" ]; then
          break
        fi
        if ! retrieve_file "${url}${dir}${file}" "${tmp_file}" 2>/dev/null; then
          rm -f "${tmp_file}"
        fi
      done
    done
    if [ -s "${tmp_file}" ]; then
      printf '%s %s %s\n' \
        "$(head -n1 "${tmp_file}")" \
        "${proximity}" \
        "${url}"
    else
      printf '%s %s %s\n' \
        "$(($(date +%s) - 120 + 60*proximity))" \
        "${proximity}" \
        "${url}"
    fi
  done
  rm -f "${tmp_file}"
}

remove_unindexed_packages() {
  find "$1" \
    -type d \
    -name any \
    -printf '%p\n' \
  | while read -r any_repo; do
    {
      find "${any_repo%/*}" \
        -name 'any' -prune , \
        -type l \
        -name '*.pkg*' \
        -printf '%f\n' \
      | sed '
        s@^@'"${any_repo}"'/@
        p
      '
      find "${any_repo}" \
        -mindepth 1 \
        -maxdepth 1 \
        -printf '%p\n'
    } \
    | sort \
    | uniq -u \
    | xargs -r rm
  done
  find "$1" \
    -type d \
    -name pool \
    -printf '%p\n' \
  | while read -r pool; do
    {
      find "${pool%/*}" \
        -name 'pool' -prune , \
        -type l \
        -name '*.pkg*' \
        -readlink -e {} + \
        grep '^'"${pool}"'/'
      find "${pool}" \
        -type f \
        -printf '%p\n'
    } \
    | sort \
    | uniq -u \
    | xargs -r rm
  done
  find "$1" \
    -name '.~tmp~' -prune , \
    -name '*.db' -printf '%p\n' \
  | while read -r db; do
    db_file="${db##*/}"
    {
      {
        find "${db%/*}/" \
          \( \
            -name "${db_file}*" -o \
            -name "${db_file%db}files*" \
          \) \
          -printf '%p\n'
        bsdtar -Oxf "${db}" \
        | sed '
          /^%FILENAME%$/ {
            N
            s@^%FILENAME%\n@'"${db%/*}"'/@
            t
          }
          D
        ' \
        | sed '
          p
          s/$/.sig/
        '
      } \
      | sed 'p'
      find "${db%/*}/" \
        -mindepth 1 \
        -maxdepth 1 \
        -printf '%p\n'
    } \
    | sort \
    | uniq -u \
    | xargs -r rm
  done
  find "$1" \
    -type d \
    -name '.~tmp~' \
    -exec rm -rf --one-file-system {} +
}

if [ $# -eq 2 ] && [ "$1" = 'mirror' ]; then
  to_sync="$2"

  for file in 'lastsync' 'lastupdate'; do
    for dir in '' 'os/'; do
      if [ -f "${mirror_dir}${to_sync}${dir}${file}" ]; then
        break
      fi
    done
    if [ -f "${mirror_dir}${to_sync}${dir}${file}" ]; then
      break
    fi
  done

  >&2 printf 'synchronizing %s ...\n' "${to_sync}"

  {
    if [ -f "${mirror_dir}${to_sync}${dir}${file}" ]; then
      printf '%s 0 ME\n' "$(head -n1 "${mirror_dir}${to_sync}${dir}${file}")"
    else
      unset dir
      unset file
    fi
    { 
      sed 's/^/1 /' "${mirror_dir}/.tmp/close-mirrors"
      sed 's/^/2 /' "${mirror_dir}/.tmp/far-mirrors"
    } \
    | sed '
      s@^\([12] \)'"${to_sync}"'#@\1@
      t
      d
    ' \
    | fetch_lastsync "${dir}${file}"
  } \
  | sort -k1nr,1 -k2n,2 \
  | sed '/ ME$/,$ d' \
  | sort -k2n,2 -k1nr,1 \
  | {
    last_successful_level=0
    while read -r _ level url; do
      if [ "${level}" -le "${last_successful_level}" ]; then
        continue
      fi
      if retrieve_content "${url}" "${mirror_dir}${to_sync}"; then
        last_successful_level="${level}"
      fi
    done
    if [ "${last_successful_level}" -eq 2 ]; then
      >&2 printf 'clean up %s ...\n' "${to_sync}"
      remove_unindexed_packages "${mirror_dir}${to_sync}"
    fi
  }
  >&2 printf '%s done.\n' "${to_sync}"

  exit

fi

if [ $# -ne 0 ]; then
  >&2 printf 'usage:\n'
  >&2 printf '  %s\n' "$0"
  >&2 printf '( %s mirror /sub-destination/#proto://host/url/ )\n' "$0"
  exit 1
fi

mkdir -p "${mirror_dir}"'/.tmp'

exec 9> "${mirror_dir}/.arch-mirror.lock"
flock -n 9 || exit 0

{
  resolve_complete_mirror 'rsync://jeti100.ioq.uni-jena.de/archlinux-all/'
  fetch_archlinux_mirrors \
  | shuf -n10
  printf '%s\n' \
    '/archlinux/#rsync://ftp.gwdg.de/pub/linux/archlinux/' \
    '/archlinuxarm/#rsync://ftp.halifax.rwth-aachen.de/archlinux-arm/' \
    '/archlinuxfr/os/#https://repo.archlinux.fr/' \
    '/quarry/os/#https://pkgbuild.com/~anatolik/quarry/'
} \
> "${mirror_dir}/.tmp/far-mirrors"

resolve_complete_mirror 'rsync://arch.eckner.net/archlinux-all/' \
> "${mirror_dir}/.tmp/close-mirrors"

cat \
  "${mirror_dir}/.tmp/close-mirrors" \
  "${mirror_dir}/.tmp/far-mirrors" \
| sed 's/#.*$//' \
| sort -u \
| parallel -j0 --lb -L1 "${0}" mirror

>&2 printf 'refreshing symlinks ...\n'

find "${mirror_dir}/" \
  -name '.*' -prune , \
  \( -name '*.files.tar.?z' -o -name '*.db.tar.?z' \) -printf '%p\n' \
| sed '
  s@\.\(files\|db\)\.tar\.[xg]z$@@
' \
| sort -u \
| sed '
  s@\(/\([^/]\+\)/os/\([^/]\+\)\)/\2$@\1 \2/os/\3@
  t
  s@\(/\([^/]\+\)/\([^/]\+\)\)/\3$@\1 \3/os/\2@
  t
  d
' \
| while read -r link_target link_name; do
  link_target="$(
    echo "${link_name%/*}/" \
    | sed '
      s@[^/]\+/@../@g
    '
  )..${link_target#${mirror_dir}}"
  link_name="${mirror_dir}/merged/${link_name}"
  mkdir -p "${link_name%/*}"
  rm -f "${link_name}"
  ln -s "${link_target}" "${link_name}"
done

>&2 printf 'symlinks done.\n'

>&2 printf 'refreshing timestamps ...\n'

declare -A dates

while read -r var date; do
  if [ "${var}" != 'lastupdate' ] \
  && [ "${var}" != 'lastsync' ]; then
    continue
  fi
  if [ -z "${dates[${var}.min]}" ] \
  || [ "${dates[${var}.min]}" -gt "${date}" ]; then
    dates["${var}.min"]="${date}";
  fi
  if [ -z "${dates[${var}.max]}" ] \
  || [ "${dates[${var}.max]}" -lt "${date}" ]; then
    dates["${var}.max"]="${date}";
  fi
done < <(
  find "${mirror_dir}/" -maxdepth 3 \
    -name '.*' -prune , \
    -type f \( \
      -name 'lastsync' -o -name 'lastupdate' \
    \) \
    -printf '%f ' \
    -exec head -n1 {} \; \
    -printf '\n' \
  | grep -vxF ''
)

for key in "${!dates[@]}"; do
  printf '%s\n' "${dates["${key}"]}" \
  > "${mirror_dir}/${key}"
done

>&2 printf 'timestamps done.\n'