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:
22
crates/tessera-agent/Cargo.toml
Normal file
22
crates/tessera-agent/Cargo.toml
Normal file
@@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "tessera-agent"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "tessera-agent"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
tessera-core = { path = "../tessera-core" }
|
||||
tessera-providers = { path = "../tessera-providers" }
|
||||
anyhow.workspace = true
|
||||
clap.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
uuid.workspace = true
|
||||
base64 = "0.22"
|
||||
58
crates/tessera-agent/src/http.rs
Normal file
58
crates/tessera-agent/src/http.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
use anyhow::{Context, Result};
|
||||
use std::collections::HashMap;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::TcpStream;
|
||||
|
||||
pub fn post_json(url: &str, body: &serde_json::Value, headers: &[(&str, &str)]) -> Result<String> {
|
||||
let payload = serde_json::to_string(body)?;
|
||||
request("POST", url, Some(&payload), Some("application/json"), headers)
|
||||
}
|
||||
|
||||
fn request(
|
||||
method: &str,
|
||||
url: &str,
|
||||
body: Option<&str>,
|
||||
content_type: Option<&str>,
|
||||
headers: &[(&str, &str)],
|
||||
) -> Result<String> {
|
||||
let (host, port, path) = parse_url(url)?;
|
||||
let mut stream = TcpStream::connect((host.as_str(), port)).context("connect")?;
|
||||
let mut req = format!("{method} {path} HTTP/1.1\r\nHost: {host}\r\nConnection: close\r\n");
|
||||
for (k, v) in headers {
|
||||
req.push_str(&format!("{k}: {v}\r\n"));
|
||||
}
|
||||
if let Some(b) = body {
|
||||
req.push_str(&format!("Content-Length: {}\r\n", b.len()));
|
||||
if let Some(ct) = content_type {
|
||||
req.push_str(&format!("Content-Type: {ct}\r\n"));
|
||||
}
|
||||
req.push_str("\r\n");
|
||||
req.push_str(b);
|
||||
} else {
|
||||
req.push_str("\r\n");
|
||||
}
|
||||
stream.write_all(req.as_bytes())?;
|
||||
let mut buf = String::new();
|
||||
stream.read_to_string(&mut buf)?;
|
||||
let body_start = buf.find("\r\n\r\n").map(|i| i + 4).unwrap_or(0);
|
||||
Ok(buf[body_start..].trim().to_string())
|
||||
}
|
||||
|
||||
fn parse_url(url: &str) -> Result<(String, u16, String)> {
|
||||
let url = url.strip_prefix("http://").context("only http supported")?;
|
||||
let (host_port, path) = url.split_once('/').unwrap_or((url, ""));
|
||||
let path = if path.is_empty() {
|
||||
"/".to_string()
|
||||
} else {
|
||||
format!("/{path}")
|
||||
};
|
||||
let (host, port) = if let Some((h, p)) = host_port.split_once(':') {
|
||||
(h.to_string(), p.parse()?)
|
||||
} else {
|
||||
(host_port.to_string(), 80)
|
||||
};
|
||||
Ok((host, port, path))
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub type HeaderMap = HashMap<String, String>;
|
||||
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