forge
Explore
Log in

Don't you dare go Hollow

kur

Clone
★ 0

0 watching0 forksRustAGPL-3.073.7 KiB6ae6238Tahinli21 days ago5 commitsv0.1.0 · 2 assets · 22 downloads

README.md

kur

kur ("install" in Turkish) installs the newest release of each configured app on a server. It runs from a systemd user timer, reads releases from a self-hosted forge (git.tahinli.com), and swaps a binary in only when the release asset differs from the installed file. After a swap it restarts the app's systemd user unit and health-checks it, and it rolls back to the previous binary if the check fails. It is one binary that runs as an unprivileged user and has no daemon. It never runs as root.

Releases

For each app, kur lists the repo's releases (newest first) and takes the first one that is not a draft and is not a prerelease. Set prerelease = true on an app to also accept prereleases. Within the chosen release it looks up the asset named by asset (with {arch} filled in). The forge reports each asset's sha256; kur verifies the download against that value and refuses a mismatch.

Run flow

For each app, in order:

  1. Fetch the release and asset.
  2. Compare the sha256 of the installed binary with the asset's. If equal, report up to date.
  3. If the asset's sha256 is listed in {state_dir}/{name}.bad, report it as skipped.
  4. Download to {path}.kur-new, hashing while writing. Mode 0755, fsync.
  5. If check is set, run the new file with those arguments (10 s limit). Failure marks the build bad.
  6. Rename the installed binary to {path}.prev and the new file to {path}.
  7. If unit is set, restart it with systemctl --user and wait until systemctl --user is-active succeeds, health_url returns 2xx, and the unit is still active 5 s later. Times out after health_timeout_secs.
  8. On health failure, the build's sha256 is appended to {name}.bad. If {path}.prev exists, it is renamed back to {path} and the unit is restarted. Otherwise the run reports that there was no previous binary to restore.

A run holds a lock on {state_dir}/lock, so a manual kur run exits at once while the timer's run is in progress. kur status takes no lock. One app failing does not stop the others. Output goes to stdout and errors to stderr, which the journal collects.

The bad list

{state_dir}/{name}.bad holds one sha256 per line. kur skips any release whose asset hash is in the file. To retry a build, delete its line, or delete the file to clear the list for that app.

Install

kur and the apps it installs run as one unprivileged account, for example apps. Do not run kur as root. Get a working user session for the account first. Plain sudo -u apps does not work, because it has no XDG_RUNTIME_DIR and systemctl --user fails there. Use machinectl:

sudo machinectl shell apps@

As that account, in the directory with the release file for this machine (kur-x86_64-linux, or kur-aarch64-linux on aarch64 hosts):

sha256sum -c SHA256SUMS      # verify the download; add --ignore-missing if you fetched one binary only
chmod +x kur-x86_64-linux
./kur-x86_64-linux           # first run: sets kur up

With no command, kur runs setup when the config file is missing, and otherwise behaves like kur status. Setup is idempotent and does this, printing one line per step:

  1. Refuses to run as root.
  2. Copies the binary to ~/bin/kur (skipped if the same file is already there).
  3. Writes a starter config to ~/.config/kur/kur.toml (mode 0600). It contains kur itself as an app. An existing config is never touched.
  4. Writes kur.service and kur.timer to ~/.config/systemd/user/ (only if missing or different).
  5. Runs systemctl --user daemon-reload and systemctl --user enable --now kur.timer.
  6. Prints the config path and the done line.

Then add your apps to the config. Each one needs a [[app]] table (see the reference below). The config file must be mode 0600. If token_file is set, the token file must be mode 0600 too. For private repos, put a read-only forge token in ~/.config/kur/token with mode 0600, and set token_file = "~/.config/kur/token" in the config.

kur setup runs setup explicitly. Use it to repair or reinstall: it puts back any missing binary, config or unit, and it changes nothing that is already in place.

Each app must be a user unit of the same account. Its binary lives under ~/bin, and its unit goes in ~/.config/systemd/user/. The app units are not part of this repo. For example, im's unit is deploy/user/im.service in the im repo, installed the same way:

install -m 0644 deploy/user/im.service ~/.config/systemd/user/
systemctl --user daemon-reload
systemctl --user enable --now im.service

An app's unit must exist before kur runs for that app. Do not set User= in these units: they run as the account already.

Logs

journalctl --user -u kur

On some distros the user journal needs persistent journald storage. If journalctl --user shows nothing, create /var/log/journal as root and restart systemd-journald. Until then, read the logs as root with:

journalctl _SYSTEMD_USER_UNIT=kur.service

Commands

kur                first run: sets up if the config file is missing, else same as status
kur setup          set up kur (idempotent); see Install
kur run            process every app once
kur run <name>     process one app
kur status         show installed and newest versions; changes nothing
kur --version      print "kur <version>"
kur help           print usage (also used for unknown commands, exit 2)

A bare kur runs setup when the config file does not exist, and otherwise does the same as kur status. kur run and kur status never run setup.

Global option: --config <path>, placed before the command. Otherwise KUR_CONFIG is used, then $XDG_CONFIG_HOME/kur/kur.toml, or ~/.config/kur/kur.toml when XDG_CONFIG_HOME is not set. Empty environment variables count as unset. If neither XDG_CONFIG_HOME nor HOME is set, kur prints an error and exits 1.

Exit code is 0 when every app succeeded or was up to date, and 1 if any app failed.

Config reference

Paths may start with ~/, which expands to $HOME/. Only a leading ~/ is expanded. After expansion, path, state_dir and token_file must be absolute, so a bare ~ or ~user/... is an error.

Top level:

Key Required Default Meaning
forge yes none Forge base URL, https:// or http://. Trailing slash is trimmed.
token_file no none File with a forge token for private repos. Read once, trimmed. Unreadable file is a config error.
state_dir no $XDG_STATE_HOME/kur, else ~/.local/state/kur Directory for .bad and .tag files and the run lock. Created with mode 0700 if missing.

Each [[app]] table:

Key Required Default Meaning
name yes none Unique. Must match ^[a-z0-9][a-z0-9_-]{0,39}$.
repo yes none Forge repo as owner/name.
asset yes none Asset file name. {arch} is replaced by the machine architecture.
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.

Unknown keys are reported as a warning on every run and in kur status, and ignored, so a config written for a newer kur still works with an older one. A misspelled optional key is therefore ignored, so check the warnings after editing. A config error exits 1 before anything is changed.

See config/kur.example.toml for a working example.

Config changes between versions

Unknown keys are warnings, as above. When a release renames a key, the old name keeps working. kur run then rewrites the config file to the new names and keeps its comments and layout. The original is saved next to it as <config>.bak. kur status reports the renames but writes nothing. If the rewrite fails, kur warns and runs with the renamed config in memory.

Publishing an app

kur needs each app's repo to publish its binary as a forge release. The app repo needs a forge CI config with a [release] section that publishes <app>-<arch>-linux (for example im-x86_64-linux) on v* tags, so the release has an asset that kur can match. See .forge/ci.toml in this repo for an example.