horizon-server: delete old releases after an update

An install kept every release it ever unpacked in releases/. Once the game is up on the
new release (or a stopped server is switched), the update now deletes the old ones,
keeping the release in use, the one `previous` names (rollback's target), the one the
running horizon-server came from, and the `updates.keep` newest of the others (1 by
default, a new server.toml setting). A release that can't be deleted is reported and
left for the next update.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-19 09:50:26 +02:00
parent ee59df13a9
commit ee3e874eb8
4 changed files with 194 additions and 6 deletions
+20
View File
@@ -49,6 +49,9 @@ restart_on_crash = true
every = "6h" # automatic backups while the server runs: 30m, 6h, 1d...; "0" turns them off
keep = 12 # automatic backups kept (backups made by hand are never deleted)
[updates]
keep = 1 # other releases kept by updates, besides the one in use and the one rollback goes to
[panel]
bind = "127.0.0.1:8130" # the web panel: this computer only, or a tailnet address (100.x); "" turns it off
allow_public = false # true lets bind be any address; never put the panel on the open internet
@@ -86,6 +89,8 @@ pub struct Config {
#[serde(default)]
pub backups: Backups,
#[serde(default)]
pub updates: Updates,
#[serde(default)]
pub panel: Panel,
}
@@ -181,6 +186,20 @@ pub struct Backups {
pub keep: u32,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct Updates {
/// Releases kept in `releases/` after an update besides the one in use and the one `rollback` goes
/// back to (crate::update::Install::prune).
pub keep: u32,
}
impl Default for Updates {
fn default() -> Self {
Self { keep: 1 }
}
}
impl Default for Server {
fn default() -> Self {
Self {
@@ -378,6 +397,7 @@ pub fn render(config: &Config) -> String {
("engine", "restart_on_crash"),
("backups", "every"),
("backups", "keep"),
("updates", "keep"),
("panel", "bind"),
("panel", "allow_public"),
] {
+41 -4
View File
@@ -80,6 +80,8 @@ struct Watch {
backup: String,
timeout: Duration,
deadline: Instant,
/// `updates.keep`: the other releases kept once the game is up.
keep: u32,
}
pub fn run(dir: &Path) -> Result<(), String> {
@@ -315,9 +317,24 @@ fn switch(
backup,
timeout,
deadline: Instant::now() + timeout,
keep: config.updates.keep,
})
}
/// The game is up on the release switched to: the update is done, and the old releases beyond the ones
/// kept are deleted.
fn came_up(shared: &Shared, w: &Watch) {
let mut message = format!("Now on {} (was {}): the game is up", w.to, w.from);
if let Some(install) = &shared.install {
let (removed, failed) = install.prune(w.keep);
let pruned = update::pruned(&removed, &failed);
if !pruned.is_empty() {
message = format!("{message}. {pruned}");
}
}
shared.finish_update(true, message);
}
/// Undoes a switch whose game never came up: `current` back on the release before, and the world as it
/// was just before the switch (the one the new game left is kept in backups/).
fn roll_back(shared: &Shared, dir: &Path, config: &Config, w: &Watch, reason: &str) {
@@ -576,10 +593,7 @@ fn supervise(
loop {
if let Some(w) = watch.as_ref() {
if up(shared) {
shared.finish_update(
true,
format!("Now on {} (was {}): the game is up", w.to, w.from),
);
came_up(shared, w);
*watch = None;
} else if Instant::now() > w.deadline && stopping.is_none() {
note(
@@ -748,6 +762,29 @@ mod tests {
std::fs::remove_dir_all(&install.root).unwrap();
}
#[cfg(unix)]
#[test]
fn a_game_that_comes_up_ends_the_update_and_prunes_old_releases() {
let (shared, dir) = installed("prune", &[("v1", 0), ("v2", 0), ("v3", 0)]);
let install = shared.install.clone().unwrap();
let mut config = Config::default();
config.updates.keep = 0;
let watch = switch(&shared, &dir, &config, "v3", Duration::from_secs(60)).unwrap();
came_up(&shared, &watch);
assert_eq!(install.current().as_deref(), Some("v3"));
// The release before stays for `rollback`; the other one goes.
assert_eq!(install.previous().as_deref(), Some("v1"));
assert!(!install.release("v2").exists());
let update = shared.update.lock().unwrap();
assert!(update.ok && update.busy.is_none() && update.finished == 1);
assert_eq!(
update.last.as_deref(),
Some("Now on v3 (was v1): the game is up. Deleted old releases: v2")
);
drop(update);
std::fs::remove_dir_all(&install.root).unwrap();
}
#[cfg(unix)]
#[test]
fn a_game_that_cant_load_is_never_switched_to() {
+125
View File
@@ -120,6 +120,73 @@ impl Install {
}
.map_err(|e| format!("{}: {e}", file.display()))
}
/// Deletes old releases after a successful update: it keeps the one `current` points at, the one
/// `previous` names (where `rollback` goes), the one this program runs from, and the `keep` newest of
/// the others. Returns the releases deleted, and what couldn't be (a program still running from it on
/// Windows), which stays for the next update to try again.
pub fn prune(&self, keep: u32) -> (Vec<String>, Vec<String>) {
let releases = self.root.join("releases");
let running = std::env::current_exe()
.ok()
.and_then(|e| std::fs::canonicalize(e).ok());
let Some(current) = self.current() else {
// `current` points nowhere: nothing is known to be safe to delete.
return (Vec::new(), Vec::new());
};
let previous = self.previous();
let mut others: Vec<String> = std::fs::read_dir(&releases)
.into_iter()
.flatten()
.flatten()
.filter(|e| e.file_type().is_ok_and(|t| t.is_dir()))
.map(|e| e.file_name().to_string_lossy().into_owned())
// `.<tag>.unpacking` is an update in progress.
.filter(|tag| {
!tag.starts_with('.') && *tag != current && previous.as_deref() != Some(tag)
})
.filter(|tag| {
let dir = std::fs::canonicalize(releases.join(tag)).ok();
!running
.as_ref()
.zip(dir)
.is_some_and(|(exe, dir)| exe.starts_with(dir))
})
.collect();
// Newest first; names that aren't release tags (archives installed by hand) by name.
others.sort_by(|a, b| {
if newer(a, b) {
std::cmp::Ordering::Less
} else if newer(b, a) {
std::cmp::Ordering::Greater
} else {
b.cmp(a)
}
});
let (mut removed, mut failed) = (Vec::new(), Vec::new());
for tag in others.into_iter().skip(keep as usize) {
match std::fs::remove_dir_all(releases.join(&tag)) {
Ok(()) => removed.push(tag),
Err(e) => failed.push(format!("{tag} ({e})")),
}
}
(removed, failed)
}
}
/// What [`Install::prune`] did, for the log: empty when it deleted nothing.
pub fn pruned(removed: &[String], failed: &[String]) -> String {
let mut parts = Vec::new();
if !removed.is_empty() {
parts.push(format!("Deleted old releases: {}", removed.join(", ")));
}
if !failed.is_empty() {
parts.push(format!(
"Couldn't delete old releases: {}",
failed.join(", ")
));
}
parts.join(". ")
}
/// Makes `link` point at the folder `target`, replacing what it pointed at.
@@ -582,6 +649,11 @@ fn switch_stopped(dir: &Path, install: &Install, tag: &str) -> Result<(), String
""
}
);
let (removed, failed) = install.prune(config.updates.keep);
let pruned = pruned(&removed, &failed);
if !pruned.is_empty() {
println!("{pruned}.");
}
Ok(())
}
@@ -701,6 +773,59 @@ mod tests {
std::fs::remove_dir_all(&install.root).unwrap();
}
#[cfg(unix)]
#[test]
fn pruning_keeps_current_previous_and_the_newest_others() {
let tags = [
"v2026.10.19",
"v2026.10.1",
"v2026.10.2",
"v2026.10.10",
"v2026.10.18.3",
"local",
];
let install = installed("prune", &tags);
// After a rollback `previous` can be any release, even the oldest.
std::fs::write(install.root.join("previous"), "v2026.10.1\n").unwrap();
std::fs::create_dir_all(install.root.join("releases/.v2026.10.20.unpacking")).unwrap();
std::fs::write(install.root.join("releases/notes.txt"), "").unwrap();
let left = || {
let mut names: Vec<String> = std::fs::read_dir(install.root.join("releases"))
.unwrap()
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
.collect();
names.sort();
names
};
let (removed, failed) = install.prune(1);
assert_eq!(removed, ["v2026.10.10", "v2026.10.2", "local"]);
assert!(failed.is_empty());
assert_eq!(
left(),
[
".v2026.10.20.unpacking",
"notes.txt",
"v2026.10.1",
"v2026.10.18.3",
"v2026.10.19"
]
);
assert_eq!(
pruned(&removed, &failed),
"Deleted old releases: v2026.10.10, v2026.10.2, local"
);
// Nothing more to delete at the same count; none at all kept besides current and previous.
assert_eq!(install.prune(1), (Vec::new(), Vec::new()));
assert_eq!(install.prune(0).0, ["v2026.10.18.3"]);
assert_eq!(install.current().as_deref(), Some("v2026.10.19"));
assert_eq!(install.previous().as_deref(), Some("v2026.10.1"));
// Without a `current` nothing is deleted.
std::fs::remove_file(install.root.join("current")).unwrap();
assert_eq!(install.prune(0), (Vec::new(), Vec::new()));
assert!(install.release("v2026.10.19").is_dir());
std::fs::remove_dir_all(&install.root).unwrap();
}
#[cfg(unix)]
#[test]
fn an_archive_unpacks_beside_the_others_with_the_game_files_linked() {
+8 -2
View File
@@ -289,6 +289,9 @@ restart_on_crash = true
[backups]
every = "6h" # automatic backups while the server runs: 30m, 6h, 1d...; "0" turns them off
keep = 12 # automatic backups kept (backups made by hand are never deleted)
[updates]
keep = 1 # other releases kept by updates, besides the one in use and the one rollback goes to
```
This is the file `init` writes today (PR 4 adds `[panel] bind = "127.0.0.1:8130"` and `allow_public =
@@ -419,8 +422,11 @@ runtime or new dependency. Server-sent events can replace the polling later if a
and kept as it is). The supervisor keeps running as it was: the new `horizon-server` itself takes over
the next time the server starts (`stop` and `start`, or the service's restart). A stopped server is
switched and its new game checked to load; it starts on the new release next time. One install serves
every server folder on the machine, so each one runs the new release from its next start. Old releases
stay in `<dir>/releases/` until they are deleted by hand (keep `current` and `previous`).
every server folder on the machine, so each one runs the new release from its next start. Once the new
game is up (or a stopped server is switched), old releases are deleted from `<dir>/releases/`: the one in
use, the one `previous` names (where `rollback` goes), the one the running `horizon-server` itself came
from, and the `updates.keep` newest of the others (1 by default) stay. A release that can't be deleted (a
program still running from it on Windows) is left for the next update.
## 8. Control API and MCP