Unpack mode: an app's asset can be a .tar.gz that kur unpacks into the path directory, swapping it in and keeping the old one as .prev, with the installed sha256 kept in {name}.sha
Browse files at this commit6ae6238afd1c63500e6aca8c1af76dd00fa41788Parent: 286a6e7All checks have passed
Show checks
ci/buildSucceededDetails
- feat
- unpack mode: an app's asset can be a .tar.gz that kur unpacks into the path directory, swapping it in and keeping the old one as .prev, with the installed sha256 kept in {name}.sha
4 changed files +109 -14
- README.md +1 -0
- config/kur.example.toml +9 -0
- src/config.rs +6 -0
- src/install.rs +93 -14
README.mdmodified+1 -0
| @@ -149,6 +149,7 @@ Each `[[app]]` table: | ||
| 149 | 149 | | `path` | yes | none | Path of the installed binary, absolute after `~/` expansion, e.g. `~/bin/im`. | |
| 150 | 150 | | `unit` | no | none | systemd user unit to restart (`systemctl --user`) and health-check. Without it kur only swaps the file. | |
| 151 | 151 | | `prerelease` | no | `false` | Also accept prerelease releases. | |
| 152 | +| `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`. | | |
| 152 | 153 | | `check` | no | none | Arguments for the new binary, run before the swap. Must exit 0 within 10 s. | |
| 153 | 154 | | `health_url` | no | none | URL that must return 2xx after restart. No token is sent. | |
| 154 | 155 | | `health_timeout_secs` | no | `30` | Time allowed for the health check to pass. | |
config/kur.example.tomlmodified+9 -0
| @@ -34,3 +34,12 @@ repo = "Tahinli/kur" | ||
| 34 | 34 | asset = "kur-{arch}-linux" |
| 35 | 35 | path = "~/bin/kur" |
| 36 | 36 | check = ["--version"] |
| 37 | + | |
| 38 | +# A directory served from a release tarball. kur unpacks it into ~/courier.kur-new, | |
| 39 | +# swaps it in, and keeps the old one as ~/courier.prev. | |
| 40 | +# [[app]] | |
| 41 | +# name = "courier" | |
| 42 | +# repo = "Tahinli/courier" | |
| 43 | +# asset = "courier-web.tar.gz" | |
| 44 | +# path = "~/courier" | |
| 45 | +# unpack = true |
src/config.rsmodified+6 -0
| @@ -20,6 +20,7 @@ const KNOWN_APP_KEYS: &[&str] = &[ | ||
| 20 | 20 | "path", |
| 21 | 21 | "unit", |
| 22 | 22 | "prerelease", |
| 23 | + "unpack", | |
| 23 | 24 | "check", |
| 24 | 25 | "health_url", |
| 25 | 26 | "health_timeout_secs", |
| @@ -45,6 +46,8 @@ pub struct App { | ||
| 45 | 46 | pub unit: Option<String>, |
| 46 | 47 | #[serde(default)] |
| 47 | 48 | pub prerelease: bool, |
| 49 | + #[serde(default)] | |
| 50 | + pub unpack: bool, | |
| 48 | 51 | pub check: Option<Vec<String>>, |
| 49 | 52 | pub health_url: Option<String>, |
| 50 | 53 | #[serde(default = "default_health_timeout")] |
| @@ -187,6 +190,9 @@ pub fn load(path: &Path, write: bool) -> Res<Config> { | ||
| 187 | 190 | if parts.len() != 2 || parts.iter().any(|p| p.is_empty()) { |
| 188 | 191 | return Err(format!("{}: repo must be owner/name", app.name)); |
| 189 | 192 | } |
| 193 | + if app.unpack && app.check.is_some() { | |
| 194 | + return Err(format!("{}: check cannot be used with unpack", app.name)); | |
| 195 | + } | |
| 190 | 196 | } |
| 191 | 197 | for app in &mut raw.apps { |
| 192 | 198 | app.path = absolute_path(&app.path, &format!("{}: path", app.name))?; |
src/install.rsmodified+93 -14
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | //! Per-app install flow: download, check, swap, restart, health check, rollback. |
| 2 | -//! State files in `state_dir`: `{name}.bad` lists rejected sha256s, `{name}.tag` the last tag installed. | |
| 2 | +//! State files in `state_dir`: `{name}.bad` lists rejected sha256s, `{name}.tag` the last tag installed, | |
| 3 | +//! `{name}.sha` the installed sha256 (unpack mode only). | |
| 3 | 4 | |
| 4 | 5 | use crate::Res; |
| 5 | 6 | use crate::config::{App, Config}; |
| @@ -38,7 +39,7 @@ pub fn lock(dir: &Path) -> Res<File> { | ||
| 38 | 39 | /// `kur status` text for one app (after the name). Changes nothing. |
| 39 | 40 | pub fn status(forge: &Forge, cfg: &Config, app: &App) -> Res<String> { |
| 40 | 41 | let (tag, asset) = forge.latest(app)?; |
| 41 | - let installed = file_sha256(&app.path)?; | |
| 42 | + let installed = installed_sha(cfg, app)?; | |
| 42 | 43 | let state = if installed.as_deref() == Some(asset.sha256.as_str()) { |
| 43 | 44 | "up to date" |
| 44 | 45 | } else if is_bad(cfg, app, &asset.sha256)? { |
| @@ -59,7 +60,7 @@ pub fn status(forge: &Forge, cfg: &Config, app: &App) -> Res<String> { | ||
| 59 | 60 | /// Installs the newest release of one app if needed. Returns the status text after the app name. |
| 60 | 61 | pub fn update(forge: &Forge, cfg: &Config, app: &App) -> Res<String> { |
| 61 | 62 | let (tag, asset) = forge.latest(app)?; |
| 62 | - if file_sha256(&app.path)?.as_deref() == Some(asset.sha256.as_str()) { | |
| 63 | + if installed_sha(cfg, app)?.as_deref() == Some(asset.sha256.as_str()) { | |
| 63 | 64 | return Ok(format!("up to date ({tag})")); |
| 64 | 65 | } |
| 65 | 66 | if is_bad(cfg, app, &asset.sha256)? { |
| @@ -67,25 +68,42 @@ pub fn update(forge: &Forge, cfg: &Config, app: &App) -> Res<String> { | ||
| 67 | 68 | } |
| 68 | 69 | |
| 69 | 70 | let staged = app.sibling(".kur-new"); |
| 70 | - let _ = fs::remove_file(&staged); | |
| 71 | - if let Err(e) = forge.download(&asset, &staged) { | |
| 72 | - let _ = fs::remove_file(&staged); | |
| 73 | - return Err(e); | |
| 74 | - } | |
| 75 | - if let Err(e) = check_binary(app, &staged) { | |
| 71 | + if app.unpack { | |
| 72 | + let archive = app.sibling(".kur-new.tar.gz"); | |
| 73 | + let _ = fs::remove_file(&archive); | |
| 74 | + remove_tree(&staged)?; | |
| 75 | + if let Err(e) = forge.download(&asset, &archive) { | |
| 76 | + let _ = fs::remove_file(&archive); | |
| 77 | + return Err(e); | |
| 78 | + } | |
| 79 | + let unpacked = unpack(&archive, &staged); | |
| 80 | + let _ = fs::remove_file(&archive); | |
| 81 | + if let Err(e) = unpacked { | |
| 82 | + let _ = fs::remove_dir_all(&staged); | |
| 83 | + mark_bad(cfg, app, &asset.sha256)?; | |
| 84 | + return Err(e); | |
| 85 | + } | |
| 86 | + } else { | |
| 76 | 87 | let _ = fs::remove_file(&staged); |
| 77 | - mark_bad(cfg, app, &asset.sha256)?; | |
| 78 | - return Err(e); | |
| 88 | + if let Err(e) = forge.download(&asset, &staged) { | |
| 89 | + let _ = fs::remove_file(&staged); | |
| 90 | + return Err(e); | |
| 91 | + } | |
| 92 | + if let Err(e) = check_binary(app, &staged) { | |
| 93 | + let _ = fs::remove_file(&staged); | |
| 94 | + mark_bad(cfg, app, &asset.sha256)?; | |
| 95 | + return Err(e); | |
| 96 | + } | |
| 79 | 97 | } |
| 80 | 98 | swap(app, &staged)?; |
| 81 | 99 | |
| 82 | 100 | let Some(unit) = app.unit.as_deref() else { |
| 83 | - write_tag(cfg, app, &tag)?; | |
| 101 | + record_install(cfg, app, &tag, &asset.sha256)?; | |
| 84 | 102 | return Ok(format!("{tag} installed")); |
| 85 | 103 | }; |
| 86 | 104 | match deploy(app, unit) { |
| 87 | 105 | Ok(()) => { |
| 88 | - write_tag(cfg, app, &tag)?; | |
| 106 | + record_install(cfg, app, &tag, &asset.sha256)?; | |
| 89 | 107 | Ok(format!("{tag} installed")) |
| 90 | 108 | } |
| 91 | 109 | Err(reason) => { |
| @@ -104,6 +122,30 @@ fn short(sha: &str) -> &str { | ||
| 104 | 122 | sha.get(..12).unwrap_or(sha) |
| 105 | 123 | } |
| 106 | 124 | |
| 125 | +/// sha256 of the installed app: of the file, or in unpack mode the one recorded at install. | |
| 126 | +/// None if nothing is installed. | |
| 127 | +fn installed_sha(cfg: &Config, app: &App) -> Res<Option<String>> { | |
| 128 | + if !app.unpack { | |
| 129 | + return file_sha256(&app.path); | |
| 130 | + } | |
| 131 | + if !app.path.exists() { | |
| 132 | + return Ok(None); | |
| 133 | + } | |
| 134 | + match fs::read_to_string(state_file(cfg, app, "sha")) { | |
| 135 | + Ok(text) => Ok(Some(text.trim().to_string())), | |
| 136 | + Err(e) if e.kind() == ErrorKind::NotFound => Ok(None), | |
| 137 | + Err(e) => Err(format!("read sha: {e}")), | |
| 138 | + } | |
| 139 | +} | |
| 140 | + | |
| 141 | +/// Removes the directory tree at `path`. A missing path is not an error. | |
| 142 | +fn remove_tree(path: &Path) -> Res<()> { | |
| 143 | + match fs::remove_dir_all(path) { | |
| 144 | + Err(e) if e.kind() != ErrorKind::NotFound => Err(format!("remove {}: {e}", path.display())), | |
| 145 | + _ => Ok(()), | |
| 146 | + } | |
| 147 | +} | |
| 148 | + | |
| 107 | 149 | /// Lowercase hex sha256 of the file at `path`, or None if it does not exist. |
| 108 | 150 | pub(crate) fn file_sha256(path: &Path) -> Res<Option<String>> { |
| 109 | 151 | let mut file = match File::open(path) { |
| @@ -153,6 +195,34 @@ fn write_tag(cfg: &Config, app: &App, tag: &str) -> Res<()> { | ||
| 153 | 195 | fs::write(&path, tag).map_err(|e| format!("write {}: {e}", path.display())) |
| 154 | 196 | } |
| 155 | 197 | |
| 198 | +/// Records a successful install: the tag, and in unpack mode the asset sha256. | |
| 199 | +fn record_install(cfg: &Config, app: &App, tag: &str, sha: &str) -> Res<()> { | |
| 200 | + write_tag(cfg, app, tag)?; | |
| 201 | + if app.unpack { | |
| 202 | + let path = state_file(cfg, app, "sha"); | |
| 203 | + fs::write(&path, sha).map_err(|e| format!("write {}: {e}", path.display()))?; | |
| 204 | + } | |
| 205 | + Ok(()) | |
| 206 | +} | |
| 207 | + | |
| 208 | +/// Unpacks the gzip tar at `archive` into `dir`, which must not exist yet. | |
| 209 | +fn unpack(archive: &Path, dir: &Path) -> Res<()> { | |
| 210 | + fs::create_dir(dir).map_err(|e| format!("create {}: {e}", dir.display()))?; | |
| 211 | + let status = Command::new("tar") | |
| 212 | + .arg("-xzf") | |
| 213 | + .arg(archive) | |
| 214 | + .arg("-C") | |
| 215 | + .arg(dir) | |
| 216 | + .stdin(Stdio::null()) | |
| 217 | + .status() | |
| 218 | + .map_err(|e| format!("cannot run tar: {e}"))?; | |
| 219 | + if status.success() { | |
| 220 | + Ok(()) | |
| 221 | + } else { | |
| 222 | + Err(format!("tar exited with {status}")) | |
| 223 | + } | |
| 224 | +} | |
| 225 | + | |
| 156 | 226 | /// Runs the staged binary with the app's `check` args, if any. Fails on non-zero exit or timeout. |
| 157 | 227 | fn check_binary(app: &App, staged: &Path) -> Res<()> { |
| 158 | 228 | let Some(args) = &app.check else { |
| @@ -183,8 +253,12 @@ fn check_binary(app: &App, staged: &Path) -> Res<()> { | ||
| 183 | 253 | } |
| 184 | 254 | } |
| 185 | 255 | |
| 186 | -/// Moves the current binary to `.prev` (if any) and the staged file into place, then fsyncs the dir. | |
| 256 | +/// Moves the current binary (or directory, in unpack mode) to `.prev` and the staged one into place, | |
| 257 | +/// then fsyncs the dir. | |
| 187 | 258 | fn swap(app: &App, staged: &Path) -> Res<()> { |
| 259 | + if app.unpack { | |
| 260 | + remove_tree(&app.sibling(".prev"))?; | |
| 261 | + } | |
| 188 | 262 | match fs::rename(&app.path, app.sibling(".prev")) { |
| 189 | 263 | Err(e) if e.kind() != ErrorKind::NotFound => { |
| 190 | 264 | return Err(format!("keep previous {}: {e}", app.path.display())); |
| @@ -219,6 +293,11 @@ fn deploy(app: &App, unit: &str) -> Result<(), String> { | ||
| 219 | 293 | /// Restores `.prev` over the failed binary and restarts the unit. Returns the error text. |
| 220 | 294 | fn rollback(app: &App, unit: &str, tag: &str, reason: &str) -> String { |
| 221 | 295 | let failed = format!("{tag} failed health check ({reason})"); |
| 296 | + if app.unpack | |
| 297 | + && let Err(e) = remove_tree(&app.path) | |
| 298 | + { | |
| 299 | + return format!("{failed}, restore failed: {e}"); | |
| 300 | + } | |
| 222 | 301 | match fs::rename(app.sibling(".prev"), &app.path) { |
| 223 | 302 | Ok(()) => match restart(unit) { |
| 224 | 303 | Ok(()) => format!("{failed}, rolled back"), |