#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" source "${ROOT_DIR}/tools/cockpit/json_contract.sh" ARTIFACT_DIR="$ROOT_DIR/artifacts/cockpit" LOG_DIR="$ARTIFACT_DIR/log_ops" mkdir -p "$LOG_DIR" STAMP="$(date +%Y%m%d-%H%M%S)" RUN_LOG="$LOG_DIR/log_ops-$STAMP.log" RETENTION_DAYS="${RETENTION_DAYS:-7}" ACTION="summary" COMPONENT="log_ops" JSON=0 APPLY=0 TARGET_DIRS=( "$ROOT_DIR/artifacts" "$ROOT_DIR/logs" "$ROOT_DIR/tmp" ) if [ -n "${LOG_OPS_TARGET_DIRS:-}" ]; then IFS=':' read -r -a TARGET_DIRS <<< "${LOG_OPS_TARGET_DIRS}" fi usage() { cat <&2 } collect_logs() { local dir for dir in "${TARGET_DIRS[@]}"; do [ -d "$dir" ] || continue find "$dir" -type f \( -name '*.log' -o -name '*.out' -o -name '*.err' -o -name '*.jsonl' \) -print done | sort -u } main() { while [ "$#" -gt 0 ]; do case "$1" in --action) ACTION="${2:-}" shift 2 ;; --json) JSON=1 shift ;; --apply) APPLY=1 shift ;; --retention-days) RETENTION_DAYS="${2:-7}" shift 2 ;; -h|--help) usage exit 0 ;; *) printf 'Unknown arg: %s\n' "$1" >&2 usage >&2 exit 2 ;; esac done local files files="$(collect_logs || true)" local count=0 local stale=0 local bytes=0 local listed_json="" local purged_json="" local purged_count=0 local status="done" local contract_status="ok" local artifacts=("${RUN_LOG}") local degraded_reasons=() local next_steps=() if [ -n "$files" ]; then while IFS= read -r file; do [ -n "$file" ] || continue count=$((count + 1)) local size size="$(wc -c < "$file" | tr -d ' ')" bytes=$((bytes + size)) listed_json="$(json_contract_append_string "$listed_json" "$file")" if find "$file" -mtime +"$RETENTION_DAYS" -print -quit | grep -q .; then stale=$((stale + 1)) if [ "$ACTION" = "purge" ] && [ "$APPLY" -eq 1 ]; then rm -f "$file" purged_json="$(json_contract_append_string "$purged_json" "$file")" purged_count=$((purged_count + 1)) fi fi done <