Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/bin/return-assignment
blob: 64a6059a0d3913499df3f13d92ec7aae1e68d2a7 (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
#!/bin/sh

# report back on a build assignment
# either on success via:
#   "$0 $package $revision $mod_revision $repository" and tar'ed packages and logs
#   (= a tar of package(s), signature(s) and log(s)) on stdin
# or on failure via:
#   "$0 $package $revision $mod_revision $repository ERROR" and tar'ed logs

# exit codes:
#  0: ok
#  1: another instance was already running
#  2: outdated package
#  3: signature error
#  4: package error (e.g. wrong packages sent)

# TODO: sign database

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

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

# Create a lock file and a trap.

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

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

clean_up_lock_file() {
  rm -f "${build_list_lock_file}"
}

trap clean_up_lock_file EXIT

if [ "$5" = 'ERROR' ]; then
# the build failed on the build slave

  # shellcheck disable=SC2016
  infos=$(
    {
      printf 'SELECT DISTINCT `build_assignments`.`id`,IF(`build_assignments`.`is_broken`,"true","false") FROM `build_slaves`'
      mysql_join_build_slaves_build_assignments
      mysql_join_build_assignments_package_sources
      mysql_join_package_sources_upstream_repositories
      mysql_join_build_assignments_binary_packages
      mysql_join_binary_packages_repositories
      printf ' WHERE `build_slaves`.`name`=from_base64("%s")' \
        "$(
          # shellcheck disable=SC2154
          printf '%s' "${slave}" | \
            base64 -w0
        )"
      printf ' AND `package_sources`.`%s`=from_base64("%s")' \
        'pkgbase' "$(printf '%s' "$1" | base64 -w0)" \
        'git_revision' "$(printf '%s' "$2" | base64 -w0)" \
        'mod_git_revision' "$(printf '%s' "$3" | base64 -w0)"
      printf ' AND `upstream_repositories`.`name`=from_base64("%s")' \
        "$(printf '%s' "$4" | base64 -w0)"
      printf ' AND `repositories`.`name`="build-list"'
    } | \
      mysql_run_query | \
      tr '\t' ' '
  )
  if [ -z "${infos}" ]; then
    >&2 echo 'You do not build this package (anymore) - move on.'
    exit 2
  fi
  was_broken_before="${infos##* }"

  # save sent build logs
  saved_build_logs=$(
    tar -vx \
      -C "${build_log_directory}/error" \
      --wildcards \
      --no-wildcards-match-slash \
      --transform="s|^|$1.$2.$3.$4.|" \
      '*.build-log.gz'
  )

  # shellcheck disable=SC2016
  {
    if [ -n "${saved_build_logs}" ]; then
      printf 'CREATE TEMPORARY TABLE `failures` ('
      printf '`%s` %s,' \
        'date' 'DATETIME' \
        'reason' 'SMALLINT' \
        'log_file' 'VARCHAR(512)' | \
        sed 's/,$//'
      printf ');\n'
      printf 'INSERT INTO `failures` (`date`,`reason`,`log_file`) VALUES'
      fail_reason_identifiers=$(
        {
          printf 'SELECT `fail_reasons`.`id`,replace(to_base64(`fail_reasons`.`identifier`),"\\n","")'
          printf ' FROM `fail_reasons` ORDER BY `fail_reasons`.`severity`'
        } | \
          mysql_run_query
      )
      for saved_build_log in ${saved_build_logs}; do
        printf '%s' "${fail_reason_identifiers}" | \
          while read -r reason_id identifier; do
            if zgrep -q "^$(
                printf '%s' "${identifier}" | \
                  base64 -d
              )\$" \
              "${build_log_directory}/error/$1.$2.$3.$4.${saved_build_log}"; then

              printf ' (from_base64("%s"),%s,from_base64("%s")),' \
                "$(
                  printf '%s' "${saved_build_log}" | \
                    sed 's|\.build-log\.gz$||;s|^.*\.||' | \
                    base64 -w0
                )" \
                "${reason_id}" \
                "$(
                  printf '%s' "$1.$2.$3.$4.${saved_build_log}" | \
                    base64 -w0
                )"
              break
            fi
          done
      done | \
        sed 's/,$//'
      printf ';\n'
      printf 'INSERT INTO `failed_builds` (`build_slave`,`build_assignment`,`date`,`reason`,`log_file`)'
      printf ' SELECT '
      printf '`build_slaves`.`%s`,' \
        'id' 'currently_building'
      printf '`failures`.`%s`,' \
        'date' 'reason' 'log_file' | \
        sed 's/,$//'
      printf ' FROM `build_slaves` JOIN `failures`'
      printf ' WHERE `build_slaves`.`name`=from_base64("%s");\n' \
        "$(
          printf '%s' "${slave}" | \
            base64 -w0
        )"
      printf 'DROP TABLE `failures`;\n'
    fi
    printf 'UPDATE `build_assignments`'
    mysql_join_build_assignments_build_slaves
    printf ' SET `build_assignments`.`is_broken`=1, `build_slaves`.`currently_building`=NULL'
    printf ' WHERE `build_slaves`.`name`=from_base64("%s");\n' \
      "$(
        printf '%s' "${slave}" | \
          base64 -w0
      )"
  } | \
    mysql_run_query

  # release lock on build-list - otherwise seed-build-list won't run
  flock -u 9

  if ! ${was_broken_before}; then

    haskell_rebuild_packages=$(
      find "${build_log_directory}/error" -type f \
        -name "$1.$2.$3.$4.*.build-log.gz" \
        -exec zgrep -qFx '    The following packages are broken because other packages they depend on are missing. These broken packages must be rebuilt before they can be used.' {} \; \
        -exec zcat {} \; | \
        sed -n '
          s/^installed package \(.*\) is broken due to missing package .*$/\1/
          T
          p
        ' | \
        tr ' ' '\n' | \
        sed '
          s/^/-p ^(haskell-)?/
          s/-[0-9.]\+$/\$/
        ' | \
        sort -u
    )

    rescheduled_packages=$(
      if [ -n "${haskell_rebuild_packages}" ]; then
        # shellcheck disable=SC2086
        "${base_dir}/bin/seed-build-list" ${haskell_rebuild_packages} | \
          sed 's/ .*$//'
      fi
    )

    {
      printf '%s is broken (says %s).' \
        "$1" \
        "${slave}"
      if [ -n "${rescheduled_packages}" ]; then
        printf -- ' - I rescheduled:'
        # shellcheck disable=SC2086
        printf ' %s,' ${rescheduled_packages} | \
          sed 's/,$/./'
      fi
      printf '\n'
    } | \
      irc_say
  fi

  exit 0

