diff --git a/hack/overlay-main-images.sh b/hack/overlay-main-images.sh index adb6b8638..2e6c4a1c4 100755 --- a/hack/overlay-main-images.sh +++ b/hack/overlay-main-images.sh @@ -151,7 +151,10 @@ bare_ref_line='^[^[:space:]#][^[:space:]]*/[^[:space:]]+$' # alternative is a ref that may not pull at all. Third-party refs are never # touched: main can bump one alongside its config, and the committed pair is # the one known to work together. The split shape (`repository:` + `tag:` -# without `digest:`) is not handled; no package uses it for a bare ref today. +# without `digest:`) is not handled, nor is a single-quoted value or one +# followed by a `# comment`; such a line stays as committed, and no package +# writes a bare first-party ref that way today. Prints nothing and fails when +# the file could not be rewritten, so the caller counts it as failed. # # Prints the number of lines replaced. replace_bare_refs() { @@ -199,11 +202,11 @@ replace_bare_refs() { } END { close(out); print r + 0 } ' "$1") - if [ "$n" -gt 0 ]; then - mv "$tmp" "$1" - else - rm -f "$tmp" - fi + case "$n" in + ''|*[!0-9]*) rm -f "$tmp"; return 1 ;; + 0) rm -f "$tmp" ;; + *) mv "$tmp" "$1" || { rm -f "$tmp"; return 1; } ;; + esac echo "$n" } @@ -248,19 +251,26 @@ for new in $(find "$MAINPKGS" -type d -name charts -prune -o \ if diff "$cur" "$new" | sed -n 's/^[<>] //p' \ | grep -vE '^[[:space:]]*$' | grep -qvE "$line_pat"; then - echo "drift (non-ref change) in $cur -> keeping committed ref" drift=$((drift + 1)) + r=0 case "$cur" in *.yaml) - r=$(replace_bare_refs "$cur" "$new") || r=0 - if [ "$r" -gt 0 ]; then - echo "bare first-party ref in $cur -> current-main ($r line(s)); its other refs stay committed" - bare=$((bare + r)) - d=$(dirname "${cur#packages/}") - bare_files="$bare_files $d" + if ! r=$(replace_bare_refs "$cur" "$new"); then + echo "WARN: rewriting bare refs failed for $cur (keeping committed refs)" + failed=$((failed + 1)) + r=0 fi ;; esac + if [ "$r" -eq 0 ]; then + echo "drift (non-ref change) in $cur -> keeping committed ref" + else + echo "drift (non-ref change) in $cur -> keeping committed refs except bare first-party ones" + echo "bare first-party ref in $cur -> current-main ($r line(s)); its other refs stay committed" + bare=$((bare + r)) + d=$(dirname "${cur#packages/}") + bare_files="$bare_files $d" + fi # Name the PACKAGE, not the directory the file happens to sit in: an # images/*.tag drift would otherwise read as "/images", which is not # something anyone can go and look at. diff --git a/hack/overlay-main-images_test.bats b/hack/overlay-main-images_test.bats index 28f04526f..4059f9ea1 100644 --- a/hack/overlay-main-images_test.bats +++ b/hack/overlay-main-images_test.bats @@ -618,3 +618,23 @@ load test_helper grep -q 'bare-refs=0' "$w/out.txt" rm -rf "$w" } + +@test "a bare-ref rewrite that cannot write the file is counted as failed, not replaced" { + # Mode bits do not stop root from writing, and the POSIX runner has no + # `skip`, so there is nothing to observe as root. + if [ "$(id -u)" -eq 0 ]; then return 0; fi + root=$(pwd) + w=$(mktemp -d) + mkdir -p "$w/packages/system/ro" "$w/main/system/ro" + printf 'tuning: old\nimage: ghcr.io/cozystack/cozystack/ro:v0.0.0\n' > "$w/packages/system/ro/values.yaml" + printf 'tuning: new\nimage: iad.ocir.io/x/cozystack/ro:main@sha256:bbbb\n' > "$w/main/system/ro/values.yaml" + chmod a-w "$w/packages/system/ro" + ( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt" 2>&1 + chmod u+w "$w/packages/system/ro" + grep -q '^image: ghcr.io/cozystack/cozystack/ro:v0.0.0$' "$w/packages/system/ro/values.yaml" + grep -q 'bare-refs=0 failed=1' "$w/out.txt" + if ls "$w/packages/system/ro" | grep -q 'overlay-bare'; then + echo "FAIL: temp file left behind" >&2; exit 1 + fi + rm -rf "$w" +}