This commit is contained in:
Your Name 2025-04-30 19:16:49 +12:00
parent 10050f0c27
commit 47d64a1a0d
9 changed files with 58 additions and 52 deletions

View File

@ -12,33 +12,33 @@
namespace dropshell { 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()) if (server_name.empty())
return; return;
// Construct the full path to server.env // 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 // Check if file exists
if (!std::filesystem::exists(env_path)) { if (!std::filesystem::exists(server_env_path)) {
std::cerr << "Server environment file not found: " + env_path << std::endl; std::cerr << "Server environment file not found: " + server_env_path << std::endl;
return; return;
} }
try { try {
// Use envmanager to handle the environment file // Use envmanager to handle the environment file
m_env_manager = std::unique_ptr<envmanager>(new envmanager(env_path)); envmanager env_manager(server_env_path);
m_env_manager->load(); env_manager.load();
// Get all variables // Get all variables
m_env_manager->get_all_variables_substituted(variables); env_manager.get_all_variables_substituted(mVariables);
// Verify required variables exist // Verify required variables exist
for (const auto& var : {"SSH_HOST", "SSH_USER", "SSH_PORT", "DROPSHELL_DIR"}) { 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 // Print the variables identified in the file
std::cout << "Variables identified in the file:" << std::endl; 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; std::cout << " " << v.first << std::endl;
} }
throw std::runtime_error("Missing required variable: " + std::string(var)); 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 { 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 "";
} }
return m_env_manager->get_variable_substituted(name); return it->second;
} }
// Helper method implementations // 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<std::string> args, bool silent) const std::string server_env_manager::construct_standard_command_run_cmd(const std::string &service_name, const std::string &command, std::vector<std::string> args, bool silent) const
{ {
std::string remote_service_template_path = remotepath::service_template(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(mServer_name,service_name); std::string remote_service_config_path = remotepath::service_config(mServerName,service_name);
std::string script_path = remote_service_template_path + "/" + command + ".sh"; 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(); all_env_vars.clear();
// add in some handy variables. // add in some handy variables.
all_env_vars["CONFIG_PATH"] = remotepath::service_config(mServer_name,service_name); all_env_vars["CONFIG_PATH"] = remotepath::service_config(mServerName,service_name);
all_env_vars["SERVER"] = mServer_name; all_env_vars["SERVER"] = mServerName;
all_env_vars["SERVICE"] = service_name; all_env_vars["SERVICE"] = service_name;
{ // load service.env from the service on this machine. { // load service.env from the service on this machine.
std::map<std::string, std::string> env_vars; std::map<std::string, std::string> 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.load();
env_manager.get_all_variables(env_vars); env_manager.get_all_variables(env_vars);
all_env_vars.merge(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<std::string, std::string> env_vars; std::map<std::string, std::string> env_vars;
ServiceInfo service_info = get_service_info(mServer_name, service_name); envmanager env_manager(localfile::template_info_env(mServerName,service_name));
std::string defaultenvpath = service_info.local_template_default_env_path; 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<std::string, std::string> 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)) { if (std::filesystem::exists(defaultenvpath)) {
envmanager env_manager(defaultenvpath); envmanager env_manager(defaultenvpath);
env_manager.load(); env_manager.load();
@ -117,6 +126,7 @@ void server_env_manager::get_all_service_env_vars(const std::string &service_nam
else else
std::cerr << "Warning: _default.env not found in template: " << defaultenvpath << std::endl; std::cerr << "Warning: _default.env not found in template: " << defaultenvpath << std::endl;
} }
} }

View File

@ -49,7 +49,7 @@ class server_env_manager {
std::string get_variable(const std::string& name) const; std::string get_variable(const std::string& name) const;
// trivial getters. // trivial getters.
const std::map<std::string, std::string>& get_variables() const { return variables; } const std::map<std::string, std::string>& get_variables() const { return mVariables; }
std::string get_SSH_HOST() const { return get_variable("SSH_HOST"); } 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_USER() const { return get_variable("SSH_USER"); }
std::string get_SSH_PORT() const { return get_variable("SSH_PORT"); } 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<std::string, std::string> & all_env_vars) const; void get_all_service_env_vars(const std::string& service_name, std::map<std::string, std::string> & all_env_vars) const;
private: private:
std::string mServer_name; std::string mServerName;
std::map<std::string, std::string> variables; std::map<std::string, std::string> mVariables;
bool mValid; bool mValid;
std::unique_ptr<envmanager> m_env_manager; //std::unique_ptr<envmanager> m_env_manager;
}; };
} // namespace dropshell } // namespace dropshell

View File

