Skip to content

Commit

Permalink
Use ghcr.io latest release for image removal when no running as a doc…
Browse files Browse the repository at this point in the history
…ker swarm service.

Some improvements on debug messages
  • Loading branch information
shizunge committed Feb 2, 2024
1 parent 9088119 commit d6aebb4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/docker_hub_rate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,16 @@ 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
fi
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
Expand All @@ -83,15 +84,16 @@ 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
fi
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
Expand Down
13 changes: 8 additions & 5 deletions src/lib-gantry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}."
Expand All @@ -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;
Expand Down Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions tests/spec_gantry_test_helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit d6aebb4

Please sign in to comment.