90 lines
3.2 KiB
C++
90 lines
3.2 KiB
C++
#include "service_env_overrides.hpp"
|
|
#include "directories.hpp"
|
|
#include "envmanager.hpp"
|
|
#include "service_env_validator.hpp"
|
|
#include "services.hpp"
|
|
#include "templates.hpp"
|
|
#include "output.hpp"
|
|
|
|
#include <filesystem>
|
|
#include <set>
|
|
|
|
namespace dropshell {
|
|
|
|
std::vector<OverrideChange> apply_overrides(
|
|
const std::string& server_name,
|
|
const std::string& service_name)
|
|
{
|
|
std::vector<OverrideChange> changes;
|
|
|
|
std::string overrides_path = localfile::overrides_env_for_server(server_name);
|
|
if (overrides_path.empty() || !std::filesystem::exists(overrides_path))
|
|
return changes;
|
|
|
|
std::string service_env_path = localfile::service_env(server_name, service_name);
|
|
if (service_env_path.empty() || !std::filesystem::exists(service_env_path))
|
|
return changes;
|
|
|
|
// Load overrides
|
|
envmanager overrides(overrides_path);
|
|
if (!overrides.load())
|
|
return changes;
|
|
|
|
ordered_env_vars override_vars;
|
|
overrides.get_all_variables(override_vars);
|
|
if (override_vars.empty())
|
|
return changes;
|
|
|
|
// Build set of allowed variable names from template service.env and template_info.env
|
|
std::set<std::string> allowed_vars;
|
|
LocalServiceInfo service_info = get_service_info(server_name, service_name);
|
|
if (!service_info.template_name.empty()) {
|
|
template_info tinfo = gTemplateManager().get_template_info(service_info.template_name);
|
|
if (tinfo.is_set()) {
|
|
std::filesystem::path template_service_env = tinfo.local_template_service_env_path();
|
|
if (std::filesystem::exists(template_service_env)) {
|
|
envmanager tmpl_env(template_service_env.string());
|
|
if (tmpl_env.load()) {
|
|
ordered_env_vars tmpl_vars;
|
|
tmpl_env.get_all_variables(tmpl_vars);
|
|
for (const auto& [key, value] : tmpl_vars)
|
|
allowed_vars.insert(key);
|
|
}
|
|
}
|
|
|
|
std::filesystem::path template_info_env = tinfo.local_template_info_env_path();
|
|
if (std::filesystem::exists(template_info_env)) {
|
|
envmanager info_env(template_info_env.string());
|
|
if (info_env.load()) {
|
|
ordered_env_vars info_vars;
|
|
info_env.get_all_variables(info_vars);
|
|
for (const auto& [key, value] : info_vars)
|
|
allowed_vars.insert(key);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Load current service env
|
|
envmanager service_env(service_env_path);
|
|
if (!service_env.load())
|
|
return changes;
|
|
|
|
// Apply each override, skipping variables not defined in the template
|
|
for (const auto& [key, new_val] : override_vars) {
|
|
if (!allowed_vars.empty() && allowed_vars.find(key) == allowed_vars.end()) {
|
|
debug << "Override skipped: " << key << " is not defined in the template for " << service_name << std::endl;
|
|
continue;
|
|
}
|
|
std::string current_val = service_env.get_variable(key);
|
|
if (current_val != new_val) {
|
|
set_env_variable(service_env_path, key, new_val);
|
|
changes.push_back({key, current_val, new_val});
|
|
}
|
|
}
|
|
|
|
return changes;
|
|
}
|
|
|
|
} // namespace dropshell
|