ansible-collection/roles/cron_healthcheck_script/templates/healthcheck.sh.j2
GregoryDosh b24a51083d
Some checks failed
Create Release / Create Release (push) Failing after 6s
Update Version / Update Version (push) Successful in 6s
ci: adjust ci to use more license information & sha for tracing
2024-12-10 20:40:44 -06:00

70 lines
2.7 KiB
Django/Jinja

#!/bin/sh
# Ansible Roles for managing Auengun.net Infrastructure & Testing/Learning.
# Source available at git.auengun.net/homelab/ansible-collection
# Copyright (C) 2023 GregoryDosh
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# SPDX-License-Identifier: AGPL-3.0-or-later
# SPDX-FileCopyrightText: 2023 GregoryDosh
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