From a51794031092b8046b15ce05008fb98f4b22bcb4 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 27 Apr 2025 14:55:07 +1200 Subject: [PATCH] getting there --- src/main.cpp | 2 +- .../dropshell-agent/_allservicesstatus.sh | 31 +++++++++++++------ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index b21a78f..cbe2dde 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -212,7 +212,7 @@ int main(int argc, char* argv[]) { // handle running a command. std::set commands; dropshell::get_all_used_commands(commands); - commands.merge(std::set{"ssh","edit"}); // handled by service_runner, but not in template_shell_commands. + commands.merge(std::set{"ssh","edit","_allservicesstatus"}); // handled by service_runner, but not in template_shell_commands. for (const auto& command : commands) { if (cmd == command) { std::string server_name; diff --git a/templates/dropshell-agent/_allservicesstatus.sh b/templates/dropshell-agent/_allservicesstatus.sh index 8397ffc..0e16fd2 100644 --- a/templates/dropshell-agent/_allservicesstatus.sh +++ b/templates/dropshell-agent/_allservicesstatus.sh @@ -1,5 +1,4 @@ #!/bin/bash -set -x # This script checks ALL services on the server and returns a status for each. @@ -22,6 +21,16 @@ SCRIPT_DIR="$(dirname "$0")" # // |-- service.env # // |-- (other config files for specific server&service) +CURRENT_OUTPUT="" +CURRENT_EXIT_CODE=0 + +load_dotenv(){ + local file_path=$1 + if [ -f "${file_path}" ]; then + source <("${file_path}" | sed -e '/^#/d;/^\s*$/d' -e "s/'/'\\\''/g" -e "s/=\(.*\)/='\1'/g") + fi +} + function run_command() { local service_path=$1 local command=$2 @@ -33,8 +42,11 @@ function run_command() { fi # run the command in a subshell to prevent environment changes - ( - source "${service_path}/config/service.env" + CURRENT_OUTPUT=$( + set -a + load_dotenv "${service_path}/template/_default.env" + load_dotenv "${service_path}/config/service.env" + set +a # update the main variables. export CONFIG_PATH="${service_path}/config" @@ -43,16 +55,15 @@ function run_command() { if [ "$capture_output" = "true" ]; then # Capture and return output - CURRENT_OUTPUT=$(bash "${service_path}/template/${command}.sh" 2>&1) - CURRENT_EXIT_CODE=$? + bash "${service_path}/template/${command}.sh" 2>&1 else # Run silently and return exit code - bash "${service_path}/template/${command}.sh" >/dev/null 2>&1 - CURRENT_OUTPUT="" - CURRENT_EXIT_CODE=$? + bash "${service_path}/template/${command}.sh" > /dev/null 2>&1 fi ) + CURRENT_EXIT_CODE=$? } + function command_exists() { local service_path=$1 local command=$2 @@ -63,7 +74,7 @@ function command_exists() { } # Get all services on the server -SERVICES_PATH="${SCRIPT_DIR}/../../" +SERVICES_PATH=$(realpath "${SCRIPT_DIR}/../../") # Get all service names SERVICE_NAMES=$(ls "${SERVICES_PATH}") @@ -71,7 +82,7 @@ SERVICE_NAMES=$(ls "${SERVICES_PATH}") # Iterate over all service names for SERVICE_NAME in ${SERVICE_NAMES}; do - SERVICE_PATH="${SERVICES_PATH}/${SERVICE_NAME}" + SERVICE_PATH=$(realpath "${SERVICES_PATH}/${SERVICE_NAME}") #-------------------------------- # Get the service health