fi

# the build was successful on the build slave

# so we also need a lock on the package database

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

clean_up_lock_file() {
  rm -f "${build_list_lock_file}"
  rm -f "${package_database_lock_file}"
}

# shellcheck disable=SC2016
if ! {
    printf 'SELECT count(*)'
    printf ' FROM `binary_packages`'
    mysql_join_binary_packages_build_assignments
    mysql_join_build_assignments_build_slaves
    mysql_join_binary_packages_repositories
    mysql_join_build_assignments_package_sources
    mysql_join_package_sources_upstream_repositories
    printf ' WHERE `repositories`.`name`="build-list"'
    printf ' AND `build_slaves`.`name`=from_base64("%s")' \
      "$(printf '%s' "${slave}" | base64 -w0)"
    printf ' AND `package_sources`.`%s`=from_base64("%s")' \
      'pkgbase' "$(printf '%s' "$1" | base64 -w0)" \
      'git_revision' "$(printf '%s' "$2" | base64 -w0)" \
      'mod_git_revision' "$(printf '%s' "$3" | base64 -w0)"
    printf ' AND `upstream_repositories`.`name`=from_base64("%s")' \
      "$(printf '%s' "$4" | base64 -w0)"
    printf ' AND `binary_packages`.`sub_pkgrel`=from_base64("%s");\n' \
      "$(printf '%s' "$5" | base64 -w0)"
  } | \
    mysql_run_query | \
    grep -qvxF '0'; then
  >&2 echo 'Sorry, the sent package is outdated.'
  exit 2
fi

clean_up_tmp_dir() {
  cd "${base_dir}"
  rm -rf --one-file-system "${tmp_dir}"
  clean_up_lock_file
}

tmp_dir=$(mktemp -d "${work_dir}/tmp.return-assignment.XXXXXXXXXX")
cd "${tmp_dir}"
trap clean_up_tmp_dir EXIT

# extract package(s)
tar -x \
  --wildcards \
  --no-wildcards-match-slash \
  '*.pkg.tar.xz' \
  '*.pkg.tar.xz.sig' \
  '*.pkg.tar.xz-namcap.log.gz' \
  '*.pkg.tar.xz.so.needs.gz' \
  '*.pkg.tar.xz.so.provides.gz'

