summaryrefslogtreecommitdiff
path: root/bin/copy-to-build-support
blob: 82acc50a52803844eea160575f1b16e01e088922 (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
#!/bin/sh

# copy the given package(s) into build-support

# shellcheck disable=SC2119,SC2120

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

# TODO: allow to introduce a manually built package into build-support

# TODO: remove hard-coded package suffixes

# shellcheck disable=SC2016
usage() {
  >&2 echo ''
  >&2 echo 'copy-to-build-support [options] package-list:'
  >&2 echo '  copy the packages listed in package-list into [build-support]'
  >&2 echo ''
  >&2 echo 'possible options:'
  >&2 echo '  -h|--help:'
  >&2 echo '    Show this help and exit.'
  >&2 echo '  -w|--wait:'
  >&2 echo '    Wait for lock if necessary.'
  >&2 echo ''
  >&2 echo 'lines in package-list should contain:'
  >&2 echo '$repo_arch $package_file_name'
  [ -z "$1" ] && exit 1 || exit "$1"
}

eval set -- "$(
  getopt -o hw \
    --long help \
    --long wait \
    -n "$(basename "$0")" -- "$@" || \
    echo usage
  )"

wait_for_lock='-n'

while true
do
  case "$1" in
    -h|--help)
      usage 0
    ;;
    -w|--wait)
      wait_for_lock=''
    ;;
    --)
      shift
      break
    ;;
    *)
      >&2 echo 'Whoops, forgot to implement option "'"$1"'" internally.'
      exit 42
    ;;
  esac
  shift
done

if [ "$#" -ne 1 ]; then
  >&2 echo 'No package-list was given.'
  usage
fi

if [ ! -r "$1" ]; then
  >&2 printf 'Package-list "%s" is not readable.\n' "$1"
  usage
fi

exec 9> "${sanity_check_lock_file}"
# shellcheck disable=SC2086
verbose_flock -s ${wait_for_lock} 9

exec 8> "${package_database_lock_file}"
# shellcheck disable=SC2086
verbose_flock ${wait_for_lock} 8

# shellcheck disable=SC2086
if intentions_left ${wait_for_lock}; then
  >&2 echo 'come back (shortly) later - There are still intentions in the queue.'
  exit 1
fi

tmp_dir=$(mktemp -d "${work_dir}/tmp.copy-to-build-support.0.XXXXXXXXXX")
trap 'rm -rf --one-file-system "${tmp_dir}"' EXIT

export TMPDIR="${tmp_dir}"

sed -n '
  s/ \S\+/\0 \0/
  T
  s/\.pkg\.tar\.zst$//
  T
  s/\(-[0-9]\+\)\(-[^- ]\+\)$/\1.0\2/
  s/-\([^-: ]\+\)\(\(-[^- ]\+\)\{2\}\)$/-0:\1\2/
  s/-\([^-: ]\+\):\([^-: ]\+\)-\([^-. ]\+\).\([^-. ]\+\)-\([^- ]\+\)$/ \1 \2 \3 \4 \5/
  p
' "$1" | \
  while read -r repo_arch package pkgname epoch pkgver pkgrel sub_pkgrel architecture; do

    # shellcheck disable=SC2016
    id=$(
      {
        printf 'SELECT `binary_packages`.`id`'
        printf ' FROM `binary_packages`'
        mysql_join_binary_packages_architectures
        mysql_join_binary_packages_binary_packages_in_repositories
        mysql_join_binary_packages_in_repositories_repositories
        printf ' WHERE'
        printf ' `binary_packages`.`%s`=from_base64("%s") AND' \
          'epoch' "$(printf '%s' "${epoch}" | base64 -w0)" \
          'pkgver' "$(printf '%s' "${pkgver}" | base64 -w0)" \
          'pkgrel' "$(printf '%s' "${pkgrel}" | base64 -w0)" \
          'sub_pkgrel' "$(printf '%s' "${sub_pkgrel}" | base64 -w0)" \
          'pkgname' "$(printf '%s' "${pkgname}" | base64 -w0)"
        printf ' `architectures`.`name`=from_base64("%s")' \
          "$(printf '%s' "${architecture}" | base64 -w0)"
        printf ' AND NOT EXISTS ('
          printf 'SELECT 1'
          printf ' FROM `binary_packages_in_repositories` AS `bs_bpir`'
          mysql_join_binary_packages_in_repositories_repositories 'bs_bpir' 'bs_r'
          printf ' WHERE `bs_bpir`.`package`=`binary_packages`.`id`'
          printf ' AND `bs_r`.`name`="build-support"'
        printf ') AND `repositories`.`is_on_master_mirror`'
        printf ' LIMIT 1;\n'
      } | \
        mysql_run_query
    )
    if [ -z "${id}" ]; then
      continue
    fi

    printf '%s\n' "${package}" >> \
      "${tmp_dir}/packages"
    for suffix in '' '.sig'; do
      printf 'ln -s "../../pool/%s%s" "%s/build-support/%s%s"\n' \
        "${package}" \
        "${suffix}" \
        "${repo_arch}" \
        "${package}" \
        "${suffix}"
    done >> \
      "${tmp_dir}/sftp-command"
    printf '%s %s/pool/%s\n' \
      "${repo_arch}" \
      "${master_mirror_rsync_directory}" \
      "${package}" | \
      sed '
        p
        s/$/.sig/
      ' >> \
      "${tmp_dir}/to-copy"
    printf '%s\n' \
      "${repo_arch}" >> \
      "${tmp_dir}/repo-archs"

    # shellcheck disable=SC2016
    {
      printf 'INSERT IGNORE INTO `binary_packages_in_repositories`'
      printf ' (`repository`,`package`,`is_to_be_deleted`)'
      # shellcheck disable=SC2154
      printf ' VALUES (%s,%s,0);\n' \
        "$(eval echo '"${repository_ids__'"${repo_arch}"'_build_support}"')" \
        "${id}"
      printf 'SELECT LAST_INSERT_ID();\n'
    } >> \
      "${tmp_dir}/mysql-command"
  done

