summaryrefslogtreecommitdiff
path: root/bin/delete-packages
blob: 3a615aab8912d65aabe1247bb98afcdfd4db99f2 (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
#!/bin/sh

# delete obsolete binary packages

# 1] Condition for deleting a package A is that:
#   a) nothing on the build-list (make|check|)depends on A and
#   b) no built package B which is not being deleted depends on A

# "Package x depends on package y" means, that something needed by x
# is provided by y and no other package which will not be deleted.

# shellcheck disable=SC2039,SC2119,SC2120

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

# shellcheck disable=SC2016
usage() {
  >&2 echo ''
  >&2 echo 'delete-packages [options]:'
  >&2 echo ' delete obsolete binary packages.'
  >&2 echo ''
  >&2 echo 'possible options:'
  >&2 echo '  -b|--build-support $package-list:'
  >&2 echo '    Only delete the given packages from [build-support].'
  >&2 echo '    $package-list contains one package per line of'
  >&2 echo '    the form "$repo_arch $pkgfile".'
  >&2 echo '    conflicts -f; only accepted once'
  >&2 echo '  -f|--force $package:'
  >&2 echo '    Only delete the selected packages (given by'
  >&2 echo '    $repo_arch/$repo/$pkgname) - but delete'
  >&2 echo '    in any case. conflicts -b'
  >&2 echo '  -h|--help:'
  >&2 echo '    Show this help and exit.'
  >&2 echo '  -i|--ignore-insanity:'
  >&2 echo '    Do not abort when insane.'
  >&2 echo '  -n|--no-action:'
  >&2 echo '    Only print what would be deleted.'
  >&2 echo '  -w|--wait:'
  >&2 echo '    If necessary, wait for lock blocking.'
  [ -z "$1" ] && exit 1 || exit "$1"
}

eval set -- "$(
  getopt -o b:f:hinw \
    --long build-support: \
    --long force: \
    --long help \
    --long ignore-insanity \
    --long no-action \
    --long wait \
    -n "$(basename "$0")" -- "$@" || \
  echo usage
)"

block_flag='-n'
build_support=''
force_packages=''
ignore_insanity=false
no_action=false

while true
do
  case "$1" in
    -b|--build-support)
      if [ -n "${build_support}" ]; then
        >&2 printf 'Option given multiple times: %s\n' "$1"
        usage
      fi
      shift
      build_support="$1"
    ;;
    -f|--force)
      shift
      force_packages="${force_packages} $1"
    ;;
    -h|--help)
      usage 0
    ;;
    -i|--ignore-insanity)
      ignore_insanity=true
    ;;
    -n|--no-action)
      no_action=true
    ;;
    -w|--wait)
      block_flag=''
    ;;
    --)
      shift
      break
    ;;
    *)
      >&2 echo 'Whoops, forgot to implement option "'"$1"'" internally.'
      exit 42
    ;;
  esac
  shift
done

if [ -n "${build_support}" ] && \
  [ -n "${force_packages}" ]; then
  >&2 echo 'Conflicting flags: -b & -f.'
  usage
fi

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

# Create a lock file and a trap.

if ! ${no_action}; then

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

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

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

fi

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

export TMPDIR="${tmp_dir}"