# check if all packages come with:
#  - a package file
#  - a signature
#  - a namcap log
#  - a list of needed libraries
#  - a list of provided libraries
missing_files=$(
  find . -maxdepth 1 \( \
    \( \
      -name '*.pkg.tar.xz' \
      -printf '%f package\n' \
    \) -o \
    \( \
      -name '*.pkg.tar.xz.sig' \
      -printf '%f signature\n' \
    \) -o \
    \( \
      -name '*.pkg.tar.xz-namcap.log.gz' \
      -printf '%f namcap\n' \
    \) -o \
    \( \
      -name '*.pkg.tar.xz.so.needs.gz' \
      -printf '%f needed-libraries\n' \
    \) -o \
    \( \
      -name '*.pkg.tar.xz.so.provides.gz' \
      -printf '%f provided-libraries\n' \
    \) \
  \) | \
    sed '
      s/\(\.pkg\.tar\.xz\)\(\.sig\|-namcap\.log\.gz\|\.so\.\(provides\|needs\)\.gz\) /\1 /
    ' | \
    sort -k1,1 -k2,2 | \
    sed '
      :a
        $!N
        s/^\(\(\S\+\) [^\n]\+\)\n\2 /\1 /
        ta
      P
      D
    ' | \
    sed -n '
      s/$/ /
      / package /!{
        h
        s/^\(\S\+\) .*$/Package "\1" is missing./
        p
        g
      }
      / signature /!{
        h
        s/^\(\S\+\) .*$/Signature of "\1" is missing./
        p
        g
      }
      / namcap /!{
        h
        s/^\(\S\+\) .*$/Namcap log of "\1" is missing./
        p
        g
      }
      / needed-libraries /!{
        h
        s/^\(\S\+\) .*$/List of libraries needed by "\1" is missing./
        p
        g
      }
      / provided-libraries /!{
        h
        s/^\(\S\+\) .*$/List of libraries provided by "\1" is missing./
        p
        g
      }
    '
)

if [ -n "${missing_files}" ]; then
  >&2 echo 'The following packages lack a signature, namcap log, package file or list of needed/provided libraries:'
  >&2 echo "${missing_files}"
  exit 3
fi

# check if the signatures are valid
signatures=$(
  find . -maxdepth 1 -name '*.pkg.tar.xz' \
    -printf 'package file %f\n' \
    -exec gpg --batch --status-fd 1 -q --homedir /etc/pacman.d/gnupg --verify '{}.sig' '{}' \; 2> /dev/null
)
if [ -z "$(
  echo "${signatures}" | \
    cut -d' ' -f2 | \
    grep -x 'file\|TRUST_FULLY' | \
    sort | \
    uniq -c | \
    awk '{print $1}' | \
    uniq -d
)" ]; then
  >&2 echo 'Signature(s) is/are not fully trusted:'
  >&2 echo "${signatures}"
  exit 3
fi

# check if the sent packages are the expected ones
packages=$(
  find . -maxdepth 1 -name '*.pkg.tar.xz' -printf '%f\n'
)
package_errors=$(
  {
    # shellcheck disable=SC2086
    printf '%s\n' ${packages} | \
      sed '
        s|^|was_built: |
      '
    # shellcheck disable=SC2016
    {
      printf 'SELECT CONCAT('
        printf '"expected: ",'
        mysql_package_name_query
      printf ')'
      printf ' FROM `binary_packages`'
      mysql_join_binary_packages_architectures
      mysql_join_binary_packages_build_slaves
      mysql_join_binary_packages_repositories
      printf ' WHERE `build_slaves`.`name`=from_base64("%s")' \
        "$(
          printf '%s' "${slave}" | \
            base64 -w0
        )"
      printf ' AND `repositories`.`name`="build-list"'
      printf ';\n'
    } | \
      mysql_run_query
  } | \
    sort -k2 | \
    uniq -u -f1
)

if [ -n "${package_errors}" ]; then
  >&2 echo 'The following packages should have been built but are missing or vice versa:'
  >&2 echo "${package_errors}"
  exit 4
fi

# move namcap.logs
find . -maxdepth 1 -name '*.pkg.tar.xz-namcap.log.gz' -execdir mv '{}' "${build_log_directory}/success/" \;

