diff --git a/Cargo.lock b/Cargo.lock
index c9eb13c20..8efa57882 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1563,8 +1563,10 @@ dependencies = [
"libc",
"serde",
"serde_json",
+ "sha2",
"toml",
"toml_edit",
+ "ureq",
"windows-sys 0.61.2",
"zip",
]
diff --git a/README.md b/README.md
index 0efae1088..a1e6a38f6 100644
--- a/README.md
+++ b/README.md
@@ -43,6 +43,8 @@ Horizon is downloaded only through **the Horizon launcher**: get it from [playho
**You need** Minecraft: Java Edition installed with the official launcher, with 26.3 played once; Python 3.10 or newer and Java 25 (a JDK, e.g. Eclipse Temurin 25) for the one-time resource preparation; and a GPU with Vulkan on Linux and Windows, or any Apple Silicon Mac.
+**Host a server:** `horizon-server` is one native program with no Java, one settings file, a command line, a web panel, backups and updates that roll back by themselves. It installs with one command (`curl -fsSL https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.sh | sh` on Linux and macOS, `irm https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.ps1 | iex` on Windows). The guide is at [playhorizon.dev/server](https://playhorizon.dev/server) ([docs/hosting.md](docs/hosting.md)).
+
### The launcher
diff --git a/crates/horizon-server/Cargo.toml b/crates/horizon-server/Cargo.toml
index 2432cd481..ec3b9bbc4 100644
--- a/crates/horizon-server/Cargo.toml
+++ b/crates/horizon-server/Cargo.toml
@@ -17,6 +17,10 @@ serde_json = "1"
toml = "1.1"
# Edits server.toml in place, keeping the owner's comments and order.
toml_edit = "0.25"
+# Updates: the release from GitHub, checked against the SHA-256 GitHub publishes (both already in the
+# lockfile, ureq through horizon-server-files).
+sha2 = "0.10"
+ureq = { version = "2", default-features = false, features = ["tls"] }
# Backups: one zip anyone can open, Windows included.
zip = { version = "2", default-features = false, features = ["deflate"] }
diff --git a/crates/horizon-server/panel/index.html b/crates/horizon-server/panel/index.html
index 051b7283e..5d77cb956 100644
--- a/crates/horizon-server/panel/index.html
+++ b/crates/horizon-server/panel/index.html
@@ -21,6 +21,11 @@
+
+
+
+
+
TPS –
diff --git a/crates/horizon-server/panel/panel.css b/crates/horizon-server/panel/panel.css
index 5d635d509..7946b5f8f 100644
--- a/crates/horizon-server/panel/panel.css
+++ b/crates/horizon-server/panel/panel.css
@@ -126,3 +126,17 @@ code { font-family: var(--mono); font-size: 13px; }
border-radius: var(--radius);
max-width: 90vw;
}
+
+.notice {
+ display: flex;
+ align-items: center;
+ gap: 12px;
+ margin: 16px max(24px, calc((100% - 1152px) / 2)) 0;
+ padding: 10px 14px;
+ border-radius: var(--radius);
+ background: var(--plate-soft);
+ border: 1px solid var(--rule);
+}
+.notice.failed {
+ color: var(--danger);
+}
diff --git a/crates/horizon-server/panel/panel.js b/crates/horizon-server/panel/panel.js
index f74c3e1c2..5405907ca 100644
--- a/crates/horizon-server/panel/panel.js
+++ b/crates/horizon-server/panel/panel.js
@@ -87,6 +87,7 @@ function renderStatus(status) {
const parts = [`up ${duration(status.engine_uptime_seconds)}`, `horizon-server ${status.version}`];
if (status.crashes) parts.push(`${status.crashes} crash${status.crashes > 1 ? "es" : ""}`);
$("summary").textContent = parts.join(" · ");
+ renderUpdate(status.update);
const players = (game && game.players) || [];
$("player-count").textContent = game ? `${players.length} of ${game.max_players}` : "";
@@ -108,6 +109,20 @@ function renderStatus(status) {
);
}
+// Updates (horizon-server update): a newer release, one under way, or how the last one ended.
+function renderUpdate(update) {
+ const box = $("update");
+ const offer = update && !update.busy && update.available;
+ let text = "";
+ if (update && update.busy) text = `Updating: ${update.busy}…`;
+ else if (offer) text = `Horizon ${update.available} is out (this server runs ${update.installed}).`;
+ else if (update && update.last && !update.ok) text = update.last;
+ box.hidden = !text;
+ box.className = update && update.last && !update.ok && !update.busy && !offer ? "notice failed" : "notice";
+ $("update-text").textContent = text;
+ $("update-button").hidden = !offer;
+}
+
function renderBackups(list) {
const key = JSON.stringify(list);
if (key === backupsShown) return;
@@ -255,6 +270,12 @@ function setUp() {
toast(e.message);
}
});
+ $("update-button").addEventListener("click", async () => {
+ const update = lastStatus && lastStatus.update;
+ if (!update || !update.available) return;
+ if (!confirm(`Update to ${update.available}? The world is backed up, then the game restarts on it (players are disconnected for a moment). If it doesn't come up, the server goes back to ${update.installed} by itself.`)) return;
+ await api("POST", "/api/v1/update", {}).then(() => toast("Updating…"), (e) => toast(e.message));
+ });
$("restart").addEventListener("click", async () => {
if (!confirm("Restart the game? Players are disconnected for a moment.")) return;
await api("POST", "/api/v1/restart").then(() => toast("Restarting…"), (e) => toast(e.message));
diff --git a/crates/horizon-server/src/api.rs b/crates/horizon-server/src/api.rs
index 85bc0e2b5..03f7ae079 100644
--- a/crates/horizon-server/src/api.rs
+++ b/crates/horizon-server/src/api.rs
@@ -9,6 +9,9 @@
//! - `GET /api/v1/metrics?since=T`: the samples taken after T (seconds since 1970), up to 10 minutes.
//! - `GET /api/v1/backups`; `POST /api/v1/backups {"name": "..."}` backs up now; `POST /api/v1/restore
//! {"name": "..."}` restarts the game with that backup's world.
+//! - `POST /api/v1/update {"tag": "...", "timeout": seconds}`: restarts the game on that installed release,
+//! and goes back to the one in use if it isn't up within the timeout; without a tag, downloads the newest
+//! release first (update.rs). `GET /api/v1/status` says how it went (`update`).
//! - `POST /api/v1/panel/login`: a one-time login link for the web panel (with the token only).
use crate::{
@@ -17,6 +20,7 @@ use crate::{
engine,
http::{self, Request, Response},
panel,
+ update::{self, Install},
};
use serde_json::{Value, json};
use std::{
@@ -91,6 +95,25 @@ impl Log {
}
}
+/// How long a game started on another release has to come up before the switch is undone.
+pub const UPDATE_TIMEOUT: Duration = Duration::from_secs(300);
+
+/// Updates and rollbacks (update.rs): what the check found, what is under way, how the last one ended.
+#[derive(Default)]
+pub struct Updates {
+ /// A release newer than the one in use, from the last check.
+ pub available: Option,
+ /// What is happening now, or nothing.
+ pub busy: Option,
+ /// How the last update or rollback ended, and whether it went well.
+ pub last: Option,
+ pub ok: bool,
+ /// Counts the updates that ended, so a client can wait for the one it asked for.
+ pub finished: u64,
+ /// The release to restart the game on, and how long it has to come up.
+ pub pending: Option<(String, Duration)>,
+}
+
/// What the supervisor and the API threads share.
pub struct Shared {
pub dir: PathBuf,
@@ -107,10 +130,17 @@ pub struct Shared {
pub metrics: Mutex>,
/// The panel's address, login links and sessions.
pub panel: Mutex,
+ /// The installer's folder this program runs from (none for a build of your own: no updates).
+ pub install: Option,
+ pub update: Mutex,
}
impl Shared {
pub fn new(dir: &Path) -> Arc {
+ Self::with_install(dir, Install::find())
+ }
+
+ pub fn with_install(dir: &Path, install: Option) -> Arc {
Arc::new(Self {
dir: dir.to_owned(),
state: Mutex::new(State {
@@ -129,8 +159,85 @@ impl Shared {
started: Instant::now(),
metrics: Mutex::new(VecDeque::new()),
panel: Mutex::new(panel::Auth::default()),
+ install,
+ update: Mutex::new(Updates::default()),
})
}
+
+ /// Ends an update or rollback: its outcome, in the log and in `status`.
+ pub fn finish_update(&self, ok: bool, message: String) {
+ {
+ let mut update = self.update.lock().unwrap();
+ update.busy = None;
+ update.ok = ok;
+ update.finished += 1;
+ if ok {
+ update.available = None;
+ }
+ update.last = Some(message.clone());
+ }
+ crate::supervisor::note(self, if ok { "INFO" } else { "ERROR" }, &message);
+ }
+
+ /// Asks for a restart on the installed release `tag` (the supervisor switches when the game stops).
+ fn switch_to(&self, tag: String, timeout: Duration) {
+ self.update.lock().unwrap().busy = Some(format!("Restarting the game on {tag}"));
+ self.update.lock().unwrap().pending = Some((tag, timeout));
+ self.restart.store(true, Ordering::SeqCst);
+ }
+
+ /// `POST /api/v1/update`: switches to `tag`, or downloads the newest release first.
+ fn start_update(
+ self: &Arc,
+ tag: Option,
+ timeout: Duration,
+ ) -> Result<(), String> {
+ let install = self.install.clone().ok_or(
+ "this horizon-server wasn't installed by install.sh or install.ps1: no updates",
+ )?;
+ {
+ let mut update = self.update.lock().unwrap();
+ if let Some(busy) = &update.busy {
+ return Err(format!("an update is under way: {busy}"));
+ }
+ update.busy = Some("Looking for the newest release".into());
+ }
+ if let Some(tag) = tag {
+ if !install.release(&tag).is_dir() || tag.contains(['/', '\\']) {
+ self.update.lock().unwrap().busy = None;
+ return Err(format!(
+ "{tag} isn't installed (`horizon-server update` downloads it)"
+ ));
+ }
+ self.switch_to(tag, timeout);
+ return Ok(());
+ }
+ let shared = self.clone();
+ std::thread::Builder::new()
+ .name("update".into())
+ .spawn(move || {
+ let fetched = (|| {
+ let current = install.current().unwrap_or_default();
+ let release = update::release(None)?;
+ if !update::newer(&release.tag, ¤t) {
+ return Ok(None);
+ }
+ shared.update.lock().unwrap().busy =
+ Some(format!("Downloading {} ({})", release.tag, release.asset));
+ let archive = update::download(&install, &release)?;
+ let unpacked = update::unpack(&install, &archive, &release.tag);
+ let _ = std::fs::remove_file(&archive);
+ unpacked.map(|_| Some(release.tag))
+ })();
+ match fetched {
+ Ok(Some(tag)) => shared.switch_to(tag, timeout),
+ Ok(None) => shared.finish_update(true, "Already on the newest release".into()),
+ Err(e) => shared.finish_update(false, format!("Not updated: {e}")),
+ }
+ })
+ .map_err(|e| e.to_string())?;
+ Ok(())
+ }
/// Keeps one sample for the graphs.
pub fn record(&self, sample: Value) {
let mut metrics = self.metrics.lock().unwrap();
@@ -187,6 +294,23 @@ fn status(shared: &Shared) -> Value {
"crashes": state.crashes,
"last_crash": state.last_crash,
"sample": state.sample,
+ "update": updates(shared),
+ })
+}
+
+fn updates(shared: &Shared) -> Value {
+ let Some(install) = &shared.install else {
+ return Value::Null;
+ };
+ let update = shared.update.lock().unwrap();
+ json!({
+ "installed": install.current(),
+ "previous": install.previous(),
+ "available": update.available,
+ "busy": update.busy,
+ "last": update.last,
+ "ok": update.ok,
+ "finished": update.finished,
})
}
@@ -273,7 +397,7 @@ pub fn start(shared: Arc) -> Result {
}
/// The API's routes, for the CLI (with the token) and the panel (with a session).
-pub fn route(shared: &Shared, request: &Request) -> Response {
+pub fn route(shared: &Arc, request: &Request) -> Response {
match (request.method.as_str(), request.path.as_str()) {
("GET", "/api/v1/status") => Response::json(200, &status(shared)),
("GET", "/api/v1/metrics") => {
@@ -354,10 +478,21 @@ pub fn route(shared: &Shared, request: &Request) -> Response {
Err(e) => Response::error(404, &e),
}
}
+ ("POST", "/api/v1/update") => {
+ let body: Value = serde_json::from_slice(&request.body).unwrap_or(Value::Null);
+ let timeout = body["timeout"]
+ .as_u64()
+ .map_or(UPDATE_TIMEOUT, Duration::from_secs);
+ match shared.start_update(body["tag"].as_str().map(str::to_owned), timeout) {
+ Ok(()) => Response::json(200, &json!({ "ok": true })),
+ Err(e) => Response::error(409, &e),
+ }
+ }
(
_,
"/api/v1/status" | "/api/v1/logs" | "/api/v1/command" | "/api/v1/stop"
- | "/api/v1/restart" | "/api/v1/backups" | "/api/v1/restore" | "/api/v1/metrics",
+ | "/api/v1/restart" | "/api/v1/backups" | "/api/v1/restore" | "/api/v1/metrics"
+ | "/api/v1/update",
) => Response::error(405, "wrong method"),
_ => Response::error(404, "no such endpoint"),
}
diff --git a/crates/horizon-server/src/cli.rs b/crates/horizon-server/src/cli.rs
index 11d9fb6b6..69a192105 100644
--- a/crates/horizon-server/src/cli.rs
+++ b/crates/horizon-server/src/cli.rs
@@ -251,6 +251,15 @@ fn describe(status: &Value) -> String {
if !usage.is_empty() {
out += &format!("{}\n", usage.join(", "));
}
+ let update = &status["update"];
+ if let Some(busy) = update["busy"].as_str() {
+ out += &format!("Updating: {busy}\n");
+ } else if let Some(tag) = update["available"].as_str() {
+ out += &format!(
+ "Horizon {tag} is out (this server runs {}): `horizon-server update` installs it\n",
+ update["installed"].as_str().unwrap_or("?")
+ );
+ }
out
}
@@ -429,5 +438,18 @@ mod tests {
describe(&json!({ "phase": "starting", "uptime_seconds": 4 })),
"Starting, up 4 s\n"
);
+ assert_eq!(
+ describe(&json!({
+ "phase": "running",
+ "update": { "installed": "v2026.10.18", "available": "v2026.10.19" }
+ })),
+ "Running\nHorizon v2026.10.19 is out (this server runs v2026.10.18): `horizon-server update` installs it\n"
+ );
+ assert_eq!(
+ describe(
+ &json!({ "phase": "restarting", "update": { "busy": "Starting the game on v2" } })
+ ),
+ "Restarting\nUpdating: Starting the game on v2\n"
+ );
}
}
diff --git a/crates/horizon-server/src/engine.rs b/crates/horizon-server/src/engine.rs
index aa83693cb..051ae77f0 100644
--- a/crates/horizon-server/src/engine.rs
+++ b/crates/horizon-server/src/engine.rs
@@ -20,8 +20,18 @@ fn exe(name: &str) -> String {
format!("{name}{}", std::env::consts::EXE_SUFFIX)
}
-/// The game program: `engine.path`, else one installed beside `horizon-server`, else one on the PATH.
+/// The game program: `engine.path`, else one installed beside `horizon-server` (in the release in use when
+/// it was installed by the installers), else one on the PATH.
pub fn program(config: &Config, dir: &Path) -> Result {
+ program_in(config, dir, crate::update::Install::find().as_ref())
+}
+
+/// [`program`], for the install `install`.
+pub fn program_in(
+ config: &Config,
+ dir: &Path,
+ install: Option<&crate::update::Install>,
+) -> Result {
if !config.engine.path.is_empty() {
let path = dir.join(&config.engine.path);
return path
@@ -29,9 +39,12 @@ pub fn program(config: &Config, dir: &Path) -> Result {
.then_some(path)
.ok_or_else(|| format!("engine.path {} is not a file", config.engine.path));
}
- let beside = std::env::current_exe()
- .ok()
- .and_then(|e| e.parent().map(Path::to_path_buf));
+ // Installed: the release `current` points at now, so a switch applies at the next start.
+ let beside = install.map(crate::update::Install::programs).or_else(|| {
+ std::env::current_exe()
+ .ok()
+ .and_then(|e| e.parent().map(Path::to_path_buf))
+ });
let path_dirs = std::env::var_os("PATH")
.map(|p| std::env::split_paths(&p).collect::>())
.unwrap_or_default();
@@ -141,14 +154,18 @@ pub fn resources(config: &Config, dir: &Path, program: Option<&Path>) -> Result<
/// Starts the engine in the server folder, with its console and log piped to the supervisor and its status
/// on the control endpoint.
pub fn spawn(program: &Path, resources: &Path, dir: &Path) -> std::io::Result {
+ // The game runs in the server folder, so a folder given relative to ours (`--dir srv`) would put its
+ // control endpoint in `srv/srv/.horizon`, which doesn't exist.
+ let dir = std::path::absolute(dir)?;
+ let resources = std::path::absolute(resources)?;
let mut command = Command::new(program);
command
.arg("--resources")
.arg(resources)
.arg("--control-socket")
- .arg(control_path(dir))
+ .arg(control_path(&dir))
.args(["--dedicated", "--nogui"])
- .current_dir(dir)
+ .current_dir(&dir)
.env("HORIZON_NO_AUDIO", "1")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
@@ -276,6 +293,38 @@ mod tests {
);
}
+ #[cfg(unix)]
+ #[test]
+ fn a_relative_folder_reaches_the_game_whole() {
+ use std::os::unix::fs::PermissionsExt;
+ // Relative to this process's folder (the crate's, under cargo test), as `--dir srv` gives it.
+ let root = PathBuf::from(format!("reldir-test-{}", std::process::id()));
+ let server = root.join("srv");
+ std::fs::create_dir_all(server.join(".horizon")).unwrap();
+ std::fs::create_dir_all(root.join("res")).unwrap();
+ // A game that checks, from its own folder, the folders it was given.
+ let game = root.join("game");
+ std::fs::write(
+ &game,
+ "#!/bin/sh\nwhile [ $# -gt 0 ]; do case \"$1\" in --resources) r=$2; shift;; \
+ --control-socket) c=$2; shift;; esac; shift; done\n\
+ [ -d \"$r\" ] && [ -d \"$(dirname \"$c\")\" ] && [ -f server.toml ]\n",
+ )
+ .unwrap();
+ std::fs::set_permissions(&game, std::fs::Permissions::from_mode(0o755)).unwrap();
+ std::fs::write(server.join("server.toml"), "").unwrap();
+ let status = spawn(
+ &std::path::absolute(&game).unwrap(),
+ &root.join("res"),
+ &server,
+ )
+ .unwrap()
+ .wait();
+ std::fs::remove_dir_all(&root).unwrap();
+ let status = status.unwrap();
+ assert!(status.success(), "{status}");
+ }
+
#[cfg(unix)]
#[test]
fn the_check_runs_the_program() {
diff --git a/crates/horizon-server/src/main.rs b/crates/horizon-server/src/main.rs
index c37da27a5..8fda74fba 100644
--- a/crates/horizon-server/src/main.rs
+++ b/crates/horizon-server/src/main.rs
@@ -13,6 +13,7 @@ mod panel;
mod service;
mod signals;
mod supervisor;
+mod update;
use std::{path::PathBuf, process::ExitCode};
@@ -45,6 +46,11 @@ Usage: horizon-server [--dir DIR]
with sudo), a launchd agent on macOS, a task at logon on Windows
panel A one-time login link for the web panel ([panel] in server.toml; 127.0.0.1:8130 by
default)
+ update [--check] [--version TAG] [--timeout SECONDS]
+ Install the newest release (checked against GitHub's SHA-256), back up the world and
+ restart on it; goes back by itself if the game isn't up within the timeout (300 s).
+ --archive FILE --version NAME installs an archive you have
+ rollback Back to the release used before the update
import FILE Write server.toml from a vanilla server.properties
op NAME, deop NAME
whitelist on|off|list, whitelist add|remove NAME
@@ -92,6 +98,8 @@ fn main() -> ExitCode {
None => Err("restore needs a backup (`horizon-server backups` lists them)".into()),
},
Some("import") => import(&dir, &args[1..]),
+ Some("update") => update::command(&dir, &args[1..]),
+ Some("rollback") => update::rollback(&dir, &args[1..]),
Some(command @ ("op" | "deop" | "whitelist")) => lists::run(&dir, command, &args[1..]),
Some(other) => Err(format!(
"unknown command {other}; `horizon-server help` lists them"
diff --git a/crates/horizon-server/src/panel.rs b/crates/horizon-server/src/panel.rs
index 44ca24e40..76bacc047 100644
--- a/crates/horizon-server/src/panel.rs
+++ b/crates/horizon-server/src/panel.rs
@@ -145,7 +145,7 @@ fn secure(response: Response) -> Response {
.with("Referrer-Policy", "no-referrer")
}
-fn handle(shared: &Shared, address: SocketAddr, request: &Request) -> Response {
+fn handle(shared: &Arc, address: SocketAddr, request: &Request) -> Response {
if !request
.header("host")
.is_some_and(|h| host_allowed(h, address))
diff --git a/crates/horizon-server/src/supervisor.rs b/crates/horizon-server/src/supervisor.rs
index b26ac67cb..1cd36e83e 100644
--- a/crates/horizon-server/src/supervisor.rs
+++ b/crates/horizon-server/src/supervisor.rs
@@ -1,12 +1,13 @@
//! `horizon-server run`: writes `server.properties` from `server.toml`, runs the engine with the console on
//! stdin, serves the control API, stops the game cleanly on Ctrl-C, SIGTERM or `stop`, and restarts it when
-//! it crashes (docs/server.md, sections 2 and 7).
+//! it crashes (docs/server.md, sections 2 and 7). An update restarts the game on another release and goes
+//! back by itself when the new game doesn't come up.
use crate::{
api::{self, Phase, Shared},
backup, config,
config::Config,
- engine, signals,
+ engine, signals, update,
};
use serde_json::json;
use std::{
@@ -30,7 +31,7 @@ const STOP_TIMEOUT: Duration = Duration::from_secs(120);
/// A line from the supervisor, in the game's log format (`[HH:mm:ss] [thread/LEVEL]: message`, local time
/// as the game's own lines), on stdout and in the log the API serves.
-fn note(shared: &Shared, level: &str, message: &str) {
+pub fn note(shared: &Shared, level: &str, message: &str) {
let line = format!(
"[{}] [Supervisor/{level}]: {message}",
chrono::Local::now().format("%H:%M:%S")
@@ -65,6 +66,20 @@ enum Exit {
/// Asked to restart (`horizon-server restart`).
Restart,
Crashed(ExitStatus),
+ /// Started on another release, the game wasn't up in time and was stopped.
+ NotUp,
+}
+
+/// A game started on another release that has yet to come up: what to go back to if it doesn't.
+struct Watch {
+ from: String,
+ to: String,
+ /// The release `previous` named before the switch.
+ previous: Option,
+ /// The backup of the world made just before the switch.
+ backup: String,
+ timeout: Duration,
+ deadline: Instant,
}
pub fn run(dir: &Path) -> Result<(), String> {
@@ -97,11 +112,20 @@ pub fn run(dir: &Path) -> Result<(), String> {
}
sample(shared.clone());
schedule_backups(shared.clone());
+ check_updates(shared.clone());
let console = console_lines();
let mut crashes: Vec = Vec::new();
+ let mut watch: Option = None;
+ let (mut program, mut resources) = (program, resources);
loop {
- // Read again before every start: edits to server.toml apply on the next restart.
+ // Read again before every start: edits to server.toml apply on the next restart, and so does a
+ // switch to another release.
let config = Config::load(dir)?;
+ if let Ok(found) = engine::program_in(&config, dir, shared.install.as_ref())
+ && let Ok(found_resources) = engine::resources(&config, dir, Some(&found))
+ {
+ (program, resources) = (found, found_resources);
+ }
let written = config
.write_properties(dir)
.map_err(|e| format!("server.properties: {e}"))?;
@@ -122,7 +146,11 @@ pub fn run(dir: &Path) -> Result<(), String> {
state.engine_pid = Some(child.id());
state.engine_started = Some(Instant::now());
}
- let exit = supervise(&shared, &mut child, &console);
+ // The new release's time to come up starts with its game.
+ if let Some(w) = &mut watch {
+ w.deadline = Instant::now() + w.timeout;
+ }
+ let exit = supervise(&shared, &mut child, &console, &mut watch);
{
let mut state = shared.state.lock().unwrap();
state.engine_pid = None;
@@ -144,8 +172,25 @@ pub fn run(dir: &Path) -> Result<(), String> {
&format!("Could not update server.toml: {e}"),
),
}
+ // Started on another release, the game never came up: back to the release before.
+ if let Some(w) = watch.take() {
+ let reason = match &exit {
+ Exit::Crashed(status) => format!("the game stopped ({status})"),
+ Exit::NotUp => "the game wasn't up in time".into(),
+ Exit::Stopped => "the game stopped before it was up".into(),
+ Exit::Restart => "the game was restarted before it was up".into(),
+ };
+ roll_back(&shared, dir, &config, &w, &reason);
+ if matches!(exit, Exit::Stopped)
+ && shared.state.lock().unwrap().phase == Phase::Stopping
+ {
+ return Ok(());
+ }
+ continue;
+ }
let status = match exit {
Exit::Stopped => return Ok(()),
+ Exit::NotUp => continue,
Exit::Restart => {
let restore = shared.restore.lock().unwrap().take();
if let Some(name) = restore {
@@ -162,6 +207,10 @@ pub fn run(dir: &Path) -> Result<(), String> {
Err(e) => note(&shared, "ERROR", &format!("Could not restore {name}: {e}")),
}
}
+ let pending = shared.update.lock().unwrap().pending.take();
+ if let Some((to, timeout)) = pending {
+ watch = switch(&shared, dir, &config, &to, timeout);
+ }
continue;
}
Exit::Crashed(status) => status,
@@ -206,6 +255,154 @@ pub fn run(dir: &Path) -> Result<(), String> {
}
}
+/// With the game stopped: backs the world up, points `current` at the release `to` and checks that its game
+/// loads on this system. What to watch while it starts; `None` when it stayed on the release it was on.
+fn switch(
+ shared: &Shared,
+ dir: &Path,
+ config: &Config,
+ to: &str,
+ timeout: Duration,
+) -> Option {
+ let Some(install) = shared.install.as_ref() else {
+ shared.finish_update(
+ false,
+ "Not updated: this horizon-server wasn't installed".into(),
+ );
+ return None;
+ };
+ let Some(from) = install.current() else {
+ shared.finish_update(false, "Not updated: `current` points at no release".into());
+ return None;
+ };
+ let made = backup::file_name(std::time::SystemTime::now(), Some(&update::backup_name(to)))
+ .and_then(|file| backup::create(dir, config, &file).map(|_| file));
+ let backup = match made {
+ Ok(file) => file,
+ Err(e) => {
+ shared.finish_update(false, format!("Stayed on {from}: the backup failed: {e}"));
+ return None;
+ }
+ };
+ note(
+ shared,
+ "INFO",
+ &format!(
+ "Backed up the world to {}/{backup}; switching from {from} to {to}",
+ backup::DIR
+ ),
+ );
+ let previous = install.previous();
+ if let Err(e) = install.switch(to) {
+ shared.finish_update(false, format!("Stayed on {from}: {e}"));
+ return None;
+ }
+ if let Err(e) =
+ engine::program_in(config, dir, Some(install)).and_then(|p| engine::check_starts(&p))
+ {
+ let back = install
+ .switch_back(&from, previous.as_deref())
+ .err()
+ .map_or(String::new(), |e| format!(" ({e})"));
+ shared.finish_update(false, format!("Stayed on {from}{back}: {e}"));
+ return None;
+ }
+ shared.update.lock().unwrap().busy = Some(format!("Starting the game on {to}"));
+ Some(Watch {
+ from,
+ to: to.to_owned(),
+ previous,
+ backup,
+ timeout,
+ deadline: Instant::now() + timeout,
+ })
+}
+
+/// 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) {
+ note(
+ shared,
+ "ERROR",
+ &format!(
+ "The game on {} didn't come up ({reason}): going back to {}",
+ w.to, w.from
+ ),
+ );
+ let Some(install) = shared.install.as_ref() else {
+ return;
+ };
+ if let Err(e) = install.switch_back(&w.from, w.previous.as_deref()) {
+ shared.finish_update(
+ false,
+ format!(
+ "{} didn't come up ({reason}), and going back to {} failed: {e}",
+ w.to, w.from
+ ),
+ );
+ return;
+ }
+ let world = match backup::restore(dir, config, &w.backup) {
+ Ok(Some(old)) => format!(
+ "; the world is back as it was before the switch (the one {} left is in {})",
+ w.to,
+ old.display()
+ ),
+ Ok(None) => "; the world is back as it was before the switch".into(),
+ Err(e) => format!("; the world couldn't be put back from {}: {e}", w.backup),
+ };
+ shared.finish_update(
+ false,
+ format!(
+ "Rolled back to {}: {} didn't come up ({reason}){world}",
+ w.from, w.to
+ ),
+ );
+}
+
+/// The game is up: it logged `Done` and answered a status request (the sampler's, for this process).
+fn up(shared: &Shared) -> bool {
+ let state = shared.state.lock().unwrap();
+ state.phase == Phase::Running
+ && state.engine_pid.is_some()
+ && state.sample["pid"].as_u64() == state.engine_pid.map(u64::from)
+ && !state.sample["game"].is_null()
+}
+
+/// Looks for a newer release a minute after the start and then every 6 hours (installed servers only;
+/// `HORIZON_NO_UPDATE_CHECK=1` turns it off). It only tells: updating stays the owner's choice.
+fn check_updates(shared: Arc) {
+ let Some(install) = shared.install.clone() else {
+ return;
+ };
+ if std::env::var_os("HORIZON_NO_UPDATE_CHECK").is_some_and(|v| !v.is_empty() && v != "0") {
+ return;
+ }
+ let _ = thread::Builder::new()
+ .name("update-check".into())
+ .spawn(move || {
+ thread::sleep(Duration::from_secs(60));
+ loop {
+ if let (Ok(release), Some(current)) = (update::release(None), install.current()) {
+ let newer = update::newer(&release.tag, ¤t).then_some(release.tag);
+ let told = shared.update.lock().unwrap().available.clone();
+ if let Some(tag) = newer.as_ref().filter(|t| told.as_ref() != Some(*t)) {
+ note(
+ &shared,
+ "INFO",
+ &format!(
+ "Horizon {tag} is out (this server runs {current}): `horizon-server update` \
+ installs it, and goes back by itself if it doesn't start"
+ ),
+ );
+ }
+ shared.update.lock().unwrap().available = newer;
+ }
+ thread::sleep(Duration::from_secs(6 * 3600));
+ }
+ });
+}
+
/// The wait before the restart that follows the `crashes`th recent crash.
fn backoff(crashes: usize) -> Duration {
let doublings = u32::try_from(crashes.saturating_sub(1))
@@ -309,6 +506,7 @@ fn sample(shared: Arc) {
"players": game.as_ref().and_then(|g| g["players"].as_array().map(Vec::len)),
}));
shared.state.lock().unwrap().sample = json!({
+ "pid": pid,
"rss_bytes": rss,
"cpu_percent": cpu_percent,
"game": game,
@@ -358,7 +556,12 @@ fn schedule_backups(shared: Arc) {
}
/// Runs one engine process to its end: console lines in, the log out, and a clean `stop` when asked.
-fn supervise(shared: &Arc, child: &mut Child, console: &mpsc::Receiver) -> Exit {
+fn supervise(
+ shared: &Arc,
+ child: &mut Child,
+ console: &mpsc::Receiver,
+ watch: &mut Option,
+) -> Exit {
if let Some(out) = child.stdout.take() {
pump(shared, out, false);
}
@@ -368,8 +571,27 @@ fn supervise(shared: &Arc, child: &mut Child, console: &mpsc::Receiver = None;
let mut restart = false;
+ let mut not_up = false;
let mut handled = signals::requests();
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),
+ );
+ *watch = None;
+ } else if Instant::now() > w.deadline && stopping.is_none() {
+ note(
+ shared,
+ "WARN",
+ &format!("The game on {} isn't up in time: stopping it", w.to),
+ );
+ not_up = true;
+ stopping = Some(Instant::now());
+ let _ = child.kill();
+ }
+ }
while let Ok(line) = console.try_recv() {
let command = line.trim().trim_start_matches('/');
if command == "stop" && stopping.is_none() {
@@ -419,6 +641,7 @@ fn supervise(shared: &Arc, child: &mut Child, console: &mpsc::Receiver return Exit::NotUp,
Ok(Some(_)) if restart => return Exit::Restart,
Ok(Some(status)) if stopping.is_some() || status.success() => return Exit::Stopped,
Ok(Some(status)) => return Exit::Crashed(status),
@@ -447,6 +670,142 @@ mod tests {
assert_eq!(waits, [5, 10, 20, 40, 80, 160, 300, 300]);
}
+ /// An install with releases whose game exits with `code` on `--help`, and a server folder with a world.
+ #[cfg(unix)]
+ fn installed(name: &str, releases: &[(&str, i32)]) -> (Arc, std::path::PathBuf) {
+ use std::os::unix::fs::PermissionsExt;
+ let root =
+ std::env::temp_dir().join(format!("horizon-server-sw-{name}-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&root);
+ let root = {
+ std::fs::create_dir_all(&root).unwrap();
+ std::fs::canonicalize(&root).unwrap()
+ };
+ for (tag, code) in releases {
+ let release = root.join("releases").join(tag);
+ std::fs::create_dir_all(&release).unwrap();
+ std::fs::write(release.join("horizon-server"), "").unwrap();
+ let engine = release.join("horizon-engine");
+ std::fs::write(&engine, format!("#!/bin/sh\nexit {code}\n")).unwrap();
+ std::fs::set_permissions(&engine, std::fs::Permissions::from_mode(0o755)).unwrap();
+ }
+ std::os::unix::fs::symlink(
+ root.join("releases").join(releases[0].0),
+ root.join("current"),
+ )
+ .unwrap();
+ let dir = root.join("server");
+ std::fs::create_dir_all(dir.join("world")).unwrap();
+ std::fs::write(dir.join("world/level.dat"), "before").unwrap();
+ let install = update::Install { root };
+ (Shared::with_install(&dir, Some(install)), dir)
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn a_switch_backs_up_then_rolls_back_world_and_release() {
+ let (shared, dir) = installed("rollback", &[("v1", 0), ("v2", 0)]);
+ let install = shared.install.clone().unwrap();
+ let config = Config::default();
+ let watch = switch(&shared, &dir, &config, "v2", Duration::from_secs(60)).unwrap();
+ assert_eq!((watch.from.as_str(), watch.to.as_str()), ("v1", "v2"));
+ assert!(watch.backup.ends_with("-before-v2.zip"), "{}", watch.backup);
+ assert!(dir.join(backup::DIR).join(&watch.backup).is_file());
+ assert_eq!(install.current().as_deref(), Some("v2"));
+ assert_eq!(
+ shared.update.lock().unwrap().busy.as_deref(),
+ Some("Starting the game on v2")
+ );
+ // The new game wrote to the world, then never came up.
+ std::fs::write(dir.join("world/level.dat"), "after").unwrap();
+ roll_back(
+ &shared,
+ &dir,
+ &config,
+ &watch,
+ "the game stopped (exit status: 1)",
+ );
+ assert_eq!(install.current().as_deref(), Some("v1"));
+ // `rollback` won't go to the release that failed.
+ assert_eq!(install.previous(), None);
+ assert_eq!(
+ std::fs::read_to_string(dir.join("world/level.dat")).unwrap(),
+ "before"
+ );
+ let update = shared.update.lock().unwrap();
+ assert!(!update.ok && update.busy.is_none() && update.finished == 1);
+ let last = update.last.as_deref().unwrap();
+ assert!(
+ last.starts_with("Rolled back to v1: v2 didn't come up (the game stopped (exit status: 1)); the world is back"),
+ "{last}"
+ );
+ // The world the new game left is kept.
+ let kept = last.rsplit_once("is in ").unwrap().1.trim_end_matches(')');
+ assert_eq!(
+ std::fs::read_to_string(Path::new(kept).join("level.dat")).unwrap(),
+ "after"
+ );
+ std::fs::remove_dir_all(&install.root).unwrap();
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn a_game_that_cant_load_is_never_switched_to() {
+ let (shared, dir) = installed("unloadable", &[("v1", 0), ("v2", 127)]);
+ let install = shared.install.clone().unwrap();
+ assert!(
+ switch(
+ &shared,
+ &dir,
+ &Config::default(),
+ "v2",
+ Duration::from_secs(60)
+ )
+ .is_none()
+ );
+ assert_eq!(install.current().as_deref(), Some("v1"));
+ let update = shared.update.lock().unwrap();
+ assert!(!update.ok);
+ assert!(
+ update
+ .last
+ .as_deref()
+ .unwrap()
+ .starts_with("Stayed on v1: the game (")
+ );
+ std::fs::remove_dir_all(&install.root).unwrap();
+ }
+
+ #[test]
+ fn up_means_done_and_a_status_reply_from_this_game() {
+ let shared = Shared::with_install(Path::new("."), None);
+ let set = |phase, pid: Option, sample: serde_json::Value| {
+ let mut state = shared.state.lock().unwrap();
+ (state.phase, state.engine_pid, state.sample) = (phase, pid, sample);
+ };
+ set(
+ Phase::Starting,
+ Some(7),
+ json!({ "pid": 7, "game": { "tps": 20.0 } }),
+ );
+ assert!(!up(&shared));
+ set(Phase::Running, Some(7), json!({ "pid": 7, "game": null }));
+ assert!(!up(&shared));
+ // A sample of the game before the restart doesn't count.
+ set(
+ Phase::Running,
+ Some(8),
+ json!({ "pid": 7, "game": { "tps": 20.0 } }),
+ );
+ assert!(!up(&shared));
+ set(
+ Phase::Running,
+ Some(8),
+ json!({ "pid": 8, "game": { "tps": 20.0 } }),
+ );
+ assert!(up(&shared));
+ }
+
#[test]
fn one_supervisor_per_folder() {
let dir = std::env::temp_dir().join(format!("horizon-server-lock-{}", std::process::id()));
diff --git a/crates/horizon-server/src/update.rs b/crates/horizon-server/src/update.rs
new file mode 100644
index 000000000..bebbedc76
--- /dev/null
+++ b/crates/horizon-server/src/update.rs
@@ -0,0 +1,763 @@
+//! Updates and rollbacks (docs/server.md, section 7) for a `horizon-server` installed by install.sh or
+//! install.ps1: each release in `/releases/`, `/current` pointing at the one in use, and
+//! `/previous` naming the one before it. An update downloads the newest release from GitHub, checks it
+//! against the SHA-256 GitHub publishes for it, unpacks it beside the others and switches `current`; the
+//! supervisor then restarts the game on it and switches back by itself if the new game doesn't come up.
+
+use crate::{api::Client, backup, config::Config, engine, supervisor};
+use serde_json::{Value, json};
+use std::{
+ io::Read,
+ path::{Path, PathBuf},
+ process::Command,
+ time::{Duration, Instant},
+};
+
+const REPO: &str = "ludovic111/horizon";
+/// The release archives for this computer, most specific first: on Linux the server build (no ALSA or
+/// udev), else the full game (releases before the server build).
+const ASSETS: &[&str] = if cfg!(all(target_os = "linux", target_arch = "x86_64")) {
+ &[
+ "horizon-server-linux-x86_64.tar.gz",
+ "horizon-linux-x86_64.tar.gz",
+ ]
+} else if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
+ &["horizon-macos-arm64.zip"]
+} else if cfg!(all(target_os = "windows", target_arch = "x86_64")) {
+ &["horizon-windows-x64.zip"]
+} else {
+ &[]
+};
+
+fn exe(name: &str) -> String {
+ format!("{name}{}", std::env::consts::EXE_SUFFIX)
+}
+
+/// The installer's folder: `releases/`, `current`, `previous` and `game-files/`.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct Install {
+ pub root: PathBuf,
+}
+
+impl Install {
+ /// The install this program runs from, if it runs from one.
+ pub fn find() -> Option {
+ Self::around(&std::env::current_exe().ok()?)
+ }
+
+ /// The install holding `program` (`/releases//horizon-server`, reached directly or through
+ /// `current`).
+ pub fn around(program: &Path) -> Option {
+ let program = std::fs::canonicalize(program).ok()?;
+ let releases = program.parent()?.parent()?;
+ if releases.file_name()? != "releases" {
+ return None;
+ }
+ let root = releases.parent()?.to_path_buf();
+ root.join("current").is_dir().then_some(Self { root })
+ }
+
+ /// The install, or why there is none to update.
+ pub fn required() -> Result {
+ Self::find().ok_or_else(|| {
+ "this horizon-server wasn't installed by install.sh or install.ps1 (no releases/ and current \
+ around it): update it the way you installed it"
+ .into()
+ })
+ }
+
+ pub fn release(&self, tag: &str) -> PathBuf {
+ self.root.join("releases").join(tag)
+ }
+
+ /// Where the game programs in use are: `current`, so a switch applies at the next start.
+ pub fn programs(&self) -> PathBuf {
+ self.root.join("current")
+ }
+
+ /// The release in use.
+ pub fn current(&self) -> Option {
+ let target = std::fs::canonicalize(self.root.join("current")).ok()?;
+ Some(target.file_name()?.to_string_lossy().into_owned())
+ }
+
+ /// The release used before the current one, if it is still there.
+ pub fn previous(&self) -> Option {
+ let tag = std::fs::read_to_string(self.root.join("previous")).ok()?;
+ let tag = tag.trim();
+ (!tag.is_empty() && self.release(tag).is_dir()).then(|| tag.to_owned())
+ }
+
+ /// Points `current` at the release `tag`, and remembers the one it pointed at as `previous`.
+ pub fn switch(&self, tag: &str) -> Result<(), String> {
+ let target = self.release(tag);
+ if !target.join(exe("horizon-server")).is_file() {
+ return Err(format!("{} holds no horizon-server", target.display()));
+ }
+ if let Some(old) = self.current().filter(|old| old != tag) {
+ std::fs::write(self.root.join("previous"), format!("{old}\n"))
+ .map_err(|e| format!("{}: {e}", self.root.join("previous").display()))?;
+ }
+ point(&self.root.join("current"), &target)
+ .map_err(|e| format!("switching current to {tag}: {e}"))
+ }
+
+ /// Undoes a [`Install::switch`]: `current` back on `from` and `previous` as it was before, so a later
+ /// rollback doesn't go to the release that failed.
+ pub fn switch_back(&self, from: &str, previous: Option<&str>) -> Result<(), String> {
+ point(&self.root.join("current"), &self.release(from))
+ .map_err(|e| format!("switching current back to {from}: {e}"))?;
+ let file = self.root.join("previous");
+ match previous {
+ Some(tag) => std::fs::write(&file, format!("{tag}\n")),
+ None => std::fs::remove_file(&file).or_else(|e| {
+ if e.kind() == std::io::ErrorKind::NotFound {
+ Ok(())
+ } else {
+ Err(e)
+ }
+ }),
+ }
+ .map_err(|e| format!("{}: {e}", file.display()))
+ }
+}
+
+/// Makes `link` point at the folder `target`, replacing what it pointed at.
+#[cfg(unix)]
+fn point(link: &Path, target: &Path) -> std::io::Result<()> {
+ // A new link renamed over the old one: `current` always points at a whole release.
+ let fresh = link.with_extension("switching");
+ let _ = std::fs::remove_file(&fresh);
+ std::os::unix::fs::symlink(target, &fresh)?;
+ std::fs::rename(&fresh, link)
+}
+
+/// Makes `link` a junction to `target` (install.ps1's links: they need no admin rights).
+#[cfg(windows)]
+fn point(link: &Path, target: &Path) -> std::io::Result<()> {
+ if link.exists() || link.symlink_metadata().is_ok() {
+ std::fs::remove_dir(link)?;
+ }
+ let status = Command::new("cmd")
+ .arg("/C")
+ .arg("mklink")
+ .arg("/J")
+ .arg(link)
+ .arg(target)
+ .stdout(std::process::Stdio::null())
+ .status()?;
+ if status.success() {
+ Ok(())
+ } else {
+ Err(std::io::Error::other(format!(
+ "mklink /J failed ({status})"
+ )))
+ }
+}
+
+/// A release on GitHub: its tag and the archive for this computer.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct Release {
+ pub tag: String,
+ pub asset: String,
+ pub url: String,
+ pub sha256: String,
+}
+
+/// The newest release (or `tag`), from GitHub's API.
+pub fn release(tag: Option<&str>) -> Result {
+ let url = match tag {
+ Some(tag) => format!("https://api.github.com/repos/{REPO}/releases/tags/{tag}"),
+ None => format!("https://api.github.com/repos/{REPO}/releases/latest"),
+ };
+ let text = ureq::get(&url)
+ .set("Accept", "application/vnd.github+json")
+ .set("User-Agent", "horizon-server")
+ .call()
+ .map_err(|e| match e {
+ ureq::Error::Status(404, _) => format!("no release {} on GitHub", tag.unwrap_or("")),
+ e => format!("GitHub didn't answer: {e}"),
+ })?
+ .into_string()
+ .map_err(|e| format!("GitHub's answer: {e}"))?;
+ pick(&serde_json::from_str(&text).map_err(|e| format!("GitHub's answer: {e}"))?)
+}
+
+/// The archive for this computer in a release's JSON, with the SHA-256 GitHub publishes for it.
+fn pick(release: &Value) -> Result {
+ let tag = release["tag_name"]
+ .as_str()
+ .ok_or("GitHub's answer has no tag")?;
+ let assets = release["assets"].as_array().cloned().unwrap_or_default();
+ let asset = ASSETS
+ .iter()
+ .find_map(|name| assets.iter().find(|a| a["name"] == *name))
+ .ok_or_else(|| {
+ format!(
+ "release {tag} has no archive for this computer ({})",
+ ASSETS.join(", ")
+ )
+ })?;
+ let name = asset["name"].as_str().unwrap_or_default();
+ let sha256 = asset["digest"]
+ .as_str()
+ .and_then(|d| d.strip_prefix("sha256:"))
+ .ok_or_else(|| format!("release {tag} publishes no SHA-256 for {name}"))?;
+ Ok(Release {
+ tag: tag.to_owned(),
+ asset: name.to_owned(),
+ url: asset["browser_download_url"]
+ .as_str()
+ .unwrap_or_default()
+ .to_owned(),
+ sha256: sha256.to_ascii_lowercase(),
+ })
+}
+
+/// Whether the release `tag` is newer than `than`: releases are tagged `vYYYY.MM.DD[.N]`, compared
+/// number by number. A tag that isn't one (an archive installed by hand) is never older than a release.
+pub fn newer(tag: &str, than: &str) -> bool {
+ let numbers = |t: &str| -> Option> {
+ t.strip_prefix('v')?
+ .split('.')
+ .map(|n| n.parse().ok())
+ .collect()
+ };
+ match (numbers(tag), numbers(than)) {
+ (Some(a), Some(b)) => a > b,
+ (Some(_), None) => true,
+ _ => false,
+ }
+}
+
+/// Downloads a release's archive into `/downloads/` and checks its SHA-256; the archive's path.
+pub fn download(install: &Install, release: &Release) -> Result {
+ use sha2::Digest;
+ let folder = install.root.join("downloads");
+ std::fs::create_dir_all(&folder).map_err(|e| format!("{}: {e}", folder.display()))?;
+ let path = folder.join(&release.asset);
+ let part = folder.join(format!("{}.part", release.asset));
+ let result = (|| {
+ let mut reader = ureq::get(&release.url)
+ .set("User-Agent", "horizon-server")
+ .call()
+ .map_err(|e| format!("downloading {}: {e}", release.asset))?
+ .into_reader();
+ let mut file =
+ std::fs::File::create(&part).map_err(|e| format!("{}: {e}", part.display()))?;
+ let mut hasher = sha2::Sha256::new();
+ let mut buffer = vec![0; 1 << 16];
+ loop {
+ let n = reader
+ .read(&mut buffer)
+ .map_err(|e| format!("downloading {}: {e}", release.asset))?;
+ if n == 0 {
+ break;
+ }
+ hasher.update(&buffer[..n]);
+ std::io::Write::write_all(&mut file, &buffer[..n]).map_err(|e| e.to_string())?;
+ }
+ let got: String = hasher
+ .finalize()
+ .iter()
+ .map(|b| format!("{b:02x}"))
+ .collect();
+ if got != release.sha256 {
+ return Err(format!(
+ "{} doesn't match its SHA-256 (got {got}, expected {}): not installed",
+ release.asset, release.sha256
+ ));
+ }
+ std::fs::rename(&part, &path).map_err(|e| e.to_string())
+ })();
+ if result.is_err() {
+ let _ = std::fs::remove_file(&part);
+ }
+ result.map(|()| path)
+}
+
+/// Unpacks a release archive into `/releases/`, with its `.local/horizon-client` linked to the
+/// install's prepared game files (which updates keep), as the installers do. It doesn't switch to it.
+pub fn unpack(install: &Install, archive: &Path, tag: &str) -> Result {
+ if tag.is_empty() || tag.contains(['/', '\\']) || tag.starts_with('.') {
+ return Err(format!("{tag:?} isn't a release name"));
+ }
+ let target = install.release(tag);
+ if install.current().as_deref() == Some(tag) {
+ return Err(format!("{tag} is the release in use"));
+ }
+ let scratch = install
+ .root
+ .join("releases")
+ .join(format!(".{tag}.unpacking"));
+ let _ = std::fs::remove_dir_all(&scratch);
+ std::fs::create_dir_all(&scratch).map_err(|e| format!("{}: {e}", scratch.display()))?;
+ let result = (|| {
+ let name = archive.to_string_lossy();
+ if name.ends_with(".zip") {
+ let file =
+ std::fs::File::open(archive).map_err(|e| format!("{}: {e}", archive.display()))?;
+ zip::ZipArchive::new(file)
+ .and_then(|mut z| z.extract(&scratch))
+ .map_err(|e| format!("{}: {e}", archive.display()))?;
+ } else if name.ends_with(".tar.gz") || name.ends_with(".tgz") {
+ // tar is on every Linux and macOS, and on Windows 10 and later.
+ let status = Command::new("tar")
+ .arg("-xzf")
+ .arg(archive)
+ .arg("-C")
+ .arg(&scratch)
+ .status()
+ .map_err(|e| format!("tar: {e}"))?;
+ if !status.success() {
+ return Err(format!(
+ "tar couldn't unpack {} ({status})",
+ archive.display()
+ ));
+ }
+ } else {
+ return Err(format!(
+ "{} is neither a .tar.gz nor a .zip",
+ archive.display()
+ ));
+ }
+ let source = find_program(&scratch, &exe("horizon-server"))
+ .and_then(|p| p.parent().map(Path::to_path_buf))
+ .ok_or_else(|| format!("{} holds no horizon-server", archive.display()))?;
+ #[cfg(unix)]
+ for name in [
+ "horizon-server",
+ "horizon-engine",
+ "horizon-client",
+ "horizon-prepare",
+ ] {
+ use std::os::unix::fs::PermissionsExt;
+ let path = source.join(name);
+ if path.is_file() {
+ let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755));
+ }
+ }
+ if target.exists() {
+ std::fs::remove_dir_all(&target).map_err(|e| format!("{}: {e}", target.display()))?;
+ }
+ std::fs::rename(&source, &target).map_err(|e| format!("{}: {e}", target.display()))?;
+ let game_files = install.root.join("game-files/horizon-client");
+ std::fs::create_dir_all(&game_files).map_err(|e| e.to_string())?;
+ let local = target.join(".local");
+ std::fs::create_dir_all(&local).map_err(|e| e.to_string())?;
+ let link = local.join("horizon-client");
+ if link.symlink_metadata().is_err() {
+ point(&link, &game_files).map_err(|e| format!("{}: {e}", link.display()))?;
+ }
+ Ok(target.clone())
+ })();
+ let _ = std::fs::remove_dir_all(&scratch);
+ result
+}
+
+/// The first file called `name` under `dir`, breadth first.
+fn find_program(dir: &Path, name: &str) -> Option {
+ let mut queue = std::collections::VecDeque::from([dir.to_path_buf()]);
+ while let Some(folder) = queue.pop_front() {
+ let mut entries: Vec<_> = std::fs::read_dir(&folder).ok()?.flatten().collect();
+ entries.sort_by_key(|e| e.file_name());
+ for entry in entries {
+ let path = entry.path();
+ let kind = entry.file_type().ok()?;
+ if kind.is_file() && entry.file_name() == name {
+ return Some(path);
+ }
+ if kind.is_dir() {
+ queue.push_back(path);
+ }
+ }
+ }
+ None
+}
+
+/// A backup name for the world before a switch to `tag` (backup names take letters, digits, - and _).
+pub fn backup_name(tag: &str) -> String {
+ let clean: String = tag
+ .chars()
+ .map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
+ .take(48)
+ .collect();
+ format!("before-{clean}")
+}
+
+/// `update [--check] [--version TAG] [--archive FILE --version TAG] [--timeout SECONDS]`.
+pub fn command(dir: &Path, args: &[String]) -> Result<(), String> {
+ let mut check = false;
+ let mut version = None;
+ let mut archive = None;
+ let mut timeout = crate::api::UPDATE_TIMEOUT;
+ let mut it = args.iter();
+ while let Some(arg) = it.next() {
+ let mut value = || {
+ it.next()
+ .cloned()
+ .ok_or_else(|| format!("{arg} needs a value"))
+ };
+ match arg.as_str() {
+ "--check" => check = true,
+ "--version" => version = Some(value()?),
+ "--archive" => archive = Some(PathBuf::from(value()?)),
+ "--timeout" => timeout = seconds(&value()?)?,
+ other => return Err(format!("update: unknown option {other}")),
+ }
+ }
+ let install = Install::required()?;
+ let current = install
+ .current()
+ .ok_or("the installer's `current` points at no release")?;
+ let (tag, archive, downloaded) = match archive {
+ Some(_) if check => return Err("--check looks at GitHub's releases: drop --archive".into()),
+ Some(archive) => {
+ let tag = version
+ .ok_or("--archive needs --version NAME: the release name to install it as")?;
+ (tag, archive, false)
+ }
+ None => {
+ let release = release(version.as_deref())?;
+ if check {
+ if newer(&release.tag, ¤t) {
+ println!(
+ "Horizon {} is out; this server runs {current}. `horizon-server update` installs it.",
+ release.tag
+ );
+ } else {
+ println!(
+ "Up to date: {current} (the newest release is {}).",
+ release.tag
+ );
+ }
+ return Ok(());
+ }
+ if version.is_none() && !newer(&release.tag, ¤t) {
+ println!("Already on the newest release ({current}).");
+ return Ok(());
+ }
+ println!("Downloading Horizon {} ({})...", release.tag, release.asset);
+ let archive = download(&install, &release)?;
+ (release.tag, archive, true)
+ }
+ };
+ if tag == current {
+ println!("Already on {tag}.");
+ return Ok(());
+ }
+ let unpacked = unpack(&install, &archive, &tag);
+ if downloaded {
+ let _ = std::fs::remove_file(&archive);
+ }
+ unpacked?;
+ switch_to(dir, &install, &tag, timeout)
+}
+
+/// `rollback [--timeout SECONDS]`: back to the release used before the current one.
+pub fn rollback(dir: &Path, args: &[String]) -> Result<(), String> {
+ let timeout = match args {
+ [] => crate::api::UPDATE_TIMEOUT,
+ [flag, value] if flag == "--timeout" => seconds(value)?,
+ _ => return Err("rollback takes only --timeout SECONDS".into()),
+ };
+ let install = Install::required()?;
+ let previous = install.previous().ok_or(
+ "no release to go back to: this install hasn't been updated, or the one before is gone",
+ )?;
+ switch_to(dir, &install, &previous, timeout)
+}
+
+fn seconds(text: &str) -> Result {
+ text.parse()
+ .ok()
+ .filter(|&s| s > 0)
+ .map(Duration::from_secs)
+ .ok_or_else(|| format!("--timeout needs a number of seconds (got {text:?})"))
+}
+
+/// Switches the install to the release `tag`. A running server restarts on it through its supervisor, which
+/// goes back by itself if the game isn't up within `timeout`; a stopped one is switched and its game checked.
+/// Either way the world is backed up first.
+fn switch_to(dir: &Path, install: &Install, tag: &str, timeout: Duration) -> Result<(), String> {
+ let Some(client) = Client::open(dir)? else {
+ return switch_stopped(dir, install, tag);
+ };
+ let finished = |status: &Value| status["update"]["finished"].as_u64().unwrap_or(0);
+ let before = finished(&client.call("GET", "/api/v1/status", None)?);
+ client
+ .call(
+ "POST",
+ "/api/v1/update",
+ Some(&json!({ "tag": tag, "timeout": timeout.as_secs() })),
+ )
+ .map_err(|e| {
+ if e == "no such endpoint" {
+ "the running server is older than updates: stop it, then run `horizon-server update` again".into()
+ } else {
+ e
+ }
+ })?;
+ print!("Restarting the server on {tag}");
+ // The backup, the stop, the start and, if it fails, the way back.
+ let deadline = Instant::now() + timeout + Duration::from_secs(3600);
+ loop {
+ std::thread::sleep(Duration::from_millis(500));
+ let status = client.call("GET", "/api/v1/status", None)?;
+ if finished(&status) > before {
+ let update = &status["update"];
+ let last = update["last"].as_str().unwrap_or_default().to_owned();
+ if update["ok"] == true {
+ println!("\n{last}");
+ return Ok(());
+ }
+ // Rolled back (or stayed): say so once the game is up again on the release it went back to.
+ let mut phase = status["phase"].as_str().unwrap_or_default().to_owned();
+ let until = Instant::now() + crate::api::UPDATE_TIMEOUT;
+ while phase != "running" && phase != "stopping" && Instant::now() < until {
+ std::thread::sleep(Duration::from_millis(500));
+ print!(".");
+ let _ = std::io::Write::flush(&mut std::io::stdout());
+ phase = client.call("GET", "/api/v1/status", None)?["phase"]
+ .as_str()
+ .unwrap_or_default()
+ .to_owned();
+ }
+ println!();
+ return Err(if phase == "running" {
+ format!("{last}. The game is up again.")
+ } else {
+ last
+ });
+ }
+ if Instant::now() > deadline {
+ println!();
+ return Err(
+ "no word from the server about the update; `horizon-server logs` shows where it is"
+ .into(),
+ );
+ }
+ print!(".");
+ let _ = std::io::Write::flush(&mut std::io::stdout());
+ }
+}
+
+/// The switch for a stopped server (or outside any server folder): the world backed up, `current` moved,
+/// and the new game checked to load on this system, else switched back.
+fn switch_stopped(dir: &Path, install: &Install, tag: &str) -> Result<(), String> {
+ let from = install
+ .current()
+ .ok_or("the installer's `current` points at no release")?;
+ let server = dir.join(crate::config::FILE).is_file();
+ let _lock = if server {
+ Some(supervisor::lock(dir)?)
+ } else {
+ None
+ };
+ let config = if server {
+ Config::load(dir)?
+ } else {
+ Config::default()
+ };
+ let backed_up = if server {
+ let file = backup::file_name(std::time::SystemTime::now(), Some(&backup_name(tag)))?;
+ backup::create(dir, &config, &file)?;
+ format!(" The world is backed up in {}/{file}.", backup::DIR)
+ } else {
+ String::new()
+ };
+ let previous = install.previous();
+ install.switch(tag)?;
+ if let Err(e) =
+ engine::program_in(&config, dir, Some(install)).and_then(|p| engine::check_starts(&p))
+ {
+ install.switch_back(&from, previous.as_deref())?;
+ return Err(format!("stayed on {from}: {e}"));
+ }
+ println!(
+ "Switched from {from} to {tag}.{backed_up}{} `horizon-server rollback` goes back to {from}.",
+ if server {
+ " The server is stopped: `horizon-server start` runs the new version."
+ } else {
+ ""
+ }
+ );
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use serde_json::json;
+
+ fn scratch(name: &str) -> PathBuf {
+ let dir = std::env::temp_dir().join(format!(
+ "horizon-server-update-{name}-{}",
+ std::process::id()
+ ));
+ let _ = std::fs::remove_dir_all(&dir);
+ std::fs::create_dir_all(&dir).unwrap();
+ dir
+ }
+
+ /// An install as install.sh leaves it, with releases `tags`, the first in use.
+ #[cfg(unix)]
+ fn installed(name: &str, tags: &[&str]) -> Install {
+ // Canonical, as `around` sees it (macOS's temporary folder is behind a link).
+ let root = std::fs::canonicalize(scratch(name)).unwrap();
+ for tag in tags {
+ let release = root.join("releases").join(tag);
+ std::fs::create_dir_all(&release).unwrap();
+ std::fs::write(release.join("horizon-server"), tag).unwrap();
+ }
+ std::os::unix::fs::symlink(root.join("releases").join(tags[0]), root.join("current"))
+ .unwrap();
+ Install { root }
+ }
+
+ #[test]
+ fn tags_compare_by_number() {
+ assert!(newer("v2026.10.19", "v2026.10.18.27"));
+ assert!(newer("v2026.10.18.27", "v2026.10.18.9"));
+ assert!(newer("v2026.10.19.1", "v2026.10.19"));
+ assert!(!newer("v2026.10.19", "v2026.10.19"));
+ assert!(!newer("v2026.10.18", "v2026.10.19"));
+ // An archive installed by hand: any release is newer, and it is never newer than one.
+ assert!(newer("v2026.10.19", "local"));
+ assert!(!newer("local", "v2026.10.19"));
+ }
+
+ #[test]
+ fn the_archive_for_this_computer_is_picked_with_its_digest() {
+ let asset = |name: &str| {
+ json!({
+ "name": name,
+ "browser_download_url": format!("https://example.invalid/{name}"),
+ "digest": "sha256:ABCDEF",
+ })
+ };
+ let release = json!({
+ "tag_name": "v2026.10.19",
+ "assets": ASSETS.iter().rev().map(|n| asset(n)).chain([asset("other.zip")]).collect::>(),
+ });
+ if let Some(first) = ASSETS.first() {
+ let picked = pick(&release).unwrap();
+ assert_eq!(picked.tag, "v2026.10.19");
+ // The most specific archive (Linux: the server build), even when listed last.
+ assert_eq!(picked.asset, *first);
+ assert_eq!(picked.sha256, "abcdef");
+ let undigested = json!({ "tag_name": "v1", "assets": [{ "name": first, "browser_download_url": "x" }] });
+ assert!(pick(&undigested).unwrap_err().contains("no SHA-256"));
+ }
+ assert!(
+ pick(&json!({ "tag_name": "v1", "assets": [asset("other.zip")] }))
+ .unwrap_err()
+ .contains("no archive for this computer")
+ );
+ }
+
+ #[test]
+ fn backup_names_are_valid() {
+ let name = backup_name("v2026.10.19");
+ assert_eq!(name, "before-v2026-10-19");
+ crate::backup::file_name(std::time::SystemTime::now(), Some(&name)).unwrap();
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn switching_keeps_the_previous_release() {
+ let install = installed("switch", &["v1", "v2"]);
+ let exe = install.root.join("current/horizon-server");
+ assert_eq!(Install::around(&exe), Some(install.clone()));
+ assert_eq!(
+ Install::around(&install.release("v2").join("horizon-server")),
+ Some(install.clone())
+ );
+ assert_eq!(install.current().as_deref(), Some("v1"));
+ assert_eq!(install.previous(), None);
+ install.switch("v2").unwrap();
+ assert_eq!(install.current().as_deref(), Some("v2"));
+ assert_eq!(install.previous().as_deref(), Some("v1"));
+ assert_eq!(std::fs::read_to_string(&exe).unwrap(), "v2");
+ // Undoing it leaves `previous` as it was.
+ install.switch_back("v1", None).unwrap();
+ assert_eq!(install.current().as_deref(), Some("v1"));
+ assert_eq!(install.previous(), None);
+ install.switch("v2").unwrap();
+ // Rolling back is a switch to the previous release, which then becomes the one to go back to.
+ install.switch("v1").unwrap();
+ assert_eq!(install.current().as_deref(), Some("v1"));
+ assert_eq!(install.previous().as_deref(), Some("v2"));
+ // Switching to the release in use changes nothing; to a missing one is refused.
+ install.switch("v1").unwrap();
+ assert_eq!(install.previous().as_deref(), Some("v2"));
+ assert!(
+ install
+ .switch("v3")
+ .unwrap_err()
+ .contains("no horizon-server")
+ );
+ assert_eq!(install.current().as_deref(), Some("v1"));
+ std::fs::remove_dir_all(&install.root).unwrap();
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn an_archive_unpacks_beside_the_others_with_the_game_files_linked() {
+ let install = installed("unpack", &["v1"]);
+ let source = scratch("unpack-source");
+ let inner = source.join("horizon-v2");
+ std::fs::create_dir_all(&inner).unwrap();
+ std::fs::write(inner.join("horizon-server"), "v2").unwrap();
+ std::fs::write(inner.join("horizon-engine"), "engine").unwrap();
+ let archive = source.join("horizon-server-linux-x86_64.tar.gz");
+ let status = Command::new("tar")
+ .arg("-czf")
+ .arg(&archive)
+ .arg("-C")
+ .arg(&source)
+ .arg("horizon-v2")
+ .status()
+ .unwrap();
+ assert!(status.success());
+ let release = unpack(&install, &archive, "v2").unwrap();
+ assert_eq!(release, install.release("v2"));
+ assert_eq!(
+ std::fs::read_to_string(release.join("horizon-engine")).unwrap(),
+ "engine"
+ );
+ {
+ use std::os::unix::fs::PermissionsExt;
+ let mode = std::fs::metadata(release.join("horizon-engine"))
+ .unwrap()
+ .permissions()
+ .mode();
+ assert_eq!(mode & 0o111, 0o111);
+ }
+ assert_eq!(
+ std::fs::canonicalize(release.join(".local/horizon-client")).unwrap(),
+ std::fs::canonicalize(install.root.join("game-files/horizon-client")).unwrap()
+ );
+ // Not switched yet; nothing left behind; the release in use can't be unpacked over.
+ assert_eq!(install.current().as_deref(), Some("v1"));
+ assert!(!install.root.join("releases/.v2.unpacking").exists());
+ assert!(
+ unpack(&install, &archive, "v1")
+ .unwrap_err()
+ .contains("in use")
+ );
+ assert!(unpack(&install, &archive, "../x").is_err());
+ let empty = source.join("empty.zip");
+ zip::ZipWriter::new(std::fs::File::create(&empty).unwrap())
+ .finish()
+ .unwrap();
+ assert!(
+ unpack(&install, &empty, "v3")
+ .unwrap_err()
+ .contains("holds no horizon-server")
+ );
+ assert!(!install.release("v3").exists());
+ std::fs::remove_dir_all(&install.root).unwrap();
+ std::fs::remove_dir_all(&source).unwrap();
+ }
+}
diff --git a/docs/README.md b/docs/README.md
index 9a5ea0483..ffe8e3654 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -46,4 +46,4 @@ To play, use [the launcher](../launcher/README.md); the project site is [playhor
## The earlier Java track (no longer developed)
-- [Java performance packs](java-packs.md), with [Vulkan on your Mac](vulkan.md), [collector comparisons](collectors.md), [profiling](profiling.md), [process memory](process-memory.md) and [tick state](tick-state.md); [server quickstart](server-quickstart.txt).
+- [Java performance packs](java-packs.md), with [Vulkan on your Mac](vulkan.md), [collector comparisons](collectors.md), [profiling](profiling.md), [process memory](process-memory.md) and [tick state](tick-state.md). The Fabric server starter is retired: [host a server](hosting.md) with horizon-server.
diff --git a/docs/collectors.md b/docs/collectors.md
index 6be57f8be..98b497328 100644
--- a/docs/collectors.md
+++ b/docs/collectors.md
@@ -28,16 +28,15 @@ is not a measured pause total. Use JFR root pause events for pause analysis.
The streaming reader is checked against the JDK's event exporter with both
default-GC and ZGC fixtures in CI on Windows, Linux and ARM macOS.
-Developer demo and server launchers accept `--gc default`, `--gc g1` or `--gc zgc`.
+The developer demo launcher accepts `--gc default`, `--gc g1` or `--gc zgc`.
For example:
```sh
python3 tools/runtime.py demo --instance .local/runtime/collector-test --heap 6G --gc zgc --compact-headers
-python3 tools/server.py run --heap 4G --gc zgc
python3 tools/test_jfr.py --gc zgc
```
-Prepare demo mods/worlds before comparing. The server retains its EULA requirement.
+Prepare demo mods/worlds before comparing.
Options affect only the launched process; they do not change global Java settings.
The collector and compact-header experiments can be tested independently.
diff --git a/docs/hosting.md b/docs/hosting.md
index f350ad5f4..255b473a2 100644
--- a/docs/hosting.md
+++ b/docs/hosting.md
@@ -204,6 +204,8 @@ horizon-server restore BACKUP [--yes]
horizon-server service install|uninstall|print [--system]
horizon-server panel a one-time login link for the web panel
horizon-server import FILE server.toml from a vanilla server.properties
+horizon-server update [--check] install the newest release and restart on it (below)
+horizon-server rollback back to the release before
```
While the server runs, `op`, `whitelist` and `cmd` go to the game's console, so they act exactly as vanilla's
@@ -213,14 +215,31 @@ console commands do. While it's stopped, they edit vanilla's files directly. Any
The server restarts the game by itself if it crashes: after 5 s, then waiting longer each time, up to 5
minutes. It keeps the crash report and gives up after 5 crashes in 10 minutes, and `status` says why.
-Coming: `update` and `rollback`, `console` (attach to a server running in the background), `ban` and
-`pardon` as their own commands, and `mcp` (an MCP server so an AI assistant can run the server).
+**Updates** (a server installed with the install script). The server checks for a newer release a minute
+after it starts and every 6 hours after, and says so in its log, in `status` and in the panel; it never
+updates by itself. `horizon-server update --check` asks once. `horizon-server update`:
+
+1. downloads the newest release and checks it against the SHA-256 GitHub publishes for it;
+2. stops the game, backs up the world (`backups/