From 39e53ccb11ac43c8e1911c86315d08d5714f7098 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 27 Apr 2025 14:25:06 +1200 Subject: [PATCH] Fix script --- .../dropshell-agent/_allservicesstatus.sh | 55 +++++++++++++------ 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/templates/dropshell-agent/_allservicesstatus.sh b/templates/dropshell-agent/_allservicesstatus.sh index 062bfe8..8397ffc 100644 --- a/templates/dropshell-agent/_allservicesstatus.sh +++ b/templates/dropshell-agent/_allservicesstatus.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -x # This script checks ALL services on the server and returns a status for each. @@ -21,10 +22,10 @@ SCRIPT_DIR="$(dirname "$0")" # // |-- service.env # // |-- (other config files for specific server&service) - function run_command() { local service_path=$1 local command=$2 + local capture_output=${3:-false} # default to false if not specified # check if the command is a file 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 ( source "${service_path}/config/service.env" - # SERVER is correct + + # update the main variables. export CONFIG_PATH="${service_path}/config" - # update the SERVICE variable + # SERVER is correct 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 SERVICES_PATH="${SCRIPT_DIR}/../../" @@ -50,29 +70,32 @@ SERVICE_NAMES=$(ls "${SERVICES_PATH}") # Iterate over all service names for SERVICE_NAME in ${SERVICE_NAMES}; do - # Get the service health + 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 - if $(bash "${STATUS_FILE}" "${SERVICE_PATH}/config" > /dev/null 2>&1); then + + #-------------------------------- + # Get the service health + 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" else SERVICE_HEALTH="unhealthy" fi - else - SERVICE_HEALTH="unknown" fi + #-------------------------------- # Get the service ports - PORTS_FILE="${SERVICE_PATH}/template/ports.sh" - if [ -f "${PORTS_FILE}" ]; then - # capture output from the ports script - SERVICE_PORTS=$(bash "${PORTS_FILE}" "${SERVICE_PATH}/config" 2>&1) - else + if ! command_exists "${SERVICE_PATH}" "ports"; then SERVICE_PORTS="" + else + run_command "${SERVICE_PATH}" "ports" "true" + SERVICE_PORTS="${CURRENT_OUTPUT}" fi + #-------------------------------- # return the health and ports echo "${SERVICE_NAME}_HEALTH=${SERVICE_HEALTH}" echo "${SERVICE_NAME}_PORTS=${SERVICE_PORTS}"