forge
Explore
Log in

Don't you dare go Hollow

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 commit

6ae6238afd1c63500e6aca8c1af76dd00fa41788Ahmet Kaan GümüşParent: 286a6e7
All checks have passed
Show checks
  • 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
Show full message
✨ 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
README.mdmodified+1 -0
@@ -149,6 +149,7 @@ Each `[[app]]` table:
149149 | `path` | yes | none | Path of the installed binary, absolute after `~/` expansion, e.g. `~/bin/im`. |
150150 | `unit` | no | none | systemd user unit to restart (`systemctl --user`) and health-check. Without it kur only swaps the file. |
151151 | `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`. |
152153 | `check` | no | none | Arguments for the new binary, run before the swap. Must exit 0 within 10 s. |
153154 | `health_url` | no | none | URL that must return 2xx after restart. No token is sent. |
154155 | `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"
3434 asset = "kur-{arch}-linux"
3535 path = "~/bin/kur"
3636 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] = &[
2020 "path",
2121 "unit",
2222 "prerelease",
23+ "unpack",
2324 "check",
2425 "health_url",
2526 "health_timeout_secs",
@@ -45,6 +46,8 @@ pub struct App {
4546 pub unit: Option<String>,
4647 #[serde(default)]
4748 pub prerelease: bool,
49+ #[serde(default)]
50+ pub unpack: bool,
4851 pub check: Option<Vec<String>>,
4952 pub health_url: Option<String>,
5053 #[serde(default = "default_health_timeout")]
@@ -187,6 +190,9 @@ pub fn load(path: &Path, write: bool) -> Res<Config> {
187190 if parts.len() != 2 || parts.iter().any(|p| p.is_empty()) {
188191 return Err(format!("{}: repo must be owner/name", app.name));
189192 }
193+ if app.unpack && app.check.is_some() {
194+ return Err(format!("{}: check cannot be used with unpack", app.name));
195+ }
190196 }
191197 for app in &mut raw.apps {
192198 app.path = absolute_path(&app.path, &format!("{}: path", app.name))?;
src/install.rsmodified+93 -14
@@ -1,5 +1,6 @@
11 //! 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).
34
45 use crate::Res;
56 use crate::config::{App, Config};
@@ -38,7 +39,7 @@ pub fn lock(dir: &Path) -> Res<File> {
3839 /// `kur status` text for one app (after the name). Changes nothing.
3940 pub fn status(forge: &Forge, cfg: &Config, app: &App) -> Res<String> {
4041 let (tag, asset) = forge.latest(app)?;
41- let installed = file_sha256(&app.path)?;
42+ let installed = installed_sha(cfg, app)?;
4243 let state = if installed.as_deref() == Some(asset.sha256.as_str()) {
4344 "up to date"
4445 } else if is_bad(cfg, app, &asset.sha256)? {
@@ -59,7 +60,7 @@ pub fn status(forge: &Forge, cfg: &Config, app: &App) -> Res<String> {
5960 /// Installs the newest release of one app if needed. Returns the status text after the app name.
6061 pub fn update(forge: &Forge, cfg: &Config, app: &App) -> Res<String> {
6162 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()) {
6364 return Ok(format!("up to date ({tag})"));
6465 }
6566 if is_bad(cfg, app, &asset.sha256)? {
@@ -67,25 +68,42 @@ pub fn update(forge: &Forge, cfg: &Config, app: &App) -> Res<String> {
6768 }
6869
6970 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 {
7687 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+ }
7997 }
8098 swap(app, &staged)?;
8199
82100 let Some(unit) = app.unit.as_deref() else {
83- write_tag(cfg, app, &tag)?;
101+ record_install(cfg, app, &tag, &asset.sha256)?;
84102 return Ok(format!("{tag} installed"));
85103 };
86104 match deploy(app, unit) {
87105 Ok(()) => {
88- write_tag(cfg, app, &tag)?;
106+ record_install(cfg, app, &tag, &asset.sha256)?;
89107 Ok(format!("{tag} installed"))
90108 }
91109 Err(reason) => {
@@ -104,6 +122,30 @@ fn short(sha: &str) -> &str {
104122 sha.get(..12).unwrap_or(sha)
105123 }
106124
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+
107149 /// Lowercase hex sha256 of the file at `path`, or None if it does not exist.
108150 pub(crate) fn file_sha256(path: &Path) -> Res<Option<String>> {
109151 let mut file = match File::open(path) {
@@ -153,6 +195,34 @@ fn write_tag(cfg: &Config, app: &App, tag: &str) -> Res<()> {
153195 fs::write(&path, tag).map_err(|e| format!("write {}: {e}", path.display()))
154196 }
155197
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+
156226 /// Runs the staged binary with the app's `check` args, if any. Fails on non-zero exit or timeout.
157227 fn check_binary(app: &App, staged: &Path) -> Res<()> {
158228 let Some(args) = &app.check else {
@@ -183,8 +253,12 @@ fn check_binary(app: &App, staged: &Path) -> Res<()> {
183253 }
184254 }
185255
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.
187258 fn swap(app: &App, staged: &Path) -> Res<()> {
259+ if app.unpack {
260+ remove_tree(&app.sibling(".prev"))?;
261+ }
188262 match fs::rename(&app.path, app.sibling(".prev")) {
189263 Err(e) if e.kind() != ErrorKind::NotFound => {
190264 return Err(format!("keep previous {}: {e}", app.path.display()));
@@ -219,6 +293,11 @@ fn deploy(app: &App, unit: &str) -> Result<(), String> {
219293 /// Restores `.prev` over the failed binary and restarts the unit. Returns the error text.
220294 fn rollback(app: &App, unit: &str, tag: &str, reason: &str) -> String {
221295 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+ }
222301 match fs::rename(app.sibling(".prev"), &app.path) {
223302 Ok(()) => match restart(unit) {
224303 Ok(()) => format!("{failed}, rolled back"),