Skip to content

Commit cc5a362

Browse files
committed
improve usage and comments
1 parent 80adc32 commit cc5a362

File tree

1 file changed

+66
-25
lines changed

1 file changed

+66
-25
lines changed

scripts/check-images.sh

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,82 @@
11
#!/usr/bin/env bash
2-
32
set -euo pipefail
4-
#set -x
3+
#set -x # Uncomment for debugging
54

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+
}
825

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
1036

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."
1447
exit 1
15-
}
48+
fi
1649

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
1959
fi
60+
echo "Using container tool: ${CONTAINER_TOOL}"
2061

2162
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."
2266

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."
3070

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'
3375

3476
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
4081
done
4182

0 commit comments

Comments
 (0)