diff --git a/src/server_env_manager.cpp b/src/server_env_manager.cpp index 7ddffab..d4c7760 100644 --- a/src/server_env_manager.cpp +++ b/src/server_env_manager.cpp @@ -12,33 +12,33 @@ namespace dropshell { -server_env_manager::server_env_manager(const std::string& server_name) : mValid(false), mServer_name(server_name) { +server_env_manager::server_env_manager(const std::string& server_name) : mValid(false), mServerName(server_name) { if (server_name.empty()) return; // Construct the full path to server.env - std::string env_path = localfile::server_env(server_name); + std::string server_env_path = localfile::server_env(server_name); // Check if file exists - if (!std::filesystem::exists(env_path)) { - std::cerr << "Server environment file not found: " + env_path << std::endl; + if (!std::filesystem::exists(server_env_path)) { + std::cerr << "Server environment file not found: " + server_env_path << std::endl; return; } try { // Use envmanager to handle the environment file - m_env_manager = std::unique_ptr(new envmanager(env_path)); - m_env_manager->load(); + envmanager env_manager(server_env_path); + env_manager.load(); // Get all variables - m_env_manager->get_all_variables_substituted(variables); + env_manager.get_all_variables_substituted(mVariables); // Verify required variables exist for (const auto& var : {"SSH_HOST", "SSH_USER", "SSH_PORT", "DROPSHELL_DIR"}) { - if (variables.find(var) == variables.end()) { + if (mVariables.find(var) == mVariables.end()) { // Print the variables identified in the file std::cout << "Variables identified in the file:" << std::endl; - for (const auto& v : variables) { + for (const auto& v : mVariables) { std::cout << " " << v.first << std::endl; } throw std::runtime_error("Missing required variable: " + std::string(var)); @@ -52,10 +52,11 @@ server_env_manager::server_env_manager(const std::string& server_name) : mValid( } std::string server_env_manager::get_variable(const std::string& name) const { - if (!m_env_manager) { + auto it = mVariables.find(name); + if (it == mVariables.end()) { return ""; } - return m_env_manager->get_variable_substituted(name); + return it->second; } // Helper method implementations @@ -68,8 +69,8 @@ std::string server_env_manager::construct_ssh_cmd() const { std::string server_env_manager::construct_standard_command_run_cmd(const std::string &service_name, const std::string &command, std::vector args, bool silent) const { - std::string remote_service_template_path = remotepath::service_template(mServer_name,service_name); - std::string remote_service_config_path = remotepath::service_config(mServer_name,service_name); + std::string remote_service_template_path = remotepath::service_template(mServerName,service_name); + std::string remote_service_config_path = remotepath::service_config(mServerName,service_name); std::string script_path = remote_service_template_path + "/" + command + ".sh"; @@ -91,23 +92,31 @@ void server_env_manager::get_all_service_env_vars(const std::string &service_nam all_env_vars.clear(); // add in some handy variables. - all_env_vars["CONFIG_PATH"] = remotepath::service_config(mServer_name,service_name); - all_env_vars["SERVER"] = mServer_name; + all_env_vars["CONFIG_PATH"] = remotepath::service_config(mServerName,service_name); + all_env_vars["SERVER"] = mServerName; all_env_vars["SERVICE"] = service_name; { // load service.env from the service on this machine. std::map env_vars; - envmanager env_manager(localfile::service_env(mServer_name,service_name)); + envmanager env_manager(localfile::service_env(mServerName,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. + { // load .template_info.env from the service 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; + envmanager env_manager(localfile::template_info_env(mServerName,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 - gets overridden by service.env + std::map env_vars; + ServiceInfo service_info = get_service_info(mServerName, service_name); + std::string defaultenvpath = service_info.local_template_path + "/_default.env"; if (std::filesystem::exists(defaultenvpath)) { envmanager env_manager(defaultenvpath); env_manager.load(); @@ -117,6 +126,7 @@ void server_env_manager::get_all_service_env_vars(const std::string &service_nam else std::cerr << "Warning: _default.env not found in template: " << defaultenvpath << std::endl; } + } diff --git a/src/server_env_manager.hpp b/src/server_env_manager.hpp index 0639e4e..1c80790 100644 --- a/src/server_env_manager.hpp +++ b/src/server_env_manager.hpp @@ -49,7 +49,7 @@ class server_env_manager { std::string get_variable(const std::string& name) const; // trivial getters. - const std::map& get_variables() const { return variables; } + const std::map& get_variables() const { return mVariables; } std::string get_SSH_HOST() const { return get_variable("SSH_HOST"); } std::string get_SSH_USER() const { return get_variable("SSH_USER"); } std::string get_SSH_PORT() const { return get_variable("SSH_PORT"); } @@ -80,10 +80,10 @@ class server_env_manager { 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; + std::string mServerName; + std::map mVariables; bool mValid; - std::unique_ptr m_env_manager; + //std::unique_ptr m_env_manager; }; } // namespace dropshell diff --git a/src/servers.cpp b/src/servers.cpp index 3f9741e..88e8afb 100644 --- a/src/servers.cpp +++ b/src/servers.cpp @@ -175,11 +175,6 @@ void create_server(const std::string &server_name) // 4. add dropshell-agent service to server create_service(server_name, "dropshell-agent", "dropshell-agent", true); // silently create service. - // std::string service_dir = server_dir + "/dropshell-agent"; - // std::filesystem::create_directory(service_dir); - // std::string service_env_path = service_dir + "/service.env"; - // std::filesystem::copy(get_local_system_templates_path() + "/dropshell-agent/example/service.env", service_env_path); - std::cout << "Server created successfully: " << server_name << std::endl; std::cout << "Please complete the installation:" < get_server_services_info(const std::string& server_name); diff --git a/src/utils/directories.cpp b/src/utils/directories.cpp index 76dbe28..113887d 100644 --- a/src/utils/directories.cpp +++ b/src/utils/directories.cpp @@ -11,33 +11,32 @@ namespace dropshell { namespace localfile { + std::string dropshell_env() { - // Try ~/.config/dropshell/dropshell.env - const char* home = std::getenv("HOME"); - if (home) { - fs::path user_path = fs::path(home) / ".config" / "dropshell" / "dropshell.env"; - return user_path.string(); - } - std::cerr << "Warning: Couldn't determine user directory" << std::endl; - return std::string(); + // Try ~/.config/dropshell/dropshell.env + const char* home = std::getenv("HOME"); + if (home) { + fs::path user_path = fs::path(home) / ".config" / "dropshell" / "dropshell.env"; + return user_path.string(); + } + std::cerr << "Warning: Couldn't determine user directory" << std::endl; + return std::string(); } std::string server_env(const std::string &server_name) { - if (server_name.empty()) - return std::string(); std::string serverpath = localpath::server(server_name); - if (serverpath.empty()) - return std::string(); - return (fs::path(serverpath) / "server.env").string(); + return (serverpath.empty() ? "" : (fs::path(serverpath) / "server.env").string()); } std::string service_env(const std::string &server_name, const std::string &service_name) { - if (server_name.empty() || service_name.empty()) - return std::string(); std::string servicepath = localpath::service(server_name, service_name); - if (servicepath.empty()) - return std::string(); - return (fs::path(servicepath) / "service.env").string(); + return (servicepath.empty() ? "" : (fs::path(servicepath) / "service.env").string()); + } + + std::string template_info_env(const std::string &server_name, const std::string &service_name) + { + std::string servicepath = localpath::service(server_name, service_name); + return (servicepath.empty() ? "" : (fs::path(servicepath) / ".template_info.env").string()); } std::string service_hash(const std::string &server_name, const std::string &service_name) { @@ -80,6 +79,7 @@ namespace localpath { } std::string server(const std::string &server_name) { + if (server_name.empty()) return ""; for (auto &dir : gConfig().get_local_config_directories()) if (fs::exists(dir + "/servers/" + server_name)) return dir + "/servers/" + server_name; @@ -88,7 +88,7 @@ namespace localpath { std::string service(const std::string &server_name, const std::string &service_name) { std::string serverpath = localpath::server(server_name); - return (serverpath.empty() ? "" : (serverpath+"/"+service_name)); + return (serverpath.empty() || service_name.empty() ? "" : (serverpath+"/"+service_name)); } } // namespace localpath diff --git a/src/utils/directories.hpp b/src/utils/directories.hpp index e10e733..ffa0034 100644 --- a/src/utils/directories.hpp +++ b/src/utils/directories.hpp @@ -16,12 +16,15 @@ namespace dropshell { // | |-- services // | |-- service_name // | |-- service.env + // | |-- .template_info.env // | |-- (other config files for specific server&service) // |-- templates // | |-- template_name // | |-- (script files) + // | |-- _default.env // | |-- example // | |-- service.env + // | |-- .template_info.env // | |-- (other service config files) // |-- .remote_versions // | |-- server_name @@ -31,6 +34,7 @@ namespace dropshell { std::string dropshell_env(); std::string server_env(const std::string &server_name); std::string service_env(const std::string &server_name, const std::string &service_name); + std::string template_info_env(const std::string &server_name, const std::string &service_name); std::string service_hash(const std::string &server_name, const std::string &service_name); } // namespace localfile diff --git a/templates/example-nginx/example/.template_info.env b/templates/example-nginx/example/.template_info.env new file mode 100644 index 0000000..ca4a270 --- /dev/null +++ b/templates/example-nginx/example/.template_info.env @@ -0,0 +1,2 @@ +# Template to use - always required! +TEMPLATE=example-nginx diff --git a/templates/example-nginx/example/service.env b/templates/example-nginx/example/service.env index d7718dd..8dacb42 100644 --- a/templates/example-nginx/example/service.env +++ b/templates/example-nginx/example/service.env @@ -1,6 +1,3 @@ -# Template to use - always required! -TEMPLATE=example-nginx - # Service settings specific to this server # (can also override anything in the _default.env file in the template to make it specific to this server) HOST_PORT=60123