# insert provided libraries into database
# shellcheck disable=SC2016
{
  for lib_link in 'pl:provides' 'nl:needs'; do
    printf 'CREATE TEMPORARY TABLE `%s` (`pkgfile` VARCHAR(64), `lib` VARCHAR(128));\n' \
      "${lib_link%:*}"
    find . -maxdepth 1 -name '*.pkg.tar.xz.so.'"${lib_link#*:}"'.gz' -execdir zgrep -HF '' '{}' \; | \
      sed -n '
        s,^\./\(.\+\.pkg\.tar\.xz\)\.so\.'"${lib_link#*:}"'\.gz:\([^:]\+\)$,\1\n\2,
        T
        p
      ' | \
      base64_encode_each | \
      sed '
        N
        s/^\(\S\+\)\n\(\S\+\)$/(from_base64("\1"),from_base64("\2")),/
        $s/,$/;/
        2 s/^/INSERT INTO `'"${lib_link%:*}"'` (`pkgfile`,`lib`) VALUES /
      '
    printf 'INSERT IGNORE INTO `install_targets` (`name`)'
    printf ' SELECT DISTINCT `%s`.`lib` FROM `%s`;\n' \
      "${lib_link%:*}" "${lib_link%:*}"
    if [ "${lib_link%:*}" = 'pl' ]; then
      printf 'INSERT IGNORE INTO `install_target_providers` (`package`,`install_target`)'
    else
      printf 'INSERT IGNORE INTO `dependencies` (`dependent`,`depending_on`,`dependency_type`)'
    fi
    printf ' SELECT `binary_packages`.`id`,`install_targets`.`id`'
    if [ "${lib_link%:*}" = 'nl' ]; then
      printf ',`dependency_types`.`id`'
    fi
    printf ' FROM `install_targets`'
    if [ "${lib_link%:*}" = 'nl' ]; then
      printf ' JOIN `dependency_types` ON `dependency_types`.`name`="link"'
    fi
    printf ' JOIN `%s` ON `%s`.`lib`=`install_targets`.`name`' \
      "${lib_link%:*}" "${lib_link%:*}"
    printf ' JOIN `binary_packages`'
    mysql_join_binary_packages_architectures
    mysql_join_binary_packages_build_slaves
    mysql_join_binary_packages_repositories
    printf ' WHERE `build_slaves`.`name`=from_base64("%s")' \
      "$(
        printf '%s' "${slave}" | \
          base64 -w0
      )"
    printf ' AND `repositories`.`name`="build-list"'
    printf ' AND '
    mysql_package_name_query
    printf '=`%s`.`pkgfile`' \
      "${lib_link%:*}"
    printf ';\n'
  done
} | \
  mysql_run_query

# move packages
destination=$(official_or_community "$1.$2.$3.$4" 'staging')

${master_mirror_rsync_command} \
  "${master_mirror_rsync_directory}/i686/${destination}/${destination}.db."* \
  "${master_mirror_rsync_directory}/i686/${destination}/${destination}.files."* \
  .
# shellcheck disable=SC2086
repo-add "${destination}.db.tar.gz" ${packages}
# repo-add -v -s -k "${repo_key}" "${destination}.db.tar.gz" ${packages}

${master_mirror_rsync_command} \
  "${destination}.db."* \
  "${destination}.files."* \
  ./*".pkg.tar.xz" \
  ./*".pkg.tar.xz.sig" \
  "${master_mirror_rsync_directory}/i686/${destination}/"

trigger_mirror_refreshs

for package in ${packages}; do
  remove_old_package_versions 'i686' "${destination}" "${package}"
done

# shellcheck disable=SC2016
{
  printf 'UPDATE `build_assignments`'
  mysql_join_build_assignments_build_slaves
  mysql_join_build_assignments_binary_packages
  mysql_join_binary_packages_repositories '' 'old_repo'
  printf ' SET'
  printf ' `build_assignments`.`is_broken`=0,'
  printf ' `build_assignments`.`priority`=0,'
  printf ' `binary_packages`.`repository`=(SELECT `new_repo`.`id` FROM `repositories` AS `new_repo` WHERE `new_repo`.`name`=from_base64("%s")),' \
    "$(
      printf '%s' "${destination}" | \
        base64 -w0
    )"
  printf ' `binary_packages`.`has_issues`=0,'
  printf ' `binary_packages`.`is_tested`=0'
  printf ' WHERE `build_slaves`.`name`=from_base64("%s")' \
    "$(
      printf '%s' "${slave}" | \
        base64 -w0
    )"
  printf ' AND `old_repo`.`name`="build-list";\n'
  printf 'UPDATE `build_slaves` AS `to_update`'
  printf ' JOIN `build_slaves` AS `current_slave`'
  printf ' ON `to_update`.`currently_building`=`current_slave`.`currently_building`'
  printf ' SET `to_update`.`currently_building`=NULL'
  printf ' WHERE `current_slave`.`name`=from_base64("%s");\n' \
    "$(
      printf '%s' "${slave}" | \
        base64 -w0
    )"
  printf 'CREATE TEMPORARY TABLE `loops_to_delete` (`loop` MEDIUMINT);\n'
  printf 'INSERT INTO `loops_to_delete`'
  printf ' SELECT `build_dependency_loops`.`loop` FROM `build_dependency_loops`'
  mysql_join_build_dependency_loops_binary_packages
  mysql_join_binary_packages_repositories
  printf ' WHERE NOT `repositories`.`name` = "build-list";\n'
  printf 'DELETE FROM `build_dependency_loops` WHERE EXISTS ('
  printf 'SELECT * FROM `loops_to_delete` WHERE `loops_to_delete`.`loop`=`build_dependency_loops`.`loop`'
  printf ');\n'
  printf 'DROP TABLE `loops_to_delete`;\n'
} | \
  mysql_run_query