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