diff --git a/source/agent/_allservicesstatus.sh b/source/agent/_allservicesstatus.sh index 4a2b694..99bdc1b 100755 --- a/source/agent/_allservicesstatus.sh +++ b/source/agent/_allservicesstatus.sh @@ -9,6 +9,7 @@ # Get all services on the server SCRIPT_DIR="$(dirname "$0")" + # //------------------------------------------------------------------------------------------------ # // remote paths # // DROPSHELL_DIR # // |-- backups @@ -19,17 +20,19 @@ SCRIPT_DIR="$(dirname "$0")" # // |-- services # // |-- service name # // |-- config - # // |-- service.env + # // |-- service.env (actual service config) + # // |-- .template_info.env # // |-- template + # // |-- _default.env # // |-- (script files) # // |-- config - # // |-- service.env + # // |-- service.env (default service config) + # // |-- .template_info.env # // |-- (other config files for specific server&service) # Get all services on the server SERVICES_PATH=$(realpath "${SCRIPT_DIR}/../services/") - CURRENT_OUTPUT="" CURRENT_EXIT_CODE=0 @@ -40,6 +43,15 @@ load_dotenv(){ fi } +_check_required_env_vars_allservicesstatus() { + local required_vars=("$@") + for var in "${required_vars[@]}"; do + if [ -z "${!var}" ]; then + _die "Required environment variable $var is not set" + fi + done +} + function run_command() { local service_path=$1 local command=$2 @@ -55,13 +67,23 @@ function run_command() { set -a load_dotenv "${service_path}/template/_default.env" load_dotenv "${service_path}/config/service.env" + load_dotenv "${service_path}/config/.template_info.env" set +a + # all_env_vars["CONFIG_PATH"] = remotepath::service_config(server_name,service_name); + # all_env_vars["SERVER"] = server_name; + # all_env_vars["SERVICE"] = service_name; + # all_env_vars["AGENT_PATH"] = remotepath::agent(server_name); + # all_env_vars["HOST_NAME"] = server_info.ssh_host; # update the main variables. export CONFIG_PATH="${service_path}/config" - # SERVER is correct + # SERVER is already set export SERVICE="${SERVICE_NAME}" - + export AGENT_PATH="${SCRIPT_DIR}" + # HOST_NAME is already set + + _check_required_env_vars_allservicesstatus "CONFIG_PATH" "SERVER" "SERVICE" "AGENT_PATH" "HOST_NAME" "TEMPLATE" + if [ "$capture_output" = "true" ]; then # Capture and return output bash "${service_path}/template/${command}.sh" 2>&1 diff --git a/source/src/service_runner.cpp b/source/src/service_runner.cpp index 478ba0e..2cdbf7d 100644 --- a/source/src/service_runner.cpp +++ b/source/src/service_runner.cpp @@ -182,7 +182,7 @@ std::map service_runner::get_all_services_status(std } std::string output; - if (!execute_ssh_command(env.get_SSH_INFO(), sCommand(remotepath::agent(server_name), "./_allservicesstatus.sh", {}), cMode::CaptureOutput, &output)) + if (!execute_ssh_command(env.get_SSH_INFO(), sCommand(remotepath::agent(server_name), "./_allservicesstatus.sh", {{"HOST_NAME", server_name}}), cMode::CaptureOutput, &output)) return status; std::stringstream ss(output); diff --git a/source/src/services.cpp b/source/src/services.cpp index 3c7eecd..1e4277b 100644 --- a/source/src/services.cpp +++ b/source/src/services.cpp @@ -225,15 +225,17 @@ bool get_all_service_env_vars(const std::string &server_name, const std::string return false; } + ServerInfo server_info = get_server_info(server_name); + if (server_info.ssh_host.empty()) + std::cerr << "Error: Server " << server_name << " not found - ssh_host empty, so HOST_NAME not set" << std::endl; + + // add in some handy variables. + // if we change these, we also need to update agent/_allservicesstatus.sh all_env_vars["CONFIG_PATH"] = remotepath::service_config(server_name,service_name); all_env_vars["SERVER"] = server_name; all_env_vars["SERVICE"] = service_name; all_env_vars["AGENT_PATH"] = remotepath::agent(server_name); - - ServerInfo server_info = get_server_info(server_name); - if (server_info.ssh_host.empty()) - std::cerr << "Error: Server " << server_name << " not found - ssh_host empty, so HOST_NAME not set" << std::endl; all_env_vars["HOST_NAME"] = server_info.ssh_host; // Lambda function to load environment variables from a file diff --git a/source/src/utils/directories.cpp b/source/src/utils/directories.cpp index 18b4695..d3016cb 100644 --- a/source/src/utils/directories.cpp +++ b/source/src/utils/directories.cpp @@ -83,23 +83,27 @@ namespace localpath { } } // namespace localpath -// ------------------------------------------------------------------------------------------ + //------------------------------------------------------------------------------------------------ // remote paths // DROPSHELL_DIR // |-- backups // |-- temp_files // |-- agent + // | |-- bb64 + // | |-- (other agent files, including _allservicesstatus.sh) // |-- services // |-- service name // |-- config - // |-- service.env + // |-- service.env (actual service config) // |-- template + // |-- _default.env // |-- (script files) // |-- config - // |-- service.env + // |-- service.env (default service config) // |-- (other config files for specific server&service) + namespace remotefile { std::string service_env(const std::string &server_name, const std::string &service_name) diff --git a/source/src/utils/directories.hpp b/source/src/utils/directories.hpp index 758ebe8..93b47f3 100644 --- a/source/src/utils/directories.hpp +++ b/source/src/utils/directories.hpp @@ -76,11 +76,14 @@ namespace dropshell { // |-- services // |-- service name // |-- config - // |-- service.env + // |-- service.env (actual service config) + // |-- .template_info.env // |-- template + // |-- _default.env // |-- (script files) // |-- config - // |-- service.env + // |-- service.env (default service config) + // |-- .template_info.env // |-- (other config files for specific server&service) namespace remotefile {