feat: add configurable GFS retention per orbit
This commit is contained in:
@@ -11,7 +11,18 @@ pub fn post_json(url: &str, body: &serde_json::Value) -> Result<String> {
|
||||
request("POST", url, Some(&payload), Some("application/json"))
|
||||
}
|
||||
|
||||
fn request(method: &str, url: &str, body: Option<&str>, content_type: Option<&str>) -> Result<String> {
|
||||
pub fn put_json(url: &str, body: &serde_json::Value) -> Result<()> {
|
||||
let payload = serde_json::to_string(body)?;
|
||||
request("PUT", url, Some(&payload), Some("application/json"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn request(
|
||||
method: &str,
|
||||
url: &str,
|
||||
body: Option<&str>,
|
||||
content_type: Option<&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");
|
||||
@@ -28,8 +39,19 @@ fn request(method: &str, url: &str, body: Option<&str>, content_type: Option<&st
|
||||
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())
|
||||
let (headers, body) = buf.split_once("\r\n\r\n").context("invalid hub response")?;
|
||||
let status = headers
|
||||
.lines()
|
||||
.next()
|
||||
.and_then(|line| line.split_whitespace().nth(1))
|
||||
.context("missing hub response status")?
|
||||
.parse::<u16>()?;
|
||||
anyhow::ensure!(
|
||||
(200..300).contains(&status),
|
||||
"hub returned HTTP {status}: {}",
|
||||
body.trim()
|
||||
);
|
||||
Ok(body.trim().to_string())
|
||||
}
|
||||
|
||||
fn parse_url(url: &str) -> Result<(String, u16, String)> {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
mod http;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use clap::{Parser, Subcommand};
|
||||
use clap::{Args, Parser, Subcommand};
|
||||
use tessera_core::decay::RetentionPolicy;
|
||||
use tessera_store::Vault;
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -33,6 +34,16 @@ enum Commands {
|
||||
schedule: Option<String>,
|
||||
#[arg(long)]
|
||||
dry_run: bool,
|
||||
#[command(flatten)]
|
||||
retention: RetentionArgs,
|
||||
},
|
||||
/// Change an orbit's retention for future captures.
|
||||
Retention {
|
||||
orbit_id: Uuid,
|
||||
#[arg(long)]
|
||||
preset: Option<String>,
|
||||
#[command(flatten)]
|
||||
counts: RetentionArgs,
|
||||
},
|
||||
Snap {
|
||||
#[arg(long)]
|
||||
@@ -64,6 +75,45 @@ enum Commands {
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Args, Clone)]
|
||||
struct RetentionArgs {
|
||||
#[arg(long)]
|
||||
keep_daily: Option<u32>,
|
||||
#[arg(long)]
|
||||
keep_weekly: Option<u32>,
|
||||
#[arg(long)]
|
||||
keep_monthly: Option<u32>,
|
||||
#[arg(long)]
|
||||
keep_yearly: Option<u32>,
|
||||
}
|
||||
|
||||
impl RetentionArgs {
|
||||
fn policy(self) -> Result<Option<RetentionPolicy>> {
|
||||
if [
|
||||
self.keep_daily,
|
||||
self.keep_weekly,
|
||||
self.keep_monthly,
|
||||
self.keep_yearly,
|
||||
]
|
||||
.iter()
|
||||
.all(Option::is_none)
|
||||
{
|
||||
return Ok(None);
|
||||
}
|
||||
let policy = RetentionPolicy {
|
||||
keep_daily: self.keep_daily.unwrap_or(0),
|
||||
keep_weekly: self.keep_weekly.unwrap_or(0),
|
||||
keep_monthly: self.keep_monthly.unwrap_or(0),
|
||||
keep_yearly: self.keep_yearly.unwrap_or(0),
|
||||
};
|
||||
anyhow::ensure!(
|
||||
policy.has_tiers(),
|
||||
"set at least one retention count above zero"
|
||||
);
|
||||
Ok(Some(policy))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Clone)]
|
||||
enum WormholeCmd {
|
||||
Pair {
|
||||
@@ -85,7 +135,32 @@ fn main() -> Result<()> {
|
||||
path,
|
||||
schedule,
|
||||
dry_run,
|
||||
} => cmd_orbit(&cli, name, provider, path, schedule, dry_run)?,
|
||||
retention,
|
||||
} => cmd_orbit(
|
||||
&cli,
|
||||
name,
|
||||
provider,
|
||||
path,
|
||||
schedule,
|
||||
dry_run,
|
||||
retention.policy()?,
|
||||
)?,
|
||||
Commands::Retention {
|
||||
orbit_id,
|
||||
preset,
|
||||
counts,
|
||||
} => {
|
||||
let retention = counts.policy()?;
|
||||
anyhow::ensure!(
|
||||
preset.is_some() != retention.is_some(),
|
||||
"choose --preset or custom --keep-* counts"
|
||||
);
|
||||
http::put_json(
|
||||
&format!("{}/api/v1/orbits/{orbit_id}/retention", cli.hub_url),
|
||||
&serde_json::json!({ "decay_preset": preset, "retention": retention }),
|
||||
)?;
|
||||
println!("Retention saved for {orbit_id}.");
|
||||
}
|
||||
Commands::Snap { orbit } => cmd_snap(&cli, orbit)?,
|
||||
Commands::List => cmd_list(&cli)?,
|
||||
Commands::Reconstitute {
|
||||
@@ -174,6 +249,7 @@ fn cmd_orbit(
|
||||
path: Option<String>,
|
||||
schedule: Option<String>,
|
||||
dry_run: bool,
|
||||
retention: Option<RetentionPolicy>,
|
||||
) -> Result<()> {
|
||||
let config = match provider.as_str() {
|
||||
"filesystem" => serde_json::json!({ "paths": [path.unwrap_or_else(|| ".".into())] }),
|
||||
@@ -193,6 +269,7 @@ fn cmd_orbit(
|
||||
"provider_config": config,
|
||||
"schedule": schedule,
|
||||
"dry_run": dry_run,
|
||||
"retention": retention,
|
||||
});
|
||||
let raw = http::post_json(&format!("{}/api/v1/orbits", cli.hub_url), &body)?;
|
||||
let orbit: serde_json::Value = serde_json::from_str(&raw)?;
|
||||
|
||||
Reference in New Issue
Block a user