summaryrefslogtreecommitdiff
path: root/bin/db-update
blob: 9bd6024360b2d6bbaecbd949d7cde4b3fc08eaff (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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#!/bin/sh

# move binary packages from staging to testing (if possible [1]) and
# additionally tested packages from testing to the respective stable
# repository (if possible [1])

# 1] Condition for moving a package A from staging (testing) to
#    testing (stable) is that:
#   a) nothing on the build-list run-depends on A and
#   b) no done package B (in a not-more stable repository) which is
#      not being moved run-depends on A

# TODO: be (even?) more atomic

# TODO: separate locks for staging, testing (and stable)

# TODO: handle deletion of parts of a split package

# shellcheck disable=SC2039
# shellcheck source=conf/default.conf
. "${0%/*}/../conf/default.conf"

# shellcheck disable=SC2016
usage() {
  >&2 echo ''
  >&2 echo 'db-update [options] [packages]:'
  >&2 echo ' move possible packages from staging to testing.'
  >&2 echo ' move tested packages from testing to stable.'
  >&2 echo ''
  >&2 echo 'possible options:'
  >&2 echo '  -b|--block:       If necessary, wait for lock blocking.'
  >&2 echo '  -h|--help:        Show this help and exit.'
  >&2 echo '  -n|--no-action:   Only print what would be moved.'
  >&2 echo '  -s|--stabilize $package:'
  >&2 echo '                    Assume, package $package can be stabilized.'
  >&2 echo '  -u|--unstage $package:'
  >&2 echo '                    Assume, package $package can be unstaged.'
  [ -z "$1" ] && exit 1 || exit "$1"
}

# move_packages file with one "$package $from_repository $to_repository" per line
# the existence of a directory $tmp_dir is assumed

move_packages() {

  if [ -z "${tmp_dir}" ] || [ ! -d "${tmp_dir}" ]; then
    >&2 echo 'move_packages: No tmp_dir provided.'
    exit 2
  fi

  local package
  local from_repo
  local to_repo
  local from_ending
  local to_ending
  local repo
  local part
  local dummynator
  local file

  if [ -e "${tmp_dir:?}/tmp" ]; then
    rm -rf --one-file-system "${tmp_dir:?}/tmp"
  fi
  mkdir "${tmp_dir}/tmp"

  touch "${tmp_dir}/tmp/repos"
  touch "${tmp_dir}/tmp/packages"
  touch "${tmp_dir}/tmp/master-mirror-listing"
  mkdir "${tmp_dir}/tmp/transit"

  if ${no_action}; then
    dummynator='echo'
  else
    dummynator=''
  fi

  ls_master_mirror 'i686' | \
    while read -r repo; do
      ls_master_mirror "i686/${repo}" | \
        sed "s|^|i686/${repo}/|" >> \
        "${tmp_dir}/tmp/master-mirror-listing"
    done

  while read -r package from_repo to_repo; do
    if [ -z "${package}" ]; then
      continue
    fi

    if ${no_action}; then
      printf \
        'move "%s" from "%s" to "%s"\n' \
        "${package}" \
        "${from_repo}" \
        "${to_repo}"
    fi

    echo "${package}" >> \
      "${tmp_dir}/tmp/packages"

    if echo "${from_repo}" | \
      grep -q 'staging$' && \
      echo "${to_repo}" | \
        grep -q 'testing$'; then
      from_ending='done'
      to_ending='testing'
    elif echo "${from_repo}" | \
      grep -q 'testing$' && \
      ! echo "${to_repo}" | \
        grep -q 'testing$\|staging$'; then
      from_ending='tested'
      to_ending=''
    else
      >&2 printf 'move_packages: Cannot move package "%s" from "%s" to "%s".\n' "${package}" "${from_repo}" "${to_repo}"
      exit 2
    fi

    echo "${from_repo}" > \
      "${tmp_dir}/tmp/${package}.from_repo"
    echo "${to_repo}" > \
      "${tmp_dir}/tmp/${package}.to_repo"
    echo "${from_ending}" > \
      "${tmp_dir}/tmp/${package}.from_ending"
    echo "${to_ending}" > \
      "${tmp_dir}/tmp/${package}.to_ending"

    if [ ! -f "${work_dir}/package-states/${package}.${from_ending}" ]; then
      >&2 printf 'move_packages: Cannot find package state file "%s"\n' "${package}.${from_ending}"
      exit 2
    fi

    cp \
      "${work_dir}/package-states/${package}.${from_ending}" \
      "${tmp_dir}/tmp/${package}.parts"

    sed \
      's|\(-[^-]\+\)\{3\}\.pkg\.tar\.xz$||' \
      "${tmp_dir}/tmp/${package}.parts" > \
      "${tmp_dir}/tmp/${package}.parts_names"

    sed \
      'p;s|$|.sig|' \
      "${tmp_dir}/tmp/${package}.parts" > \
      "${tmp_dir}/tmp/${package}.parts_and_signatures"

    while read -r part; do
      if ! grep -qxF "i686/${from_repo}/${part}" "${tmp_dir}/tmp/master-mirror-listing"; then
        >&2 printf \
          'move_packages: Cannot find file "%s", part of package "%s".\n' \
          "i686/${from_repo}/${part}" \
          "${package}"
        exit 2
      fi
    done < \
      "${tmp_dir}/tmp/${package}.parts"

    mkdir -p "${tmp_dir}/tmp/${from_repo}"
    mkdir -p "${tmp_dir}/tmp/${to_repo}"

    repos=$(
      # shellcheck disable=SC2046
      printf '%s\n' "${from_repo}" "${to_repo}" $(cat "${tmp_dir}/tmp/repos") | \
        sort -u
    )
    echo "${repos}" > \
      "${tmp_dir}/tmp/repos"

  done < \
    "$1"

  if ${no_action}; then
    find "${tmp_dir}/tmp" -type f | \
      while read -r file; do
        if [ "${file%.pkg.tar.xz}.pkg.tar.xz" = "${file}" ] ||
          [ "${file%.pkg.tar.xz.sig}.pkg.tar.xz.sig" = "${file}" ]; then
          echo "'${file}'"
        else
          echo "${file}:"
          sed 's|^|<<|;s|$|>>|' "${file}"
        fi
        echo
      done
  fi

  # receive the *.db.tar.gz's and *.files.tar.gz's

  while read -r repo; do

    ${master_mirror_rsync_command} \
      "${master_mirror_rsync_directory}/i686/${repo}/${repo}.db."* \
      "${master_mirror_rsync_directory}/i686/${repo}/${repo}.files."* \
      "${tmp_dir}/tmp/${repo}/"

    # add and remove the packages locally

    if grep -qxF "${repo}" "${tmp_dir}/tmp/"*".from_repo"; then

      # shellcheck disable=SC2046
      repo-remove -q \
        "${tmp_dir}/tmp/${repo}/${repo}.db.tar.gz" \
        $(
          grep -lxF "${repo}" "${tmp_dir}/tmp/"*".from_repo" | \
            sed '
              s|\.from_repo$|.parts_names|
            ' | \
            xargs -rn1 cat
        )
    fi

    if grep -qxF "${repo}" "${tmp_dir}/tmp/"*".to_repo"; then
      grep -lxF "${repo}" "${tmp_dir}/tmp/"*".to_repo" | \
        sed '
          s|\.to_repo$||
        ' | \
        while read -r package; do
          while read -r part; do
            ${master_mirror_rsync_command} \
              "${master_mirror_rsync_directory}/i686/$(cat "${package}.from_repo")/${part}" \
              "${master_mirror_rsync_directory}/i686/$(cat "${package}.from_repo")/${part}.sig" \
              "${tmp_dir}/tmp/transit/"
            repo-add -q \
              "${tmp_dir}/tmp/${repo}/${repo}.db.tar.gz" \
              "${tmp_dir}/tmp/transit/${part}"
            rm \
              "${tmp_dir}/tmp/transit/${part}" \
              "${tmp_dir}/tmp/transit/${part}.sig"
          done < \
            "${package}.parts"
        done
    fi

  done < "${tmp_dir}/tmp/repos"

  if ${no_action}; then
    find "${tmp_dir}/tmp" -type f
  fi

  # move the packages remotely via sftp

  (
    while read -r package; do

      if [ -z "${package}" ]; then
        continue
      fi

      while read -r part; do
        if [ -z "${part}" ]; then
          continue
        fi
        printf \
          'rename "%s" "%s"\n' \
          "i686/$(cat "${tmp_dir}/tmp/${package}.from_repo")/${part}" \
          "i686/$(cat "${tmp_dir}/tmp/${package}.to_repo")/${part}"
      done < \
        "${tmp_dir}/tmp/${package}.parts_and_signatures"

    done < \
      "${tmp_dir}/tmp/packages"
    echo 'quit'
  ) | \
    if ${no_action}; then
      sed 's|^|sftp: |'
    else
      ${master_mirror_sftp_command}
    fi

  # and push our local *.db.tar.gz via rsync

  while read -r repo; do

    # shellcheck disable=SC2086
    ${dummynator} ${master_mirror_rsync_command} \
      "${tmp_dir}/tmp/${repo}/${repo}.db."* \
      "${tmp_dir}/tmp/${repo}/${repo}.files."* \
      "${master_mirror_rsync_directory}/i686/${repo}/"

  done < \
    "${tmp_dir}/tmp/repos"

  while read -r package; do

    # then we can safely remove old versions

    while read -r part; do
      ${dummynator} remove_old_package_versions 'i686' "$(cat "${tmp_dir}/tmp/${package}.to_repo")" "${part}"
    done < \
      "${tmp_dir}/tmp/${package}.parts"

    # and update the state files

    from_ending=$(
      cat "${tmp_dir}/tmp/${package}.from_ending"
    )
    to_ending=$(
      cat "${tmp_dir}/tmp/${package}.to_ending"
    )

    if [ -z "${to_ending}" ]; then
      ${dummynator} rm \
        "${work_dir}/package-states/${package}.${from_ending}"
    else
      # remove old state files of $package with ending $to_ending
      find "${work_dir}/package-states" -maxdepth 1 | \
        grep "/$(str_to_regex "${package%.*.*.*}")\(\.[^.]\+\)\{3\}\.${to_ending}\$" | \
        xargs -rn1 ${dummynator} rm
      ${dummynator} mv \
        "${work_dir}/package-states/${package}.${from_ending}" \
        "${work_dir}/package-states/${package}.${to_ending}"
    fi

  done < \
    "${tmp_dir}/tmp/packages"

  if ! ${no_action}; then
    date '+%s' > \
      "${tmp_dir}/tmp/lastupdate"
    # shellcheck disable=SC2086
    ${dummynator} ${master_mirror_rsync_command} \
      "${tmp_dir}/tmp/lastupdate" \
      "${master_mirror_rsync_directory}/lastupdate"
  fi

  rm -rf --one-file-system "${tmp_dir:?}/tmp"

}

eval set -- "$(
  getopt -o bf:hns:u: \
    --long block \
    --long from: \
    --long help \
    --long no-action \
    --long stabilize: \
    --long unstage: \
    -n "$(basename "$0")" -- "$@" || \
  echo usage
)"

