From 316bfbeadcd707a7b4615d8596b2ab38c213f0ac Mon Sep 17 00:00:00 2001 From: reyesj2 <94730068+reyesj2@users.noreply.github.com> Date: Mon, 30 Nov 2026 17:39:03 -0600 Subject: [PATCH] retry like docker pull and fail image setup clearly Verify and copy now retry 5 times 30 s apart, matching the old pull budget. The preflight stays quick: it stops at once on not found or access denied, retries other errors once, and takes Ctrl-C. A job that leaves no result counts as failed, .rc files are written atomically, and each failed phase ends with one line naming the failed and skipped images. Job count and timeouts are internal constants instead of env overrides. --- salt/common/tools/sbin/so-image-common | 134 ++++++++++++++++++------- 1 file changed, 99 insertions(+), 35 deletions(-) diff --git a/salt/common/tools/sbin/so-image-common b/salt/common/tools/sbin/so-image-common index ade7d235f..8fdc0f961 100755 --- a/salt/common/tools/sbin/so-image-common +++ b/salt/common/tools/sbin/so-image-common @@ -13,8 +13,14 @@ STATUS_CONF='/opt/so/conf/so-status/so-status.conf' # PIDs of the current launch_pool children, so the INT/TERM trap can kill them. POOL_PIDS=() _pool_tree=() -# Registry metadata is small, so this only bounds a stall. Overridable for tests. -SO_IMAGE_INSPECT_TIMEOUT=${SO_IMAGE_INSPECT_TIMEOUT:-120} +IMAGE_PARALLEL_JOBS=4 +IMAGE_INSPECT_TIMEOUT=120 +IMAGE_RETRIES=5 +IMAGE_RETRY_DELAY=30 +# The preflight is a quick check, not a retry budget: one retry at most, and a +# definite answer from the registry ends it right away. +PREFLIGHT_ATTEMPTS=2 +PREFLIGHT_RETRY_DELAY=10 # shellcheck disable=SC2120 container_list() { @@ -140,13 +146,13 @@ image_ref() { # image_ref => the full registry reference # One small function per inspect: retry_cmd logs its attempts on stdout, so the # command's own output is redirected to its file here. fetch_manifest() { # fetch_manifest - timeout "$SO_IMAGE_INSPECT_TIMEOUT" docker buildx imagetools inspect --raw "$1" >"$2" + timeout "$IMAGE_INSPECT_TIMEOUT" docker buildx imagetools inspect --raw "$1" >"$2" } # buildx's --format output is the contract with the signed .txt, so it stays in # one place that is easy to adjust. fetch_config() { # fetch_config - timeout "$SO_IMAGE_INSPECT_TIMEOUT" docker buildx imagetools inspect \ + timeout "$IMAGE_INSPECT_TIMEOUT" docker buildx imagetools inspect \ --format '{{json .Image}}' "$1" >"$2" } @@ -164,10 +170,10 @@ resolve_image() { # resolve_image local v1=0 local v2=0 if [[ -f "$UPDATE_DIR/salt/elasticsearch/defaults.yaml" ]]; then - v1=$(egrep " +version: " "$UPDATE_DIR/salt/elasticsearch/defaults.yaml" | awk -F: '{print $2}' | tr -d '[:space:]') + v1=$(grep -E " +version: " "$UPDATE_DIR/salt/elasticsearch/defaults.yaml" | awk -F: '{print $2}' | tr -d '[:space:]') fi if [[ -f "$DEFAULT_SALT_DIR/salt/elasticsearch/defaults.yaml" ]]; then - v2=$(egrep " +version: " "$DEFAULT_SALT_DIR/salt/elasticsearch/defaults.yaml" | awk -F: '{print $2}' | tr -d '[:space:]') + v2=$(grep -E " +version: " "$DEFAULT_SALT_DIR/salt/elasticsearch/defaults.yaml" | awk -F: '{print $2}' | tr -d '[:space:]') fi image_version=$(compare_es_versions "$v1" "$v2") printf '%s:%s%s https://sigs.securityonion.net/es-%s/%s:%s%s.sig\n' \ @@ -197,7 +203,7 @@ verify_trusted_image() { local rc=0 local digest_re='^sha256:[a-f0-9]{64}$' - retry_cmd 3 10 fetch_manifest "$REF" "$manifest_file" >>"$IMAGE_LOG" 2>&1 || rc=$? + retry_cmd "$IMAGE_RETRIES" "$IMAGE_RETRY_DELAY" fetch_manifest "$REF" "$manifest_file" >>"$IMAGE_LOG" 2>&1 || rc=$? if [ "$rc" -ne 0 ]; then echo "Could not fetch manifest for $image, please ensure connectivity to $CONTAINER_REGISTRY" >>"$IMAGE_LOG" 2>&1 echo "Command failed with error $rc" >>"$IMAGE_LOG" 2>&1 @@ -243,7 +249,7 @@ verify_trusted_image() { # By digest, not by tag: a tag can be re-pushed between the two calls, and the # .txt must describe the manifest bytes that sha256sum and the sig cover. VERIFIED_REF="${CONTAINER_REGISTRY}/${IMAGEREPO}/${image_name}@sha256:${manifest_digest}" - retry_cmd 3 10 fetch_config "$VERIFIED_REF" "$config_file" >>"$IMAGE_LOG" 2>&1 || rc=$? + retry_cmd "$IMAGE_RETRIES" "$IMAGE_RETRY_DELAY" fetch_config "$VERIFIED_REF" "$config_file" >>"$IMAGE_LOG" 2>&1 || rc=$? if [ "$rc" -ne 0 ]; then echo "Could not fetch image config for $image, please ensure connectivity to $CONTAINER_REGISTRY" >>"$IMAGE_LOG" 2>&1 echo "Command failed with error $rc" >>"$IMAGE_LOG" 2>&1 @@ -276,11 +282,9 @@ verify_trusted_image() { return 1 fi local GPGTEST - GPGTEST=$(gpg --verify "$SIGNPATH/$image.sig" "$SIGNPATH/$image.txt" 2>&1) - if [[ $? -ne 0 ]]; then - echo "There is a problem downloading the $image image. Details: " >>"$IMAGE_LOG" 2>&1 - echo "" >>"$IMAGE_LOG" 2>&1 - echo $GPGTEST >>"$IMAGE_LOG" 2>&1 + if ! GPGTEST=$(gpg --verify "$SIGNPATH/$image.sig" "$SIGNPATH/$image.txt" 2>&1); then + echo "Signature verification failed for $image. Details:" >>"$IMAGE_LOG" 2>&1 + echo "$GPGTEST" >>"$IMAGE_LOG" 2>&1 return 1 fi @@ -325,7 +329,8 @@ copy_verified_image() { mount_shared_blobs "$host" "$i" "$manifest_file" "$IMAGE_LOG" pushed_blob_repo # imagetools create, not docker push: Docker 29's containerd store cannot push a # freshly pulled image. GODEBUG prefix keeps http2client=0 local (#16342). - retry_cmd 3 10 env GODEBUG="${GODEBUG:+${GODEBUG},}http2client=0" \ + # It reads from ghcr.io, so it gets the same budget as the verify fetches. + retry_cmd "$IMAGE_RETRIES" "$IMAGE_RETRY_DELAY" env GODEBUG="${GODEBUG:+${GODEBUG},}http2client=0" \ docker buildx imagetools create --tag "${host}:5000/${IMAGEREPO}/${image}" "$VERIFIED_REF" \ >>"$IMAGE_LOG" 2>&1 || { echo "Unable to copy $image to embedded registry" >>"$IMAGE_LOG" 2>&1 @@ -385,8 +390,12 @@ _pool_abort() { # _pool_abort local pid trap '' INT TERM _pool_tree=() + # Skipping reaped PIDs matters: their numbers may already belong to an + # unrelated process, whose tree would then be killed as root. for pid in "${POOL_PIDS[@]}"; do - pool_collect_tree "$pid" + if kill -0 "$pid" 2>/dev/null; then + pool_collect_tree "$pid" + fi done for pid in "${_pool_tree[@]}"; do kill -TERM "$pid" 2>/dev/null || true @@ -398,17 +407,15 @@ _pool_abort() { # _pool_abort exit "$code" } -# Only wait on PIDs started here: soup may run its own background jobs in this -# shell (e.g. the grid highstate), and a bare `wait` would block on those. # Fail fast: once a finished job of this pool failed, no new job is launched, so # an unreachable registry costs one round of retries instead of one per image. pool_failed() { # pool_failed ... local phase=$1 shift - local img + local img rc for img in "$@"; do - if [ -f "$SIGNPATH/$img.$phase.rc" ] && - [ "$(cat "$SIGNPATH/$img.$phase.rc" 2>/dev/null)" != "0" ]; then + # No .rc yet means the job is still running, so it is not a failure. + if rc=$(cat "$SIGNPATH/$img.$phase.rc" 2>/dev/null) && [ "$rc" != "0" ]; then return 0 fi done @@ -422,7 +429,7 @@ launch_pool() { local hook=$4 shift 4 local -a active=() still=() launched=() - local img pid + local img pid rc local stop=0 POOL_PIDS=() local saved_int saved_term @@ -441,7 +448,10 @@ launch_pool() { fi ( ("$func" "$img") >>"$SIGNPATH/$img.$phase.log" 2>&1 - echo $? >"$SIGNPATH/$img.$phase.rc" + rc=$? + # Write then rename: a truncated .rc reads as a failure to pool_failed. + echo "$rc" >"$SIGNPATH/$img.$phase.rc.tmp" + mv "$SIGNPATH/$img.$phase.rc.tmp" "$SIGNPATH/$img.$phase.rc" ) & pid=$! POOL_PIDS+=("$pid") @@ -461,6 +471,8 @@ launch_pool() { fi done done + # Only wait on PIDs started here: soup may run its own background jobs in this + # shell (e.g. the grid highstate), and a bare `wait` would block on those. if [ ${#POOL_PIDS[@]} -gt 0 ]; then wait "${POOL_PIDS[@]}" 2>/dev/null || true fi @@ -522,10 +534,7 @@ update_docker_containers() { local -A pushed_blob_repo=() - local jobs=${SO_IMAGE_PARALLEL:-4} - if ! [[ "$jobs" =~ ^[1-9][0-9]*$ ]]; then - jobs=4 - fi + local jobs=$IMAGE_PARALLEL_JOBS # Nothing is copied or pulled unless every image verified (a bad signature # publishes nothing). The hook runs in the parent: it mutates caller globals. @@ -536,15 +545,42 @@ update_docker_containers() { progress_hook=$PROGRESS_CALLBACK fi - # One preflight before the pool: fail in seconds instead of letting every - # verify job spend its retries in parallel. Upstream registry, not :5000. + # One preflight before the pool, so every verify job does not spend the long + # retry budget in parallel. It is a quick check: a definite answer from the + # registry ends it at once, and only an unclear error gets one more try. + # Upstream registry, not :5000. --foreground keeps it in this shell's process + # group, so Ctrl-C reaches it before launch_pool installs its own trap. if [ ${#TRUSTED_CONTAINERS[@]} -gt 0 ]; then local preflight_image preflight_ref preflight_out + local attempt=1 preflight_ok=0 preflight_kind=transient read -r preflight_image _ < <(resolve_image "${TRUSTED_CONTAINERS[0]}") preflight_ref=$(image_ref "$preflight_image") - if ! preflight_out=$(retry_cmd 2 10 timeout 60 \ - docker buildx imagetools inspect --raw "$preflight_ref" 2>&1); then - echo "Could not reach $CONTAINER_REGISTRY, please ensure connectivity" >>"$LOG_FILE" 2>&1 + while [ "$attempt" -le "$PREFLIGHT_ATTEMPTS" ]; do + if preflight_out=$(timeout --foreground "$IMAGE_INSPECT_TIMEOUT" \ + docker buildx imagetools inspect --raw "$preflight_ref" 2>&1); then + preflight_ok=1 + break + fi + # A registry answer never changes on a retry; anything else might. + case "$preflight_out" in + *"not found"*|*"manifest unknown"*) preflight_kind=notfound ;; + *denied*|*unauthorized*) preflight_kind=denied ;; + esac + if [ "$preflight_kind" != transient ]; then + break + fi + attempt=$((attempt + 1)) + if [ "$attempt" -le "$PREFLIGHT_ATTEMPTS" ]; then + sleep "$PREFLIGHT_RETRY_DELAY" + fi + done + if [ "$preflight_ok" -ne 1 ]; then + echo "Could not inspect $preflight_ref on $CONTAINER_REGISTRY" >>"$LOG_FILE" 2>&1 + case "$preflight_kind" in + notfound) echo "The image or tag for $preflight_image was not found" >>"$LOG_FILE" 2>&1 ;; + denied) echo "Access to $preflight_ref was denied by $CONTAINER_REGISTRY" >>"$LOG_FILE" 2>&1 ;; + *) echo "Check connectivity to $CONTAINER_REGISTRY or the proxy" >>"$LOG_FILE" 2>&1 ;; + esac echo "$preflight_out" >>"$LOG_FILE" 2>&1 exit 1 fi @@ -553,16 +589,26 @@ update_docker_containers() { launch_pool verify_trusted_image verify "$jobs" "$progress_hook" "${TRUSTED_CONTAINERS[@]}" local verify_failed=0 + local -a verify_bad=() verify_skipped=() for i in "${TRUSTED_CONTAINERS[@]}"; do cat "$SIGNPATH/$i.verify.log" >> "$LOG_FILE" 2>/dev/null - if [ "$(cat "$SIGNPATH/$i.verify.rc" 2>/dev/null)" != "0" ]; then + if [ ! -f "$SIGNPATH/$i.verify.rc" ]; then + # No .rc: the pool skipped it, or the job died before writing one. + # Fail closed either way; a skipped image means another one failed. + verify_skipped+=("$i") + verify_failed=1 + elif [ "$(cat "$SIGNPATH/$i.verify.rc" 2>/dev/null)" != "0" ]; then + verify_bad+=("$i") verify_failed=1 fi done if [ "$verify_failed" -ne 0 ]; then + # Last line of the phase: the per-image logs above would bury it. + echo "verify failed: ${verify_bad[*]:-none}; skipped: ${verify_skipped[*]:-none}" >> "$LOG_FILE" 2>&1 exit 1 fi + # Verify only from here: no copy and no local pull. if [[ -n "$SKIP_TAGPUSH" ]]; then return 0 fi @@ -605,17 +651,26 @@ update_docker_containers() { deferred=("${remaining[@]:1}") fi - echo "Copy wave $wave_no: ${wave[*]}" >> "$LOG_FILE" 2>&1 + echo "Copying images (batch $wave_no): ${wave[*]}" >> "$LOG_FILE" 2>&1 launch_pool copy_verified_image copy "$jobs" "" "${wave[@]}" local wave_failed=0 + local -a copy_bad=() copy_skipped=() for img in "${wave[@]}"; do cat "$SIGNPATH/$img.copy.log" >> "$LOG_FILE" 2>/dev/null - if [ "$(cat "$SIGNPATH/$img.copy.rc" 2>/dev/null)" != "0" ]; then + if [ ! -f "$SIGNPATH/$img.copy.rc" ]; then + # No .rc: the pool skipped it, or the job died before writing one. + # Fail closed either way; a skipped image means another one failed. + copy_skipped+=("$img") + wave_failed=1 + elif [ "$(cat "$SIGNPATH/$img.copy.rc" 2>/dev/null)" != "0" ]; then + copy_bad+=("$img") wave_failed=1 fi done if [ "$wave_failed" -ne 0 ]; then + # Last line of the phase: the per-image logs above would bury it. + echo "copy failed: ${copy_bad[*]:-none}; skipped: ${copy_skipped[*]:-none}" >> "$LOG_FILE" 2>&1 exit 1 fi for img in "${wave[@]}"; do @@ -626,13 +681,22 @@ update_docker_containers() { launch_pool pull_verified_image pull "$jobs" "" "${TRUSTED_CONTAINERS[@]}" local pull_failed=0 + local -a pull_bad=() pull_skipped=() for i in "${TRUSTED_CONTAINERS[@]}"; do cat "$SIGNPATH/$i.pull.log" >> "$LOG_FILE" 2>/dev/null - if [ "$(cat "$SIGNPATH/$i.pull.rc" 2>/dev/null)" != "0" ]; then + if [ ! -f "$SIGNPATH/$i.pull.rc" ]; then + # No .rc: the pool skipped it, or the job died before writing one. + # Fail closed either way; a skipped image means another one failed. + pull_skipped+=("$i") + pull_failed=1 + elif [ "$(cat "$SIGNPATH/$i.pull.rc" 2>/dev/null)" != "0" ]; then + pull_bad+=("$i") pull_failed=1 fi done if [ "$pull_failed" -ne 0 ]; then + # Last line of the phase: the per-image logs above would bury it. + echo "pull failed: ${pull_bad[*]:-none}; skipped: ${pull_skipped[*]:-none}" >> "$LOG_FILE" 2>&1 exit 1 fi }