Skip to content

Commit

Permalink
fix shellcheck problems. fix error getting config name from a service
Browse files Browse the repository at this point in the history
  • Loading branch information
shizunge committed Jun 26, 2023
1 parent a7280d5 commit 446906c
Show file tree
Hide file tree
Showing 7 changed files with 348 additions and 246 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/shellcheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: On push to main

on:
push:
paths:
- 'src/*'
pull_request:
branches:
- 'main'

jobs:
shellcheck:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Install shellcheck
run: sudo apt-get install -y bash shellcheck
- name: Analyse with shellcheck
run: |
cd src
shellcheck -V
shellcheck *
30 changes: 30 additions & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# .shellcheckrc

# https://github.com/koalaman/shellcheck/wiki/Optional
enable=add-default-case
enable=avoid-nullary-conditions
# enable=check-unassigned-uppercase
enable=deprecate-which
enable=require-variable-braces

# SC2039: In POSIX sh, 'local' is undefined.
disable=SC2039

# TODO: Try to fix and enable SC3001
# SC3001 (warning): In POSIX sh, process substitution is undefined.
disable=SC3001

# SC3037 (warning): In POSIX sh, echo flags are undefined.
disable=SC3037

# SC3043 (warning): In POSIX sh, 'local' is undefined.
disable=SC3043

# SC3046 (warning): In POSIX sh, 'source' in place of '.' is undefined.
disable=SC3046

# SC3051 (warning): In POSIX sh, 'source' in place of '.' is undefined.
disable=SC3051

# SC3057 (warning): In POSIX sh, string indexing is undefined.
disable=SC3057
20 changes: 12 additions & 8 deletions src/docker_hub_rate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,28 @@
#

docker_hub_rate() {
local IMAGE=${1:-ratelimitpreview/test}
local RESPONSE=$(wget -qO- "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${IMAGE}:pull")
[ $? -ne 0 ] && echo "[GET TOKEN RESPONSE ERROR]" && return 1
local TOKEN=$(echo ${RESPONSE} | sed 's/.*"token":"\([^"]*\).*/\1/')
local IMAGE="${1:-ratelimitpreview/test}"
local RESPONSE=
if ! RESPONSE=$(wget -qO- "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${IMAGE}:pull"); then
echo "[GET TOKEN RESPONSE ERROR]"
return 1
fi
local TOKEN=
TOKEN=$(echo "${RESPONSE}" | sed 's/.*"token":"\([^"]*\).*/\1/')
[ -z "${TOKEN}" ] && echo "[GET TOKEN ERROR]" && return 1
local HEADER="Authorization: Bearer ${TOKEN}"
local URL="https://registry-1.docker.io/v2/${IMAGE}/manifests/latest"
# adding --spider implies that you want to send a HEAD request (as opposed to GET or POST).
RESPONSE=$(wget -qS --spider --header="${HEADER}" -O /dev/null "${URL}" 2>&1)
if [ $? -ne 0 ]; then
if ! RESPONSE=$(wget -qS --spider --header="${HEADER}" -O /dev/null "${URL}" 2>&1); then
if echo "${RESPONSE}" | grep -q "Too Many Requests" ; then
echo "0"
return 0
fi
echo "[GET RATE RESPONSE ERROR]"
return 1
fi
local RATE=$(echo ${RESPONSE} | sed -n 's/.*ratelimit-remaining: \([0-9]*\).*/\1/p' )
local RATE=
RATE=$(echo "${RESPONSE}" | sed -n 's/.*ratelimit-remaining: \([0-9]*\).*/\1/p' )
[ -z "${RATE}" ] && echo "[GET RATE ERROR]" && return 1
echo ${RATE}
echo "${RATE}"
}
44 changes: 27 additions & 17 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ source ./lib-common.sh
source ./lib-gantry.sh

