summaryrefslogtreecommitdiff
path: root/bin/get-assignment
blob: 85585c0bd18c486bf0eee0f0b65a8160f2734c45 (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
#!/bin/sh

# receive one package to be built from the build-list whose dependencies
# are already satisfied or which breaks a dependency cycle
# accepts a comma separated list of prefered packages as the first argument

# exit code shows state of success:
#  0: ok, I gave you an assignment
#  1: come back (shortly) later - I was running already
#  2: come back later - there are still packages to be built,
#     but currently none has all its dependencies ready
#  3: come back after the next run of get-package-updates - currently
#     there are no pending packages

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

# TODO: honor manual build order of tool-chain:
# toolchain build order: linux-api-headers->glibc->binutils->gcc->binutils->glibc

mkdir -p "${work_dir}/package-states"

hand_out_assignment() {

  # find out the sub_pkgrel
  sub_pkgrel=$(
    next_sub_pkgrel "$1" "$2" "$3" "$4"
  )

  # we don't care anymore if an older version of this package was
  # "locked" or "broken"
  find "${work_dir}/package-states" -maxdepth 1 -regextype grep \
    -regex '.*/'"$(str_to_regex "$1")"'\(\.[^.]\+\)\{3\}\.\(locked\|broken\)' \
    -not -regex '.*/'"$(str_to_regex "$1.$2.$3.$4.")"'[^.]\+' \
    -delete

  # move that build order to the end of the build-list
  sed -i '
    /^'"$(str_to_regex "$1 $2 $3 $4")"'$/ {
      $ b
      d
    }
    $ a '"$1 $2 $3 $4" \
    "${work_dir}/build-list"

  echo "$1 $2 $3 $4 ${sub_pkgrel}"
  {
    # shellcheck disable=SC2154
    echo "${slave}"
    if [ -r "${work_dir}/package-states/$1.$2.$3.$4.locked" ]; then
      cat "${work_dir}/package-states/$1.$2.$3.$4.locked"
    fi
  } | \
    sort -u | \
    sponge "${work_dir}/package-states/$1.$2.$3.$4.locked"
  # shellcheck disable=SC2016
  {
    printf 'UPDATE `build_slaves`'
    printf ' SET `currently_building` = ('
    printf ' SELECT `build_assignments`.`id`'
    printf ' FROM `build_assignments`'
    printf ' JOIN `package_sources` ON `build_assignments`.`package_source`=`package_sources`.`id`'
    printf ' JOIN `upstream_repositories` ON `package_sources`.`upstream_package_repository`=`upstream_repositories`.`id`'
    printf ' WHERE'
    printf ' `package_sources`.`%s` = from_base64("%s") AND' \
      'pkgbase'          "$(printf '%s' "$1" | base64 -w0)" \
      'git_revision'     "$(printf '%s' "$2" | base64 -w0)" \
      'mod_git_revision' "$(printf '%s' "$3" | base64 -w0)"
    printf ' `upstream_repositories`.`name` = from_base64("%s")' \
      "$(printf '%s' "$4" | base64 -w0)"
    printf ')'
    printf ' WHERE `build_slaves`.`name`=from_base64("%s");\n' \
      "$(printf '%s' "${slave}" | base64 -w0)"

    printf 'UPDATE `build_assignments`'
    printf ' JOIN `package_sources` ON `build_assignments`.`package_source`=`package_sources`.`id`'
    printf ' JOIN `upstream_repositories` ON `package_sources`.`upstream_package_repository`=`upstream_repositories`.`id`'
    printf ' SET `build_assignments`.`priority`=0'
    printf ' WHERE'
    printf ' `package_sources`.`%s` = from_base64("%s") AND' \
      'pkgbase'          "$(printf '%s' "$1" | base64 -w0)" \
      'git_revision'     "$(printf '%s' "$2" | base64 -w0)" \
      'mod_git_revision' "$(printf '%s' "$3" | base64 -w0)"
    printf ' `upstream_repositories`.`name` = from_base64("%s");\n' \
      "$(printf '%s' "$4" | base64 -w0)"
  } | \
    ${mysql_command}

  # lock every loop this package breaks
  find "${work_dir}/build-list.loops" -maxdepth 1 -regextype grep \
    -regex '.*/loop_[0-9]\+' \
    -exec grep -qxF "$1" '{}' \; \
    -exec touch '{}.locked' \;

  exit 0

}

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() {
  rm -f "${build_list_lock_file}"
  rm -rf --one-file-system "${tmp_dir}"
}

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

# TODO: only hand out package if it is still yet to be built

# if we're building something already, hand it out (again)
currently_building=$(
  # shellcheck disable=SC2016
  {
    printf 'SELECT '
    mysql_query_select_pkgbase_and_revision
    printf ' JOIN `build_slaves`'
    printf ' ON `build_slaves`.`currently_building`=`build_assignments`.`id`'
    printf ' WHERE `build_slaves`.`name`=from_base64("%s")' \
      "$(printf '%s' "${slave}" | base64 -w0)"
    printf ' LIMIT 1;\n'
  } | \
    ${mysql_command} --batch --raw | \
    sed '1d'
)

if [ -n "${currently_building}" ]; then
  # shellcheck disable=SC2086
  hand_out_assignment ${currently_building}
fi

# a package with all dependencies met or which is part of a loop,
#   that we requested or
#   which has been built the least times
next_building=$(
  # shellcheck disable=SC2016
  {
    printf 'SELECT '
    printf '`package_sources`.`pkgbase`=from_base64("%s") AS `requested`,' \
      "$(
        printf '%s' "$1" | \
          base64 -w0
      )"
    printf '`build_assignments`.`priority`,'
    printf '('
      printf 'SELECT COUNT(*)'
      printf ' FROM `failed_builds`'
      printf ' WHERE `failed_builds`.`build_assignment`=`build_assignments`.`id`'
    printf ') AS `trials`,'
    mysql_query_is_part_of_loop '`build_assignments`.`id`'
    printf ' AS `part_of_loop`,'
    mysql_query_select_pkgbase_and_revision
    printf ' JOIN `%s` ON `%s`.`id`=`%s`.`%s`' \
      'binary_packages' 'build_assignments' 'binary_packages' 'build_assignment' \
      'repositories' 'repositories' 'binary_packages' 'repository'
    printf ' WHERE `repositories`.`name`="build-list"'
    printf ' AND NOT EXISTS ('
      printf ' SELECT *'
      printf ' FROM `build_slaves`'
      printf ' WHERE `build_slaves`.`currently_building`=`build_assignments`.`id`'
    printf ') AND ('
      printf '`build_assignments`.`is_blocked` IS NULL'
      printf ' OR'
      printf ' `package_sources`.`pkgbase`=from_base64("%s")' \
        "$(
          printf '%s' "$1" | \
            base64 -w0
        )"
    printf ') AND ('
      mysql_query_is_part_of_loop '`build_assignments`.`id`'
      printf ' OR NOT '
      mysql_query_has_pending_dependencies '`build_assignments`.`id`'
    printf ')'
    printf ' ORDER BY `requested` DESC, `priority` DESC, `trials`, `part_of_loop`, `build_assignments`.`id`'
    printf ' LIMIT 1;\n'
  } | \
    ${mysql_command} --batch --raw | \
    sed '
      1d
      y/\t/ /
      s/^\(\S\+ \)\{4\}//
    '
)
if [ -n "${next_building}" ]; then
  # shellcheck disable=SC2086
  hand_out_assignment ${next_building}
fi

# Check if there are any pending packages at all
count_pending=$(
  # shellcheck disable=SC2016
  {
    printf 'SELECT count(*)'
    printf ' FROM `build_assignments`'
    printf ' JOIN `%s` ON `%s`.`id`=`%s`.`%s`' \
      'binary_packages' 'build_assignments' 'binary_packages' 'build_assignment' \
      'repositories' 'repositories' 'binary_packages' 'repository'
    printf ' WHERE `repositories`.`name`="build-list"'
    printf ' AND `build_assignments`.`is_blocked` IS NULL'
    printf ';\n'
  } | \
    ${mysql_command} --batch --raw | \
    sed '1d'
)

if [ "${count_pending}" -eq 0 ]; then
  >&2 echo 'come back after the next run of get-package-updates - currently there are no pending packages'
  exit 3
else
  >&2 echo 'come back later - there are still packages to be built, but currently none has all its dependencies ready'
  exit 2
fi