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() {