mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-19 19:25:56 +00:00
preview 0.9.10: bare modular core for new servers, modular copy of classic servers
- A new server goes on to the preview core without compiling any module (recommended modules are declined once; modules come from the Modules page). - Choosing a server with the classic core makes a modular copy next to it: every acore_* database (read from the running database, or from a private copy of the stopped data files opened by the new server's own mysqld), module configs and realm profiles, ports and console login. The old server is only read. The copy then takes the preview core with the modules the old server had switched on. - Installs and the preview update run on unused ports, so a server running on the shipped ports (the old one, or any other) no longer stops them; the schema check after "Preparing database" used the shipped port before. - Dungeon Clear is part of SQUID's Playerbots: no card of its own (it said "Always on" while SQUID was off), switched with SQUID, settings on the SQUID card. E2E (examples/preview_bare_and_copy_e2e, disposable servers, another server running on the shipped ports): fresh install committed with no plugins and a clean boot report; copy of a stopped and of a running classic server: data identical, old server's files byte-for-byte unchanged, Content Scaling and Companions built on the copy, preview core committed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3866e219ce
commit
9f48cfc8d5
22 files changed
+799
-56
No files matched your search
@@ -0,0 +1,177 @@
|
||||
//! 0.9.10 install paths of the Modules Preview, end to end on DISPOSABLE servers under <work>:
|
||||
//!
|
||||
//! 1. fresh: a new server goes on to the preview core with the bare core (no module is compiled).
|
||||
//! 2. copy: a classic server (stopped) gets a modular copy next to it: accounts, characters, the world and
|
||||
//! module settings arrive, the old server's files are byte-for-byte unchanged, the copy takes the
|
||||
//! preview core with the modules the old server had switched on.
|
||||
//! 3. copy-running: the same copy step from a classic server whose database is running.
|
||||
//!
|
||||
//! usage: preview_bare_and_copy_e2e <empty work folder> [fresh|copy|all]
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use coa_core::install::{install_base, Params, Source};
|
||||
use coa_core::registry::Registry;
|
||||
use coa_core::update::{self, RepackEnv};
|
||||
|
||||
const KEY: &str = coa_core::signing::EMBEDDED_PUBLIC_KEY;
|
||||
type R<T> = Result<T, Box<dyn std::error::Error>>;
|
||||
|
||||
fn free_port() -> u16 {
|
||||
std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port()
|
||||
}
|
||||
|
||||
fn sql(root: &Path, q: &str) -> String {
|
||||
coa_core::backup::with_database(root, |db| db.query(q)).unwrap_or_else(|e| panic!("{q}: {e}"))
|
||||
}
|
||||
|
||||
fn install(root: &Path, reg: &Registry) -> R<()> {
|
||||
let image = coa_core::channels::stable_source()?;
|
||||
let t = std::time::Instant::now();
|
||||
let done = install_base(
|
||||
&Params { source: Source::Installation(Box::new(image)), dest: root.into(), trusted_key: KEY, registry: reg, cancel: Default::default() },
|
||||
&|s| eprintln!(" {:>3}% {}", s.percent, s.step),
|
||||
)?;
|
||||
println!("[{}] installed {} in {:?}", root.display(), done.version, t.elapsed());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_free_ports(root: &Path) -> R<()> {
|
||||
let repack = root.join("Settings/repack.json");
|
||||
let mut cfg: serde_json::Value = serde_json::from_slice(&std::fs::read(&repack)?)?;
|
||||
for k in ["mysqlPort", "authPort", "worldPort", "raPort"] {
|
||||
cfg[k] = free_port().into();
|
||||
}
|
||||
std::fs::write(&repack, serde_json::to_vec_pretty(&cfg)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The same steps as the Manager's install_preview_core.
|
||||
fn preview_core(root: &Path) -> R<update::Outcome> {
|
||||
let meta_dir = coa_core::registry::metadata_dir_for(root)?;
|
||||
let (_, meta) = coa_core::registry::MetaDir::open(&meta_dir)?;
|
||||
let source = coa_core::update_route::select(&meta, coa_core::channels::preview_update_source()?, KEY)?.source;
|
||||
let env = RepackEnv { root, meta_dir: &meta_dir };
|
||||
let t = std::time::Instant::now();
|
||||
let out = coa_core::install::with_scratch_ports(root, || update::apply(
|
||||
&update::Params { root, meta_dir: &meta_dir, source, trusted_key: KEY, cancel: Default::default(), resolutions: Default::default(), env: &env, fail_after_ops: None },
|
||||
&|phase, percent| println!(" {percent}% {phase}"),
|
||||
));
|
||||
let _ = coa_core::driver::run(root, coa_core::driver::Verb::StopAll);
|
||||
let out = out?;
|
||||
println!("[{}] preview core: {:?} in {:?}; modules {:?}", root.display(), out.txn.state, t.elapsed(), out.modules);
|
||||
assert_eq!(out.txn.state, update::State::Committed);
|
||||
assert!(coa_core::provenance::realm_host_fingerprint(root).is_some(), "the plugin host is installed");
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// sha256 of every file under the old server, except the launcher's own run state.
|
||||
fn tree(root: &Path) -> BTreeMap<String, String> {
|
||||
fn walk(base: &Path, dir: &Path, out: &mut BTreeMap<String, String>) {
|
||||
for e in std::fs::read_dir(dir).into_iter().flatten().flatten() {
|
||||
let p = e.path();
|
||||
if p.is_dir() {
|
||||
walk(base, &p, out);
|
||||
} else {
|
||||
let rel = p.strip_prefix(base).unwrap().to_string_lossy().replace('\\', "/");
|
||||
out.insert(rel, coa_core::fsx::sha256_file(&p).unwrap_or_else(|e| format!("unreadable: {e}")));
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut out = BTreeMap::new();
|
||||
walk(root, root, &mut out);
|
||||
out
|
||||
}
|
||||
|
||||
fn fresh(work: &Path, reg: &Registry) -> R<()> {
|
||||
let root = work.join("fresh-fixture");
|
||||
install(&root, reg)?; // shipped ports, as in the Manager: another server may be running on them
|
||||
coa_core::rebuild::decline_recommended(&root)?;
|
||||
let out = preview_core(&root)?;
|
||||
assert!(out.modules.is_empty(), "a new server compiles nothing: {:?}", out.modules);
|
||||
let registry = coa_core::plugins::load_registry(&root)?;
|
||||
assert!(registry.plugins.values().all(|e| !e.installed), "no plugin installed: {:?}", registry.plugins.keys());
|
||||
let boot: serde_json::Value = serde_json::from_slice(&std::fs::read(root.join("Core/plugins/plugin-boot-report.json"))?)?;
|
||||
println!("boot report: {boot}");
|
||||
assert_eq!(boot["anyFaulted"], serde_json::json!(false));
|
||||
println!("FRESH PASS");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn copy(work: &Path, reg: &Registry) -> R<()> {
|
||||
// A classic server with data of its own.
|
||||
let old = work.join("classic-fixture");
|
||||
install(&old, reg)?;
|
||||
set_free_ports(&old)?; // the test runs this "old" server; it must not meet a real one on the shipped ports
|
||||
sql(&old, "INSERT INTO acore_auth.account (username, salt, verifier, email, reg_mail) \
|
||||
VALUES ('E2ECOPY', UNHEX(REPEAT('11',32)), UNHEX(REPEAT('22',32)), '', '')");
|
||||
let account = sql(&old, "SELECT id FROM acore_auth.account WHERE username='E2ECOPY'").trim().to_string();
|
||||
sql(&old, &format!("SET SESSION sql_mode=''; INSERT INTO acore_characters.characters (guid, account, name, race, class, gender, level, money) \
|
||||
VALUES (900001, {account}, 'Copytest', 1, 1, 0, 42, 123456)"));
|
||||
let conf = old.join("Core/configs/modules/mod-coa-content-scaling.conf");
|
||||
let conf_text = std::fs::read_to_string(&conf).unwrap_or_default() + "\n# e2e owner edit\n";
|
||||
std::fs::write(&conf, &conf_text)?;
|
||||
let fingerprint = |root: &Path| sql(root, "SELECT CONCAT((SELECT COUNT(*) FROM acore_auth.account),'|',(SELECT COUNT(*) FROM acore_characters.characters),'|',\
|
||||
(SELECT CONCAT(name,':',level,':',money) FROM acore_characters.characters WHERE guid=900001))").trim().to_string();
|
||||
let expected = fingerprint(&old);
|
||||
let _ = coa_core::driver::run(&old, coa_core::driver::Verb::StopAll);
|
||||
println!("old server data: {expected}");
|
||||
assert!(coa_core::modular_copy::is_classic(&old));
|
||||
|
||||
// 2. copy of the stopped server, then the preview core on the copy.
|
||||
let before = tree(&old);
|
||||
let new = work.join("copy-fixture");
|
||||
install(&new, reg)?;
|
||||
let report = coa_core::modular_copy::copy_data(&old, &new, &|w| println!(" copy: {w}"))?;
|
||||
println!("copy report: {report:?}");
|
||||
assert_eq!(report.read_from, "copy of the stopped database");
|
||||
assert_eq!(tree(&old), before, "the old server's files changed");
|
||||
let observed = coa_core::process::observe(&new, &coa_core::layout::read_ports(&new));
|
||||
assert_eq!(observed.mysql.state, coa_core::process::ServiceState::Stopped, "a database of the copy is still running: {:?}", observed.mysql);
|
||||
assert!(!coa_core::registry::metadata_dir_for(&new)?.join("import").exists(), "the private copy of the old data is removed");
|
||||
assert_eq!(std::fs::read_to_string(new.join("Core/configs/modules/mod-coa-content-scaling.conf"))?, conf_text);
|
||||
let out = preview_core(&new)?;
|
||||
println!("modules on the copy: {:?}", out.modules);
|
||||
let got = fingerprint(&new);
|
||||
let _ = coa_core::driver::run(&new, coa_core::driver::Verb::StopAll);
|
||||
assert_eq!(got, expected, "data of the copy after the preview core");
|
||||
assert_eq!(tree(&old), before, "the old server's files changed");
|
||||
println!("COPY (stopped) PASS");
|
||||
|
||||
// 3. copy while the old database runs: read from it, change nothing.
|
||||
let started = coa_core::driver::run(&old, coa_core::driver::Verb::StartMysql)?;
|
||||
assert!(started.ok, "{}", started.output);
|
||||
let data_before: BTreeMap<String, String> = tree(&old).into_iter().filter(|(k, _)| !k.starts_with("mysql/") && !k.starts_with(".state/") && !k.starts_with("Core/configs/")).collect();
|
||||
let rows_before = fingerprint(&old);
|
||||
let new2 = work.join("copy-running-fixture");
|
||||
install(&new2, reg)?;
|
||||
let report = coa_core::modular_copy::copy_data(&old, &new2, &|w| println!(" copy: {w}"));
|
||||
let rows_after = fingerprint(&old);
|
||||
let _ = coa_core::driver::run(&old, coa_core::driver::Verb::StopAll);
|
||||
let report = report?;
|
||||
assert_eq!(report.read_from, "running database");
|
||||
assert_eq!(rows_after, rows_before);
|
||||
let data_after: BTreeMap<String, String> = tree(&old).into_iter().filter(|(k, _)| !k.starts_with("mysql/") && !k.starts_with(".state/") && !k.starts_with("Core/configs/")).collect();
|
||||
assert_eq!(data_after, data_before, "files outside the database changed");
|
||||
assert_eq!(fingerprint(&new2), expected);
|
||||
let _ = coa_core::driver::run(&new2, coa_core::driver::Verb::StopAll);
|
||||
println!("COPY (running) PASS");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> R<()> {
|
||||
let a: Vec<String> = std::env::args().skip(1).collect();
|
||||
let work = PathBuf::from(a.first().expect("usage: preview_bare_and_copy_e2e <empty work folder> [fresh|copy|all]"));
|
||||
let mode = a.get(1).map(String::as_str).unwrap_or("all");
|
||||
assert!(!work.exists() || std::fs::read_dir(&work)?.next().is_none(), "work folder must be new or empty");
|
||||
std::fs::create_dir_all(&work)?;
|
||||
let reg = Registry::at(work.join("installs.json"));
|
||||
if mode == "fresh" || mode == "all" {
|
||||
fresh(&work, ®)?;
|
||||
}
|
||||
if mode == "copy" || mode == "all" {
|
||||
copy(&work, ®)?;
|
||||
}
|
||||
println!("ALL PASS ({mode})");
|
||||
Ok(())
|
||||
}
|
||||
@@ -337,6 +337,17 @@ impl Db {
|
||||
.map(|o| o.status.success()).unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Ask the server to shut down cleanly (`mysqladmin shutdown`).
|
||||
pub fn shutdown_server(&self) -> Result<()> {
|
||||
let mut c = self.command("mysqladmin.exe");
|
||||
c.arg("shutdown").arg("--connect-timeout=5");
|
||||
let out = bounded_client_output(&mut c, b"", Instant::now() + Duration::from_secs(60))?;
|
||||
if !out.status.success() {
|
||||
return Err(self.fail(&out.stderr));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn with_read_deadline(mut self, deadline: Instant) -> Self {
|
||||
self.read_deadline = Some(deadline);
|
||||
self
|
||||
|
||||
@@ -210,7 +210,7 @@ pub(crate) fn free_port() -> Result<u16> {
|
||||
|
||||
/// 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.
|
||||
pub(crate) fn with_scratch_ports<T>(root: &Path, f: impl FnOnce() -> Result<T>) -> Result<T> {
|
||||
pub fn with_scratch_ports<T>(root: &Path, f: impl FnOnce() -> Result<T>) -> Result<T> {
|
||||
const KEYS: [&str; 4] = ["mysqlPort", "authPort", "worldPort", "raPort"];
|
||||
let path = root.join("Settings/repack.json");
|
||||
let original: serde_json::Value = fsx::read_json(&path)?;
|
||||
@@ -378,15 +378,18 @@ pub fn install_base(p: &Params, report: &dyn Fn(Step)) -> Result<Installed> {
|
||||
})?;
|
||||
say("Preparing database", 82, None);
|
||||
bootstrap_database(&staging_root)?;
|
||||
crate::backup::with_database(&staging_root, |db| {
|
||||
let problems = crate::schema_check::check(db, &staging_root)?;
|
||||
if let Some(p) = problems.first() {
|
||||
return Err(Error::Invalid(format!(
|
||||
"The packaged database is incomplete: {}.{}.{}: {}.",
|
||||
p.database, p.table, p.column, p.detail
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
// On unused ports too: another server running on the shipped ports must not stop the installation.
|
||||
with_scratch_ports(&staging_root, || {
|
||||
crate::backup::with_database(&staging_root, |db| {
|
||||
let problems = crate::schema_check::check(db, &staging_root)?;
|
||||
if let Some(p) = problems.first() {
|
||||
return Err(Error::Invalid(format!(
|
||||
"The packaged database is incomplete: {}.{}.{}: {}.",
|
||||
p.database, p.table, p.column, p.detail
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
})?;
|
||||
// 4. Commit: the only moment the destination changes. The marker stays until everything is done, so an
|
||||
// unfinished install can always be recognised as ours (and cleaned up), wherever it stopped.
|
||||
|
||||
@@ -43,6 +43,7 @@ pub mod logging;
|
||||
pub mod server_logging;
|
||||
pub mod manifest;
|
||||
pub mod migrations;
|
||||
pub mod modular_copy;
|
||||
pub mod modules;
|
||||
pub mod module_sources;
|
||||
pub mod msvc_setup;
|
||||
|
||||
@@ -0,0 +1,336 @@
|
||||
//! A modular copy of a classic server, next to it.
|
||||
//!
|
||||
//! A server with the classic core (every module compiled into `worldserver.exe`) is never converted in place.
|
||||
//! The Modules Preview installs a new server beside it and copies the old one's data into it: every game
|
||||
//! database (accounts, characters, world, bots) and the module settings. The old server is only read:
|
||||
//!
|
||||
//! * if its database is running, the schemas are dumped from it (consistent snapshots, nothing is written);
|
||||
//! * if it is stopped, its data files are copied to a private folder and read by a temporary database
|
||||
//! process of the NEW server; nothing of the old server is started or executed.
|
||||
//!
|
||||
//! The new server then takes the preview core like any other server (the caller applies the update).
|
||||
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Child, Command, Stdio};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::db::{Account, Db};
|
||||
use crate::error::{Error, Result};
|
||||
use crate::fsx;
|
||||
use crate::process::ServiceState;
|
||||
|
||||
/// Whether `root` runs the classic core: a Windows repack whose `worldserver.exe` carries no plugin-host
|
||||
/// fingerprint.
|
||||
pub fn is_classic(root: &Path) -> bool {
|
||||
!crate::docker::is_docker(root)
|
||||
&& root.join("Core/worldserver.exe").is_file()
|
||||
&& crate::provenance::realm_host_fingerprint(root).is_none()
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct CopyReport {
|
||||
pub schemas: Vec<String>,
|
||||
pub accounts: u64,
|
||||
pub characters: u64,
|
||||
pub config_files: Vec<String>,
|
||||
/// How the old data was read: "running database" or "copy of the stopped database".
|
||||
pub read_from: String,
|
||||
}
|
||||
|
||||
/// Game schemas of a CoA server: `acore_*` (auth, characters, world, their Wildcard/Reborn variants, bots).
|
||||
fn game_schemas(db: &Db) -> Result<Vec<String>> {
|
||||
let mut out: Vec<String> = db
|
||||
.query("SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME LIKE 'acore\\_%' ORDER BY SCHEMA_NAME;")?
|
||||
.lines()
|
||||
.map(|l| l.trim().to_string())
|
||||
.filter(|s| !s.is_empty() && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_'))
|
||||
// work schemas a restore leaves behind are not game data
|
||||
.filter(|s| !s.contains("_restore_") && !s.contains("_before_restore_"))
|
||||
.collect();
|
||||
out.dedup();
|
||||
if !out.iter().any(|s| s == "acore_auth") || !out.iter().any(|s| s == "acore_characters") {
|
||||
return Err(Error::Invalid("The old server's database has no accounts or characters schema.".into()));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn count(db: &Db, sql: &str) -> u64 {
|
||||
db.query(sql).ok().and_then(|s| s.trim().parse().ok()).unwrap_or(0)
|
||||
}
|
||||
|
||||
/// A database process of the new server reading a private copy of the old server's stopped data files.
|
||||
struct TemporaryDatabase {
|
||||
child: Child,
|
||||
dir: PathBuf,
|
||||
db: Option<Db>,
|
||||
stopped: bool,
|
||||
}
|
||||
|
||||
impl TemporaryDatabase {
|
||||
/// Shut it down cleanly, force it if it does not go, and remove the private copy. Fails if the process is
|
||||
/// still there afterwards, so a leftover database never holds the new server's files.
|
||||
fn stop(&mut self) -> Result<()> {
|
||||
if self.stopped {
|
||||
return Ok(());
|
||||
}
|
||||
if let Some(db) = &self.db {
|
||||
let _ = db.shutdown_server();
|
||||
}
|
||||
let deadline = Instant::now() + Duration::from_secs(90);
|
||||
loop {
|
||||
match self.child.try_wait()? {
|
||||
Some(_) => break,
|
||||
None if Instant::now() > deadline => {
|
||||
tracing::warn!(pid = self.child.id(), "temporary database did not shut down; terminating it");
|
||||
self.child.kill()?;
|
||||
self.child.wait()?;
|
||||
break;
|
||||
}
|
||||
None => std::thread::sleep(Duration::from_millis(250)),
|
||||
}
|
||||
}
|
||||
self.stopped = true;
|
||||
fs::remove_dir_all(&self.dir)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TemporaryDatabase {
|
||||
fn drop(&mut self) {
|
||||
if !self.stopped {
|
||||
if let Err(e) = self.stop() {
|
||||
tracing::warn!(error = %e, pid = self.child.id(), "temporary database left behind");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn copy_tree(from: &Path, to: &Path) -> Result<u64> {
|
||||
let mut bytes = 0;
|
||||
fs::create_dir_all(to)?;
|
||||
for entry in fs::read_dir(from)? {
|
||||
let entry = entry?;
|
||||
let md = fs::symlink_metadata(entry.path())?;
|
||||
if md.file_type().is_symlink() {
|
||||
return Err(Error::Invalid(format!("{} is a link; the database folder is not copied.", entry.path().display())));
|
||||
}
|
||||
let target = to.join(entry.file_name());
|
||||
if md.is_dir() {
|
||||
bytes += copy_tree(&entry.path(), &target)?;
|
||||
} else {
|
||||
fs::copy(entry.path(), &target)?;
|
||||
bytes += md.len();
|
||||
}
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
fn dir_size(dir: &Path) -> u64 {
|
||||
fs::read_dir(dir).map(|rd| rd.flatten().map(|e| {
|
||||
let p = e.path();
|
||||
if p.is_dir() { dir_size(&p) } else { e.metadata().map(|m| m.len()).unwrap_or(0) }
|
||||
}).sum()).unwrap_or(0)
|
||||
}
|
||||
|
||||
fn start_temporary(old: &Path, new: &Path, work: &Path, progress: &dyn Fn(&str)) -> Result<(TemporaryDatabase, Db)> {
|
||||
let source = old.join("mysql/data");
|
||||
if !source.join("mysql").is_dir() || !source.join("ibdata1").is_file() {
|
||||
return Err(Error::Invalid("The old server's database folder is incomplete.".into()));
|
||||
}
|
||||
let dir = work.join("old-database");
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
fsx::require_space(work, dir_size(&source))?;
|
||||
progress("Copying the old server's database files");
|
||||
let data = dir.join("data");
|
||||
copy_tree(&source, &data)?;
|
||||
let (root_password, _) = crate::db::credentials(old)?;
|
||||
let bin = new.join("mysql/bin");
|
||||
let port = crate::install::free_port()?;
|
||||
let child = Command::new(bin.join("mysqld.exe"))
|
||||
.arg("--no-defaults")
|
||||
.arg(format!("--basedir={}", new.join("mysql").display()))
|
||||
.arg(format!("--datadir={}", data.display()))
|
||||
.arg(format!("--port={port}"))
|
||||
.args(["--bind-address=127.0.0.1", "--mysqlx=0", "--skip-log-bin", "--character-set-server=utf8mb4",
|
||||
"--collation-server=utf8mb4_unicode_ci", "--max_allowed_packet=128M"])
|
||||
.arg(format!("--log-error={}", dir.join("mysql-error.log").display()))
|
||||
.arg(format!("--pid-file={}", dir.join("mysql.pid").display()))
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.creation_flags_hidden()
|
||||
.spawn()?;
|
||||
let db = Db::with_tools(bin, port, "root", &root_password, crate::realms::Mode::Coa);
|
||||
let mut temp = TemporaryDatabase { child, dir, db: Some(db.clone()), stopped: false };
|
||||
progress("Opening the copy of the old database");
|
||||
let deadline = Instant::now() + Duration::from_secs(300);
|
||||
while !db.ping() {
|
||||
if let Ok(Some(status)) = temp.child.try_wait() {
|
||||
let log = fs::read_to_string(temp.dir.join("mysql-error.log")).unwrap_or_default();
|
||||
let tail: Vec<&str> = log.lines().rev().take(8).collect();
|
||||
return Err(Error::Invalid(format!(
|
||||
"The copy of the old database did not open ({status}). {}",
|
||||
tail.into_iter().rev().collect::<Vec<_>>().join(" ")
|
||||
)));
|
||||
}
|
||||
if Instant::now() > deadline {
|
||||
return Err(Error::Invalid("The copy of the old database did not open within 5 minutes.".into()));
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(300));
|
||||
}
|
||||
Ok((temp, db))
|
||||
}
|
||||
|
||||
trait HiddenWindow {
|
||||
fn creation_flags_hidden(&mut self) -> &mut Self;
|
||||
}
|
||||
|
||||
impl HiddenWindow for Command {
|
||||
fn creation_flags_hidden(&mut self) -> &mut Self {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::process::CommandExt;
|
||||
self.creation_flags(0x0800_0000);
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Settings the owner may have changed that the new core can take as they are: the module configs
|
||||
/// (`Core/configs/modules/*.conf`; the shipped `.dist` files belong to the new core) and the realm profiles.
|
||||
/// `worldserver.conf` and `authserver.conf` are rendered by the launcher from the new server's own
|
||||
/// templates at every start, so they are not copied.
|
||||
fn settings_files(old: &Path) -> Vec<String> {
|
||||
crate::backup::config_files(old)
|
||||
.into_iter()
|
||||
.filter(|f| {
|
||||
(f.starts_with("Core/configs/modules/") && f.ends_with(".conf") && !f.ends_with("/mod_ascension_compat.conf"))
|
||||
|| f.starts_with("Settings/realm-profile")
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Copy the old server's data into `new`, a server just installed and not yet started by anyone.
|
||||
pub fn copy_data(old: &Path, new: &Path, progress: &dyn Fn(&str)) -> Result<CopyReport> {
|
||||
if !is_classic(old) {
|
||||
return Err(Error::Invalid("Only a server with the classic core is copied.".into()));
|
||||
}
|
||||
if fsx::canonicalize_lenient(old)? == fsx::canonicalize_lenient(new)? {
|
||||
return Err(Error::Invalid("The new server must be in another folder.".into()));
|
||||
}
|
||||
let work = crate::registry::metadata_dir_for(new)?.join("import");
|
||||
let _ = fs::remove_dir_all(&work);
|
||||
fs::create_dir_all(&work)?;
|
||||
|
||||
// 1. Read the old databases.
|
||||
let observed = crate::process::observe(old, &crate::layout::read_ports(old));
|
||||
let running = observed.mysql.state == ServiceState::Running
|
||||
&& Db::from_repack(old, Account::Admin).map(|db| db.ping()).unwrap_or(false);
|
||||
let mut temporary: Option<TemporaryDatabase> = None;
|
||||
let (source, read_from) = if running {
|
||||
(Db::from_repack(old, Account::Admin)?.for_realm(crate::realms::Mode::Coa), "running database")
|
||||
} else {
|
||||
let (temp, db) = start_temporary(old, new, &work, progress)?;
|
||||
temporary = Some(temp);
|
||||
(db, "copy of the stopped database")
|
||||
};
|
||||
let schemas = game_schemas(&source)?;
|
||||
let accounts = count(&source, "SELECT COUNT(*) FROM acore_auth.account;");
|
||||
let characters = count(&source, "SELECT COUNT(*) FROM acore_characters.characters;");
|
||||
let mut dumps = Vec::new();
|
||||
for schema in &schemas {
|
||||
progress(&format!("Copying {schema}"));
|
||||
let file = work.join(format!("{schema}.sql.zst"));
|
||||
source.dump_to(schema, &file)?;
|
||||
dumps.push((schema.clone(), file));
|
||||
}
|
||||
// The private copy of the old data files is not needed any more; its database must be gone before the new
|
||||
// server's own database starts.
|
||||
if let Some(mut temp) = temporary.take() {
|
||||
progress("Closing the copy of the old database");
|
||||
temp.stop()?;
|
||||
}
|
||||
|
||||
// 2. Put them into the new server, on ports nobody uses, so a running old server is not disturbed.
|
||||
crate::install::with_scratch_ports(new, || {
|
||||
crate::backup::with_database(new, |db| {
|
||||
let db = db.clone().for_realm(crate::realms::Mode::Coa);
|
||||
for (schema, file) in &dumps {
|
||||
progress(&format!("Importing {schema}"));
|
||||
db.query(&format!(
|
||||
"DROP DATABASE IF EXISTS `{schema}`; CREATE DATABASE `{schema}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
|
||||
))?;
|
||||
db.import_from(schema, file)?;
|
||||
}
|
||||
let (a, c) = (
|
||||
count(&db, "SELECT COUNT(*) FROM acore_auth.account;"),
|
||||
count(&db, "SELECT COUNT(*) FROM acore_characters.characters;"),
|
||||
);
|
||||
if (a, c) != (accounts, characters) {
|
||||
return Err(Error::Invalid(format!(
|
||||
"The copy is incomplete: {a} of {accounts} accounts and {c} of {characters} characters arrived."
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
})?;
|
||||
|
||||
// 3. Settings: module configs and realm profiles; ports and console login as the old server had them
|
||||
// (the console account lives in the copied accounts database).
|
||||
progress("Copying settings");
|
||||
let files = settings_files(old);
|
||||
for rel in &files {
|
||||
let from = fsx::ensure_within(old, &fsx::safe_join(old, rel)?)?;
|
||||
let to = fsx::ensure_within(new, &fsx::safe_join(new, rel)?)?;
|
||||
fsx::atomic_write(&to, &fs::read(from)?)?;
|
||||
}
|
||||
let old_repack: serde_json::Value = fsx::read_json(&old.join("Settings/repack.json"))?;
|
||||
let mut repack: serde_json::Value = fsx::read_json(&new.join("Settings/repack.json"))?;
|
||||
if let (Some(dst), Some(src)) = (repack.as_object_mut(), old_repack.as_object()) {
|
||||
for (k, v) in src {
|
||||
dst.insert(k.clone(), v.clone());
|
||||
}
|
||||
}
|
||||
fsx::atomic_write_json(&new.join("Settings/repack.json"), &repack)?;
|
||||
let _ = fs::remove_dir_all(&work);
|
||||
Ok(CopyReport { schemas, accounts, characters, config_files: files, read_from: read_from.into() })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn only_owner_settings_are_copied() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let old = dir.path();
|
||||
for rel in [
|
||||
"Core/configs/worldserver.conf",
|
||||
"Core/configs/worldserver.conf.dist",
|
||||
"Core/configs/modules/mod_x.conf",
|
||||
"Core/configs/modules/mod_x.conf.dist",
|
||||
"Core/configs/modules/mod_ascension_compat.conf",
|
||||
"Settings/repack.json",
|
||||
"Settings/worldserver.conf.template",
|
||||
"Settings/realm-profile.json",
|
||||
] {
|
||||
let p = old.join(rel);
|
||||
fs::create_dir_all(p.parent().unwrap()).unwrap();
|
||||
fs::write(p, b"x").unwrap();
|
||||
}
|
||||
let mut got = settings_files(old);
|
||||
got.sort();
|
||||
assert_eq!(got, vec![
|
||||
"Core/configs/modules/mod_x.conf".to_string(),
|
||||
"Settings/realm-profile.json".to_string(),
|
||||
]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_folder_without_a_core_is_not_classic() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
assert!(!is_classic(dir.path()));
|
||||
}
|
||||
}
|
||||
@@ -711,6 +711,17 @@ pub fn ensure_bot_exclusivity(root: &Path) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Dungeon Clear is part of SQUID's Playerbots (the plugin core builds it into the SQUID DLL): it goes on
|
||||
/// with SQUID, and off with it, wherever its config exists.
|
||||
fn switch_on_dungeon_clear_with_squid(cat: &[Entry], root: &Path, meta: &Path) {
|
||||
if let Some(dc) = cat.iter().find(|entry| entry.id == "dungeon-clear") {
|
||||
let name = conf_name(root, dc);
|
||||
if dir(root).join(&name).exists() || dir(root).join(format!("{name}.dist")).exists() {
|
||||
let _ = set_enabled_in(cat, root, meta, "dungeon-clear", true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_enabled_in(cat: &[Entry], root: &Path, meta: &Path, id: &str, on: bool) -> Result<()> {
|
||||
match entry_in(cat, id) {
|
||||
Ok(e) => {
|
||||
@@ -770,6 +781,7 @@ fn set_enabled_in(cat: &[Entry], root: &Path, meta: &Path, id: &str, on: bool) -
|
||||
}
|
||||
if id == "playerbots" && on {
|
||||
let _ = crate::plugins::record_squid_migrations_applied(root);
|
||||
switch_on_dungeon_clear_with_squid(cat, root, meta);
|
||||
}
|
||||
|
||||
tracing::info!(module = id, on, "module switched");
|
||||
@@ -806,6 +818,9 @@ fn set_enabled_in(cat: &[Entry], root: &Path, meta: &Path, id: &str, on: bool) -
|
||||
let _ = set_enabled_in(cat, root, meta, "dungeon-clear", false);
|
||||
}
|
||||
}
|
||||
if id == "playerbots" && on {
|
||||
switch_on_dungeon_clear_with_squid(cat, root, meta);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
@@ -1255,7 +1270,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dungeon_clear_requires_squid_and_stays_off_after_squid_is_reenabled() {
|
||||
fn dungeon_clear_requires_squid_and_follows_it() {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
let root = d.path();
|
||||
fs::create_dir_all(dir(root)).unwrap();
|
||||
@@ -1266,13 +1281,13 @@ mod tests {
|
||||
let cat = catalog();
|
||||
assert!(set_enabled_in(&cat, root, root, "dungeon-clear", true).is_err());
|
||||
assert!(!list_in(&cat, root).iter().find(|entry| entry.id == "dungeon-clear").unwrap().switchable);
|
||||
// part of SQUID: on with it, off with it, on again with it
|
||||
set_enabled_in(&cat, root, root, "playerbots", true).unwrap();
|
||||
set_enabled_in(&cat, root, root, "dungeon-clear", true).unwrap();
|
||||
assert!(read(&dungeon).unwrap().get("DungeonClear.Enable").map(truthy).unwrap());
|
||||
set_enabled_in(&cat, root, root, "playerbots", false).unwrap();
|
||||
assert!(!read(&dungeon).unwrap().get("DungeonClear.Enable").map(truthy).unwrap());
|
||||
set_enabled_in(&cat, root, root, "playerbots", true).unwrap();
|
||||
assert!(!read(&dungeon).unwrap().get("DungeonClear.Enable").map(truthy).unwrap());
|
||||
assert!(read(&dungeon).unwrap().get("DungeonClear.Enable").map(truthy).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -259,9 +259,32 @@ fn split_repo(repo: &str) -> std::result::Result<(&str, &str), String> {
|
||||
.ok_or_else(|| format!("invalid repository '{repo}'"))
|
||||
}
|
||||
|
||||
/// Mark a server that starts with the bare core: the modules the core recommends are not installed on it
|
||||
/// (the owner adds modules from the Modules page). Taken into account the first time a plugin-host core
|
||||
/// arrives; after that every recommended module counts as dealt with.
|
||||
pub fn decline_recommended(realm_root: &Path) -> Result<()> {
|
||||
let body = serde_json::json!({ "schema": 1, "handled": load_handled(realm_root), "declineAll": true });
|
||||
fs::create_dir_all(plugins::meta_dir(realm_root))?;
|
||||
crate::fsx::atomic_write(&handled_path(realm_root), serde_json::to_string_pretty(&body)?.as_bytes())
|
||||
}
|
||||
|
||||
fn declined_all(realm_root: &Path) -> bool {
|
||||
fs::read(handled_path(realm_root)).ok()
|
||||
.and_then(|b| serde_json::from_slice::<serde_json::Value>(&b).ok())
|
||||
.and_then(|v| v.get("declineAll")?.as_bool())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn install_recommended(realm_root: &Path, progress: &dyn Fn(&str, &str)) -> Vec<ModuleRebuild> {
|
||||
let Some(rec) = recommended(realm_root) else { return Vec::new() };
|
||||
let mut handled = load_handled(realm_root);
|
||||
if declined_all(realm_root) {
|
||||
for m in &rec.modules {
|
||||
handled.entry(m.id.clone()).or_insert_with(|| "not installed: new server with the bare core".into());
|
||||
}
|
||||
save_handled(realm_root, &handled); // drops the mark: it applies to this first core only
|
||||
return Vec::new();
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
for m in &rec.modules {
|
||||
let registry = plugins::load_registry(realm_root).unwrap_or_default();
|
||||
@@ -455,6 +478,25 @@ mod tests {
|
||||
assert!(!handled.contains_key("companions"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_new_server_with_the_bare_core_installs_no_recommended_module() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let server = dir.path();
|
||||
fs::create_dir_all(server.join("Core/configs/modules")).unwrap();
|
||||
fs::create_dir_all(server.join("Core/plugins")).unwrap();
|
||||
fs::write(server.join("Core/worldserver.exe"), image("worldserver", &"c".repeat(64))).unwrap();
|
||||
decline_recommended(server).unwrap();
|
||||
// the mark is written before the plugin-host core (and its recommended.json) arrives
|
||||
fs::write(server.join("Core/plugins/recommended.json"), r#"{"schema":1,"modules":[
|
||||
{"id":"companions","repo":"not-a-repo","commit":"0000000000000000000000000000000000000000"}]}"#).unwrap();
|
||||
let out = rebuild_for_current_core(server, &|_, _| panic!("nothing is built")).unwrap();
|
||||
assert!(out.is_empty(), "{out:?}");
|
||||
assert!(load_handled(server)["companions"].contains("bare core"));
|
||||
assert!(!declined_all(server), "the mark applies to the first plugin-host core only");
|
||||
// a later core change does not bring it back either
|
||||
assert!(rebuild_for_current_core(server, &|_, _| panic!("nothing is built")).unwrap().is_empty());
|
||||
}
|
||||
|
||||
/// Real run: COA_TEST_RECOMMENDED_REALM is a plugin-host realm with Core/plugins/recommended.json (or
|
||||
/// plugins/recommended.json); the modules are downloaded from GitHub, built and switched on.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user