fc-common: add declarative sync for remote builders and channels

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I3dae89f04777f6d941824606aebe34446a6a6964
This commit is contained in:
raf 2026-02-08 21:16:32 +03:00
commit d4d9297d96
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
6 changed files with 455 additions and 27 deletions

View file

@ -2,6 +2,7 @@ use sqlx::PgPool;
use uuid::Uuid;
use crate::{
config::DeclarativeChannel,
error::{CiError, Result},
models::{Channel, CreateChannel},
};
@ -86,6 +87,61 @@ pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
Ok(())
}
/// Upsert a channel (insert or update on conflict).
pub async fn upsert(
pool: &PgPool,
project_id: Uuid,
name: &str,
jobset_id: Uuid,
) -> Result<Channel> {
sqlx::query_as::<_, Channel>(
"INSERT INTO channels (project_id, name, jobset_id) VALUES ($1, $2, $3) \
ON CONFLICT (project_id, name) DO UPDATE SET jobset_id = EXCLUDED.jobset_id \
RETURNING *",
)
.bind(project_id)
.bind(name)
.bind(jobset_id)
.fetch_one(pool)
.await
.map_err(CiError::Database)
}
/// Sync channels from declarative config.
/// Deletes channels not in the declarative list and upserts those that are.
pub async fn sync_for_project(
pool: &PgPool,
project_id: Uuid,
channels: &[DeclarativeChannel],
resolve_jobset: impl Fn(&str) -> Option<Uuid>,
) -> Result<()> {
// Get channel names from declarative config
let names: Vec<&str> = channels.iter().map(|c| c.name.as_str()).collect();
// Delete channels not in declarative config
sqlx::query("DELETE FROM channels WHERE project_id = $1 AND name != ALL($2::text[])")
.bind(project_id)
.bind(&names)
.execute(pool)
.await
.map_err(CiError::Database)?;
// Upsert each channel
for channel in channels {
if let Some(jobset_id) = resolve_jobset(&channel.jobset_name) {
upsert(pool, project_id, &channel.name, jobset_id).await?;
} else {
tracing::warn!(
channel = %channel.name,
jobset_name = %channel.jobset_name,
"Could not resolve jobset for declarative channel"
);
}
}
Ok(())
}
/// Find the channel for a jobset and auto-promote if all builds in the
/// evaluation succeeded.
pub async fn auto_promote_if_complete(

View file

@ -2,6 +2,7 @@ use sqlx::PgPool;
use uuid::Uuid;
use crate::{
config::DeclarativeRemoteBuilder,
error::{CiError, Result},
models::{CreateRemoteBuilder, RemoteBuilder},
};
@ -133,3 +134,81 @@ pub async fn count(pool: &PgPool) -> Result<i64> {
.map_err(CiError::Database)?;
Ok(row.0)
}
/// Upsert a remote builder (insert or update on conflict by name).
pub async fn upsert(
pool: &PgPool,
name: &str,
ssh_uri: &str,
systems: &[String],
max_jobs: i32,
speed_factor: i32,
supported_features: &[String],
mandatory_features: &[String],
enabled: bool,
public_host_key: Option<&str>,
ssh_key_file: Option<&str>,
) -> Result<RemoteBuilder> {
sqlx::query_as::<_, RemoteBuilder>(
"INSERT INTO remote_builders (name, ssh_uri, systems, max_jobs, \
speed_factor, supported_features, mandatory_features, enabled, \
public_host_key, ssh_key_file) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, \
$10) ON CONFLICT (name) DO UPDATE SET ssh_uri = EXCLUDED.ssh_uri, systems = \
EXCLUDED.systems, max_jobs = EXCLUDED.max_jobs, speed_factor = \
EXCLUDED.speed_factor, supported_features = EXCLUDED.supported_features, \
mandatory_features = EXCLUDED.mandatory_features, enabled = \
EXCLUDED.enabled, public_host_key = COALESCE(EXCLUDED.public_host_key, \
remote_builders.public_host_key), ssh_key_file = \
COALESCE(EXCLUDED.ssh_key_file, remote_builders.ssh_key_file) RETURNING *",
)
.bind(name)
.bind(ssh_uri)
.bind(systems)
.bind(max_jobs)
.bind(speed_factor)
.bind(supported_features)
.bind(mandatory_features)
.bind(enabled)
.bind(public_host_key)
.bind(ssh_key_file)
.fetch_one(pool)
.await
.map_err(CiError::Database)
}
/// Sync remote builders from declarative config.
/// Deletes builders not in the declarative list and upserts those that are.
pub async fn sync_all(
pool: &PgPool,
builders: &[DeclarativeRemoteBuilder],
) -> Result<()> {
// Get builder names from declarative config
let names: Vec<&str> = builders.iter().map(|b| b.name.as_str()).collect();
// Delete builders not in declarative config
sqlx::query("DELETE FROM remote_builders WHERE name != ALL($1::text[])")
.bind(&names)
.execute(pool)
.await
.map_err(CiError::Database)?;
// Upsert each builder
for builder in builders {
upsert(
pool,
&builder.name,
&builder.ssh_uri,
&builder.systems,
builder.max_jobs,
builder.speed_factor,
&builder.supported_features,
&builder.mandatory_features,
builder.enabled,
builder.public_host_key.as_deref(),
builder.ssh_key_file.as_deref(),
)
.await?;
}
Ok(())
}