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:
114
crates/tessera-agent/src/main.rs
Normal file
114
crates/tessera-agent/src/main.rs
Normal file
@@ -0,0 +1,114 @@
|
||||
mod http;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use base64::Engine;
|
||||
use clap::{Parser, Subcommand};
|
||||
use tessera_core::config::ProviderKind;
|
||||
use tessera_providers::provider_from_config;
|
||||
use tracing::info;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "tessera-agent", about = "Tessera outpost daemon")]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
command: Option<AgentCmd>,
|
||||
#[arg(long, env = "TESSERA_HUB_URL")]
|
||||
hub_url: Option<String>,
|
||||
#[arg(long, env = "TESSERA_UPLOAD_TOKEN")]
|
||||
upload_token: Option<String>,
|
||||
#[arg(long, env = "TESSERA_ORBIT_ID")]
|
||||
orbit_id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Subcommand)]
|
||||
enum AgentCmd {
|
||||
Run,
|
||||
Pair {
|
||||
code: String,
|
||||
#[arg(long, default_value = "outpost")]
|
||||
name: String,
|
||||
},
|
||||
Beam,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
tracing_subscriber::fmt::init();
|
||||
let cli = Cli::parse();
|
||||
match cli.command.clone().unwrap_or(AgentCmd::Run) {
|
||||
AgentCmd::Run => run_daemon(&cli).await?,
|
||||
AgentCmd::Pair { code, name } => pair(&cli, &code, &name)?,
|
||||
AgentCmd::Beam => beam_once(&cli).await?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pair(cli: &Cli, code: &str, name: &str) -> Result<()> {
|
||||
let hub = cli
|
||||
.hub_url
|
||||
.clone()
|
||||
.unwrap_or_else(|| "http://127.0.0.1:8787".into());
|
||||
let body = serde_json::json!({ "code": code, "outpost_name": name });
|
||||
let raw = http::post_json(
|
||||
&format!("{hub}/api/v1/wormhole/claim"),
|
||||
&body,
|
||||
&[],
|
||||
)?;
|
||||
let res: serde_json::Value = serde_json::from_str(&raw)?;
|
||||
println!("TESSERA_UPLOAD_TOKEN={}", res["upload_token"]);
|
||||
println!("TESSERA_HUB_URL={}", res["hub_url"]);
|
||||
println!("Outpost {}", res["outpost_id"]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn run_daemon(cli: &Cli) -> Result<()> {
|
||||
info!("outpost online — wormhole listening for beam cycles");
|
||||
loop {
|
||||
if let Err(e) = beam_once(cli).await {
|
||||
tracing::warn!("beam failed: {e}");
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_secs(300)).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn beam_once(cli: &Cli) -> Result<()> {
|
||||
let hub = cli
|
||||
.hub_url
|
||||
.clone()
|
||||
.context("TESSERA_HUB_URL")?
|
||||
.trim_end_matches('/')
|
||||
.to_string();
|
||||
let token = cli.upload_token.clone().context("TESSERA_UPLOAD_TOKEN")?;
|
||||
let orbit_id = cli.orbit_id.clone().context("TESSERA_ORBIT_ID")?;
|
||||
|
||||
let config = serde_json::json!({ "paths": ["./"] });
|
||||
let provider = provider_from_config(&ProviderKind::Filesystem, &config)?;
|
||||
let capture = provider.capture().await?;
|
||||
let chunk_b64 = base64::engine::general_purpose::STANDARD.encode(&capture.payload);
|
||||
|
||||
let init_body = serde_json::json!({
|
||||
"orbit_id": orbit_id,
|
||||
"total_bytes": capture.payload.len(),
|
||||
});
|
||||
let init_raw = http::post_json(
|
||||
&format!("{hub}/api/v1/wormhole/upload/init"),
|
||||
&init_body,
|
||||
&[("x-tessera-upload-token", &token)],
|
||||
)?;
|
||||
let init: serde_json::Value = serde_json::from_str(&init_raw)?;
|
||||
let session_id = init["session_id"].as_str().context("session_id")?;
|
||||
|
||||
let chunk_body = serde_json::json!({
|
||||
"session_id": session_id,
|
||||
"offset": 0,
|
||||
"data_b64": chunk_b64,
|
||||
});
|
||||
http::post_json(
|
||||
&format!("{hub}/api/v1/wormhole/upload/chunk"),
|
||||
&chunk_body,
|
||||
&[("x-tessera-upload-token", &token)],
|
||||
)?;
|
||||
|
||||
info!("beamed {} bytes via wormhole session {session_id}", capture.payload.len());
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user