# shellcheck disable=SC2016
{
  printf 'CREATE TEMPORARY TABLE `bpir_to_delete` (`id` BIGINT, UNIQUE KEY (`id`));\n'
  printf 'INSERT IGNORE INTO `bpir_to_delete`'
  if [ -n "${force_packages}" ]; then
    printf ' SELECT'
    printf ' `binary_packages_in_repositories`.`id`'
    printf ' FROM `binary_packages_in_repositories`'
    mysql_join_binary_packages_in_repositories_binary_packages
    mysql_join_binary_packages_in_repositories_repositories
    mysql_join_repositories_architectures
    printf ' WHERE CONCAT('
      printf '`architectures`.`name`,"/",'
      printf '`repositories`.`name`,"/",'
      printf '`binary_packages`.`pkgname`'
    printf ') IN ('
    # shellcheck disable=SC2086
    printf '%s\n' ${force_packages} | \
      base64_encode_each | \
      sed '
        s/^/(from_base64("/
        s/$/")),/
        $ s/,$/);/
      '
  elif [ -n "${build_support}" ]; then
    printf ' SELECT DISTINCT `binary_packages_in_repositories`.`id`'
    printf ' FROM `binary_packages_in_repositories`'
    mysql_join_binary_packages_in_repositories_binary_packages
    mysql_join_binary_packages_in_repositories_repositories
    mysql_join_binary_packages_architectures
    mysql_join_repositories_architectures '' 'r_a'
    printf ' WHERE `repositories`.`name`="build-support"'
    printf ' AND CONCAT(`r_a`.`name`," ",'
    mysql_package_name_query
    printf ') IN ('
    base64_encode_each < "${build_support}" | \
      sed '
        s/^/from_base64("/
        s/$/"),/
        $ s/,$/);/
      '
  else
    printf ' SELECT DISTINCT `binary_packages_in_repositories`.`id`'
    printf ' FROM `binary_packages_in_repositories`'
    mysql_join_binary_packages_in_repositories_binary_packages
    mysql_join_binary_packages_in_repositories_repositories
    printf ' LEFT' # should not be necessary, but is formally more correct
    mysql_join_binary_packages_install_target_providers
    printf ' WHERE `binary_packages_in_repositories`.`is_to_be_deleted`'
    printf ' AND `repositories`.`is_on_master_mirror`'
    printf ' AND NOT EXISTS ('
      # no packages depending on that one exist
      printf 'SELECT 1'
      printf ' FROM `dependencies`'
      mysql_join_dependencies_dependency_types
      printf ' AND `dependency_types`.`relevant_for_binary_packages`'
      mysql_join_dependencies_binary_packages '' 'd_bp'
      mysql_join_binary_packages_binary_packages_in_repositories 'd_bp' 'd_bpir'
      # deliberately break dependencies of deletion-list packages
      printf ' AND NOT `d_bpir`.`is_to_be_deleted`'
      mysql_join_binary_packages_in_repositories_repositories 'd_bpir' 'd_r'
      # this is deliberately less restrict than `d_r`.`is_on_master_mirror`
      # shellcheck disable=SC2154
      printf ' AND `d_r`.`stability` NOT IN (%s,%s)' \
        "${repository_stability_ids__forbidden}" \
        "${repository_stability_ids__virtual}"
      printf ' WHERE `install_target_providers`.`install_target`=`dependencies`.`depending_on`'
      printf ' AND `d_r`.`architecture`=`repositories`.`architecture`'
      printf ' AND NOT EXISTS ('
        printf 'SELECT 1'
        printf ' FROM `binary_packages` AS `s_bp`'
        mysql_join_binary_packages_binary_packages_in_repositories 's_bp' 's_bpir'
        printf ' AND NOT `s_bpir`.`is_to_be_deleted`'
        mysql_join_binary_packages_install_target_providers 's_bp' 's_itp'
        mysql_join_binary_packages_in_repositories_repositories 's_bpir' 's_r'
        printf ' AND `s_r`.`is_on_master_mirror`'
        printf ' JOIN `repository_stability_relations`'
          printf ' ON `repository_stability_relations`.`more_stable`=`s_r`.`stability`'
        printf ' WHERE `s_itp`.`install_target`=`install_target_providers`.`install_target`'
        printf ' AND `s_r`.`architecture`=`repositories`.`architecture`'
        printf ' AND `repository_stability_relations`.`less_stable`=`d_r`.`stability`'
      printf ')'
    printf ');\n'
  fi

  # first enter all potentially to-be-deleted binary_packages into the table
  printf 'CREATE TEMPORARY TABLE `bp_to_delete` (`id` BIGINT, UNIQUE KEY (`id`));\n'
  printf 'INSERT IGNORE INTO `bp_to_delete`'
  printf ' SELECT `binary_packages_in_repositories`.`package`'
  printf ' FROM `bpir_to_delete`'
  printf ' JOIN `binary_packages_in_repositories`'
  printf ' ON `bpir_to_delete`.`id`=`binary_packages_in_repositories`.`id`;\n'

  # then remove all bp_to_delete which are actually still needed
  printf 'DELETE `bp_to_delete`'
  printf ' FROM `bp_to_delete`'
  printf ' JOIN `binary_packages`'
  printf ' ON `binary_packages`.`id`=`bp_to_delete`.`id`'
  mysql_join_binary_packages_binary_packages_in_repositories
  printf ' WHERE NOT EXISTS ('
    printf 'SELECT 1'
    printf ' FROM `bpir_to_delete`'
    printf ' WHERE `bpir_to_delete`.`id`=`binary_packages_in_repositories`.`id`'
  printf ');\n'

  printf 'SELECT DISTINCT'
  printf ' "repo",'
  printf '`architectures`.`name`,'
  printf '`repositories`.`name`'
  printf ' FROM `bpir_to_delete`'
  printf ' JOIN `binary_packages_in_repositories`'
  printf ' ON `bpir_to_delete`.`id`=`binary_packages_in_repositories`.`id`'
  mysql_join_binary_packages_in_repositories_binary_packages
  mysql_join_binary_packages_in_repositories_repositories
  mysql_join_repositories_architectures
  printf ';\n'

  printf 'SELECT'
  printf ' "package",'
  printf '`architectures`.`name`,'
  printf '`repositories`.`name`,'
  printf '`binary_packages`.`pkgname`'
  printf ' FROM `bpir_to_delete`'
  printf ' JOIN `binary_packages_in_repositories`'
  printf ' ON `bpir_to_delete`.`id`=`binary_packages_in_repositories`.`id`'
  mysql_join_binary_packages_in_repositories_binary_packages
  mysql_join_binary_packages_in_repositories_repositories
  mysql_join_repositories_architectures
  printf ';\n'

  printf 'SELECT "package-file",CONCAT('
  printf '`r_a`.`name`,"/",'
  printf '`repositories`.`name`,"/",'
  mysql_package_name_query
  printf ') FROM `bpir_to_delete`'
  printf ' JOIN `binary_packages_in_repositories`'
  printf ' ON `bpir_to_delete`.`id`=`binary_packages_in_repositories`.`id`'
  mysql_join_binary_packages_in_repositories_binary_packages
  mysql_join_binary_packages_in_repositories_repositories
  mysql_join_binary_packages_architectures
  mysql_join_repositories_architectures '' 'r_a'
  printf ';\n'

  printf 'SELECT "package-in-repository-id",`bpir_to_delete`.`id`'
  printf ' FROM `bpir_to_delete`'
  printf ';\n'

  printf 'SELECT "package-file",CONCAT('
  printf '"pool/",'
  mysql_package_name_query
  printf ') FROM `bp_to_delete`'
  printf ' JOIN `binary_packages`'
  printf ' ON `bp_to_delete`.`id`=`binary_packages`.`id`'
  mysql_join_binary_packages_architectures
  printf ';\n'

  # we do not need the ids of the to-be-deleted binary_packages, because
  # they stay in the database and are merely moved to the deletion-list
} | \
  mysql_run_query | \
  sed '
    y/\t/ /
    /^repo /{
      s/^\S\+ //
      w '"${tmp_dir}"'/repositories
      d
    }
    /^package /{
      s/^\S\+ //
      w '"${tmp_dir}"'/packages
      d
    }
    /^package-file /{
      s/^\S\+ //
      w '"${tmp_dir}"'/package-files
      s/$/.sig/
      w '"${tmp_dir}"'/package-files
      d
    }
    /^package-in-repository-id /{
      s/^\S\+ //
      w '"${tmp_dir}"'/package-in-repository-ids
      d
    }
  '

if [ -n "${build_support}" ] && \
  [ -w "${build_support}" ] && \
  [ ! -p "${build_support}" ]; then
  sed -n '
    \,^pool/, d
    s/\.sig$//
    T
    p
  ' "${tmp_dir}/package-files" > \
    "${build_support}"
fi

if [ ! -s "${tmp_dir}/packages" ]; then
  printf 'Nothing to delete.\n'
  exit
fi

mkdir "${tmp_dir}/repos"

while read -r arch repo; do
  mkdir -p "${tmp_dir}/repos/${arch}"
  failsafe_rsync \
    "${master_mirror_rsync_directory}/${arch}/${repo}/${repo}.db.tar.gz" \
    "${master_mirror_rsync_directory}/${arch}/${repo}/${repo}.files.tar.gz" \
    "${tmp_dir}/repos/${arch}"

  # shellcheck disable=SC2046
  repo-remove "${tmp_dir}/repos/${arch}/${repo}.db.tar.gz" \
    $(
      grep "^$(str_to_regex "${arch} ${repo}") " "${tmp_dir}/packages" | \
        cut -d' ' -f3
    )
  if ! ${no_action}; then
    recompress_gz \
      "${tmp_dir}" \
      "${tmp_dir}/repos/${arch}/${repo}."*".tar.gz" \
      "${tmp_dir}/repos/${arch}/${repo}."*".tar.gz.old"
    failsafe_rsync \
      "${tmp_dir}/repos/${arch}/${repo}.db."* \
      "${tmp_dir}/repos/${arch}/${repo}.files."* \
      "${master_mirror_rsync_directory}/${arch}/${repo}/"
  fi
done < \
  "${tmp_dir}/repositories"

if ${no_action}; then
  printf 'Now, I would remove the packages from the database and delete the following files from the master mirror:\n'
  sed '
    s,^,  ,
  ' "${tmp_dir}/package-files"
  exit
fi

# shellcheck disable=SC2016
{
  printf 'CREATE TEMPORARY TABLE `bpir_to_delete` (`id` BIGINT, UNIQUE KEY (`id`));\n'
  printf 'LOAD DATA LOCAL INFILE "%s" INTO TABLE `bpir_to_delete`;\n' \
    "${tmp_dir}/package-in-repository-ids"

  printf 'UPDATE `binary_packages_in_repositories`'
  printf ' JOIN `bpir_to_delete`'
  printf ' ON `bpir_to_delete`.`id`=`binary_packages_in_repositories`.`id`'
  mysql_join_binary_packages_in_repositories_repositories
  printf ' JOIN `repositories` AS `d_r`'
  printf ' ON `d_r`.`stability`=%s' \
    "${repository_stability_ids__forbidden}"
  printf ' AND `d_r`.`architecture`=`repositories`.`architecture`'
  printf ' SET `binary_packages_in_repositories`.`repository`=`d_r`.`id`'
  # shellcheck disable=SC2154
  printf ' WHERE `repositories`.`stability`!=%s;\n' \
    "${repository_stability_ids__standalone}"

  printf 'DELETE `binary_packages_in_repositories`'
  printf ' FROM `binary_packages_in_repositories`'
  printf ' JOIN `bpir_to_delete`'
  printf ' ON `bpir_to_delete`.`id`=`binary_packages_in_repositories`.`id`'
  mysql_join_binary_packages_in_repositories_repositories
  printf ' WHERE `repositories`.`stability`!=%s;\n' \
    "${repository_stability_ids__forbidden}"

  printf 'DELETE `binary_packages`'
  printf ' FROM `binary_packages`'
  printf ' WHERE NOT EXISTS ('
    printf 'SELECT 1'
    printf ' FROM `binary_packages_in_repositories`'
    printf ' WHERE `binary_packages_in_repositories`.`package`=`binary_packages`.`id`'
  printf ');\n'
} | \
  mysql_run_query

sed '
  s/^/rm "/
  s/$/"/
' "${tmp_dir}/package-files" | \
  failsafe_sftp