From d6aebb40ef6747ffbe94594ef6f87df674c20509 Mon Sep 17 00:00:00 2001 From: Shizun Ge Date: Fri, 2 Feb 2024 02:40:37 -0800 Subject: [PATCH] Use ghcr.io latest release for image removal when no running as a docker swarm service. Some improvements on debug messages --- src/docker_hub_rate.sh | 10 ++++++---- src/lib-gantry.sh | 13 ++++++++----- tests/spec_gantry_test_helper.sh | 1 + 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/docker_hub_rate.sh b/src/docker_hub_rate.sh index 69a7ae9..9a97576 100755 --- a/src/docker_hub_rate.sh +++ b/src/docker_hub_rate.sh @@ -66,7 +66,7 @@ docker_hub_rate() { fi local RESPONSE= if ! RESPONSE=$(_docker_hub_rate_token "${IMAGE}" "${USER_AND_PASS}"); then - log DEBUG "_docker_hub_rate_token error: RESPONSE=" + log DEBUG "Read docker hub token error: RESPONSE=" echo "${RESPONSE}" | log_lines DEBUG echo "[GET TOKEN RESPONSE ERROR]" return 1 @@ -74,7 +74,8 @@ docker_hub_rate() { local TOKEN= TOKEN=$(echo "${RESPONSE}" | sed 's/.*"token":"\([^"]*\).*/\1/') if [ -z "${TOKEN}" ]; then - log DEBUG "parse token error: RESPONSE=${RESPONSE}" + log DEBUG "Parse docker hub token error: RESPONSE=" + echo "${RESPONSE}" | log_lines DEBUG echo "[PARSE TOKEN ERROR]" return 1 fi @@ -83,7 +84,7 @@ docker_hub_rate() { echo "0" return 0 fi - log DEBUG "_docker_hub_rate_read_rate error: RESPONSE=" + log DEBUG "Read docker hub rate error: RESPONSE=" echo "${RESPONSE}" | log_lines DEBUG echo "[GET RATE RESPONSE ERROR]" return 1 @@ -91,7 +92,8 @@ docker_hub_rate() { local RATE= RATE=$(echo "${RESPONSE}" | sed -n 's/.*ratelimit-remaining: \([0-9]*\).*/\1/p' ) if [ -z "${RATE}" ]; then - log DEBUG "parse rate error: RESPONSE=${RESPONSE}" + log DEBUG "Parse docker hub rate error: RESPONSE=" + echo "${RESPONSE}" | log_lines DEBUG echo "[PARSE RATE ERROR]" return 1 fi diff --git a/src/lib-gantry.sh b/src/lib-gantry.sh index 86c8b89..be09b2e 100755 --- a/src/lib-gantry.sh +++ b/src/lib-gantry.sh @@ -175,6 +175,8 @@ gantry_remove_images() { _remove_images() { local CLEANUP_IMAGES="${GANTRY_CLEANUP_IMAGES:-"true"}" local CLEANUP_IMAGES_OPTIONS="${GANTRY_CLEANUP_IMAGES_OPTIONS:-""}" + # Use this image when not running gantry as a docker swarm service. + local DEFAULT_IMAGES_REMOVER="${GANTRY_CLEANUP_IMAGES_REMOVER:="ghcr.io/shizunge/gantry"}" if ! is_true "${CLEANUP_IMAGES}"; then log INFO "Skip removing images." return 0 @@ -193,9 +195,10 @@ _remove_images() { for I in $(echo "${IMAGES_TO_REMOVE}" | tr '\n' ' '); do log INFO "- ${I}" done - local IMAGE_OF_THIS_CONTAINER= - IMAGE_OF_THIS_CONTAINER=$(_get_service_image "$(_current_service_name)") - [ -z "${IMAGE_OF_THIS_CONTAINER}" ] && IMAGE_OF_THIS_CONTAINER="ghcr.io/shizunge/gantry-development" + local IMAGES_REMOVER= + IMAGES_REMOVER=$(_get_service_image "$(_current_service_name)") + [ -z "${IMAGES_REMOVER}" ] && IMAGES_REMOVER="${DEFAULT_IMAGES_REMOVER}" + log DEBUG "Set IMAGES_REMOVER=${IMAGES_REMOVER}" local IMAGES_TO_REMOVE_LIST= IMAGES_TO_REMOVE_LIST=$(echo "${IMAGES_TO_REMOVE}" | tr '\n' ' ') [ -n "${CLEANUP_IMAGES_OPTIONS}" ] && log DEBUG "Adding options \"${CLEANUP_IMAGES_OPTIONS}\" to the global job ${SERVICE_NAME}." @@ -208,7 +211,7 @@ _remove_images() { --mount type=bind,source=/var/run/docker.sock,destination=/var/run/docker.sock \ --env "GANTRY_IMAGES_TO_REMOVE=${IMAGES_TO_REMOVE_LIST}" \ ${CLEANUP_IMAGES_OPTIONS} \ - "${IMAGE_OF_THIS_CONTAINER}" 2>&1); then + "${IMAGES_REMOVER}" 2>&1); then log ERROR "Failed to remove images: ${RMI_MSG}" fi wait_service_state "${SERVICE_NAME}" --complete; @@ -645,7 +648,7 @@ gantry_initialize() { # So here we use the file system to pass value between multiple processes. STATIC_VARIABLES_FOLDER=$(mktemp -d) export STATIC_VARIABLES_FOLDER - log DEBUG "Created ${STATIC_VARIABLES_FOLDER} to store static variables." + log DEBUG "Created STATIC_VARIABLES_FOLDER ${STATIC_VARIABLES_FOLDER}" _authenticate_to_registries } diff --git a/tests/spec_gantry_test_helper.sh b/tests/spec_gantry_test_helper.sh index 7976a84..ea44cb9 100644 --- a/tests/spec_gantry_test_helper.sh +++ b/tests/spec_gantry_test_helper.sh @@ -229,6 +229,7 @@ initialize_test() { export GANTRY_UPDATE_TIMEOUT_SECONDS= export GANTRY_CLEANUP_IMAGES= export GANTRY_CLEANUP_IMAGES_OPTIONS= + export GANTRY_CLEANUP_IMAGES_REMOVER=ghcr.io/shizunge/gantry-development export GANTRY_IMAGES_TO_REMOVE= export GANTRY_NOTIFICATION_APPRISE_URL= export GANTRY_NOTIFICATION_TITLE=