Fix script

This commit is contained in:
John 2025-04-27 14:25:06 +12:00
parent 7a6e77420a
commit 39e53ccb11

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
set -x
# This script checks ALL services on the server and returns a status for each. # This script checks ALL services on the server and returns a status for each.
@ -21,10 +22,10 @@ SCRIPT_DIR="$(dirname "$0")"
# // |-- service.env # // |-- service.env
# // |-- (other config files for specific server&service) # // |-- (other config files for specific server&service)
function run_command() { function run_command() {
local service_path=$1 local service_path=$1
local command=$2 local command=$2
local capture_output=${3:-false} # default to false if not specified
# check if the command is a file # check if the command is a file
if [ ! -f "${service_path}/template/${command}.sh" ]; then if [ ! -f "${service_path}/template/${command}.sh" ]; then
@ -34,13 +35,32 @@ function run_command() {
# run the command in a subshell to prevent environment changes # run the command in a subshell to prevent environment changes
( (
source "${service_path}/config/service.env" source "${service_path}/config/service.env"
# SERVER is correct
# update the main variables.
export CONFIG_PATH="${service_path}/config" export CONFIG_PATH="${service_path}/config"
# update the SERVICE variable # SERVER is correct
export SERVICE="${SERVICE_NAME}" export SERVICE="${SERVICE_NAME}"
bash "${service_path}/template/${command}.sh" 2>&1
if [ "$capture_output" = "true" ]; then
# Capture and return output
CURRENT_OUTPUT=$(bash "${service_path}/template/${command}.sh" 2>&1)
CURRENT_EXIT_CODE=$?
else
# Run silently and return exit code
bash "${service_path}/template/${command}.sh" >/dev/null 2>&1
CURRENT_OUTPUT=""
CURRENT_EXIT_CODE=$?
fi
) )
} }
function command_exists() {
local service_path=$1
local command=$2
if [ ! -f "${service_path}/template/${command}.sh" ]; then
return 1
fi
return 0
}
# Get all services on the server # Get all services on the server
SERVICES_PATH="${SCRIPT_DIR}/../../" SERVICES_PATH="${SCRIPT_DIR}/../../"
@ -50,29 +70,32 @@ SERVICE_NAMES=$(ls "${SERVICES_PATH}")
# Iterate over all service names # Iterate over all service names
for SERVICE_NAME in ${SERVICE_NAMES}; do for SERVICE_NAME in ${SERVICE_NAMES}; do
# Get the service health
SERVICE_PATH="${SERVICES_PATH}/${SERVICE_NAME}" SERVICE_PATH="${SERVICES_PATH}/${SERVICE_NAME}"
STATUS_FILE="${SERVICE_PATH}/template/status.sh"
if [ -f "${STATUS_FILE}" ]; then #--------------------------------
# suppress all output from the status script # Get the service health
if $(bash "${STATUS_FILE}" "${SERVICE_PATH}/config" > /dev/null 2>&1); then if ! command_exists "${SERVICE_PATH}" "status"; then
SERVICE_HEALTH="unknown"
else
run_command "${SERVICE_PATH}" "status" "false"
if [ "${CURRENT_EXIT_CODE}" -eq 0 ]; then
SERVICE_HEALTH="healthy" SERVICE_HEALTH="healthy"
else else
SERVICE_HEALTH="unhealthy" SERVICE_HEALTH="unhealthy"
fi fi
else
SERVICE_HEALTH="unknown"
fi fi
#--------------------------------
# Get the service ports # Get the service ports
PORTS_FILE="${SERVICE_PATH}/template/ports.sh" if ! command_exists "${SERVICE_PATH}" "ports"; then
if [ -f "${PORTS_FILE}" ]; then
# capture output from the ports script
SERVICE_PORTS=$(bash "${PORTS_FILE}" "${SERVICE_PATH}/config" 2>&1)
else
SERVICE_PORTS="" SERVICE_PORTS=""
else
run_command "${SERVICE_PATH}" "ports" "true"
SERVICE_PORTS="${CURRENT_OUTPUT}"
fi fi
#--------------------------------
# return the health and ports # return the health and ports
echo "${SERVICE_NAME}_HEALTH=${SERVICE_HEALTH}" echo "${SERVICE_NAME}_HEALTH=${SERVICE_HEALTH}"
echo "${SERVICE_NAME}_PORTS=${SERVICE_PORTS}" echo "${SERVICE_NAME}_PORTS=${SERVICE_PORTS}"