Skip to content

Commit

Permalink
Misc cleanup
Browse files Browse the repository at this point in the history
* Clarify uncessary commands.
* Update debug log on DOCKER_HOST.
* Do not set GANTRY_REGISTRY_HOST in tests.
* Load lib-common.sh st begining of tests.
  • Loading branch information
shizunge committed Feb 12, 2024
1 parent 6d42891 commit fa0d4fb
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

* `docker manifest` CLI failed to get the image meta data for some registries.
* High usage of docker hub rate. Getting manifest and then pulling the image double the usage.
* Unnecessary `docker service update` commands slow down the overall process.
* Running `docker service update` command when there is no new image slows down the overall process.
* Removing images related
* Failure of removing old images will exit and block subsequent updating.
* `docker rmi` only works for the current host.
Expand Down
10 changes: 5 additions & 5 deletions src/docker_hub_rate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ _docker_hub_rate_token() {
local TOKEN_URL="https://auth.docker.io/token?service=registry.docker.io&scope=repository:${IMAGE}:pull"
if curl --version 1>/dev/null 2>&1; then
if [ -n "${USER_AND_PASS}" ]; then
curl -s -S --user "${USER_AND_PASS}" "${TOKEN_URL}"
curl --silent --show-error --user "${USER_AND_PASS}" "${TOKEN_URL}"
return $?
fi
curl -s -S "${TOKEN_URL}"
curl --silent --show-error "${TOKEN_URL}"
return $?
fi
[ -n "${USER_AND_PASS}" ] && log WARN "Cannot read docker hub rate for the given user because curl is not available."
Expand All @@ -38,7 +38,7 @@ _docker_hub_rate_read_rate() {
local HEADER="Authorization: Bearer ${TOKEN}"
local URL="https://registry-1.docker.io/v2/${IMAGE}/manifests/latest"
if curl --version 1>/dev/null 2>&1; then
curl -s -S --head -H "${HEADER}" "${URL}" 2>&1
curl --silent --show-error --head -H "${HEADER}" "${URL}" 2>&1
return $?
fi
# Add `--spider`` implies that you want to send a HEAD request (as opposed to GET or POST).
Expand All @@ -57,7 +57,7 @@ docker_hub_rate() {
log_lines() { local LEVEL="${1}"; while read -r LINE; do [ -z "${LINE}" ] && continue; log "${LEVEL}" "${LINE}"; done; }
fi
local RESPONSE=
if ! RESPONSE=$(_docker_hub_rate_token "${IMAGE}" "${USER_AND_PASS}"); then
if ! RESPONSE=$(_docker_hub_rate_token "${IMAGE}" "${USER_AND_PASS}" 2>&1); then
log DEBUG "Read docker hub token error: RESPONSE="
echo "${RESPONSE}" | log_lines DEBUG
echo "[GET TOKEN RESPONSE ERROR]"
Expand All @@ -71,7 +71,7 @@ docker_hub_rate() {
echo "[PARSE TOKEN ERROR]"
return 1
fi
if ! RESPONSE=$(_docker_hub_rate_read_rate "${IMAGE}" "${TOKEN}"); then
if ! RESPONSE=$(_docker_hub_rate_read_rate "${IMAGE}" "${TOKEN}" 2>&1); then
if echo "${RESPONSE}" | grep -q "Too Many Requests" ; then
echo "0"
return 0
Expand Down
2 changes: 1 addition & 1 deletion src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ gantry() {
local START_TIME=
START_TIME=$(date +%s)

[ -n "${DOCKER_HOST}" ] && log DEBUG "DOCKER_HOST=${DOCKER_HOST}"
local RUN_ON_NODE=
if ! RUN_ON_NODE=$(_run_on_node); then
local HOST_STRING="${DOCKER_HOST:-"the current node"}"
Expand All @@ -91,7 +92,6 @@ gantry() {
log DEBUG "Set NODE_NAME=${RUN_ON_NODE}"
export NODE_NAME="${RUN_ON_NODE}"
fi
[ -n "${DOCKER_HOST}" ] && log DEBUG "DOCKER_HOST=${DOCKER_HOST}"
log INFO "Run on Docker host ${RUN_ON_NODE}."

local ACCUMULATED_ERRORS=0
Expand Down
5 changes: 1 addition & 4 deletions src/notification.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ _notify_via_apprise() {
[ -z "${BODY}" ] && BODY="${TITLE}"
TITLE=$(_replace_newline "${TITLE}")
BODY=$(_replace_newline "${BODY}")
local RETURN_VALUE=0
local LOG=
LOG=$(curl --silent -X POST -H "Content-Type: application/json" --data "{\"title\": \"${TITLE}\", \"body\": \"${BODY}\", \"type\": \"${TYPE}\"}" "${URL}" 2>&1)
RETURN_VALUE="${?}"
if [ "${RETURN_VALUE}" = "0" ]; then
if LOG=$(curl --silent --show-error -X POST -H "Content-Type: application/json" --data "{\"title\": \"${TITLE}\", \"body\": \"${BODY}\", \"type\": \"${TYPE}\"}" "${URL}" 2>&1); then
log INFO "Sent notification via Apprise:"
echo "${LOG}" | log_lines INFO
else
Expand Down
3 changes: 2 additions & 1 deletion tests/gantry_login_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ Describe 'Login'
# Since we pass credentials via the configs file, we can use other envs to login to docker hub and check the rate.
# However we do not actually check whether we read rates correctly, in case password or usrename for docker hub is not set.
# It seems there is no rate limit when running from the github actions, which also gives us a NaN error.
export GANTRY_REGISTRY_HOST="docker.io"
# Do not set GANTRY_REGISTRY_HOST to test the default config.
# export GANTRY_REGISTRY_HOST="docker.io"
export GANTRY_REGISTRY_PASSWORD="${DOCKERHUB_PASSWORD:-""}"
export GANTRY_REGISTRY_USER="${DOCKERHUB_USERNAME:-""}"
local RETURN_VALUE=
Expand Down
11 changes: 4 additions & 7 deletions tests/gantry_notify_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,15 @@ Describe 'Notify'
local EMAIL_API_PORT=8025
local SERVICE_NAME_APPRISE="${SERVICE_NAME}-apprise"
local SERVICE_NAME_MAILPIT="${SERVICE_NAME}-mailpit"
local SCRIPT_DIR=
SCRIPT_DIR="$(get_script_dir)" || return 1
source "${SCRIPT_DIR}/../src/lib-common.sh"
docker pull caronc/apprise
docker pull axllent/mailpit
# Use docker_run to improve coverage on lib-common.sh.
# Use docker_run to improve coverage on lib-common.sh. `docker run` can do the same thing.
docker_run -d --restart=on-failure:10 --name="${SERVICE_NAME_APPRISE}" --network=host \
-e "APPRISE_STATELESS_URLS=mailto://localhost:${SMTP_PORT}?user=userid&pass=password" \
caronc/apprise
docker_run -d --restart=on-failure:10 --name="${SERVICE_NAME_MAILPIT}" --network=host \
axllent/mailpit \
--smtp "0.0.0.0:${SMTP_PORT}" --listen "0.0.0.0:${EMAIL_API_PORT}" \
--smtp "localhost:${SMTP_PORT}" --listen "localhost:${EMAIL_API_PORT}" \
--smtp-auth-accept-any --smtp-auth-allow-insecure
export GANTRY_NOTIFICATION_APPRISE_URL="http://localhost:${APPRISE_PORT}/notify"
export GANTRY_NOTIFICATION_TITLE="TEST_TITLE"
Expand All @@ -58,7 +55,7 @@ Describe 'Notify'
docker logs "${SERVICE_NAME_APPRISE}" 2>&1
echo "Print Mailpit log:"
docker logs "${SERVICE_NAME_MAILPIT}" 2>&1
# Use docker_remove to improve coverage on lib-common.sh.
# Use docker_remove to improve coverage on lib-common.sh. `docker stop` and `docker rm` can do the same thing.
docker_remove "${SERVICE_NAME_APPRISE}"
docker_remove "${SERVICE_NAME_MAILPIT}"
return "${RETURN_VALUE}"
Expand Down Expand Up @@ -135,4 +132,4 @@ Describe 'Notify'
The stderr should satisfy spec_expect_message "Failed to send notification via Apprise"
End
End
End # Describe 'Notify'
End # Describe 'Notify'
7 changes: 5 additions & 2 deletions tests/spec_gantry_test_helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ _stop_registry() {
initialize_all_tests() {
local SUITE_NAME="${1:-"gantry"}"
SUITE_NAME=$(echo "${SUITE_NAME}" | tr ' ' '-')
local SCRIPT_DIR=
SCRIPT_DIR="$(_get_script_dir)" || return 1
source "${SCRIPT_DIR}/../src/lib-common.sh"
echo "=============================="
echo "== Starting tests in ${SUITE_NAME}"
echo "=============================="
Expand Down Expand Up @@ -455,7 +458,7 @@ stop_service() {
docker service rm "${SERVICE_NAME}"
}

get_script_dir() {
_get_script_dir() {
# SC2128: Expanding an array without an index only gives the first element.
# SC3054 (warning): In POSIX sh, array references are undefined.
# shellcheck disable=SC2128,SC3054
Expand All @@ -477,7 +480,7 @@ _get_entrypoint() {
return 0
fi
local SCRIPT_DIR=
SCRIPT_DIR="$(get_script_dir)" || return 1
SCRIPT_DIR="$(_get_script_dir)" || return 1
export STATIC_VAR_ENTRYPOINT="${SCRIPT_DIR}/../src/entrypoint.sh"
echo "source ${STATIC_VAR_ENTRYPOINT}"
}
Expand Down

0 comments on commit fa0d4fb

Please sign in to comment.