|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | +#set -x # Uncomment for debugging |
| 4 | + |
| 5 | +usage() { |
| 6 | + echo "$0" |
| 7 | + echo |
| 8 | + echo " Check and pull NGINX Agent Docker images from the official NGINX Docker registry." |
| 9 | + echo " Args:" |
| 10 | + echo " 1. Registry URL (default: docker-registry.nginx.com/nginx)" |
| 11 | + echo " 2. Image Name (default: agentv3)" |
| 12 | + echo " 3. Search Pattern (optional): A regex pattern to filter tags before checking versions" |
| 13 | + echo |
| 14 | + echo " Usage:" |
| 15 | + echo " > $0 <registry-url> <image-name> [search-pattern]" |
| 16 | + echo |
| 17 | + echo " Example:" |
| 18 | + echo " Search for all tags for the 'agentv3' image in the NGINX Docker registry:" |
| 19 | + echo " > $0 docker-registry.nginx.com/nginx agentv3" |
| 20 | + echo |
| 21 | + echo " Search for all tags containing 'alpine' for the 'agentv3' image in the NGINX Docker registry:" |
| 22 | + echo " > $0 docker-registry.nginx.com/nginx agentv3 alpine" |
| 23 | + exit 0 |
| 24 | +} |
| 25 | + |
| 26 | +while getopts "h" opt; do |
| 27 | + case ${opt} in |
| 28 | + h ) |
| 29 | + usage |
| 30 | + ;; |
| 31 | + \? ) |
| 32 | + usage |
| 33 | + ;; |
| 34 | + esac |
| 35 | +done |
| 36 | + |
| 37 | +# Input parameters with defaults |
| 38 | +REGISTRY_URL=${1:-"docker-registry.nginx.com/nginx"} |
| 39 | +IMAGE_NAME=${2:-"agentv3"} |
| 40 | +RE_PATTERN=${3:-""} |
| 41 | +IMAGE_PATH="${REGISTRY_URL}/${IMAGE_NAME}" |
| 42 | +CONTAINER_TOOL=docker |
| 43 | +SKOPEO_IMAGE="quay.io/skopeo/stable" |
| 44 | +SKOPEO_TAG="latest" |
| 45 | + |
| 46 | +# Check for docker installation |
| 47 | +if ! command -v ${CONTAINER_TOOL} &> /dev/null; then |
| 48 | + echo "${CONTAINER_TOOL} could not be found." |
| 49 | + # check podman as an alternative |
| 50 | + CONTAINER_TOOL=podman |
| 51 | + if ! command -v ${CONTAINER_TOOL} &> /dev/null; then |
| 52 | + echo "Neither docker nor podman could be found. Please install one of them to proceed." |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | +fi |
| 56 | + |
| 57 | +echo "Using container tool: ${CONTAINER_TOOL}" |
| 58 | +${CONTAINER_TOOL} --version |
| 59 | + |
| 60 | +echo "Getting skopeo tool..." |
| 61 | +${CONTAINER_TOOL} pull ${SKOPEO_IMAGE}:${SKOPEO_TAG} || { echo "Failed to pull skopeo image"; exit 1; } |
| 62 | + |
| 63 | +echo "Checking images in ${REGISTRY_URL}/${IMAGE_NAME}" |
| 64 | +echo "Saving all tags to ${IMAGE_NAME}_tags.txt" |
| 65 | +${CONTAINER_TOOL} run quay.io/skopeo/stable list-tags docker://${IMAGE_PATH} | jq -r '.Tags[]' > ${IMAGE_NAME}_tags.txt |
| 66 | +echo $(wc -l < ${IMAGE_NAME}_tags.txt) "tags fetched." |
| 67 | + |
| 68 | +# Filter out tags that end with four or more digits (nightly/build tags) |
| 69 | +grep -Ev '\d{4,}$' ${IMAGE_NAME}_tags.txt | sort -u > ${IMAGE_NAME}_filteredtags.txt |
| 70 | +echo $(wc -l < ${IMAGE_NAME}_filteredtags.txt) "tags after filtering." |
| 71 | + |
| 72 | +# Search for tags matching the provided pattern |
| 73 | +FOUND=($(grep -E "${RE_PATTERN}" ${IMAGE_NAME}_filteredtags.txt)) |
| 74 | +echo "tags matching '${RE_PATTERN}':" ${#FOUND[@]} |
| 75 | +echo "${FOUND[@]}" | sed 's/ /\n/g' |
| 76 | + |
| 77 | +for tag in "${FOUND[@]}"; do |
| 78 | + echo ":: ${IMAGE_PATH}:$tag" |
| 79 | + ${CONTAINER_TOOL} pull ${IMAGE_PATH}:$tag > /dev/null 2>&1 |
| 80 | + ${CONTAINER_TOOL} run ${IMAGE_PATH}:$tag nginx -v |
| 81 | + ${CONTAINER_TOOL} run --rm ${IMAGE_PATH}:$tag nginx-agent --version | sed 's/version/version:/g' # --rm to clean up container after run |
| 82 | +done |
| 83 | + |
0 commit comments