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

# shellcheck disable=SC2119

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

# this script shall host all the tests that are too slow to be run on
# the buildmaster (thus, it runs asynchronously and must therefor be
# resilient against transient states of the buildmaster):
# - check sanity of git
# TODO:
# - check for differences of dependencies between mysql and git
# - check for differences of dependencies between mysql and packages
# - check for installability of packages

if [ $# -ge 1 ] && [ "x$1" = 'x-n' ]; then
  >&2 echo 'not joining irc'
  irc=false
  repair_dependencies=false
  shift
elif [ $# -ge 1 ] && [ "x$1" = 'x-r' ]; then
  >&2 echo 'repair all wrong dependencies (remove superfluid, add missing)'
  irc=false
  repair_dependencies=true
  shift
else
  irc=true
  repair_dependencies=false
  # shellcheck disable=SC2016
  if [ $# -ne 0 ]; then
    >&2 echo 'usage: nit-picker [-n|-r [$single_test]]'
    >&2 echo '  -n: do not join irc'
    >&2 echo '  -r: remove superfluid dependencies in the database'
    >&2 echo '  $single_test: only execute the given test'
    exit 1
  fi
fi

clean_up() {
  rm -rf --one-file-system "${tmp_dir}"
}

tmp_dir=$(mktemp -d 'tmp.nit-picker.XXXXXXXXXX' --tmpdir)
trap 'clean_up' EXIT

if ${irc}; then
  if pgrep -x ii; then
    >&2 echo 'ii is already running - this will not work'
    exit 1
  fi

  rm -rf --one-file-system "${irc_dir}"

  ii -s irc.freenode.net -n nit-picker -f nit-picker >/dev/null 2>&1 &
  ii_pid=$!

  clean_up() {
    rm -rf --one-file-system "${tmp_dir}"
    kill "${ii_pid}"
  }

  # wait for nickserv complaint
  while ! grep -qF 'This nickname is registered. Please choose a different nickname' "${irc_dir}/nickserv/out"; do
    sleep 1
  done

  # register
  printf 'identify %s\n' "${irc_password}" | \
    sponge "${irc_dir}/nickserv/in"

  # wait for registering to succeed
  while ! grep -qF 'You are now identified for' "${irc_dir}/nickserv/out"; do
    sleep 1
  done

  # join channel
  echo '/j #archlinux32' | \
    sponge "${irc_dir}/in"

  while [ ! -f "${irc_dir}/#archlinux32/out" ]; do
    sleep 1
  done

fi

# shellcheck disable=SC2120
local_irc_say() {
  if ${irc}; then
    if printf 'SHOW STATUS LIKE "Slave_running"' | mysql_run_query | cut -f2 | grep -qxF 'ON'; then
      irc_say "$@"
    else
      printf 'The replication slave is not running.\n' \
      | irc_say 'deep42thought'
      irc_say 'deep42thought'
    fi
  else
    sed 's/^/irc: /'
  fi
}

mysql_load_min_and_max_versions

build_duration_check=0

while pgrep -x ii >/dev/null \
|| ! ${irc}; do
  if [ "$(date +%s)" -gt "$((build_duration_check + 60*60*24))" ]; then
    check_build_duration=true
    build_duration_check=$(date +%s)
  else
    check_build_duration=false
  fi
  if [ $# -eq 0 ]; then
    # shellcheck disable=SC2016
    {
      if ! "${repair_dependencies}"; then
        printf 'SELECT DISTINCT'
        printf ' "commit",'
        printf '`git_repositories`.`name`,'
        printf '`git_repositories`.`head`,'
        printf '`package_sources`.`git_revision`'
        printf ' FROM `package_sources`'
        mysql_join_package_sources_upstream_repositories
        mysql_join_upstream_repositories_git_repositories
        printf ';\n'

        printf 'SELECT DISTINCT'
        printf ' "commit",'
        printf '"archlinux32",'
        # shellcheck disable=SC2154
        printf '"%s",' \
          "${repo_heads__archlinux32}"
        printf '`package_sources`.`mod_git_revision`'
        printf ' FROM `package_sources`'
        printf ';\n'

        printf 'SELECT DISTINCT'
        printf ' "binary-signature",'
        mysql_package_name_query
        printf ' FROM `binary_packages`'
        mysql_join_binary_packages_architectures
        printf ' LEFT'
        mysql_join_binary_packages_compressions
        mysql_join_binary_packages_binary_packages_in_repositories
        mysql_join_binary_packages_in_repositories_repositories
        printf ' WHERE `repositories`.`is_on_master_mirror`'
        printf ';\n'

        if "${check_build_duration}"; then
          printf 'SELECT'
          printf ' "build-duration",'
          printf '`build_slaves`.`name`'
          printf ' FROM `build_slaves`'
          printf ';\n'
        fi
      fi

      printf 'SELECT DISTINCT'
      printf ' "binary-dependencies",'
      mysql_package_name_query
      printf ' FROM `binary_packages`'
      mysql_join_binary_packages_architectures
      printf ' LEFT'
      mysql_join_binary_packages_compressions
      mysql_join_binary_packages_binary_packages_in_repositories
      mysql_join_binary_packages_in_repositories_repositories
      printf ' WHERE `repositories`.`is_on_master_mirror`'
      printf ';\n'
    } | \
      mysql_run_query | \
      tr '\t' ' ' | \
      shuf
  else
    printf '%s\n' "$*"
  fi | \
    while read -r action parameters; do
      if ${irc} && ! pgrep -x ii >/dev/null; then
        break
      fi
      case "${action}" in
        'commit') # check whether a given commit is present in the git repo
          git_repo="${parameters%% *}"
          git_rev="${parameters#${git_repo} }"
          git_head="${git_rev%% *}"
          git_rev="${git_rev#${git_head} }"
          # shellcheck disable=SC2016
          eval "$(
            printf 'git_dir="${repo_paths__%s}"\n' \
              "${git_repo}"
          )"
          # shellcheck disable=SC2154
          if ! git -C "${git_dir}" cat-file -t "${git_rev}" 2> /dev/null | \
            grep -qxF 'commit'; then
            git -C "${git_dir}" fetch --all -p >/dev/null 2>&1
            if ! git -C "${git_dir}" cat-file -t "${git_rev}" 2> /dev/null | \
              grep -qxF 'commit'; then
              printf 'commit %s is missing from repository %s\n' \
                "${git_rev}" \
                "${git_repo}" \
              | local_irc_say
            fi
          fi
          # shellcheck disable=SC2154
          if ! git -C "${git_dir}" cat-file -t "${git_head}" 2> /dev/null | \
            grep -qxF 'commit'; then
            git -C "${git_dir}" fetch --all -p >/dev/null 2>&1
            if ! git -C "${git_dir}" cat-file -t "${git_head}" 2> /dev/null | \
              grep -qxF 'commit'; then
              printf 'commit %s is missing from repository %s\n' \
                "${git_head}" \
                "${git_repo}" \
              | local_irc_say
            fi
          fi
          # shellcheck disable=SC2154
          if ! git -C "${git_dir}" merge-base --is-ancestor "${git_rev}" "${git_head}" 2> /dev/null; then
            current_git_head=$(
              # shellcheck disable=SC2016
              {
                printf 'SELECT DISTINCT'
                printf ' `git_repositories`.`head`'
                printf ' FROM `git_repositories`'
                printf ' WHERE `git_repositories`.`name`=from_base64("%s");\n' \
                  "$(
                    printf '%s' "${git_repo}" \
                    | base64 -w0
                  )"
              } \
              | mysql_run_query
            )
            if ! git -C "${git_dir}" merge-base --is-ancestor "${git_rev}" "${current_git_head}" 2> /dev/null; then
              git -C "${git_dir}" fetch --all -p >/dev/null 2>&1
              if ! git -C "${git_dir}" merge-base --is-ancestor "${git_rev}" "${current_git_head}" 2> /dev/null; then
                printf 'commit %s is not an ancestor of HEAD %s in repository %s\n' \
                  "${git_rev}" \
                  "${current_git_head}" \
                  "${git_repo}" \
                | local_irc_say
              fi
            fi
          fi
        ;;
        'binary-dependencies')
          if ! ${master_mirror_rsync_command} \
            "${master_mirror_rsync_directory}/pool/${parameters}" \
            "${tmp_dir}/"; then
            rm -f "${tmp_dir}/${parameters}"
            continue
          fi
          extract_dependencies_from_package \
            "${tmp_dir}/${parameters}" \
          > "${tmp_dir}/pkg-deps"
          # shellcheck disable=SC2016
          {
            printf 'SELECT'
            printf ' `dependency_types`.`name`,'
            printf '`install_targets`.`name`,'
            printf '`dependencies`.`version_relation`,'
            printf '`versions`.`epoch`,'
            printf '`versions`.`version`'
            printf ' FROM `binary_packages`'
            mysql_join_binary_packages_dependencies
            mysql_join_binary_packages_architectures
            printf ' LEFT'
            mysql_join_binary_packages_compressions
            mysql_join_dependencies_dependency_types
            mysql_join_dependencies_versions
            mysql_join_dependencies_install_targets
            printf ' WHERE '
            mysql_package_name_query
            printf '="%s"' \
              "${parameters}"
            printf ' AND `dependency_types`.`name` IN ("run","make","check")'
            printf ';\n'
          } \
          | mysql_run_query \
          | tr '\t' ' ' \
          | sort -u \
          > "${tmp_dir}/db-deps"
          if ! diff -q "${tmp_dir}/db-deps" "${tmp_dir}/pkg-deps"; then
            if "${repair_dependencies}"; then
              cat "${tmp_dir}/db-deps" "${tmp_dir}/pkg-deps" "${tmp_dir}/pkg-deps" \
              | sort \
              | uniq -u \
              | sed 's@^@'"${parameters}"' @' \
              | tr ' ' '\t' \
              >> "${tmp_dir}/remove-those-dependencies"
              cat "${tmp_dir}/db-deps" "${tmp_dir}/db-deps" "${tmp_dir}/pkg-deps" \
              | sort \
              | uniq -u \
              | sed 's@^@'"${parameters}"' @' \
              | tr ' ' '\t' \
              >> "${tmp_dir}/add-those-dependencies"
            else
              build_date=$(
                bsdtar -Oxf "${tmp_dir}/${parameters}" '.PKGINFO' \
                | sed '
                  s/^builddate = //
                  t
                  d
                '
              )
              build_date=$(
                date -I -d@"${build_date}"
              )
              printf 'dependencies of %s (built on %s) differ between the package and our database\n' \
                "${parameters}" \
                "${build_date}" \
              | local_irc_say
              if ! ${irc}; then
                diff -u --color "${tmp_dir}/db-deps" "${tmp_dir}/pkg-deps" || true
              fi
              if [ $# -eq 0 ]; then
                sleep 60
              fi
            fi
          fi
          rm \
            "${tmp_dir}/${parameters}" \
            "${tmp_dir}/db-deps" \
            "${tmp_dir}/pkg-deps"
        ;;
        'binary-signature')
          if ! ${master_mirror_rsync_command} \
            "${master_mirror_rsync_directory}/pool/${parameters}" \
            "${master_mirror_rsync_directory}/pool/${parameters}.sig" \
            "${tmp_dir}/"; then
            rm -f "${tmp_dir}/${parameters}" "${tmp_dir}/${parameters}.sig"
            continue
          fi
          unset error_message
          if ! gpg_output=$(
            gpg --batch --status-fd 1 -q --homedir /etc/pacman.d/gnupg \
              --verify "${tmp_dir}/${parameters}.sig" "${tmp_dir}/${parameters}" \
              2>/dev/null
          ); then
            sleep 1
            if ! gpg_output=$(
              gpg --batch --status-fd 1 -q --homedir /etc/pacman.d/gnupg \
                --verify "${tmp_dir}/${parameters}.sig" "${tmp_dir}/${parameters}" \
                2>/dev/null
            ); then
              error_message="package ${parameters} has an invalid signature."
            fi
          fi
          if [ -z "${error_message}" ]; then
            gpg_key=$(
              printf '%s\n' "${gpg_output}" \
              | sed '
                s/^\[GNUPG:] KEY_CONSIDERED \([0-9A-F]\{40\}\) 0$/\1/
                t
                d
              ' \
              | sort -u
            )
            if [ -z "${gpg_key}" ]; then
              error_message="cannot find pgp_key of package ${parameters}."
            fi
          fi
          if [ -z "${error_message}" ]; then
            for expiration in $(
              gpg --batch --homedir /etc/pacman.d/gnupg --with-colons --list-keys "0x${gpg_key}" \
              2>/dev/null \
              | grep '^\(sub\|pub\):' \
              | cut -d: -f7
            ); do
              expiration_days=$(((expiration - $(date +%s))/24/60/60))
              if [ ${expiration_days} -lt 100 ]; then
                error_message=$(
                  printf 'signing key %s (from %s) for package %s expires on %s (in %s < 100 days).\n' \
                  "${gpg_key}" \
                  "$(
                    gpg --batch --homedir /etc/pacman.d/gnupg --with-colons --list-keys "0x${gpg_key}" \
                    2>/dev/null \
                    | grep '^\(uid\):' \
                    | cut -d: -f10
                  )" \
                  "${parameters}" \
                  "$(date -I -d@"${expiration}")" \
                  "${expiration_days}"
                )
                break
              fi
            done
          fi
          if [ -n "${error_message}" ]; then
            printf '%s\n' "${error_message}" \
            | local_irc_say
            if [ $# -eq 0 ]; then
              sleep 60
            fi
          fi
          rm \
            "${tmp_dir}/${parameters}" \
            "${tmp_dir}/${parameters}.sig"
        ;;
        'build-duration')
          infos=$(
            # shellcheck disable=SC2016
            {
              printf 'SELECT'
              printf '`persons`.`name`,'
              printf '`build_slaves`.`name`,'
              printf '`architectures`.`name`,'
              printf '`package_sources`.`pkgbase`,'
              printf 'UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP('
                printf 'MAX('
                  printf 'IF('
                    printf '`ssh_log`.`action`="get-assignment",'
                    printf '`ssh_log`.`date`,'
                    printf 'NULL'
                  printf ')'
                printf ')'
              printf ')'
              printf ' FROM `build_slaves`'
              mysql_join_build_slaves_build_assignments
              mysql_join_build_assignments_architectures
              mysql_join_build_assignments_package_sources
              mysql_join_build_slaves_ssh_keys
              mysql_join_ssh_keys_persons
              mysql_join_build_slaves_ssh_log
              printf ' WHERE `ssh_log`.`date`>=ADDDATE(NOW(),"-7 00:00:00")'
              printf ' AND `build_slaves`.`name`=from_base64("%s")' \
                "$(
                  printf '%s' "${parameters}" \
                  | base64 -w0
                )"
              printf ' GROUP BY `build_slaves`.`id`'
              printf ';\n'
            } \
            | mysql_run_query \
            | tr '\t' ' '
          )
          if [ -z "${infos}" ]; then
            continue
          fi
          if [ "${infos##* }" -lt $((60*60*24)) ]; then
            continue
          fi
          printf '%s %s\n' \
            "${infos% *}" \
            "$(
              date -d"@${infos##* }" +'%-j %H:%M:%S'
            )" \
          | awk '{print $1 ": your slave " $2 " builds " $3 "/" $4 " for more than a day, now (" ($5-1) " day(s) " $6 ")"}' \
          | local_irc_say
          if [ $# -eq 0 ]; then
            sleep 60
          fi
        ;;
        *)
          >&2 printf 'action "%s" is not yet implemented ...\n' "${action}"
        ;;
      esac
    done
  if [ $# -ge 1 ] || "${repair_dependencies}"; then
    break
  fi
  sleep 120
done

{
  if [ -s "${tmp_dir}/remove-those-dependencies" ]; then
    # shellcheck disable=SC2016
    {
      printf 'CREATE TEMPORARY TABLE `rd`('
        printf '`pf` VARCHAR(128),'
        printf '`dt` VARCHAR(32),'
        printf '`it` VARCHAR(128),'
        printf '`vr` VARCHAR(2),'
        printf '`e` MEDIUMINT,'
        printf '`v` VARCHAR(64)'
      printf ');\n'
      printf 'LOAD DATA LOCAL INFILE "%s" INTO TABLE `rd`(`pf`,`dt`,`it`,`vr`,`e`,`v`);\n' \
        "${tmp_dir}/remove-those-dependencies"
      printf 'SELECT `d`.`id` FROM `binary_packages`'
      mysql_join_binary_packages_architectures
      printf ' LEFT'
      mysql_join_binary_packages_compressions
      printf ' JOIN `rd`'
      printf ' ON `rd`.`pf`='
      mysql_package_name_query
      mysql_join_binary_packages_dependencies '' 'd'
      printf ' AND `d`.`version_relation`=`rd`.`vr`'
      mysql_join_dependencies_install_targets 'd' 'it'
      printf ' AND `it`.`name`=`rd`.`it`'
      mysql_join_dependencies_versions 'd' 'v'
      printf ' AND `v`.`epoch`=`rd`.`e`'
      printf ' AND `v`.`version`=`rd`.`v`'
      mysql_join_dependencies_dependency_types 'd' 'dt'
      printf ' AND `dt`.`name`=`rd`.`dt`'
      printf ';\n'
    } \
    | mysql_run_query \
    | sed '
      1 i DELETE `dependencies` FROM `dependencies` WHERE `dependencies`.`id` IN (
      $! s/$/,/
      $ a );
    '
  fi
  # shellcheck disable=SC2016
  if [ -s "${tmp_dir}/add-those-dependencies" ]; then
    rsync "${tmp_dir}/add-those-dependencies" 'buildmaster:/tmp/add-those-dependencies' >/dev/null 2>&1

    printf 'CREATE TEMPORARY TABLE `ad`('
      printf '`pf` VARCHAR(128),'
      printf '`dt` VARCHAR(32),'
      printf '`it` VARCHAR(128),'
      printf '`vr` VARCHAR(2),'
      printf '`e` MEDIUMINT,'
      printf '`v` VARCHAR(64)'
    printf ');\n'

    printf 'LOAD DATA LOCAL INFILE "%s" INTO TABLE `ad`(`pf`,`dt`,`it`,`vr`,`e`,`v`);\n' \
      '/tmp/add-those-dependencies'

    printf 'INSERT IGNORE INTO `install_targets`('
      printf '`name`'
    printf ')'
    printf ' SELECT'
    printf ' `ad`.`it`'
    printf ' FROM `ad`;\n'

    printf 'INSERT IGNORE INTO `versions`('
      printf '`epoch`,'
      printf '`version`'
    printf ')'
    printf ' SELECT'
    printf ' `ad`.`e`,'
    printf '`ad`.`v`'
    printf ' FROM `ad`;\n'

    printf 'INSERT IGNORE INTO `dependencies`('
      printf '`dependent`,'
      printf '`depending_on`,'
      printf '`dependency_type`,'
      printf '`version`,'
      printf '`version_relation`'
    printf ')'
    printf 'SELECT'
    printf ' `binary_packages`.`id`,'
    printf '`install_targets`.`id`,'
    printf '`dependency_types`.`id`,'
    printf '`versions`.`id`,'
    printf '`ad`.`vr`'
    printf ' FROM `binary_packages`'
    mysql_join_binary_packages_architectures
    printf ' LEFT'
    mysql_join_binary_packages_compressions
    printf ' JOIN `ad`'
    printf ' ON `ad`.`pf`='
    mysql_package_name_query
    printf ' JOIN `install_targets`'
    printf ' ON `install_targets`.`name`=`ad`.`it`'
    printf ' JOIN `versions`'
    printf ' ON `versions`.`epoch`=`ad`.`e`'
    printf ' AND `versions`.`version`=`ad`.`v`'
    printf ' JOIN `dependency_types`'
    printf ' ON `dependency_types`.`name`=`ad`.`dt`'
    printf ';\n'
  fi
} \
| ifne ssh buildmaster 'mysql buildmaster
  rm -f "/tmp/add-those-dependencies"
'