block_flag='-n'
no_action=false

while true
do
  case "$1" in
    -b|--block)
      block_flag=''
    ;;
    -h|--help)
      usage 0
    ;;
    -n|--no-action)
      no_action=true
    ;;
    -s|--stabilze)
      shift
      packages_to_force_stabilize="${packages_to_force_stabilize} $1"
    ;;
    -u|--unstage)
      shift
      packages_to_force_unstage="${packages_to_force_unstage} $1"
    ;;
    --)
      shift
      break
    ;;
    *)
      >&2 echo 'Whoops, forgot to implement option "'"$1"'" internally.'
      exit 42
    ;;
  esac
  shift
done

if [ $# -ne 0 ]; then
  >&2 echo 'db-update: too many arguments'
  usage
fi

if [ -s "${work_dir}/build-master-sanity" ]; then
  >&2 echo 'Build master is not sane.'
  exit 1
fi

tmp_dir=$(mktemp -d "${work_dir}/tmp.XXXXXX")
trap 'rm -rf --one-file-system "${tmp_dir}"' EXIT

for package in ${packages_to_force_stabilize}; do
  # some sanity checks
  if [ ! -f "${work_dir}/package-states/${package}.tested" ] && \
    [ ! -f "${work_dir}/package-states/${package}.testing" ] && \
    [ ! -f "${work_dir}/package-states/${package}.done" ] && \
    ! tr ' ' '.' < \
      "${work_dir}/build-list" | \
      grep -qxF "${package}"; then
    >&2 printf 'Package "%s" is not in testing, staging or on the build list!\n' "${package}"
    exit 2
  fi
done

for package in ${packages_to_force_unstage}; do
  # some sanity checks
  if [ ! -f "${work_dir}/package-states/${package}.done" ] && \
    ! tr ' ' '.' < \
      "${work_dir}/build-list" | \
      grep -qxF "${package}"; then
    >&2 printf 'Package "%s" is not in staging or on the build list!\n' "${package}"
    exit 2
  fi
done

# Create a lock file and a trap.

exec 9> "${build_list_lock_file}"
if ! flock ${block_flag} 9; then
  >&2 echo 'come back (shortly) later - I cannot lock build list.'
  exit 1
fi

exec 8> "${package_database_lock_file}"
if ! flock ${block_flag} 8; then
  >&2 echo 'come back (shortly) later - I cannot lock package database.'
  exit 1
fi

exec 7> "${sanity_check_lock_file}"
if ! flock -s ${block_flag} 7; then
  >&2 echo 'come back (shortly) later - sanity-check currently running.'
  exit 1
fi

clean_up_lock_file() {
  rm -f "${package_database_lock_file}" "${build_list_lock_file}"
  rm -rf --one-file-system "${tmp_dir}"
}

trap clean_up_lock_file EXIT

# sanity check

for ending in 'done' 'tested'; do
  errors=$(
    find "${work_dir}/package-states" -name "*.${ending}" -printf '%f\n' | \
      sed 's|\(\.[^.]\+\)\{4\}$||' | \
      sort | \
      uniq -d
  )
  if [ -n "${errors}" ]; then
    >&2 echo 'Removing duplicates not yet implemented:'
    >&2 echo "${errors}"
    exit 42
  fi
done

# packages which are done

find "${work_dir}/package-states" -maxdepth 1 -type f -name '*.done' -printf '%f\n' | \
  sed '
    s|\.done$||
  ' | \
  sort -u > \
  "${tmp_dir}/done-packages"

# packages still on the build-list

grep -vxF 'break_loops' "${work_dir}/build-list" | \
  tr ' ' '.' | \
  sort -u > \
  "${tmp_dir}/build-list-packages"

find "${work_dir}/package-infos" -name '*.groups' \
  -exec grep -qxF 'base' {} \; \
  -printf '%f\n' | \
  sed '
    s|\.groups$||
  ' | \
  sort -u > \
  "${tmp_dir}/base-packages"

# shellcheck disable=SC2086
printf '%s\n' ${packages_to_force_unstage} > \
  "${tmp_dir}/force-unstage-packages"

# calculate what packages should be unstaged:

find_biggest_subset_of_packages "${tmp_dir}/done-packages" "${tmp_dir}/build-list-packages" "${tmp_dir}/all-builds" "${tmp_dir}/force-unstage-packages" > \
  "${tmp_dir}/unstage-packages"

# no base / base-devel packages on the build list anymore?
if [ -z "$(
    join -j 1 \
      "${tmp_dir}/base-packages" \
      "${tmp_dir}/build-list-packages"
  )" ]; then

  # we pretend, the group "base" does not exist, so we only fetch 'direct' dependencies on base-packages

  mv "${tmp_dir}/all-builds" "${tmp_dir}/really-all-builds"
  grep -v ' base$' "${tmp_dir}/really-all-builds" > \
    "${tmp_dir}/all-builds" || \
    true

  find_biggest_subset_of_packages "${tmp_dir}/done-packages" "${tmp_dir}/build-list-packages" "${tmp_dir}/all-builds" "${tmp_dir}/force-unstage-packages" > \
    "${tmp_dir}/unstage-packages"

  mv "${tmp_dir}/really-all-builds" "${tmp_dir}/all-builds"
fi

# shellcheck disable=SC2086
printf '%s\n' ${packages_to_force_stabilize} > \
  "${tmp_dir}/force-stabilize-packages"

# calculate what packages should be stabilized

{
  cat "${tmp_dir}/done-packages" "${tmp_dir}/build-list-packages"
  find "${work_dir}/package-states" -maxdepth 1 -type f -name '*.testing' -printf '%f\n' | \
    sed 's|\.testing$||'
} | \
  sort -u > \
  "${tmp_dir}/keep-packages"

find "${work_dir}/package-states" -maxdepth 1 -type f -name '*.tested' -printf '%f\n' | \
    sed 's|\.tested$||' > \
  "${tmp_dir}/stabilize-packages"

find_biggest_subset_of_packages "${tmp_dir}/stabilize-packages" "${tmp_dir}/keep-packages" "${tmp_dir}/all-builds" "${tmp_dir}/force-stabilize-packages" | \
  sponge "${tmp_dir}/stabilize-packages"

# unlock build list

rm -f "${build_list_lock_file}"
flock -u 9

clean_up_lock_file() {
  rm -rf --one-file-system "${tmp_dir}"
  rm -f "${package_database_lock_file}"
}

# testing -> stable

while read -r package; do
  if [ -z "${package}" ]; then
    continue
  fi
  printf '%s %s %s\n' \
    "${package}" \
    "$(official_or_community "${package}" 'testing')" \
    "$(repository_of_package "${package}")"
done < \
  "${tmp_dir}/stabilize-packages" | \
  sponge "${tmp_dir}/stabilize-packages"

# staging -> testing

while read -r package; do
  if [ -z "${package}" ]; then
    continue
  fi
  printf '%s %s %s\n' \
    "${package}" \
    "$(official_or_community "${package}" 'staging')" \
    "$(official_or_community "${package}" 'testing')"
done < \
  "${tmp_dir}/unstage-packages" | \
  sponge "${tmp_dir}/unstage-packages"

# move packages in packages_to_stabilize from *testing/ to the stable repos
if [ -s "${tmp_dir}/stabilize-packages" ]; then
  move_packages "${tmp_dir}/stabilize-packages"
fi

# move packages from *staging to *testing
if [ -s "${tmp_dir}/unstage-packages" ]; then
  move_packages "${tmp_dir}/unstage-packages"
fi

clean_up_lock_file