Skip to content

Commit

Permalink
Fix unused client removal on restarted container
Browse files Browse the repository at this point in the history
Handle the case where the target does not exist
in the process of deleting unused clients.
Such a situation will not occur in newly launched containers,
but will occur if they are restarted.

During container restarts, container status are preserved.
If the unused database client was deleted in the last run,
grep will not match anything.
It returns non-zero code and the container stops there
because entrypoint sets option `-e`
(exit immediately on non-zero exit code excluding some special cases)

This commit make the uninstall process to handle the case
UNUSED_DB_CLIENTS is empty.
  • Loading branch information
kkimurak committed Jun 2, 2023
1 parent 456fc76 commit 7140f03
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion assets/runtime/functions
Expand Up @@ -237,7 +237,16 @@ gitlab_uninstall_unused_database_client() {
REGEX_DB_CLIENT_VERSIONS_IN_USE="-common${DB_CLIENT_VERSIONS_IN_USE}"

# remove unused client using regex above
UNUSED_DB_CLIENTS=$(apt-cache pkgnames postgresql-client | grep -v -e "${REGEX_DB_CLIENT_VERSIONS_IN_USE}" | tr '\n' ' ')
# grep may return non-zero code on mo match, so fake the exit code with the `|| true` to swallow that
UNUSED_DB_CLIENTS=$(apt-cache pkgnames postgresql-client | grep -v -e "${REGEX_DB_CLIENT_VERSIONS_IN_USE}" || true)
if [[ "${UNUSED_DB_CLIENTS}" == "" ]]; then
echo "- All installed version of clients are in use. Did not uninstalled any client..."
return
fi

# just to get clean log, convert newline (package name delimiter) to single whitespace
UNUSED_DB_CLIENTS=$(echo ${UNUSED_DB_CLIENTS} | tr '\n' ' ')

echo "- Uninstalling unused client(s): ${UNUSED_DB_CLIENTS}"
DEBIAN_FRONTEND=noninteractive apt-get -qq -y purge -- ${UNUSED_DB_CLIENTS} >/dev/null
fi
Expand Down

0 comments on commit 7140f03

Please sign in to comment.