diff --git a/src/server_env.cpp b/src/server_env.cpp index dc06c3f..1e4c730 100644 --- a/src/server_env.cpp +++ b/src/server_env.cpp @@ -4,6 +4,7 @@ #include "utils/utils.hpp" #include "services.hpp" #include "contrib/base64.hpp" +#include "templates.hpp" #include #include @@ -76,10 +77,7 @@ std::string server_env::construct_standard_command_run_cmd(const std::string &se std::string script_path = remote_service_template_path + "/" + command + ".sh"; std::map env_vars; - envmanager env_manager(get_local_service_env_path(mServer_name,service_name)); - env_manager.load(); - env_manager.get_all_variables(env_vars); - env_vars["CONFIG_PATH"] = remote_service_config_path; + get_all_service_env_vars(service_name, env_vars); std::string argstr = quote(remote_service_config_path); for (const auto& arg : args) { @@ -91,6 +89,40 @@ std::string server_env::construct_standard_command_run_cmd(const std::string &se return run_cmd; } +void server_env::get_all_service_env_vars(const std::string &service_name, std::map & all_env_vars) const +{ + all_env_vars.clear(); + + // add in some handy variables. + all_env_vars["CONFIG_PATH"] = get_remote_service_config_path(mServer_name,service_name); + all_env_vars["SERVER"] = mServer_name; + all_env_vars["SERVICE"] = service_name; + + + { // load service.env from the service on this machine. + std::map env_vars; + envmanager env_manager(get_local_service_env_path(mServer_name,service_name)); + env_manager.load(); + env_manager.get_all_variables(env_vars); + all_env_vars.merge(env_vars); + } + + { // load _default.env from the template on this machine. + std::map env_vars; + ServiceInfo service_info = get_service_info(mServer_name, service_name); + std::string defaultenvpath = service_info.local_template_default_env_path; + if (std::filesystem::exists(defaultenvpath)) { + envmanager env_manager(defaultenvpath); + env_manager.load(); + env_manager.get_all_variables(env_vars); + all_env_vars.merge(env_vars); + } + else + std::cerr << "Warning: _default.env not found in template: " << defaultenvpath << std::endl; + } +} + + bool server_env::check_remote_dir_exists(const std::string &dir_path) const { sCommand scommand("test -d " + quote(dir_path)); @@ -125,9 +157,6 @@ bool server_env::check_remote_items_exist(const std::vector &file_p bool server_env::execute_ssh_command(const sCommand& command) const { std::string full_cmd = construct_ssh_cmd() + " " + quote(command.construct_safecmd()); bool okay = execute_local_command(full_cmd); - if (!okay) { - std::cerr << "Error: Failed to execute command on remote server: " << full_cmd << std::endl; - } return okay; } @@ -135,9 +164,6 @@ bool server_env::execute_ssh_command_and_capture_output(const sCommand& command, { std::string full_cmd = construct_ssh_cmd() + " " + quote(command.construct_safecmd()); bool okay = execute_local_command_and_capture_output(full_cmd, output); - if (!okay) { - std::cerr << "Error: Failed to execute command on remote server: " << full_cmd << std::endl; - } return okay; } @@ -145,17 +171,11 @@ bool server_env::run_remote_template_command(const std::string &service_name, co { std::string full_cmd = construct_standard_command_run_cmd(service_name, command, args, silent); bool okay = execute_ssh_command(full_cmd); - if (!okay) { - std::cerr << "Error: Failed to execute command on remote server: " << full_cmd << std::endl; - } return okay; } bool server_env::execute_local_command(const sCommand& command) { bool okay = (system(command.construct_safecmd().c_str()) == 0); - if (!okay) { - std::cerr << "Error: Failed to execute command on local machine: " << command.construct_safecmd() << std::endl; - } return okay; } diff --git a/src/server_env.hpp b/src/server_env.hpp index 8a86455..5dd2bd4 100644 --- a/src/server_env.hpp +++ b/src/server_env.hpp @@ -73,6 +73,9 @@ class server_env { std::string construct_ssh_cmd() const; std::string construct_standard_command_run_cmd(const std::string& service_name, const std::string& command, std::vector args, bool silent) const; + private: + void get_all_service_env_vars(const std::string& service_name, std::map & all_env_vars) const; + private: std::string mServer_name; std::map variables; diff --git a/src/services.cpp b/src/services.cpp index 2785b8c..8a52583 100644 --- a/src/services.cpp +++ b/src/services.cpp @@ -95,6 +95,7 @@ ServiceInfo get_service_info(const std::string &server_name, const std::string & // find the template path service.local_template_path = tinfo.local_template_path; + service.local_template_default_env_path = tinfo.local_template_default_env_path; return service; } diff --git a/src/services.hpp b/src/services.hpp index 66f4235..a29d259 100644 --- a/src/services.hpp +++ b/src/services.hpp @@ -12,6 +12,7 @@ namespace dropshell { std::string template_name; std::string local_service_path; std::string local_template_path; + std::string local_template_default_env_path; }; std::vector get_server_services_info(const std::string& server_name); diff --git a/src/templates.cpp b/src/templates.cpp index b2fc501..09a853d 100644 --- a/src/templates.cpp +++ b/src/templates.cpp @@ -27,6 +27,7 @@ bool get_templates(std::vector& templates) { template_info info; info.template_name = entry.path().filename().string(); info.local_template_path = entry.path().string(); + info.local_template_default_env_path = entry.path() / "_default.env"; // Check if template with same name already exists bool duplicate = false; diff --git a/src/templates.hpp b/src/templates.hpp index 6544b36..6babee8 100644 --- a/src/templates.hpp +++ b/src/templates.hpp @@ -8,6 +8,7 @@ class template_info { public: std::string template_name; std::string local_template_path; + std::string local_template_default_env_path; }; // templates are stored in two locations: diff --git a/templates/example/_common.sh b/templates/example/_common.sh index e4e99d5..ec4aca2 100755 --- a/templates/example/_common.sh +++ b/templates/example/_common.sh @@ -14,41 +14,6 @@ die() { exit 1 } -# Load environment variables from .env file -# Usage: load_env [path_to_env_file] -# If no path is provided, looks for .env in the same directory as the script -load_env() { - local script_dir="$(dirname "${BASH_SOURCE[0]}")" - local env_file - - # first load basic.env for the template defaults - if [ -f "$script_dir/_basic.env" ]; then - set -a - source "$script_dir/_basic.env" - set +a - else - echo "Warning: _basic.env file not found at $script_dir/_basic.env. Broken template?" - return 1 - fi - - # now load the server specific env file - if [ -z "$1" ]; then - echo "Usage: $0 [path_to_env_file]" - return 1 - fi - - env_file="$1/service.env" - - if [ ! -f "$env_file" ]; then - echo "Warning: service.env file not found at $1/service.env" - return 1 - fi - - set -a - source "$env_file" - set +a -} - grey_start() { echo -e -n "\033[90m" } diff --git a/templates/example/_basic.env b/templates/example/_default.env similarity index 64% rename from templates/example/_basic.env rename to templates/example/_default.env index fc207fa..adf19cd 100644 --- a/templates/example/_basic.env +++ b/templates/example/_default.env @@ -4,5 +4,5 @@ CONTAINER_PORT=8181 # Image settings -IMAGE_REGISTRY="gitea.jde.nz" -IMAGE_REPO="example/example" +IMAGE_REGISTRY="docker.io" +IMAGE_REPO="nginx" diff --git a/templates/example/example/service.env b/templates/example/example/service.env index 851c557..37f056c 100644 --- a/templates/example/example/service.env +++ b/templates/example/example/service.env @@ -2,8 +2,14 @@ TEMPLATE=example # Service settings specific to this server -# (can also override anything in the _basic.env file in the template to make it specific to this server) -HOST_PORT=80 +# (can also override anything in the _default.env file in the template to make it specific to this server) +HOST_PORT=60123 LOCAL_DATA_FOLDER="${HOME}/.example" +CONTAINER_NAME=example-nginx IMAGE_TAG="latest" +# Scripts will have these environment variables set, plus those in _default.env, plus: +# SERVER, SERVICE, CONFIG_PATH +# CONFIG_PATH points to this directory! + + diff --git a/templates/example/install.sh b/templates/example/install.sh index fcb998d..33b736b 100755 --- a/templates/example/install.sh +++ b/templates/example/install.sh @@ -6,18 +6,23 @@ # It is called with the path to the server specific env file as an argument. source "$(dirname "$0")/_common.sh" -load_env "$1" || die "Failed to load environment variables" # Required environment variables check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "LOCAL_DATA_FOLDER" - # Create local data folder if it doesn't exist if [ -d "${LOCAL_DATA_FOLDER}" ]; then echo "Local data folder ${LOCAL_DATA_FOLDER} exists, using existing data." else echo "Local data folder ${LOCAL_DATA_FOLDER} does not exist, creating..." mkdir -p "${LOCAL_DATA_FOLDER}" + cat < "${LOCAL_DATA_FOLDER}/index.html" + + +

