fn sh_squote(s: &str) -> String {
format!("'{}'", s.replace('\'', r"'\''"))
}
pub fn build_install_script(version: &str, url: &str, sha256: &str, sudo: bool) -> String {
let sudo_kw = if sudo { "sudo" } else { "" };
format!(
r#"set -euo pipefail
SUDO={sudo_kw}
URL={url}
SHA={sha}
VER={ver}
WORK="$(mktemp -d /tmp/yah-roll.XXXXXX)"
trap 'rm -rf "$WORK"' EXIT
cd "$WORK"
echo "== fetch + verify (sha256 from signed manifest) =="
curl -fsSL -o pair.tar.gz "$URL"
printf '%s pair.tar.gz\n' "$SHA" | sha256sum -c -
mkdir x && tar -xzf pair.tar.gz -C x
D="$(find x -maxdepth 1 -type d -name 'yubaba-*' | head -1)"
[ -n "$D" ] || {{ echo 'tarball layout unexpected: no yubaba-* dir' >&2; exit 1; }}
# Atomic install: stage next to the target on the SAME filesystem, then rename.
# A rename within a filesystem is atomic, so a half-written binary/unit is
# never observable. This script touches /usr/local/bin bytes + unit files ONLY
# (durable host state is deliberately out of scope — see the module docs).
install_atomic() {{ # src mode dest
$SUDO install -m"$2" "$1" "$3.roll-new.$$"
$SUDO mv -f "$3.roll-new.$$" "$3"
}}
install_atomic "$D/yubaba" 0755 /usr/local/bin/yubaba
install_atomic "$D/kamaji" 0755 /usr/local/bin/kamaji
install_atomic "$D/yubaba.slice" 0644 /etc/systemd/system/yubaba.slice
install_atomic "$D/kamaji.service" 0644 /etc/systemd/system/kamaji.service
install_atomic "$D/yubaba.service" 0644 /etc/systemd/system/yubaba.service
echo "== restart supervision tree (kamaji then yubaba, W154 order) =="
$SUDO systemctl daemon-reload
$SUDO systemctl restart kamaji.service
$SUDO systemctl restart yubaba.service
echo "installed target=$VER yubaba=$(/usr/local/bin/yubaba --version 2>/dev/null) kamaji=$(/usr/local/bin/kamaji --version 2>/dev/null)"
"#,
sudo_kw = sudo_kw,
url = sh_squote(url),
sha = sh_squote(sha256),
ver = sh_squote(version),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn script_embeds_the_signed_digest_and_url() {
let url = "https://cdn.yah.dev/yubaba/0.8.19/yubaba-0.8.19-x86_64-unknown-linux-musl.tar.gz";
let sha = "abc123def456";
let s = build_install_script("0.8.19", url, sha, false);
assert!(s.contains(sha), "script must carry the manifest sha256");
assert!(s.contains(url), "script must carry the manifest url");
assert!(s.contains("sha256sum -c -"), "script must verify the digest");
}
#[test]
fn script_is_atomic_and_installs_the_whole_pair() {
let s = build_install_script("0.8.19", "u", "d", false);
assert!(s.contains("mv -f"), "install must be an atomic rename");
assert!(s.contains(".roll-new.$$"), "install must stage to a temp name");
for target in [
"/usr/local/bin/yubaba",
"/usr/local/bin/kamaji",
"/etc/systemd/system/yubaba.slice",
"/etc/systemd/system/kamaji.service",
"/etc/systemd/system/yubaba.service",
] {
assert!(s.contains(target), "script must install {target}");
}
let k = s.find("restart kamaji.service").unwrap();
let y = s.find("restart yubaba.service").unwrap();
assert!(k < y, "kamaji must restart before yubaba");
assert!(s.contains("daemon-reload"));
}
#[test]
fn script_never_touches_durable_state() {
let s = build_install_script("0.8.19", "u", "d", true);
assert!(!s.contains("identity.json"), "must not touch host identity");
assert!(!s.contains("/var/lib/yah-cloud"), "must not touch state dir");
assert!(!s.contains("raft"), "must not touch the raft log dir");
assert!(!s.contains("rm -rf /"), "must not wipe system paths");
}
#[test]
fn sudo_prefix_tracks_the_caller() {
let s = build_install_script("0.8.19", "u", "d", true);
assert!(s.contains("SUDO=sudo"));
let s = build_install_script("0.8.19", "u", "d", false);
assert!(s.contains("SUDO=\n") || s.contains("SUDO="));
}
}