@ -175,11 +175,6 @@ void create_server(const std::string &server_name)
// 4. add dropshell-agent service to server // 4. add dropshell-agent service to server
create_service(server_name, "dropshell-agent", "dropshell-agent", true); // silently create service. 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 << "Server created successfully: " << server_name << std::endl;
std::cout << "Please complete the installation:" <<std::endl; std::cout << "Please complete the installation:" <<std::endl;
std::cout << "1) edit the server configuration: dropshell edit " << server_name << std::endl; std::cout << "1) edit the server configuration: dropshell edit " << server_name << std::endl;

View File

@ -95,7 +95,6 @@ ServiceInfo get_service_info(const std::string &server_name, const std::string &
// find the template path // find the template path
service.local_template_path = tinfo.local_template_path; service.local_template_path = tinfo.local_template_path;
service.local_template_default_env_path = tinfo.local_template_path + "/_default.env";
return service; return service;
} }

View File

@ -12,7 +12,6 @@ namespace dropshell {
std::string template_name; std::string template_name;
std::string local_service_path; std::string local_service_path;
std::string local_template_path; std::string local_template_path;
std::string local_template_default_env_path;
}; };
std::vector<ServiceInfo> get_server_services_info(const std::string& server_name); std::vector<ServiceInfo> get_server_services_info(const std::string& server_name);

View File

@ -11,33 +11,32 @@ namespace dropshell {
namespace localfile { namespace localfile {
std::string dropshell_env() { std::string dropshell_env() {
// Try ~/.config/dropshell/dropshell.env // Try ~/.config/dropshell/dropshell.env
const char* home = std::getenv("HOME"); const char* home = std::getenv("HOME");
if (home) { if (home) {
fs::path user_path = fs::path(home) / ".config" / "dropshell" / "dropshell.env"; fs::path user_path = fs::path(home) / ".config" / "dropshell" / "dropshell.env";
return user_path.string(); return user_path.string();
} }
std::cerr << "Warning: Couldn't determine user directory" << std::endl; std::cerr << "Warning: Couldn't determine user directory" << std::endl;
return std::string(); return std::string();
} }
std::string server_env(const std::string &server_name) { std::string server_env(const std::string &server_name) {
if (server_name.empty())
return std::string();
std::string serverpath = localpath::server(server_name); std::string serverpath = localpath::server(server_name);
if (serverpath.empty()) return (serverpath.empty() ? "" : (fs::path(serverpath) / "server.env").string());
return std::string();
return (fs::path(serverpath) / "server.env").string();
} }
std::string service_env(const std::string &server_name, const std::string &service_name) { 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); std::string servicepath = localpath::service(server_name, service_name);
if (servicepath.empty()) return (servicepath.empty() ? "" : (fs::path(servicepath) / "service.env").string());
return std::string(); }
return (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) { 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) { std::string server(const std::string &server_name) {
if (server_name.empty()) return "";
for (auto &dir : gConfig().get_local_config_directories()) for (auto &dir : gConfig().get_local_config_directories())
if (fs::exists(dir + "/servers/" + server_name)) if (fs::exists(dir + "/servers/" + server_name))
return 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 service(const std::string &server_name, const std::string &service_name) {
std::string serverpath = localpath::server(server_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 } // namespace localpath

View File

@ -16,12 +16,15 @@ namespace dropshell {
// | |-- services // | |-- services
// | |-- service_name // | |-- service_name
// | |-- service.env // | |-- service.env
// | |-- .template_info.env
// | |-- (other config files for specific server&service) // | |-- (other config files for specific server&service)
// |-- templates // |-- templates
// | |-- template_name // | |-- template_name
// | |-- (script files) // | |-- (script files)
// | |-- _default.env
// | |-- example // | |-- example
// | |-- service.env // | |-- service.env
// | |-- .template_info.env
// | |-- (other service config files) // | |-- (other service config files)
// |-- .remote_versions // |-- .remote_versions
// | |-- server_name // | |-- server_name
@ -31,6 +34,7 @@ namespace dropshell {
std::string dropshell_env(); std::string dropshell_env();
std::string server_env(const std::string &server_name); 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 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); std::string service_hash(const std::string &server_name, const std::string &service_name);
} // namespace localfile } // namespace localfile

View File

@ -0,0 +1,2 @@
# Template to use - always required!
TEMPLATE=example-nginx

View File

@ -1,6 +1,3 @@
# Template to use - always required!
TEMPLATE=example-nginx
# Service settings specific to this server # 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) # (can also override anything in the _default.env file in the template to make it specific to this server)
HOST_PORT=60123 HOST_PORT=60123