Skip to content

Commit 16e18e2

Browse files
committed
packaging: fix package index tree URLs for release listing
tree --fromfile embeds the listing file path in hrefs; use a fixed catalog-paths.txt in the work dir and strip that prefix so package links always resolve under packages.fluentbit.io. Signed-off-by: lecaros <lecaros@chronosphere.io>
1 parent 4e11543 commit 16e18e2

2 files changed

Lines changed: 145 additions & 10 deletions

File tree

packaging/generate-packages-index.sh

Lines changed: 102 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fi
106106
BASE_PATH="$(cd "$BASE_PATH" && pwd)"
107107
WORK_DIR="$(mktemp -d)"
108108
OBJECT_LIST="$WORK_DIR/objects.txt"
109-
TREE_LIST="$WORK_DIR/tree.txt"
109+
TREE_PATHS="$WORK_DIR/catalog-paths.txt"
110110
VERSION_ROWS="$WORK_DIR/version-rows.tsv"
111111
trap cleanup EXIT
112112

@@ -190,6 +190,101 @@ build_object_list()
190190
sort -u -o "$OBJECT_LIST" "$OBJECT_LIST"
191191
}
192192

193+
filter_package_paths()
194+
{
195+
local candidates="$WORK_DIR/package-candidates.txt"
196+
local rc=0
197+
198+
grep -Ev 'source-|pool|dists' "$OBJECT_LIST" > "$candidates"
199+
rc=$?
200+
if [[ "$rc" -ne 0 && "$rc" -ne 1 ]]; then
201+
echo "ERROR: failed filtering package paths from object list" >&2
202+
exit 1
203+
fi
204+
if [[ "$rc" -eq 1 ]]; then
205+
: > "$candidates"
206+
fi
207+
208+
grep -E '\.(rpm|deb|key|repo|exe|msi|zip|pkg)$' "$candidates" > "$TREE_PATHS"
209+
rc=$?
210+
if [[ "$rc" -ne 0 && "$rc" -ne 1 ]]; then
211+
echo "ERROR: failed selecting package file paths" >&2
212+
exit 1
213+
fi
214+
if [[ "$rc" -eq 1 ]]; then
215+
: > "$TREE_PATHS"
216+
fi
217+
}
218+
219+
absolutize_tree_hrefs()
220+
{
221+
awk -v base="$BASE_URL" '
222+
function absolutize(url) {
223+
if (url ~ /^https?:\/\//) {
224+
return url
225+
}
226+
if (url == "/") {
227+
return base "/"
228+
}
229+
if (substr(url, 1, 1) == "/") {
230+
return base url
231+
}
232+
return base "/" url
233+
}
234+
{
235+
line = $0
236+
out = ""
237+
while (match(line, /href="[^"]+"/)) {
238+
out = out substr(line, 1, RSTART - 1)
239+
href = substr(line, RSTART, RLENGTH)
240+
url = substr(href, 7, length(href) - 7)
241+
out = out "href=\"" absolutize(url) "\""
242+
line = substr(line, RSTART + RLENGTH)
243+
}
244+
print out line
245+
}'
246+
}
247+
248+
build_tree_html()
249+
{
250+
local -a tree_pipe_status=()
251+
252+
BASE_URL="${BASE_URL%/}"
253+
(
254+
cd "$WORK_DIR"
255+
tree --noreport --charset utf-8 --fromfile catalog-paths.txt -H "${BASE_URL}/"
256+
) | sed -e "s|${BASE_URL}/catalog-paths.txt/|${BASE_URL}/|g" \
257+
-e "s|${BASE_URL}/catalog-paths.txt|${BASE_URL}/|g" \
258+
-e "s|${BASE_URL}catalog-paths.txt/|${BASE_URL}/|g" \
259+
-e "s|${BASE_URL}catalog-paths.txt|${BASE_URL}/|g" \
260+
-e 's|>catalog-paths.txt</a>|>/</a>|g' \
261+
-e 's|\(https://[^/]*\)//|\1/|g' \
262+
| absolutize_tree_hrefs \
263+
| awk '/<hr>/ { exit } { print } END { print "</body></html>" }' > "$WORK_DIR/tree.html"
264+
tree_pipe_status=("${PIPESTATUS[@]}")
265+
266+
if [[ "${tree_pipe_status[0]}" -ne 0 ]]; then
267+
echo "ERROR: tree failed to generate package index HTML" >&2
268+
exit 1
269+
fi
270+
if [[ "${tree_pipe_status[1]}" -ne 0 ]]; then
271+
echo "ERROR: failed to normalize package index tree links" >&2
272+
exit 1
273+
fi
274+
if [[ "${tree_pipe_status[2]}" -ne 0 ]]; then
275+
echo "ERROR: failed to absolutize package index tree links" >&2
276+
exit 1
277+
fi
278+
if [[ "${tree_pipe_status[3]}" -ne 0 ]]; then
279+
echo "ERROR: failed to finalize package index tree HTML" >&2
280+
exit 1
281+
fi
282+
if [[ ! -s "$WORK_DIR/tree.html" ]]; then
283+
echo "ERROR: package index tree HTML is empty" >&2
284+
exit 1
285+
fi
286+
}
287+
193288
render_index_html()
194289
{
195290
local latest="$1"
@@ -207,27 +302,25 @@ if [[ ! -s "$OBJECT_LIST" ]]; then
207302
exit 1
208303
fi
209304

210-
grep -Ev 'source-|pool|dists' "$OBJECT_LIST" | \
211-
grep -E '\.(rpm|deb|key|repo|exe|msi|zip|pkg)$' > "$TREE_LIST" || true
305+
filter_package_paths
212306

213307
awk -v emit_mode=filter \
214308
-v min_catalog_major="$MIN_CATALOG_MAJOR" \
215309
-f "$SCRIPT_DIR/build-catalog.awk" \
216-
"$TREE_LIST" > "$WORK_DIR/tree-filtered.txt"
310+
"$TREE_PATHS" > "$WORK_DIR/tree-filtered.txt"
217311

218312
if [[ -s "$WORK_DIR/tree-filtered.txt" ]]; then
219-
mv "$WORK_DIR/tree-filtered.txt" "$TREE_LIST"
313+
mv "$WORK_DIR/tree-filtered.txt" "$TREE_PATHS"
220314
else
221-
: > "$TREE_LIST"
315+
: > "$TREE_PATHS"
222316
fi
223317

224-
if [[ ! -s "$TREE_LIST" ]]; then
318+
if [[ ! -s "$TREE_PATHS" ]]; then
225319
echo "ERROR: no package files found for index.html" >&2
226320
exit 1
227321
fi
228322

229-
tree --noreport --charset utf-8 --fromfile "$TREE_LIST" -H "$BASE_URL" | \
230-
awk '/<hr>/ { exit } { print } END { print "</body></html>" }' > "$WORK_DIR/tree.html"
323+
build_tree_html
231324

232325
REPO_PATHS="$(printf '%s ' "${LINUX_REPO_PATHS[@]}")"
233326
awk -v emit_mode=versions \

packaging/testing/test-packages-index.sh

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ S3_LISTING_FILE=""
1010
cleanup()
1111
{
1212
rm -rf "$OUTPUT_DIR" "${OUTPUT_DIR_S3:-}" "${OUTPUT_DIR_OVERRIDE:-}" \
13-
"${OUTPUT_DIR_MIN:-}" "${OUTPUT_DIR_ALL:-}" \
13+
"${OUTPUT_DIR_MIN:-}" "${OUTPUT_DIR_ALL:-}" "${OUTPUT_DIR_TRAILING:-}" \
14+
"${OUTPUT_DIR_FILTERED:-}" \
1415
"${EMPTY_BASE_PATH:-}" "${JQ_STRESS_DIR:-}" "$S3_LISTING_FILE"
1516
}
1617

@@ -142,6 +143,47 @@ assert_contains "$OUTPUT_DIR/index.html" "Latest release: <strong>4.2.7</strong>
142143
assert_contains "$OUTPUT_DIR/index.html" "4.2.6"
143144
assert_contains "$OUTPUT_DIR/index.html" "fluent-bit-4.2.7-win64.exe"
144145
assert_contains "$OUTPUT_DIR/index.html" "https://packages.example.test/versions.json"
146+
assert_contains "$OUTPUT_DIR/index.html" \
147+
'href="https://packages.example.test/centos/9/fluent-bit-4.2.7-1.x86_64.rpm"'
148+
149+
if grep -Eo 'href="[^"]*(catalog-paths\.txt|/tree\.txt/)' "$OUTPUT_DIR/index.html" >/dev/null; then
150+
echo "ERROR: index.html contains broken tree listing paths in href attributes" >&2
151+
grep -Eo 'href="[^"]*(catalog-paths\.txt|/tree\.txt/)' "$OUTPUT_DIR/index.html" >&2 || true
152+
exit 1
153+
fi
154+
155+
OUTPUT_DIR_TRAILING="$(mktemp -d)"
156+
setup_local_fixture "$OUTPUT_DIR_TRAILING"
157+
158+
BASE_PATH="$OUTPUT_DIR_TRAILING" \
159+
AWS_S3_REMOTE_DISCOVERY=false \
160+
BASE_URL=https://packages.example.test/ \
161+
"$GENERATOR"
162+
163+
assert_contains "$OUTPUT_DIR_TRAILING/index.html" \
164+
'href="https://packages.example.test/centos/9/fluent-bit-4.2.7-1.x86_64.rpm"'
165+
166+
if grep -Eo 'href="https://[^/]+//[^"]*"' "$OUTPUT_DIR_TRAILING/index.html" >/dev/null; then
167+
echo "ERROR: index.html contains double slashes in href attributes" >&2
168+
grep -Eo 'href="https://[^/]+//[^"]*"' "$OUTPUT_DIR_TRAILING/index.html" >&2 || true
169+
exit 1
170+
fi
171+
172+
if ! jq -e '.base_url == "https://packages.example.test"' "$OUTPUT_DIR_TRAILING/versions.json" >/dev/null; then
173+
echo "ERROR: versions.json base_url was not normalized without trailing slash" >&2
174+
jq . "$OUTPUT_DIR_TRAILING/versions.json" >&2 || true
175+
exit 1
176+
fi
177+
178+
OUTPUT_DIR_FILTERED="$(mktemp -d)"
179+
mkdir -p "$OUTPUT_DIR_FILTERED/centos/9" "$OUTPUT_DIR_FILTERED/windows"
180+
touch \
181+
"$OUTPUT_DIR_FILTERED/centos/9/fluent-bit-2.1.0-1.x86_64.rpm" \
182+
"$OUTPUT_DIR_FILTERED/windows/fluent-bit-2.1.0-win64.exe"
183+
184+
assert_fails_with "ERROR: no package files found for index.html" \
185+
env BASE_PATH="$OUTPUT_DIR_FILTERED" AWS_S3_REMOTE_DISCOVERY=false \
186+
BASE_URL=https://packages.example.test "$GENERATOR"
145187

146188
if ! jq -e '.latest == "4.2.7" and (.versions | length) == 2' "$OUTPUT_DIR/versions.json" >/dev/null; then
147189
echo "ERROR: versions.json did not contain the expected version entries" >&2

0 commit comments

Comments
 (0)