From 6ae6238afd1c63500e6aca8c1af76dd00fa41788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 9 Nov 2026 18:05:19 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20unpack=20mode:=20an=20app's?= =?UTF-8?q?=20asset=20can=20be=20a=20.tar.gz=20that=20kur=20unpacks=20into?= =?UTF-8?q?=20the=20path=20directory,=20swapping=20it=20in=20and=20keeping?= =?UTF-8?q?=20the=20old=20one=20as=20.prev,=20with=20the=20installed=20sha?= =?UTF-8?q?256=20kept=20in=20{name}.sha?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + config/kur.example.toml | 9 ++++ src/config.rs | 6 +++ src/install.rs | 107 ++++++++++++++++++++++++++++++++++------ 4 files changed, 109 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index c7b5894..6dfad95 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,7 @@ Each `[[app]]` table: | `path` | yes | none | Path of the installed binary, absolute after `~/` expansion, e.g. `~/bin/im`. | | `unit` | no | none | systemd user unit to restart (`systemctl --user`) and health-check. Without it kur only swaps the file. | | `prerelease` | no | `false` | Also accept prerelease releases. | +| `unpack` | no | `false` | The asset is a `.tar.gz`. `path` is a directory: kur unpacks into `{path}.kur-new`, swaps it in, keeps the old one as `{path}.prev`, and records the installed sha256 in `{state_dir}/{name}.sha`. Needs `tar`. Not with `check`. | | `check` | no | none | Arguments for the new binary, run before the swap. Must exit 0 within 10 s. | | `health_url` | no | none | URL that must return 2xx after restart. No token is sent. | | `health_timeout_secs` | no | `30` | Time allowed for the health check to pass. | diff --git a/config/kur.example.toml b/config/kur.example.toml index 0036416..f655e31 100644 --- a/config/kur.example.toml +++ b/config/kur.example.toml @@ -34,3 +34,12 @@ repo = "Tahinli/kur" asset = "kur-{arch}-linux" path = "~/bin/kur" check = ["--version"] + +# A directory served from a release tarball. kur unpacks it into ~/courier.kur-new, +# swaps it in, and keeps the old one as ~/courier.prev. +# [[app]] +# name = "courier" +# repo = "Tahinli/courier" +# asset = "courier-web.tar.gz" +# path = "~/courier" +# unpack = true diff --git a/src/config.rs b/src/config.rs index 010f5a9..79ae647 100644 --- a/src/config.rs +++ b/src/config.rs @@ -20,6 +20,7 @@ const KNOWN_APP_KEYS: &[&str] = &[ "path", "unit", "prerelease", + "unpack", "check", "health_url", "health_timeout_secs", @@ -45,6 +46,8 @@ pub struct App { pub unit: Option, #[serde(default)] pub prerelease: bool, + #[serde(default)] + pub unpack: bool, pub check: Option>, pub health_url: Option, #[serde(default = "default_health_timeout")] @@ -187,6 +190,9 @@ pub fn load(path: &Path, write: bool) -> Res { if parts.len() != 2 || parts.iter().any(|p| p.is_empty()) { return Err(format!("{}: repo must be owner/name", app.name)); } + if app.unpack && app.check.is_some() { + return Err(format!("{}: check cannot be used with unpack", app.name)); + } } for app in &mut raw.apps { app.path = absolute_path(&app.path, &format!("{}: path", app.name))?; diff --git a/src/install.rs b/src/install.rs index 5ea05ce..032b600 100644 --- a/src/install.rs +++ b/src/install.rs @@ -1,5 +1,6 @@ //! Per-app install flow: download, check, swap, restart, health check, rollback. -//! State files in `state_dir`: `{name}.bad` lists rejected sha256s, `{name}.tag` the last tag installed. +//! State files in `state_dir`: `{name}.bad` lists rejected sha256s, `{name}.tag` the last tag installed, +//! `{name}.sha` the installed sha256 (unpack mode only). use crate::Res; use crate::config::{App, Config}; @@ -38,7 +39,7 @@ pub fn lock(dir: &Path) -> Res { /// `kur status` text for one app (after the name). Changes nothing. pub fn status(forge: &Forge, cfg: &Config, app: &App) -> Res { let (tag, asset) = forge.latest(app)?; - let installed = file_sha256(&app.path)?; + let installed = installed_sha(cfg, app)?; let state = if installed.as_deref() == Some(asset.sha256.as_str()) { "up to date" } else if is_bad(cfg, app, &asset.sha256)? { @@ -59,7 +60,7 @@ pub fn status(forge: &Forge, cfg: &Config, app: &App) -> Res { /// Installs the newest release of one app if needed. Returns the status text after the app name. pub fn update(forge: &Forge, cfg: &Config, app: &App) -> Res { let (tag, asset) = forge.latest(app)?; - if file_sha256(&app.path)?.as_deref() == Some(asset.sha256.as_str()) { + if installed_sha(cfg, app)?.as_deref() == Some(asset.sha256.as_str()) { return Ok(format!("up to date ({tag})")); } if is_bad(cfg, app, &asset.sha256)? { @@ -67,25 +68,42 @@ pub fn update(forge: &Forge, cfg: &Config, app: &App) -> Res { } let staged = app.sibling(".kur-new"); - let _ = fs::remove_file(&staged); - if let Err(e) = forge.download(&asset, &staged) { - let _ = fs::remove_file(&staged); - return Err(e); - } - if let Err(e) = check_binary(app, &staged) { + if app.unpack { + let archive = app.sibling(".kur-new.tar.gz"); + let _ = fs::remove_file(&archive); + remove_tree(&staged)?; + if let Err(e) = forge.download(&asset, &archive) { + let _ = fs::remove_file(&archive); + return Err(e); + } + let unpacked = unpack(&archive, &staged); + let _ = fs::remove_file(&archive); + if let Err(e) = unpacked { + let _ = fs::remove_dir_all(&staged); + mark_bad(cfg, app, &asset.sha256)?; + return Err(e); + } + } else { let _ = fs::remove_file(&staged); - mark_bad(cfg, app, &asset.sha256)?; - return Err(e); + if let Err(e) = forge.download(&asset, &staged) { + let _ = fs::remove_file(&staged); + return Err(e); + } + if let Err(e) = check_binary(app, &staged) { + let _ = fs::remove_file(&staged); + mark_bad(cfg, app, &asset.sha256)?; + return Err(e); + } } swap(app, &staged)?; let Some(unit) = app.unit.as_deref() else { - write_tag(cfg, app, &tag)?; + record_install(cfg, app, &tag, &asset.sha256)?; return Ok(format!("{tag} installed")); }; match deploy(app, unit) { Ok(()) => { - write_tag(cfg, app, &tag)?; + record_install(cfg, app, &tag, &asset.sha256)?; Ok(format!("{tag} installed")) } Err(reason) => { @@ -104,6 +122,30 @@ fn short(sha: &str) -> &str { sha.get(..12).unwrap_or(sha) } +/// sha256 of the installed app: of the file, or in unpack mode the one recorded at install. +/// None if nothing is installed. +fn installed_sha(cfg: &Config, app: &App) -> Res> { + if !app.unpack { + return file_sha256(&app.path); + } + if !app.path.exists() { + return Ok(None); + } + match fs::read_to_string(state_file(cfg, app, "sha")) { + Ok(text) => Ok(Some(text.trim().to_string())), + Err(e) if e.kind() == ErrorKind::NotFound => Ok(None), + Err(e) => Err(format!("read sha: {e}")), + } +} + +/// Removes the directory tree at `path`. A missing path is not an error. +fn remove_tree(path: &Path) -> Res<()> { + match fs::remove_dir_all(path) { + Err(e) if e.kind() != ErrorKind::NotFound => Err(format!("remove {}: {e}", path.display())), + _ => Ok(()), + } +} + /// Lowercase hex sha256 of the file at `path`, or None if it does not exist. pub(crate) fn file_sha256(path: &Path) -> Res> { let mut file = match File::open(path) { @@ -153,6 +195,34 @@ fn write_tag(cfg: &Config, app: &App, tag: &str) -> Res<()> { fs::write(&path, tag).map_err(|e| format!("write {}: {e}", path.display())) } +/// Records a successful install: the tag, and in unpack mode the asset sha256. +fn record_install(cfg: &Config, app: &App, tag: &str, sha: &str) -> Res<()> { + write_tag(cfg, app, tag)?; + if app.unpack { + let path = state_file(cfg, app, "sha"); + fs::write(&path, sha).map_err(|e| format!("write {}: {e}", path.display()))?; + } + Ok(()) +} + +/// Unpacks the gzip tar at `archive` into `dir`, which must not exist yet. +fn unpack(archive: &Path, dir: &Path) -> Res<()> { + fs::create_dir(dir).map_err(|e| format!("create {}: {e}", dir.display()))?; + let status = Command::new("tar") + .arg("-xzf") + .arg(archive) + .arg("-C") + .arg(dir) + .stdin(Stdio::null()) + .status() + .map_err(|e| format!("cannot run tar: {e}"))?; + if status.success() { + Ok(()) + } else { + Err(format!("tar exited with {status}")) + } +} + /// Runs the staged binary with the app's `check` args, if any. Fails on non-zero exit or timeout. fn check_binary(app: &App, staged: &Path) -> Res<()> { let Some(args) = &app.check else { @@ -183,8 +253,12 @@ fn check_binary(app: &App, staged: &Path) -> Res<()> { } } -/// Moves the current binary to `.prev` (if any) and the staged file into place, then fsyncs the dir. +/// Moves the current binary (or directory, in unpack mode) to `.prev` and the staged one into place, +/// then fsyncs the dir. fn swap(app: &App, staged: &Path) -> Res<()> { + if app.unpack { + remove_tree(&app.sibling(".prev"))?; + } match fs::rename(&app.path, app.sibling(".prev")) { Err(e) if e.kind() != ErrorKind::NotFound => { return Err(format!("keep previous {}: {e}", app.path.display())); @@ -219,6 +293,11 @@ fn deploy(app: &App, unit: &str) -> Result<(), String> { /// Restores `.prev` over the failed binary and restarts the unit. Returns the error text. fn rollback(app: &App, unit: &str, tag: &str, reason: &str) -> String { let failed = format!("{tag} failed health check ({reason})"); + if app.unpack + && let Err(e) = remove_tree(&app.path) + { + return format!("{failed}, restore failed: {e}"); + } match fs::rename(app.sibling(".prev"), &app.path) { Ok(()) => match restart(unit) { Ok(()) => format!("{failed}, rolled back"),