fix: merge existing services in load balancer config

This commit is contained in:
2025-09-01 07:39:53 -04:00
parent 62fa3c2b10
commit e9a1aa4831
5 changed files with 79 additions and 22 deletions

View File

@@ -29,8 +29,7 @@ impl SshConfigManager {
self.opnsense_shell
.exec(&format!(
"cp /conf/config.xml /conf/backup/{}",
backup_filename
"cp /conf/config.xml /conf/backup/{backup_filename}"
))
.await
}

View File

@@ -40,21 +40,87 @@ impl<'a> LoadBalancerConfig<'a> {
self.with_haproxy(|haproxy| haproxy.general.enabled = enabled as i32);
}
pub fn add_backend(&mut self, backend: HAProxyBackend) {
/// Adds or updates a backend pool.
/// If a backend with the same name exists, it is updated. Otherwise, it is added.
pub fn add_backend(&mut self, mut backend: HAProxyBackend) {
warn!("TODO make sure this new backend does not refer non-existing entities like servers or health checks");
self.with_haproxy(|haproxy| haproxy.backends.backends.push(backend));
self.with_haproxy(|haproxy| {
let existing_backend = haproxy
.backends
.backends
.iter_mut()
.find(|b| b.name == backend.name);
if let Some(existing_backend) = existing_backend {
backend.uuid = existing_backend.uuid.clone(); // This breaks the `frontend` config
// as it is now relying on a stale uuid
backend.id = existing_backend.id.clone();
*existing_backend = backend;
} else {
haproxy.backends.backends.push(backend);
}
});
}
pub fn add_frontend(&mut self, frontend: Frontend) {
self.with_haproxy(|haproxy| haproxy.frontends.frontend.push(frontend));
/// Adds or updates a frontend.
/// If a frontend with the same name exists, it is updated. Otherwise, it is added.
pub fn add_frontend(&mut self, mut frontend: Frontend) {
self.with_haproxy(|haproxy| {
let existing_frontend = haproxy
.frontends
.frontend
.iter_mut()
.find(|f| f.name == frontend.name);
if let Some(existing_frontend) = existing_frontend {
frontend.uuid = existing_frontend.uuid.clone();
frontend.id = existing_frontend.id.clone();
*existing_frontend = frontend;
} else {
haproxy.frontends.frontend.push(frontend);
}
});
}
pub fn add_healthcheck(&mut self, healthcheck: HAProxyHealthCheck) {
self.with_haproxy(|haproxy| haproxy.healthchecks.healthchecks.push(healthcheck));
/// Adds or updates a health check.
/// If a health check with the same name exists, it is updated. Otherwise, it is added.
pub fn add_healthcheck(&mut self, mut healthcheck: HAProxyHealthCheck) {
self.with_haproxy(|haproxy| {
let existing_healthcheck = haproxy
.healthchecks
.healthchecks
.iter_mut()
.find(|h| h.name == healthcheck.name);
if let Some(existing_check) = existing_healthcheck {
healthcheck.uuid = existing_check.uuid.clone();
*existing_check = healthcheck;
} else {
haproxy.healthchecks.healthchecks.push(healthcheck);
}
});
}
pub fn add_servers(&mut self, mut servers: Vec<HAProxyServer>) {
self.with_haproxy(|haproxy| haproxy.servers.servers.append(&mut servers));
/// Adds or updates a list of servers to the HAProxy configuration.
/// If a server with the same name already exists, it is updated. Otherwise, it is added.
pub fn add_servers(&mut self, servers: Vec<HAProxyServer>) {
self.with_haproxy(|haproxy| {
for server in servers {
let existing_server = haproxy
.servers
.servers
.iter_mut()
.find(|s| s.name == server.name);
if let Some(existing_server) = existing_server {
existing_server.address = server.address;
existing_server.port = server.port;
existing_server.enabled = server.enabled;
} else {
haproxy.servers.servers.push(server);
}
}
});
}
pub async fn reload_restart(&self) -> Result<(), Error> {