|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright The Conforma Contributors |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +# SPDX-License-Identifier: Apache-2.0 |
| 17 | + |
| 18 | +set -euo pipefail |
| 19 | +# Exit on error, undefined variable, or pipeline failure |
| 20 | + |
| 21 | +# --------------------------- |
| 22 | +# Ensure required commands are available |
| 23 | +# --------------------------- |
| 24 | +for cmd in skopeo jq curl date; do |
| 25 | + if ! command -v "$cmd" >/dev/null 2>&1; then |
| 26 | + echo "❌ '$cmd' is not installed or not in your PATH. Please install $cmd." |
| 27 | + exit 1 |
| 28 | + fi |
| 29 | +done |
| 30 | + |
| 31 | +# --------------------------- |
| 32 | +# Defaults for CLI arguments |
| 33 | +# --------------------------- |
| 34 | +REPO="" |
| 35 | +FILTER="" |
| 36 | +DAYS="" |
| 37 | +BEFORE="" |
| 38 | +QUAY_TOKEN="" |
| 39 | +DRY_RUN=false |
| 40 | + |
| 41 | +# --------------------------- |
| 42 | +# Parse flags |
| 43 | +# --------------------------- |
| 44 | +while [[ $# -gt 0 ]]; do |
| 45 | + case "$1" in |
| 46 | + --repo) REPO="$2"; shift 2;; |
| 47 | + --filter) FILTER="$2"; shift 2;; |
| 48 | + --days) DAYS="$2"; shift 2;; |
| 49 | + --before) BEFORE="$2"; shift 2;; |
| 50 | + --token) QUAY_TOKEN="$2"; shift 2;; |
| 51 | + --dry-run) DRY_RUN=true; shift;; |
| 52 | + *) echo "Usage: $0 --repo <repo> --filter <regex> [--days N | --before YYYY-MM-DD] [--token] [--dry-run]"; exit 1;; |
| 53 | + esac |
| 54 | +done |
| 55 | + |
| 56 | +# --------------------------- |
| 57 | +# Validate required arguments |
| 58 | +# --------------------------- |
| 59 | +if [[ -z "$REPO" || -z "$FILTER" ]]; then |
| 60 | + echo "❌ Missing --repo or --filter" |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | +if [[ -z "$DAYS" && -z "$BEFORE" ]]; then |
| 64 | + echo "❌ You must supply either --days or --before" |
| 65 | + exit 1 |
| 66 | +fi |
| 67 | + |
| 68 | +# --------------------------- |
| 69 | +# Compute cutoff epoch time |
| 70 | +# --------------------------- |
| 71 | +if [[ -n "$DAYS" ]]; then |
| 72 | + CUTOFF=$(date -d "-$DAYS days" +%s) |
| 73 | +else |
| 74 | + CUTOFF=$(date -d "$BEFORE" +%s) |
| 75 | +fi |
| 76 | + |
| 77 | +# --------------------------- |
| 78 | +# Echo context to user |
| 79 | +# --------------------------- |
| 80 | +echo "🔍 Repo: $REPO" |
| 81 | +echo "🔎 Filter: $FILTER" |
| 82 | +echo "📅 Cutoff: Before $(date -d "@$CUTOFF" +'%Y-%m-%d %H:%M:%S %Z')" |
| 83 | +if [[ -n "${QUAY_TOKEN:-}" ]]; then |
| 84 | + echo "🔑 Token: ${QUAY_TOKEN:0:4}... (truncated)" |
| 85 | +fi |
| 86 | +$DRY_RUN && echo "🧪 Dry‑run: ON" |
| 87 | + |
| 88 | +# --------------------------- |
| 89 | +# Authentication check |
| 90 | +# --------------------------- |
| 91 | +# Look for quay.io credentials in various files |
| 92 | +if jq -e '.auths["quay.io"]' ~/.docker/config.json >/dev/null 2>&1; then |
| 93 | + echo "✅ 🐋 Logged in via Docker credentials" |
| 94 | +# check to Podman/containers |
| 95 | +elif jq -e '."quay.io"' ~/.config/containers/auth.json >/dev/null 2>&1; then |
| 96 | + echo "✅ 🦭 Logged in via containers/auth.json" |
| 97 | +# check Skopeo |
| 98 | +elif jq -e '.auths["quay.io"]' $XDG_RUNTIME_DIR/containers/auth.json >/dev/null 2>&1; then |
| 99 | + echo "✅ 📦 Logged in via \$XDG_RUNTIME_DIR/containers/auth.json" |
| 100 | +# no creds? Suggest logging in |
| 101 | +else |
| 102 | + echo "❌ No quay.io entry found in your credential files" |
| 103 | + echo "Please log in to quay.io using Podman, Docker, or Skopeo first." |
| 104 | + exit 1 |
| 105 | +fi |
| 106 | + |
| 107 | +echo "" |
| 108 | + |
| 109 | +# --------------------------- |
| 110 | +# Pagination & tag fetching |
| 111 | +# --------------------------- |
| 112 | +PAGE=1 |
| 113 | +BASE_URL="https://quay.io/api/v1/repository/${REPO}/tag/?limit=100" |
| 114 | +DELETED=0 |
| 115 | +FOUND=0 |
| 116 | + |
| 117 | +while :; do |
| 118 | + # Set up curl arguments |
| 119 | + # -s: silent mode |
| 120 | + # -G: use GET method |
| 121 | + curl_args=(-s -G) |
| 122 | + |
| 123 | + # If we have a token, add it to the curl request as a header |
| 124 | + # -H: add a header |
| 125 | + if [[ -n "${QUAY_TOKEN:-}" ]]; then |
| 126 | + curl_args+=( -H "Authorization: Bearer ${QUAY_TOKEN}" ) |
| 127 | + fi |
| 128 | + |
| 129 | + # now append the URL + query args |
| 130 | + # --data-urlencode: URL-encode the data |
| 131 | + curl_args+=( "${BASE_URL}" \ |
| 132 | + --data-urlencode "page=${PAGE}" \ |
| 133 | + --data-urlencode "onlyActiveTags=true" \ |
| 134 | + --data-urlencode "filter_tag_name=like:${FILTER}" \ |
| 135 | + ) |
| 136 | + |
| 137 | + RESPONSE=$(curl "${curl_args[@]}") |
| 138 | + |
| 139 | + # check for errors in the response: |
| 140 | + if [[ "$(jq -r '.error' <<<"$RESPONSE")" != "null" ]]; then |
| 141 | + echo "❌ Error fetching tags: $(jq -r '.error' <<<"$RESPONSE")" |
| 142 | + exit 1 |
| 143 | + fi |
| 144 | + # pull matching lines into a variable: |
| 145 | + mapfile -t LINES < <( |
| 146 | + echo "$RESPONSE" | |
| 147 | + jq -r --arg f "$FILTER" ' |
| 148 | + .tags[] |
| 149 | + | "\(.name)|\(.last_modified)" |
| 150 | + ' |
| 151 | + ) |
| 152 | + |
| 153 | + [[ "$(jq -r '.has_additional' <<<"$RESPONSE")" == "true" ]] \ |
| 154 | + && ((PAGE++)) || break |
| 155 | +done |
| 156 | + |
| 157 | +# --------------------------- |
| 158 | +# Process each matching tag |
| 159 | +# --------------------------- |
| 160 | +for LINE in "${LINES[@]}"; do |
| 161 | + IFS="|" read -r tag last_modified <<< "$LINE" |
| 162 | + last_sec=$(date -d "$last_modified" +%s 2>/dev/null || echo 0) |
| 163 | + if [[ "$last_sec" -lt "$CUTOFF" ]]; then |
| 164 | + # add to the count of found tags, incrementing safely: |
| 165 | + : $((FOUND++)) |
| 166 | + fmt=$(date -d "$last_modified" +"%Y-%m-%d %H:%M:%S %Z") |
| 167 | + if $DRY_RUN; then |
| 168 | + printf "%-80s %s 💡 would be deleted\n" "$tag" "$fmt" |
| 169 | + else |
| 170 | + if skopeo delete "docker://quay.io/${REPO}:${tag}" &> /dev/null; then |
| 171 | + printf "%-80s %s ✅ deleted\n" "$tag" "$fmt" |
| 172 | + # add to the count of deleted tags, incrementing safely: |
| 173 | + : $((DELETED++)) |
| 174 | + else |
| 175 | + printf "%-80s %s ❌ failed\n" "$tag" "$fmt" |
| 176 | + fi |
| 177 | + fi |
| 178 | + fi |
| 179 | +done |
| 180 | + |
| 181 | +echo "" |
| 182 | +# --------------------------- |
| 183 | +# Summary output |
| 184 | +# --------------------------- |
| 185 | +if $DRY_RUN; then |
| 186 | + echo "🧪 Dry-run: would delete $FOUND tags matching “$FILTER”" |
| 187 | +else |
| 188 | + echo "🔢 Total matching tags found: $FOUND" |
| 189 | + echo "✅ Total tags deleted: $DELETED" |
| 190 | +fi |
0 commit comments