Tidying
This commit is contained in:
parent
10050f0c27
commit
47d64a1a0d
@ -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<envmanager>(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<std::string> 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<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.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<std::string, std::string> 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<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)) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -49,7 +49,7 @@ class server_env_manager {
|
||||
std::string get_variable(const std::string& name) const;
|
||||
|
||||
// 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_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<std::string, std::string> & all_env_vars) const;
|
||||
|
||||
private:
|
||||
std::string mServer_name;
|
||||
std::map<std::string, std::string> variables;
|
||||
std::string mServerName;
|
||||
std::map<std::string, std::string> mVariables;
|
||||
bool mValid;
|
||||
std::unique_ptr<envmanager> m_env_manager;
|
||||
//std::unique_ptr<envmanager> m_env_manager;
|
||||
};
|
||||
|
||||
} // namespace dropshell
|
||||
|
@ -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:" <<std::endl;
|
||||
std::cout << "1) edit the server configuration: dropshell edit " << server_name << std::endl;
|
||||
|
@ -95,7 +95,6 @@ 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_path + "/_default.env";
|
||||
|
||||
return service;
|
||||
}
|
||||
|
@ -12,7 +12,6 @@ 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<ServiceInfo> get_server_services_info(const std::string& server_name);
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
2
templates/example-nginx/example/.template_info.env
Normal file
2
templates/example-nginx/example/.template_info.env
Normal file
@ -0,0 +1,2 @@
|
||||
# Template to use - always required!
|
||||
TEMPLATE=example-nginx
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user