Initial Tessera backup vault (M0–M2)
Scaffold Rust workspace, React canvas, vault engine, hub API, providers, agent wormhole path, and Docker compose per SPEC.
This commit is contained in:
83
crates/tessera-providers/src/filesystem.rs
Normal file
83
crates/tessera-providers/src/filesystem.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
use super::{BackupProvider, CaptureResult};
|
||||
use anyhow::{Context, Result};
|
||||
use async_trait::async_trait;
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
use std::path::PathBuf;
|
||||
use tessera_core::config::ProviderKind;
|
||||
use walkdir::WalkDir;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct FilesystemConfig {
|
||||
pub paths: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub max_file_bytes: Option<u64>,
|
||||
}
|
||||
|
||||
pub struct FilesystemProvider {
|
||||
config: FilesystemConfig,
|
||||
}
|
||||
|
||||
impl FilesystemProvider {
|
||||
pub fn from_config(config: &serde_json::Value) -> Result<Self> {
|
||||
let config: FilesystemConfig = serde_json::from_value(config.clone())?;
|
||||
Ok(Self { config })
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl BackupProvider for FilesystemProvider {
|
||||
fn kind(&self) -> ProviderKind {
|
||||
ProviderKind::Filesystem
|
||||
}
|
||||
|
||||
async fn capture(&self) -> Result<CaptureResult> {
|
||||
let mut archive = Vec::new();
|
||||
let mut paths_meta = Vec::new();
|
||||
|
||||
for path_str in &self.config.paths {
|
||||
let root = PathBuf::from(path_str);
|
||||
if !root.exists() {
|
||||
anyhow::bail!("path not found: {path_str}");
|
||||
}
|
||||
if root.is_file() {
|
||||
let data = tokio::fs::read(&root).await.context("read file")?;
|
||||
if let Some(max) = self.config.max_file_bytes {
|
||||
if data.len() as u64 > max {
|
||||
anyhow::bail!("file exceeds max_file_bytes");
|
||||
}
|
||||
}
|
||||
archive.extend_from_slice(path_str.as_bytes());
|
||||
archive.push(0);
|
||||
archive.extend(&(data.len() as u64).to_le_bytes());
|
||||
archive.extend(&data);
|
||||
paths_meta.push(path_str.clone());
|
||||
} else {
|
||||
for entry in WalkDir::new(&root).into_iter().filter_map(|e| e.ok()) {
|
||||
if entry.file_type().is_file() {
|
||||
let p = entry.path();
|
||||
let rel = p.strip_prefix(&root).unwrap_or(p);
|
||||
let data = tokio::fs::read(p).await.context("read file")?;
|
||||
if let Some(max) = self.config.max_file_bytes {
|
||||
if data.len() as u64 > max {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
let key = format!("{}/{}", path_str, rel.display());
|
||||
archive.extend_from_slice(key.as_bytes());
|
||||
archive.push(0);
|
||||
archive.extend(&(data.len() as u64).to_le_bytes());
|
||||
archive.extend(&data);
|
||||
paths_meta.push(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(CaptureResult {
|
||||
payload: archive,
|
||||
labels: vec!["filesystem".into()],
|
||||
restore_hints: json!({ "paths": paths_meta, "format": "tessera-fs-v1" }),
|
||||
})
|
||||
}
|
||||
}
|
||||
31
crates/tessera-providers/src/lib.rs
Normal file
31
crates/tessera-providers/src/lib.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
mod filesystem;
|
||||
mod postgres;
|
||||
|
||||
pub use filesystem::*;
|
||||
pub use postgres::*;
|
||||
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use serde_json::Value;
|
||||
use tessera_core::config::ProviderKind;
|
||||
|
||||
#[async_trait]
|
||||
pub trait BackupProvider: Send + Sync {
|
||||
fn kind(&self) -> ProviderKind;
|
||||
async fn capture(&self) -> Result<CaptureResult>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CaptureResult {
|
||||
pub payload: Vec<u8>,
|
||||
pub labels: Vec<String>,
|
||||
pub restore_hints: Value,
|
||||
}
|
||||
|
||||
pub fn provider_from_config(kind: &ProviderKind, config: &Value) -> Result<Box<dyn BackupProvider>> {
|
||||
match kind {
|
||||
ProviderKind::Filesystem => Ok(Box::new(FilesystemProvider::from_config(config)?)),
|
||||
ProviderKind::Postgres => Ok(Box::new(PostgresProvider::from_config(config)?)),
|
||||
ProviderKind::Command => anyhow::bail!("command provider not implemented in v1"),
|
||||
}
|
||||
}
|
||||
63
crates/tessera-providers/src/postgres.rs
Normal file
63
crates/tessera-providers/src/postgres.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use super::{BackupProvider, CaptureResult};
|
||||
use anyhow::{Context, Result};
|
||||
use async_trait::async_trait;
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
use std::process::Stdio;
|
||||
use tessera_core::config::ProviderKind;
|
||||
use tokio::process::Command;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct PostgresConfig {
|
||||
pub connection_url: String,
|
||||
#[serde(default = "default_format")]
|
||||
pub format: String,
|
||||
}
|
||||
|
||||
fn default_format() -> String {
|
||||
"custom".into()
|
||||
}
|
||||
|
||||
pub struct PostgresProvider {
|
||||
config: PostgresConfig,
|
||||
}
|
||||
|
||||
impl PostgresProvider {
|
||||
pub fn from_config(config: &serde_json::Value) -> Result<Self> {
|
||||
let config: PostgresConfig = serde_json::from_value(config.clone())?;
|
||||
Ok(Self { config })
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl BackupProvider for PostgresProvider {
|
||||
fn kind(&self) -> ProviderKind {
|
||||
ProviderKind::Postgres
|
||||
}
|
||||
|
||||
async fn capture(&self) -> Result<CaptureResult> {
|
||||
let mut cmd = Command::new("pg_dump");
|
||||
cmd.arg(&self.config.connection_url);
|
||||
match self.config.format.as_str() {
|
||||
"custom" => {
|
||||
cmd.arg("-Fc");
|
||||
}
|
||||
"plain" => {}
|
||||
other => anyhow::bail!("unknown pg format: {other}"),
|
||||
}
|
||||
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
|
||||
let output = cmd.output().await.context("pg_dump failed to start")?;
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
anyhow::bail!("pg_dump failed: {stderr}");
|
||||
}
|
||||
Ok(CaptureResult {
|
||||
payload: output.stdout,
|
||||
labels: vec!["postgres".into()],
|
||||
restore_hints: json!({
|
||||
"format": self.config.format,
|
||||
"restore_command_hint": "pg_restore -d $DATABASE_URL dump.file"
|
||||
}),
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user