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:
202
crates/tessera-cli/src/main.rs
Normal file
202
crates/tessera-cli/src/main.rs
Normal file
@@ -0,0 +1,202 @@
|
||||
mod http;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use clap::{Parser, Subcommand};
|
||||
use tessera_store::Vault;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Parser, Clone)]
|
||||
#[command(name = "tessera", about = "Tessera backup vault CLI")]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
#[arg(long, env = "TESSERA_DATA_DIR", default_value = "./data")]
|
||||
data_dir: String,
|
||||
#[arg(long, env = "TESSERA_VAULT_PASSPHRASE", default_value = "dev-vault-key")]
|
||||
vault_pass: String,
|
||||
#[arg(long, env = "TESSERA_HUB_URL", default_value = "http://127.0.0.1:8787")]
|
||||
hub_url: String,
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Clone)]
|
||||
enum Commands {
|
||||
Init,
|
||||
Status,
|
||||
Orbit {
|
||||
name: String,
|
||||
#[arg(long, default_value = "filesystem")]
|
||||
provider: String,
|
||||
#[arg(long)]
|
||||
path: Option<String>,
|
||||
#[arg(long)]
|
||||
schedule: Option<String>,
|
||||
#[arg(long)]
|
||||
dry_run: bool,
|
||||
},
|
||||
Snap {
|
||||
#[arg(long)]
|
||||
orbit: Option<String>,
|
||||
},
|
||||
List,
|
||||
Reconstitute {
|
||||
snapshot_id: Uuid,
|
||||
target: String,
|
||||
},
|
||||
Decay {
|
||||
#[arg(long, default_value = "week-of-dailies")]
|
||||
preset: String,
|
||||
},
|
||||
Wormhole {
|
||||
#[command(subcommand)]
|
||||
cmd: WormholeCmd,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Clone)]
|
||||
enum WormholeCmd {
|
||||
Pair {
|
||||
code: String,
|
||||
#[arg(long, default_value = "outpost")]
|
||||
name: String,
|
||||
},
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
let command = cli.command.clone();
|
||||
match command {
|
||||
Commands::Init => cmd_init(&cli)?,
|
||||
Commands::Status => cmd_status(&cli)?,
|
||||
Commands::Orbit {
|
||||
name,
|
||||
provider,
|
||||
path,
|
||||
schedule,
|
||||
dry_run,
|
||||
} => cmd_orbit(&cli, name, provider, path, schedule, dry_run)?,
|
||||
Commands::Snap { orbit } => cmd_snap(&cli, orbit)?,
|
||||
Commands::List => cmd_list(&cli)?,
|
||||
Commands::Reconstitute {
|
||||
snapshot_id,
|
||||
target,
|
||||
} => cmd_reconstitute(&cli, snapshot_id, target)?,
|
||||
Commands::Decay { preset } => cmd_decay(&cli, &preset)?,
|
||||
Commands::Wormhole { ref cmd } => match cmd {
|
||||
WormholeCmd::Pair { code, name } => wormhole_pair(&cli, code, name)?,
|
||||
},
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn vault_path(data_dir: &str) -> std::path::PathBuf {
|
||||
std::path::PathBuf::from(data_dir).join("vault")
|
||||
}
|
||||
|
||||
fn cmd_init(cli: &Cli) -> Result<()> {
|
||||
let vp = vault_path(&cli.data_dir);
|
||||
if vp.join("config").exists() {
|
||||
println!("Vault already initialized.");
|
||||
return Ok(());
|
||||
}
|
||||
Vault::init(&vp, &cli.vault_pass).context("init vault")?;
|
||||
println!("Vault initialized at {}", vp.display());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cmd_status(cli: &Cli) -> Result<()> {
|
||||
match http::get(&format!("{}/health", cli.hub_url)) {
|
||||
Ok(body) => println!("Hub: {body}"),
|
||||
Err(_) => println!("Hub: offline (is tessera-hub running?)"),
|
||||
}
|
||||
let vp = vault_path(&cli.data_dir);
|
||||
if vp.join("config").exists() {
|
||||
let vault = Vault::open(&vp, &cli.vault_pass)?;
|
||||
let n = vault.list_snapshots()?.len();
|
||||
println!("Vault: online — {n} snapshots");
|
||||
} else {
|
||||
println!("Vault: not initialized (run tessera init)");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cmd_orbit(
|
||||
cli: &Cli,
|
||||
name: String,
|
||||
provider: String,
|
||||
path: Option<String>,
|
||||
schedule: Option<String>,
|
||||
dry_run: bool,
|
||||
) -> Result<()> {
|
||||
let config = match provider.as_str() {
|
||||
"filesystem" => serde_json::json!({ "paths": [path.unwrap_or_else(|| ".".into())] }),
|
||||
"postgres" => serde_json::json!({ "connection_url": path.context("--path or connection url required")? }),
|
||||
_ => anyhow::bail!("unknown provider"),
|
||||
};
|
||||
let body = serde_json::json!({
|
||||
"name": name,
|
||||
"provider": provider,
|
||||
"provider_config": config,
|
||||
"schedule": schedule,
|
||||
"dry_run": dry_run,
|
||||
});
|
||||
let raw = http::post_json(&format!("{}/api/v1/orbits", cli.hub_url), &body)?;
|
||||
let orbit: serde_json::Value = serde_json::from_str(&raw)?;
|
||||
println!("Orbit registered: {}", orbit["id"]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cmd_snap(cli: &Cli, orbit_name: Option<String>) -> Result<()> {
|
||||
let raw = http::get(&format!("{}/api/v1/orbits", cli.hub_url))?;
|
||||
let orbits: serde_json::Value = serde_json::from_str(&raw)?;
|
||||
let arr = orbits.as_array().context("orbits response")?;
|
||||
let id = if let Some(name) = orbit_name {
|
||||
arr.iter()
|
||||
.find(|o| o["name"] == name)
|
||||
.and_then(|o| o["id"].as_str())
|
||||
.context("orbit not found")?
|
||||
} else {
|
||||
arr.first()
|
||||
.and_then(|o| o["id"].as_str())
|
||||
.context("no orbits defined")?
|
||||
};
|
||||
http::post_empty(&format!("{}/api/v1/orbits/{id}/run", cli.hub_url))?;
|
||||
println!("Orbit run triggered.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cmd_list(cli: &Cli) -> Result<()> {
|
||||
let vault = Vault::open(&vault_path(&cli.data_dir), &cli.vault_pass)?;
|
||||
for s in vault.list_snapshots()? {
|
||||
println!(
|
||||
"{} {} {} bytes orbit={}",
|
||||
s.taken_at.to_rfc3339(),
|
||||
s.id,
|
||||
s.size_bytes,
|
||||
s.manifest.orbit_name
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cmd_reconstitute(cli: &Cli, snapshot_id: Uuid, target: String) -> Result<()> {
|
||||
let vault = Vault::open(&vault_path(&cli.data_dir), &cli.vault_pass)?;
|
||||
let n = vault.restore_snapshot(snapshot_id, std::path::Path::new(&target))?;
|
||||
println!("Reconstituted {n} bytes to {target}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cmd_decay(cli: &Cli, preset: &str) -> Result<()> {
|
||||
let mut vault = Vault::open(&vault_path(&cli.data_dir), &cli.vault_pass)?;
|
||||
let pruned = vault.prune_with_decay(preset)?;
|
||||
println!("{} snapshots decayed.", pruned.len());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn wormhole_pair(cli: &Cli, code: &str, name: &str) -> Result<()> {
|
||||
let body = serde_json::json!({ "code": code, "outpost_name": name });
|
||||
let raw = http::post_json(&format!("{}/api/v1/wormhole/claim", cli.hub_url), &body)?;
|
||||
let res: serde_json::Value = serde_json::from_str(&raw)?;
|
||||
println!("Paired outpost {}", res["outpost_id"]);
|
||||
println!("Set TESSERA_UPLOAD_TOKEN on the agent.");
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user