if [ ! -s "${tmp_dir}/repo-archs" ]; then
  >&2 echo 'Nothing to do.'
  if [ -w "$1" ]; then
    printf '' > "$1"
  fi
  exit 0
fi

sort -u "${tmp_dir}/repo-archs" | \
  sponge "${tmp_dir}/repo-archs"

while read -r repo_arch; do
  mkdir "${tmp_dir}/${repo_arch}"
  {
    printf 'failsafe_rsync'
    for suffix in 'db' 'files'; do
      printf ' "%s/%s/build-support/build-support.%s."*' \
        "${master_mirror_rsync_directory}" \
        "${repo_arch}" \
        "${suffix}"
    done
    printf ' "%s/%s/"\n' \
      "${tmp_dir}" \
      "${repo_arch}"
  } \
  | intent_something
done < \
  "${tmp_dir}/repo-archs"

if [ -s "${tmp_dir}/to-copy" ]; then
  mkdir "${tmp_dir}/transit/"
  while read -r repo_arch; do
    mkdir "${tmp_dir}/transit/${repo_arch}/"
    {
      printf 'failsafe_rsync '
      sed -n '
        s,^'"$(str_to_regex "${repo_arch}")"' ,,
        T
        p
      ' "${tmp_dir}/to-copy" \
      | tr '\n' ' '
      printf ' "%s/transit/%s/"\n' \
        "${tmp_dir}" \
        "${repo_arch}"
      printf 'repo-add'
      printf ' "%s/%s/build-support.db.tar.gz"' \
        "${tmp_dir}" \
        "${repo_arch}"
      printf ' "%s/transit/%s/"*".pkg.tar.zst"\n' \
        "${tmp_dir}" \
        "${repo_arch}"
    } \
    | intent_something
  done < \
    "${tmp_dir}/repo-archs"
fi

if [ -s "${tmp_dir}/sftp-command" ]; then
  printf 'failsafe_sftp < "%s/sftp-command"\n' \
    "${tmp_dir}" \
  | intent_something
fi

while read -r repo_arch; do
  {
    printf 'recompress_gz "%s"' \
      "${tmp_dir}"
    for suffix in '' '.old'; do
      printf ' "%s/%s/build-support."*".tar.gz%s"' \
        "${tmp_dir}" \
        "${repo_arch}" \
        "${suffix}"
    done
    printf '\n'
    printf 'failsafe_rsync'
    for suffix in 'db' 'files'; do
      printf ' "%s/%s/build-support.%s."*' \
        "${tmp_dir}" \
        "${repo_arch}" \
        "${suffix}"
    done
    printf ' "%s/%s/build-support/"\n' \
      "${master_mirror_rsync_directory}" \
      "${repo_arch}"
  } \
  | intent_something
done < \
  "${tmp_dir}/repo-archs"

if [ -s "${tmp_dir}/mysql-command" ]; then
  {
    printf 'mysql_run_query \\\n'
    printf '<"%s/mysql-command" \\\n' \
      "${tmp_dir}"
    printf '| sort -u \\\n'
    printf '| remove_old_package_versions\n'
  } \
  | intent_something
fi

printf 'rm -rf --one-file-system "%s"\n' \
  "${tmp_dir}" \
| intent_something

if [ -w "$1" ]; then
  cat "${tmp_dir}/packages" > \
    "$1"
fi

execute_all_intentions