172 lines
5.4 KiB
Rust
172 lines
5.4 KiB
Rust
//! Runtime configuration and named backend resources for Bedrock.
|
|
|
|
mod catalog;
|
|
mod driver;
|
|
mod format;
|
|
mod object_storage;
|
|
mod sql;
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use thiserror::Error;
|
|
|
|
pub use catalog::{Resource, ResourceRef};
|
|
pub use driver::*;
|
|
pub use format::{ConfigFormat, FormatError, ImportFormat};
|
|
pub use object_storage::ObjectStorage;
|
|
pub use sql::{Address, Cluster, ResolvedSql, Tls};
|
|
|
|
pub const DOCUMENTS: &str = "documents";
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
#[serde(default, deny_unknown_fields)]
|
|
pub struct DeploymentConfig {
|
|
pub deployment: String,
|
|
pub config_version: String,
|
|
/// Legacy shorthand for sql.documents.
|
|
pub cluster: Option<String>,
|
|
pub clusters: BTreeMap<String, Cluster>,
|
|
/// SQL role to named cluster.
|
|
pub sql: BTreeMap<String, String>,
|
|
pub object_storage: BTreeMap<String, ObjectStorage>,
|
|
pub read_only: bool,
|
|
pub sqlite_path: Address,
|
|
}
|
|
|
|
impl Default for DeploymentConfig {
|
|
fn default() -> Self {
|
|
Self {
|
|
deployment: "default".into(),
|
|
config_version: "local".into(),
|
|
cluster: None,
|
|
clusters: BTreeMap::new(),
|
|
sql: BTreeMap::new(),
|
|
object_storage: BTreeMap::new(),
|
|
read_only: false,
|
|
sqlite_path: "bedrock.sqlite3".into(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A validated startup snapshot. The version identifies the configuration release.
|
|
#[derive(Clone, Debug)]
|
|
pub struct ResolvedDeployment {
|
|
pub deployment: String,
|
|
pub config_version: String,
|
|
pub sql: BTreeMap<String, ResolvedSql>,
|
|
pub object_storage: BTreeMap<String, ObjectStorage>,
|
|
}
|
|
|
|
impl ResolvedDeployment {
|
|
pub fn documents(&self) -> Option<&ResolvedSql> {
|
|
self.sql.get(DOCUMENTS)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Error)]
|
|
pub enum ConfigError {
|
|
#[error("invalid {0} name")]
|
|
Name(&'static str),
|
|
#[error("the selected database cluster is not configured")]
|
|
UnknownCluster,
|
|
#[error("a cluster must be selected when clusters are configured")]
|
|
ClusterRequired,
|
|
#[error("legacy cluster and sql.documents refer to different resources")]
|
|
ConflictingDocuments,
|
|
#[error("invalid object storage configuration")]
|
|
ObjectStorage,
|
|
#[error("resource already exists")]
|
|
DuplicateResource,
|
|
#[error("resource does not exist")]
|
|
MissingResource,
|
|
#[error("resource is referenced by a SQL binding")]
|
|
ResourceInUse,
|
|
#[error("a resource update must preserve its category")]
|
|
ResourceCategory,
|
|
#[error("the database pool size must be greater than zero")]
|
|
PoolSize,
|
|
#[error("SQLite does not support a replica list")]
|
|
SqliteReplicas,
|
|
#[error("invalid SQLite file path")]
|
|
SqlitePath,
|
|
#[error("invalid {0} database URL")]
|
|
Url(&'static str),
|
|
}
|
|
|
|
impl DeploymentConfig {
|
|
pub fn resolve(&self) -> Result<ResolvedDeployment, ConfigError> {
|
|
validate_name(&self.deployment, "deployment")?;
|
|
validate_name(&self.config_version, "config version")?;
|
|
let mut targets = BTreeMap::new();
|
|
for (name, cluster) in &self.clusters {
|
|
validate_name(name, "cluster")?;
|
|
targets.insert(
|
|
name.as_str(),
|
|
cluster.resolve(&self.deployment, Some(name), self.read_only)?,
|
|
);
|
|
}
|
|
for (name, store) in &self.object_storage {
|
|
validate_name(name, "object store")?;
|
|
if self.clusters.contains_key(name) {
|
|
return Err(ConfigError::DuplicateResource);
|
|
}
|
|
store.validate()?;
|
|
}
|
|
|
|
let mut sql = BTreeMap::new();
|
|
for (role, target) in &self.sql {
|
|
validate_name(role, "SQL role")?;
|
|
validate_name(target, "cluster")?;
|
|
let settings = targets
|
|
.get(target.as_str())
|
|
.ok_or(ConfigError::UnknownCluster)?;
|
|
sql.insert(role.clone(), settings.clone());
|
|
}
|
|
if let Some(name) = &self.cluster {
|
|
validate_name(name, "cluster")?;
|
|
if self.sql.get(DOCUMENTS).is_some_and(|target| target != name) {
|
|
return Err(ConfigError::ConflictingDocuments);
|
|
}
|
|
let settings = targets
|
|
.get(name.as_str())
|
|
.ok_or(ConfigError::UnknownCluster)?;
|
|
sql.insert(DOCUMENTS.into(), settings.clone());
|
|
}
|
|
if sql.is_empty() {
|
|
if !targets.is_empty() {
|
|
return Err(ConfigError::ClusterRequired);
|
|
}
|
|
let local = Cluster {
|
|
driver: SqlDriver::Sqlite,
|
|
primary: self.sqlite_path.clone(),
|
|
replicas: vec![],
|
|
tls: Tls::Disabled,
|
|
max_connections: 1,
|
|
};
|
|
sql.insert(
|
|
DOCUMENTS.into(),
|
|
local.resolve(&self.deployment, None, self.read_only)?,
|
|
);
|
|
}
|
|
Ok(ResolvedDeployment {
|
|
deployment: self.deployment.clone(),
|
|
config_version: self.config_version.clone(),
|
|
sql,
|
|
object_storage: self.object_storage.clone(),
|
|
})
|
|
}
|
|
}
|
|
|
|
pub(crate) fn validate_name(value: &str, kind: &'static str) -> Result<(), ConfigError> {
|
|
if value.is_empty()
|
|
|| value.len() > 128
|
|
|| !value
|
|
.bytes()
|
|
.all(|b| b.is_ascii_alphanumeric() || b"-_.".contains(&b))
|
|
{
|
|
return Err(ConfigError::Name(kind));
|
|
}
|
|
Ok(())
|
|
}
|