skip_current_node() {
local SELF_ID=$(docker node inspect self --format {{.Description.Hostname}} 2>/dev/null);
local SELF_ID=
SELF_ID=$(docker node inspect self --format "{{.Description.Hostname}}" 2>/dev/null);
if [ -z "${SELF_ID}" ]; then
log WARN "Skip because the current node is not a swarm manager.";
return 0
Expand All @@ -30,63 +31,72 @@ skip_current_node() {
}

gantry() {
local STACK=${1:-gantry}
local START_TIME=$(date +%s)
local STACK="${1:-gantry}"
local START_TIME=
START_TIME=$(date +%s)

if skip_current_node ; then
return 0
fi
local ACCUMULATED_ERRORS=0
local DOCKER_HUB_RATE_BEFORE=
local DOCKER_HUB_RATE_AFTER=
local DOCKER_HUB_RATE_USED=
local TIME_ELAPSED=

log INFO "Starting."
gantry_initialize "${STACK}"
ACCUMULATED_ERRORS=$((ACCUMULATED_ERRORS + $?))

local DOCKER_HUB_RATE_BEFORE=$(docker_hub_rate)
# SC2119: Use docker_hub_rate "$@" if function's $1 should mean script's $1.
# shellcheck disable=SC2119
DOCKER_HUB_RATE_BEFORE=$(docker_hub_rate)
ACCUMULATED_ERRORS=$((ACCUMULATED_ERRORS + $?))
log INFO "Before updating, Docker Hub rate remains ${DOCKER_HUB_RATE_BEFORE}."

log INFO "Starting updating."
gantry_update_services_list $(gantry_get_services_list)
gantry_update_services_list "$(gantry_get_services_list)"
ACCUMULATED_ERRORS=$((ACCUMULATED_ERRORS + $?))

local DOCKER_HUB_RATE_AFTER=$(docker_hub_rate)
# SC2119: Use docker_hub_rate "$@" if function's $1 should mean script's $1.
# shellcheck disable=SC2119
DOCKER_HUB_RATE_AFTER=$(docker_hub_rate)
ACCUMULATED_ERRORS=$((ACCUMULATED_ERRORS + $?))
local DOCKER_HUB_RATE_USED=$(difference_between "${DOCKER_HUB_RATE_BEFORE}" "${DOCKER_HUB_RATE_AFTER}")
DOCKER_HUB_RATE_USED=$(difference_between "${DOCKER_HUB_RATE_BEFORE}" "${DOCKER_HUB_RATE_AFTER}")
log INFO "After updating, Docker Hub rate remains ${DOCKER_HUB_RATE_AFTER}. Used rate ${DOCKER_HUB_RATE_USED}."

gantry_finalize "${STACK}";
ACCUMULATED_ERRORS=$((ACCUMULATED_ERRORS + $?))

local TIME_ELAPSED=$(time_elapsed_since ${START_TIME})
TIME_ELAPSED=$(time_elapsed_since "${START_TIME}")
local MESSAGE="Done. Use ${TIME_ELAPSED}. ${ACCUMULATED_ERRORS} errors."
if [ ${ACCUMULATED_ERRORS} -gt 0 ]; then
log WARN ${MESSAGE}
log WARN "${MESSAGE}"
else
log INFO ${MESSAGE}
log INFO "${MESSAGE}"
fi
return ${ACCUMULATED_ERRORS}
}

main() {
LOG_LEVEL=${GANTRY_LOG_LEVEL:-${LOG_LEVEL}}
NODE_NAME=${GANTRY_NODE_NAME:-${NODE_NAME}}
local SLEEP_SECONDS=${GANTRY_SLEEP_SECONDS:-0}
LOG_LEVEL="${GANTRY_LOG_LEVEL:-${LOG_LEVEL}}"
NODE_NAME="${GANTRY_NODE_NAME:-${NODE_NAME}}"
local SLEEP_SECONDS="${GANTRY_SLEEP_SECONDS:-0}"
if ! is_number "${SLEEP_SECONDS}"; then
log ERROR "GANTRY_SLEEP_SECONDS must be a number. Got \"${GANTRY_SLEEP_SECONDS}\"."
return 1;
fi
local STACK=${1:-gantry}
local STACK="${1:-gantry}"
local RETURN_VALUE=0
while true; do
LOG_SCOPE=${STACK}
gantry ${@}
gantry "${@}"
RETURN_VALUE=$?
[ "${SLEEP_SECONDS}" -le 0 ] && break;
log INFO "Sleeping ${SLEEP_SECONDS} seconds before next update."
sleep ${SLEEP_SECONDS}
sleep "${SLEEP_SECONDS}"
done
return ${RETURN_VALUE}
}

main ${@}
main "${@}"
Loading

0 comments on commit 446906c

Please sign in to comment.