Update remote agent scripts and add override validation
This commit is contained in:
@@ -2,8 +2,12 @@
|
||||
#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 {
|
||||
|
||||
@@ -31,13 +35,47 @@ std::vector<OverrideChange> apply_overrides(
|
||||
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
|
||||
// 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()) {
|
||||
warning << "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);
|
||||
|
||||
Reference in New Issue
Block a user