Add overrides.env support for per-location service env overrides
All checks were successful
Build-Test-Publish / build (linux/amd64) (push) Successful in 48s
Build-Test-Publish / build (linux/arm64) (push) Successful in 3m44s

This commit is contained in:
j
2026-03-30 08:04:23 +13:00
parent bf9cdeccc7
commit 735e2f083a
7 changed files with 169 additions and 3 deletions

View File

@@ -43,6 +43,14 @@ namespace dropshell
return localpath::agent_remote() + "/agent.hash";
}
std::string overrides_env_for_server(const std::string &server_name)
{
std::string serverpath = localpath::server(server_name);
if (serverpath.empty())
return "";
return (fs::path(get_parent(serverpath)) / filenames::overrides_env).string();
}
} // namespace localfile
std::string get_local_agent_hash()

View File

@@ -43,6 +43,7 @@ namespace dropshell {
static const std::string ds_run = "ds_run.sh";
static const std::string template_paths_json = "template_paths.json";
static const std::string dropshell_templates_list = "dropshell-templates.list";
static const std::string overrides_env = "overrides.env";
} // namespace filenames.
namespace localfile {
@@ -51,6 +52,7 @@ namespace dropshell {
std::string service_env(const std::string &server_name, const std::string &service_name);
std::string bb64();
std::string agent_hash(); // Returns path to agent.hash file
std::string overrides_env_for_server(const std::string &server_name); // overrides.env in server's definition path
} // namespace localfile
// Get the content of the local agent hash (empty string if not found)

View File

@@ -0,0 +1,51 @@
#include "service_env_overrides.hpp"
#include "directories.hpp"
#include "envmanager.hpp"
#include "service_env_validator.hpp"
#include <filesystem>
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;
// Load current service env
envmanager service_env(service_env_path);
if (!service_env.load())
return changes;
// Apply each override
for (const auto& [key, new_val] : override_vars) {
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

View File

@@ -0,0 +1,24 @@
#ifndef SERVICE_ENV_OVERRIDES_HPP
#define SERVICE_ENV_OVERRIDES_HPP
#include <string>
#include <vector>
namespace dropshell {
struct OverrideChange {
std::string key;
std::string old_val;
std::string new_val;
};
// Apply overrides from overrides.env (in the server's definition path) to a service's service.env.
// Returns a list of changes that were made.
// If no overrides.env exists, returns empty (no-op).
std::vector<OverrideChange> apply_overrides(
const std::string& server_name,
const std::string& service_name);
} // namespace dropshell
#endif // SERVICE_ENV_OVERRIDES_HPP