From e7b466a9778df6f99f33b0a07c8539f028f0000d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Wed, 30 Sep 2026 10:45:02 +0500 Subject: [PATCH] Install bootstrap uses free scratch ports (no collision with a running server); clean-base tool; install_pkg example Verified: clean base built from the repack (0 accounts, 0 characters, 647 migrations), packaged, signed, installed next to a running server, started with the CI-built worldserver to 'ready', first account created over RA. Co-Authored-By: Claude Sonnet 5.5 --- crates/coa-core/examples/ra_account.rs | 2 +- crates/coa-core/src/install.rs | 29 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/crates/coa-core/examples/ra_account.rs b/crates/coa-core/examples/ra_account.rs index db5a784..7600e6e 100644 --- a/crates/coa-core/examples/ra_account.rs +++ b/crates/coa-core/examples/ra_account.rs @@ -1,7 +1,7 @@ //! Create an account through the Manager's RA client on a RUNNING disposable fixture. fn main() { let a: Vec = std::env::args().skip(1).collect(); - assert!(a[0].to_lowercase().contains("fixture") || a[0].to_lowercase().contains("installtest"), "refusing: not a disposable folder"); + assert!(a[0].to_lowercase().contains("fixture") || a[0].to_lowercase().contains("installtest") || a[0].to_lowercase().contains("cleaninstall"), "refusing: not a disposable folder"); let root = std::path::Path::new(&a[0]); let mut ra = coa_core::ra::Ra::connect(root).expect("connect"); ra.create_account(&a[1], &a[2]).expect("create"); diff --git a/crates/coa-core/src/install.rs b/crates/coa-core/src/install.rs index 3ba5563..7471273 100644 --- a/crates/coa-core/src/install.rs +++ b/crates/coa-core/src/install.rs @@ -145,6 +145,35 @@ fn random_hex(n: usize) -> String { /// Rotate the database users' passwords away from the packaged bootstrap values and write the launcher's /// `Settings/database.json`. MySQL is started from `root` and stopped again before returning. fn bootstrap_database(root: &Path) -> Result<()> { + with_scratch_ports(root, || bootstrap_database_inner(root)) +} + +fn free_port() -> Result { + Ok(std::net::TcpListener::bind("127.0.0.1:0")?.local_addr()?.port()) +} + +/// Run `f` with the staging copy listening on unused ports, so an installation never collides with a server (or any +/// other program) already using the shipped ports. The shipped ports are put back afterwards. +fn with_scratch_ports(root: &Path, f: impl FnOnce() -> Result) -> Result { + const KEYS: [&str; 4] = ["mysqlPort", "authPort", "worldPort", "raPort"]; + let path = root.join("Settings/repack.json"); + let original: serde_json::Value = fsx::read_json(&path)?; + let mut scratch = original.clone(); + for k in KEYS { + scratch[k] = free_port()?.into(); + } + fsx::atomic_write_json(&path, &scratch)?; + let result = f(); + // keep whatever else changed meanwhile (console credentials), restore only the ports + let mut now: serde_json::Value = fsx::read_json(&path).unwrap_or(scratch); + for k in KEYS { + now[k] = original[k].clone(); + } + fsx::atomic_write_json(&path, &now)?; + result +} + +fn bootstrap_database_inner(root: &Path) -> Result<()> { let boot = root.join(BOOTSTRAP_CREDENTIALS); let target = root.join("Settings/database.json"); if !boot.is_file() {