Hello, World!

+ + +EOF fi # Test Docker @@ -33,3 +38,4 @@ _remove_container $CONTAINER_NAME || die "Failed to remove container ${CONTAINER bash ./start.sh $1 || die "Failed to start container ${CONTAINER_NAME}" echo "Installation of ${CONTAINER_NAME} complete" +echo "You can access the service at http://${SERVER}:{HOST_PORT}" diff --git a/templates/example/logs.sh b/templates/example/logs.sh index 691e17b..09bad2c 100644 --- a/templates/example/logs.sh +++ b/templates/example/logs.sh @@ -13,5 +13,5 @@ check_required_env_vars "CONTAINER_NAME" echo "Container ${CONTAINER_NAME} logs:" grey_start -docker logs --tail 100 "${CONTAINER_NAME}" +docker logs "${CONTAINER_NAME}" grey_end diff --git a/templates/example/ports.sh b/templates/example/ports.sh index 9779af4..6c840a2 100644 --- a/templates/example/ports.sh +++ b/templates/example/ports.sh @@ -11,4 +11,4 @@ load_env "$1" || die "Failed to load environment variables" # Required environment variables # check_required_env_vars "HOST_PORT" -# echo $HOST_PORT +echo $HOST_PORT diff --git a/templates/watchtower/_common.sh b/templates/watchtower/_common.sh index ffc5c07..ec4aca2 100755 --- a/templates/watchtower/_common.sh +++ b/templates/watchtower/_common.sh @@ -14,41 +14,6 @@ die() { exit 1 } -# Load environment variables from .env file -# Usage: load_env [path_to_env_file] -# If no path is provided, looks for .env in the same directory as the script -load_env() { - local script_dir="$(dirname "${BASH_SOURCE[0]}")" - local env_file - - # first load basic.env for the template defaults - if [ -f "$script_dir/_basic.env" ]; then - set -a - source "$script_dir/_basic.env" - set +a - else - echo "Warning: _basic.env file not found at $script_dir/_basic.env. Broken template?" - return 1 - fi - - # now load the server specific env file - if [ -z "$1" ]; then - echo "Usage: $0 [path_to_env_file]" - return 1 - fi - - env_file="$1/service.env" - - if [ ! -f "$env_file" ]; then - echo "Warning: service.env file not found at $1" - return 1 - fi - - set -a - source "$env_file" - set +a -} - grey_start() { echo -e -n "\033[90m" } diff --git a/templates/watchtower/_basic.env b/templates/watchtower/_default.env similarity index 100% rename from templates/watchtower/_basic.env rename to templates/watchtower/_default.env diff --git a/templates/watchtower/install.sh b/templates/watchtower/install.sh index 97fa6e3..f2171f0 100755 --- a/templates/watchtower/install.sh +++ b/templates/watchtower/install.sh @@ -1,6 +1,5 @@ #!/bin/bash source "$(dirname "$0")/_common.sh" -load_env "$1" || die "Failed to load environment variables" # Required environment variables check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" diff --git a/templates/watchtower/logs.sh b/templates/watchtower/logs.sh index 915b61c..05e3930 100644 --- a/templates/watchtower/logs.sh +++ b/templates/watchtower/logs.sh @@ -1,11 +1,10 @@ #!/bin/bash source "$(dirname "$0")/_common.sh" -load_env "$1" || die "Failed to load environment variables" # Required environment variables check_required_env_vars "CONTAINER_NAME" echo "Container ${CONTAINER_NAME} logs:" grey_start -docker logs --tail 100 "${CONTAINER_NAME}" +docker logs "${CONTAINER_NAME}" grey_end diff --git a/templates/watchtower/start.sh b/templates/watchtower/start.sh index ac7b67d..0eacaa4 100755 --- a/templates/watchtower/start.sh +++ b/templates/watchtower/start.sh @@ -2,7 +2,6 @@ source "$(dirname "$0")/_common.sh" SERVICE_CONFIG_DIR="$1" -load_env "$SERVICE_CONFIG_DIR" || die "Failed to load environment variables" # Required environment variables check_required_env_vars "CONTAINER_NAME" "INTERVAL" diff --git a/templates/watchtower/status.sh b/templates/watchtower/status.sh index e939eb6..93162b7 100644 --- a/templates/watchtower/status.sh +++ b/templates/watchtower/status.sh @@ -1,6 +1,5 @@ #!/bin/bash source "$(dirname "$0")/_common.sh" -load_env "$1" || die "Failed to load environment variables" # Required environment variables check_required_env_vars "CONTAINER_NAME" diff --git a/templates/watchtower/stop.sh b/templates/watchtower/stop.sh index 42ee835..b9dd8ff 100755 --- a/templates/watchtower/stop.sh +++ b/templates/watchtower/stop.sh @@ -1,6 +1,5 @@ #!/bin/bash source "$(dirname "$0")/_common.sh" -load_env "$1" || die "Failed to load environment variables" # Required environment variables check_required_env_vars "CONTAINER_NAME" diff --git a/templates/watchtower/uninstall.sh b/templates/watchtower/uninstall.sh index 8dab330..cdab565 100644 --- a/templates/watchtower/uninstall.sh +++ b/templates/watchtower/uninstall.sh @@ -1,6 +1,5 @@ #!/bin/bash source "$(dirname "$0")/_common.sh" -load_env "$1" || die "Failed to load environment variables" # Required environment variables check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG"