Skip to content

Commit

Permalink
[entrypoint] cleanup logs about loading libraries and image remover
Browse files Browse the repository at this point in the history
  • Loading branch information
shizunge committed Nov 5, 2024
1 parent 3279d57 commit f6aff72
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
27 changes: 22 additions & 5 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

load_libraries() {
local LOCAL_LOG_LEVEL="${GANTRY_LOG_LEVEL:-""}"
_get_lib_dir() {
local LIB_DIR=
if [ -n "${GANTRY_LIB_DIR:-""}" ]; then
LIB_DIR="${GANTRY_LIB_DIR}"
Expand All @@ -29,10 +28,28 @@ load_libraries() {
elif [ -r "./lib-gantry.sh" ]; then
LIB_DIR="."
fi
echo "${LIB_DIR}"
}

_log_load_libraries() {
local LOG_LEVEL="${GANTRY_LOG_LEVEL:-""}"
local IMAGES_TO_REMOVE="${GANTRY_IMAGES_TO_REMOVE:-""}"
local LIB_DIR="${1}"
# log function is not available before loading the library.
if ! echo "${LOCAL_LOG_LEVEL}" | grep -q -i "NONE"; then
echo "[$(date -Iseconds)] Loading libraries from ${LIB_DIR}" >&2
if echo "${LOG_LEVEL}" | grep -q -i "NONE"; then
return 0
fi
local TIMESTAMP=
if [ -z "${IMAGES_TO_REMOVE}" ]; then
TIMESTAMP="[$(date -Iseconds)] "
fi
echo "${TIMESTAMP}Loading libraries from ${LIB_DIR}" >&2
}

load_libraries() {
local LIB_DIR=
LIB_DIR=$(_get_lib_dir)
_log_load_libraries "${LIB_DIR}"
. "${LIB_DIR}/notification.sh"
. "${LIB_DIR}/docker_hub_rate.sh"
. "${LIB_DIR}/lib-common.sh"
Expand Down Expand Up @@ -155,7 +172,7 @@ main() {
if [ -n "${IMAGES_TO_REMOVE}" ]; then
# Image remover runs as a global job. The log will be collected via docker commands then formatted.
# Redefine the log function for the formater.
log() { echo "${@}"; }
log() { echo "${@}" >&2; }
gantry_remove_images "${IMAGES_TO_REMOVE}"
return $?
fi
Expand Down
12 changes: 7 additions & 5 deletions tests/gantry_cleanup_images_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,14 @@ Describe 'cleanup-images'
When run test_IMAGES_TO_REMOVE_none_empty "${TEST_NAME}" "${SERVICE_NAME}" "${IMAGE_WITH_TAG}"
The status should be success
The stdout should satisfy display_output
The stdout should satisfy spec_expect_message "Removed exited container.*${SERVICE_NAME0}.*${IMAGE_WITH_TAG0}"
The stdout should satisfy spec_expect_message "${REMOVED_IMAGE}.*${IMAGE_WITH_TAG0}"
The stdout should satisfy spec_expect_message "${FAILED_TO_REMOVE_IMAGE}.*${IMAGE_WITH_TAG1}"
The stdout should satisfy spec_expect_message "There is no image.*${IMAGE_WITH_TAG2}"
The stdout should satisfy spec_expect_no_message ".+"
The stderr should satisfy display_output
The stderr should satisfy spec_expect_no_message "${NOT_START_WITH_A_SQUARE_BRACKET}"
# It should not use the log function from the lib-common, the messages do not start with "[".
The stderr should satisfy spec_expect_no_message "^((?:\x1b\[[0-9;]*[mG])?\[)"
The stderr should satisfy spec_expect_message "Removed exited container.*${SERVICE_NAME0}.*${IMAGE_WITH_TAG0}"
The stderr should satisfy spec_expect_message "${REMOVED_IMAGE}.*${IMAGE_WITH_TAG0}"
The stderr should satisfy spec_expect_message "${FAILED_TO_REMOVE_IMAGE}.*${IMAGE_WITH_TAG1}"
The stderr should satisfy spec_expect_message "There is no image.*${IMAGE_WITH_TAG2}"
End
End
End # Describe 'Single service'
1 change: 1 addition & 0 deletions tests/spec_gantry_test_helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export SLEEP_SECONDS_BEFORE_NEXT_UPDATE="Sleep [0-9]+ seconds before next update

test_log() {
echo "${GANTRY_LOG_LEVEL}" | grep -q -i "^NONE$" && return 0;
[ -n "${GANTRY_IMAGES_TO_REMOVE}" ] && echo "${*}" >&2 && return 0;
echo "[$(date -Iseconds)] Test: ${*}" >&2
}

Expand Down

0 comments on commit f6aff72

Please sign in to comment.