Skip to content

Commit 571d1db

Browse files
committed
fix: retry transient Pages APT responses explicitly
1 parent 710f416 commit 571d1db

2 files changed

Lines changed: 34 additions & 21 deletions

File tree

‎packaging/termux/preserve-pages-apt.sh‎

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,40 +41,53 @@ cleanup() { rm -rf -- "$tmp"; }
4141
trap cleanup EXIT INT TERM
4242

4343
# Pages may briefly return 5xx responses while its edge is updating. Retry
44-
# transient network/server errors, while still treating 4xx and repository
44+
# transient network/server errors, while treating 4xx and repository
4545
# verification failures as final errors.
46-
curl_retry=(--retry 4 --retry-all-errors --retry-delay 2 --retry-max-time 30)
46+
request() {
47+
local path=$1 output=$2 max_size=$3 response attempt curl_status
48+
for attempt in 1 2 3 4 5; do
49+
curl_status=0
50+
response="$(curl --silent --show-error --location --connect-timeout 15 --max-time 120 \
51+
--max-filesize "$max_size" "${curl_proto[@]}" \
52+
--output "$output" --write-out '%{http_code}' "$base_url/$path")" || curl_status=$?
53+
if ((curl_status == 0)) && [[ ! "$response" =~ ^5[0-9][0-9]$ ]]; then
54+
REQUEST_CODE=$response
55+
return 0
56+
fi
57+
if ((attempt < 5)); then sleep "$attempt"; fi
58+
done
59+
if ((curl_status != 0)); then
60+
printf 'Network error retrieving published APT path %s after %s attempts (curl exit %s).\n' \
61+
"$path" "$attempt" "$curl_status" >&2
62+
else
63+
printf 'Transient HTTP %s retrieving published APT path %s after %s attempts.\n' \
64+
"$response" "$path" "$attempt" >&2
65+
fi
66+
return 1
67+
}
4768

4869
fetch() {
49-
local path=$1 output=$2 max_size=${3:-10485760} code
50-
code="$(curl "${curl_retry[@]}" --silent --show-error --location --connect-timeout 15 --max-time 120 \
51-
--max-filesize "$max_size" "${curl_proto[@]}" \
52-
--output "$output" --write-out '%{http_code}' "$base_url/$path")" || {
53-
printf 'Network error while retrieving published APT path: %s\n' "$path" >&2
54-
return 1
55-
}
56-
if [[ "$code" != 200 ]]; then
57-
printf 'Expected HTTP 200 for published APT path %s; received %s.\n' "$path" "$code" >&2
70+
local path=$1 output=$2 max_size=${3:-10485760}
71+
request "$path" "$output" "$max_size" || return 1
72+
if [[ "$REQUEST_CODE" != 200 ]]; then
73+
printf 'Expected HTTP 200 for published APT path %s; received %s.\n' "$path" "$REQUEST_CODE" >&2
5874
return 1
5975
fi
6076
}
6177

6278
# A 404 is the expected state before the first signed repository publication.
63-
code="$(curl "${curl_retry[@]}" --silent --show-error --location --connect-timeout 15 --max-time 120 \
64-
--max-filesize 10485760 "${curl_proto[@]}" \
65-
--output "$tmp/InRelease" --write-out '%{http_code}' \
66-
"$base_url/dists/stable/InRelease")" || {
79+
if ! request dists/stable/InRelease "$tmp/InRelease" 10485760; then
6780
printf 'Could not check the currently published APT repository.\n' >&2
6881
exit 1
69-
}
82+
fi
83+
code=$REQUEST_CODE
7084
if [[ "$code" == 404 ]]; then
7185
for companion in ternux-archive-key.asc bootstrap.sh; do
72-
companion_code="$(curl "${curl_retry[@]}" --silent --show-error --location --connect-timeout 15 --max-time 120 \
73-
--max-filesize 1048576 "${curl_proto[@]}" \
74-
--output /dev/null --write-out '%{http_code}' "$base_url/$companion")" || {
86+
if ! request "$companion" /dev/null 1048576; then
7587
printf 'Could not confirm that no prior APT repository is published.\n' >&2
7688
exit 1
77-
}
89+
fi
90+
companion_code=$REQUEST_CODE
7891
if [[ "$companion_code" != 404 ]]; then
7992
printf 'Published APT paths are inconsistent (InRelease=404, %s=%s); refusing website deploy.\n' \
8093
"$companion" "$companion_code" >&2

‎tests/termux_apt_pages_preservation.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class QuietHandler(http.server.SimpleHTTPRequestHandler):
3030
def log_message(self, *_args):
3131
pass
3232
def do_GET(self):
33-
if self.path.endswith("/dists/stable/InRelease") and self.path not in self.transient_503_seen:
33+
if (self.path.endswith("/dists/stable/InRelease") or self.path.endswith("/ternux-archive-key.asc")) and self.path not in self.transient_503_seen:
3434
self.transient_503_seen.add(self.path)
3535
self.send_response(503)
3636
self.end_headers()

0 commit comments

Comments
 (0)