#!/usr/bin/env bash # Chooses the versions to remove from Gitea's package registry (pulizia-docker.sh, «completa» mode). It reads only the # files it is given, with json.awk and versioni-registro.awk (POSIX awk: no jq, no Node): no network, no writes, so it # is tested with a JSON file (PipelineTests). # # versioni-registro.sh ... the sha-<7 to 40 hexadecimal digits> tags of # the febe container beyond the N most recent # versioni-registro.sh senza-etichetta ... the sha256:<64 digits> versions of the febe # container (the manifests of an index: only to # be counted) # versioni-registro.sh scadute ... the -ci. and -rilascio. # versions of the generic febe-ci package (the CI # artifacts, artefatti.sh) created more than # days before , in seconds since # 1970 # # Each page is a response of GET /api/v1/packages/{owner}/{type}/{name} of Gitea 28. Prints, oldest first and one per # line, «versioncreated». Everything else always stays: main, latest, the X.Y.Z versions (also X.Y.Z-rc.N), the # digests, every other tag, a sha- tag or an artifact without a readable date. Among the sha- tags the dates count, # not the text; with equal dates the identifier. If a file is not a JSON list it exits with 2 and prints nothing: when # in doubt nothing is removed. set -euo pipefail qui="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" uso() { echo "Uso: versioni-registro.sh | senza-etichetta | scadute ..." >&2 exit 2 } modo="${1:-}" (($# > 0)) && shift case "$modo" in senza-etichetta) tipo=container nome=febe ;; scadute) (($# >= 2)) || uso giorni="$1" adesso="$2" shift 2 [[ "$giorni" =~ ^[0-9]{1,5}$ && "$adesso" =~ ^[0-9]{1,12}$ ]] || uso tipo=generic nome=febe-ci ;; *) [[ "$modo" =~ ^[0-9]{1,6}$ ]] || uso tipo=container nome=febe ;; esac (($# > 0)) || uso export LC_ALL=C tab=$'\t' versioni=$(awk -v tipo="$tipo" -v nome="$nome" -f "$qui/json.awk" -f "$qui/versioni-registro.awk" "$@") || exit 2 # Lines: milliseconds (or «-»), identifier, order, version, date as written. case "$modo" in senza-etichetta) awk -F "$tab" -v OFS="$tab" '$4 ~ /^sha256:[0-9a-f]+$/ && length($4) == 71 { if ($1 == "-") $1 = 0; print }' <<< "$versioni" \ | sort -t "$tab" -k1,1n -k2,2n -k3,3n | cut -f4,5 ;; scadute) awk -F "$tab" -v limite="$(((adesso - giorni * 86400) * 1000))" \ '$1 != "-" && $1 + 0 < limite + 0 && $4 ~ /^[0-9]+[.][0-9]+[.][0-9]+(-[0-9A-Za-z.-]+)?-(ci|rilascio)[.][0-9]+$/' <<< "$versioni" \ | sort -t "$tab" -k1,1n -k2,2n -k3,3n | cut -f4,5 ;; *) # The most recent first (date, then identifier), the first N are skipped, the rest from the oldest. awk -F "$tab" '$1 != "-" && $4 ~ /^sha-[0-9a-f]+$/ && length($4) >= 11 && length($4) <= 44' <<< "$versioni" \ | sort -t "$tab" -k1,1nr -k2,2nr -k3,3n | tail -n "+$((modo + 1))" \ | awk '{ righe[NR] = $0 } END { for (i = NR; i > 0; i--) print righe[i] }' | cut -f4,5 ;; esac