ansible-cron/roles/healthcheck_script/templates/healthcheck.sh.j2
GregoryDosh 5f56f9342b
All checks were successful
Version Bump Galaxy.yml / Version Bump Galaxy.yml (push) Successful in 8s
Publish Collection / Publish Collection (push) Successful in 9s
Initial Dosh LLC Config 🌈🐻
2025-11-24 14:42:45 -06:00

53 lines
1.9 KiB
Django/Jinja

#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-or-later
# SPDX-FileCopyrightText: 2025 Dosh LLC
set +x
if [ "$1" = "-e" ]; then
set -x
logger="command echo"
shift
else
logger="command logger -t {{ HEALTHCHECK_FILE_NAME }}"
fi
HEALTHCHECK_TMP_FILE=$({{ MKTEMP_BIN_ABSOLUTE_PATH }} /tmp/hc_log_{{HEALTHCHECK_FILE_NAME}}.XXXXXX)
# From the following link, this removes the file for effectively most things
# except through the file descriptor or something like /proc/$PID/fd/$FD.
# It should be cleaned up after the process dies through any means (crash/close/exit/etc.)
# https://unix.stackexchange.com/questions/181937/how-create-a-temporary-file-in-shell-script
exec 3>"$HEALTHCHECK_TMP_FILE" # For Writing: echo -n 'Foo' >&3
exec 4<"$HEALTHCHECK_TMP_FILE" # For Reading: cat <&4
rm "$HEALTHCHECK_TMP_FILE"
HC_START_RESPONSE=$({{ CURL_BIN_ABSOLUTE_PATH }} --write-out '%{http_code}' --silent --output /dev/null -m 10 --retry 5 "{{ _hc_ping_url }}/start")
if [ "$HC_START_RESPONSE" -ne 200 ]; then
$logger "WARN: Unable to contact healthchecks for status updates."
$logger "WARN: Check will continue without healthcheck logging."
fi
HEALTHCHECK_CONTENT_HEADER="Content-Type: text/plain"
EXIT_STATUS=0
{% raw %}{
{% endraw %}
##########################################
# Conditional Healthcheck Specific Logic #
##########################################
{{ HEALTHCHECK_FILE_CONTENT }}
##########################################
{% raw %}} 2>&3 1>&3
{% endraw %}
HC_STOP_RESPONSE=$({{ CURL_BIN_ABSOLUTE_PATH }} --write-out '%{http_code}' --silent --output /dev/null -m 10 --retry 5 --header "${HEALTHCHECK_CONTENT_HEADER}" --data "$(cat <&4)" "{{ _hc_ping_url }}/$EXIT_STATUS")
if [ "$HC_STOP_RESPONSE" -ne 200 ]; then
$logger "WARN: Unable to contact healthchecks for status update."
fi
if [ "$EXIT_STATUS" -ne 0 ]; then
$logger "ERROR: user script failure"
exit "$EXIT_STATUS"
fi