mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-19 19:25:56 +00:00
Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1338ca56e8 | ||
|
|
e3fbb2e4b6 | ||
|
|
a7b56d6aed | ||
|
|
cda7875a9a | ||
|
|
f53c7cb221 | ||
|
|
ca6bcf0052 | ||
|
|
d6a6384f9c | ||
|
|
21ddc1e7d8 |
No files matched your search
@@ -29,3 +29,22 @@ Optional: one short paragraph in plain English for the reader (at most 300 chara
|
|||||||
`python changelog.py new --area bots --type fixed --audience players --title "..."`.
|
`python changelog.py new --area bots --type fixed --audience players --title "..."`.
|
||||||
- Full guide with examples: https://github.com/Corfirean/coa-server-build/blob/main/changelog/GUIDE.md
|
- Full guide with examples: https://github.com/Corfirean/coa-server-build/blob/main/changelog/GUIDE.md
|
||||||
<!-- changelog:end -->
|
<!-- changelog:end -->
|
||||||
|
|
||||||
|
## Shared build and release workflow
|
||||||
|
|
||||||
|
Before preparing or publishing a Manager/server release, read the maintained
|
||||||
|
[release runbook](https://github.com/Corfirean/coa-server-build/blob/main/docs/server-release.md)
|
||||||
|
and [build agent instructions](https://github.com/Corfirean/coa-server-build/blob/main/AGENTS.md).
|
||||||
|
Use the local build checkout when available; otherwise retrieve the current files from GitHub.
|
||||||
|
|
||||||
|
Recover the exact prepared run, source commit, version, hashes and artifact expiry before rebuilding.
|
||||||
|
Prepare-only is not publication permission. A normal owner command to release the identified candidate
|
||||||
|
includes its required stable promotion; do not stop at a server Pre-release or ask twice for the same action.
|
||||||
|
Reuse signed prepared Manager installers after verifying their source metadata, SHA-256, updater JSON
|
||||||
|
and signature against the matching source's updater public key. Do not rerun compilation just to upload
|
||||||
|
already verified assets, or overwrite a published version with different bytes.
|
||||||
|
|
||||||
|
Server promotion uses the persistent Windows PC and the shared workflow's verified caches. Preserve
|
||||||
|
production databases; run applicable local acceptance in disposable fixtures. Record the actual passed,
|
||||||
|
failed and unrun checks. Verify both GitHub Latest and the public signed stable pointer before reporting
|
||||||
|
that users can update. Leave the durable handoff described in the runbook for the next agent.
|
||||||
Generated
+3
-3
@@ -491,7 +491,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "coa-core"
|
name = "coa-core"
|
||||||
version = "0.6.18"
|
version = "0.6.21"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64 0.22.1",
|
"base64 0.22.1",
|
||||||
"chrono",
|
"chrono",
|
||||||
@@ -520,7 +520,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "coa-release"
|
name = "coa-release"
|
||||||
version = "0.6.18"
|
version = "0.6.21"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"coa-core",
|
"coa-core",
|
||||||
@@ -529,7 +529,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "coa-server-manager"
|
name = "coa-server-manager"
|
||||||
version = "0.6.18"
|
version = "0.6.21"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"coa-core",
|
"coa-core",
|
||||||
"serde",
|
"serde",
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@ resolver = "2"
|
|||||||
members = ["crates/coa-core", "crates/coa-release", "src-tauri"]
|
members = ["crates/coa-core", "crates/coa-release", "src-tauri"]
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
version = "0.6.18"
|
version = "0.6.21"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "AGPL-3.0-only"
|
license = "AGPL-3.0-only"
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
area: manager
|
||||||
|
type: changed
|
||||||
|
audience: admins
|
||||||
|
title: Server updates apply migrations once after saving a recovery point
|
||||||
|
---
|
||||||
|
Updates no longer build and start a second full server copy. The Manager checks both realms, saves recovery, applies pending SQL once and verifies startup. Failed SQL or startup restores the recovery point. Full-copy upgrade testing remains a release task.
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
area: manager
|
||||||
|
type: changed
|
||||||
|
audience: admins
|
||||||
|
title: Older server versions update directly without installing intermediate packages
|
||||||
|
---
|
||||||
|
The Manager checks database history and applies missing SQL from the latest signed package. A version missing from the release test list no longer blocks the update. Unknown versions, interrupted migrations and downgrades remain blocked.
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
area: manager
|
||||||
|
type: fixed
|
||||||
|
audience: admins
|
||||||
|
title: Update validation recognizes an already stopped server
|
||||||
|
---
|
||||||
|
Validation uses the same complete-server stop check as recovery instead of issuing redundant unconditional shutdown commands.
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
area: manager
|
||||||
|
type: fixed
|
||||||
|
audience: admins
|
||||||
|
title: Update checks no longer download a server archive to compare its launcher
|
||||||
|
---
|
||||||
|
Manager recognizes its supported launcher edits against the signed file hash locally. Overlapping checks wait briefly for one another, and the private update copy checks combined program and database space before copying.
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
area: manager
|
||||||
|
type: fixed
|
||||||
|
audience: admins
|
||||||
|
title: Recorded core SQL stays applied after old history tables are cleaned up
|
||||||
|
---
|
||||||
|
Existing core migration records are preserved in Manager history. Unknown legacy checksums remain explicitly unknown, and interrupted or changed migrations are still rejected.
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
area: manager
|
||||||
|
type: fixed
|
||||||
|
audience: admins
|
||||||
|
title: Switching realms no longer repeats shared account database updates
|
||||||
|
---
|
||||||
|
Solo and Wildcard use one migration history for their shared account database. Previously applied records from either realm are recognized without running their SQL again.
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
area: manager
|
||||||
|
type: fixed
|
||||||
|
audience: admins
|
||||||
|
title: Update offers distinguish version support from database validation
|
||||||
|
---
|
||||||
|
An available version sequence no longer claims that every database state has been tested. Database compatibility is checked during the update.
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
//! Read-only audit, or explicit SQL checks on a disposable database fixture.
|
||||||
|
use coa_core::{db::{Account, Db}, migrations::{self, Store}, manifest::Manifest, realms::Mode};
|
||||||
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let args: Vec<_> = std::env::args().collect();
|
||||||
|
if args.len() < 3 { return Err("usage: migration_plan <root> <signed-manifest> [--apply|--sql-only <migration-directory>]".into()); }
|
||||||
|
let root = std::path::Path::new(&args[1]);
|
||||||
|
let bytes = std::fs::read(&args[2])?;
|
||||||
|
let signature = std::fs::read_to_string(format!("{}.sig",args[2]))?;
|
||||||
|
coa_core::signing::verify(&bytes, &signature, coa_core::signing::EMBEDDED_PUBLIC_KEY)?;
|
||||||
|
let manifest = Manifest::parse(&bytes)?;
|
||||||
|
for (index,mode) in [Mode::Coa, Mode::Wildcard].into_iter().enumerate() {
|
||||||
|
let db = Db::from_repack(root, Account::Admin)?.for_realm(mode);
|
||||||
|
let rows = db.load_readonly()?;
|
||||||
|
let relevant: Vec<_> = rows.iter().filter(|r| r.id == "rev_20260903_01_live_class_baseline").map(|r| format!("{} {:?} {}",r.db,r.status,r.baseline)).collect();
|
||||||
|
println!("{} baseline history: {:?}",mode.name(),relevant);
|
||||||
|
let list: Vec<_> = manifest.migrations.iter().filter(|m|index==0 || m.db!="auth").cloned().collect();
|
||||||
|
let plan = migrations::status(&db,&list)?;
|
||||||
|
println!("{} pending: {:?}",mode.name(),plan.iter().filter(|m|m.status!=migrations::Status::Applied).map(|m|(&m.db,&m.id,m.status)).collect::<Vec<_>>());
|
||||||
|
}
|
||||||
|
if args.get(3).is_some_and(|s| s == "--apply" || s == "--sql-only") {
|
||||||
|
assert!(root.file_name().unwrap().to_string_lossy().starts_with("update-database-diagnosis-") && root.join(".coa-update-fixture").is_file(), "Only disposable fixture roots may be changed");
|
||||||
|
if args[3] == "--sql-only" {
|
||||||
|
let db = Db::from_repack(root, Account::Admin)?.for_realm(Mode::Coa);
|
||||||
|
let identity = || -> coa_core::Result<String> {
|
||||||
|
let mut rows = String::new();
|
||||||
|
for (schema,table,columns) in [("acore_auth","account","id,HEX(username)"),("acore_characters","characters","guid,account,HEX(name)"),("acore_characters_wildcard","characters","guid,account,HEX(name)")] {
|
||||||
|
rows.push_str(schema);
|
||||||
|
rows.push_str(&db.query(&format!("SELECT {columns} FROM {schema}.{table} ORDER BY 1;"))?);
|
||||||
|
}
|
||||||
|
Ok(coa_core::fsx::sha256_bytes(rows.as_bytes()))
|
||||||
|
};
|
||||||
|
let before = identity()?;
|
||||||
|
migrations::verify_files(&manifest.migrations,std::path::Path::new(&args[4]))?;
|
||||||
|
for (index,mode) in [Mode::Coa,Mode::Wildcard].into_iter().enumerate() {
|
||||||
|
let realm_db = db.clone().for_realm(mode);
|
||||||
|
let list: Vec<_> = manifest.migrations.iter().filter(|m|index==0 || m.db!="auth").cloned().collect();
|
||||||
|
let result = migrations::apply_pending(&realm_db,&list,std::path::Path::new(&args[4]),&||Ok("disposable fixture".into()))?;
|
||||||
|
println!("{} SQL result: {}",mode.name(),serde_json::to_string(&result)?);
|
||||||
|
if result.failed.is_some() {return Err("SQL failed; fixture preserved".into());}
|
||||||
|
}
|
||||||
|
assert_eq!(before,identity()?,"Account or character identity changed");
|
||||||
|
println!("Account and character identities preserved: {}",before);
|
||||||
|
let capture = root.join("captured-schema");
|
||||||
|
coa_core::schema_check::capture(&db,&capture)?;
|
||||||
|
println!("Captured schema SHA256: {}",coa_core::fsx::sha256_file(&capture.join(coa_core::schema_check::CONTRACT))?);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
if args.len() != 5 { return Err("Migration directory required".into()); }
|
||||||
|
use coa_core::update::{Env, RepackEnv};
|
||||||
|
let meta = coa_core::registry::metadata_dir_for(root)?;
|
||||||
|
let env = RepackEnv { root, meta_dir: &meta };
|
||||||
|
let result = env.migrate(&manifest, std::path::Path::new(&args[4]));
|
||||||
|
let _ = coa_core::driver::run(root, coa_core::driver::Verb::StopAll);
|
||||||
|
println!("Application result: {}",serde_json::to_string(&result?)?);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
//! Exercise the production updater only on a disposable copy of an installed server.
|
||||||
|
use coa_core::{registry::MetaDir, update::{self, Env, RepackEnv}};
|
||||||
|
fn main() -> Result<(),Box<dyn std::error::Error>> {
|
||||||
|
let args:Vec<_>=std::env::args().collect();
|
||||||
|
if args.len()!=3 && args.len()!=4 {return Err("usage: upgrade_fixture <disposable-root> <signed-package-directory>".into());}
|
||||||
|
let root=std::path::Path::new(&args[1]);
|
||||||
|
assert!(root.file_name().unwrap().to_string_lossy().starts_with("coa-schema-fixture-real") && root.join(".coa-update-fixture").is_file(),"Disposable fixture required");
|
||||||
|
let meta=coa_core::registry::metadata_dir_for(root)?;
|
||||||
|
let (_,installed)=MetaDir::open(&meta)?;
|
||||||
|
assert_eq!(coa_core::fsx::canonicalize_lenient(std::path::Path::new(&installed.server_path))?,coa_core::fsx::canonicalize_lenient(root)?,"Fixture metadata must point to the copy");
|
||||||
|
assert!(installed.client_path.is_none(),"Detach the real client from fixture metadata");
|
||||||
|
let env=RepackEnv{root,meta_dir:&meta};
|
||||||
|
if args.get(3).is_some_and(|flag|flag=="--validate-only") {
|
||||||
|
let result=env.validate();
|
||||||
|
let _=coa_core::driver::run(root,coa_core::driver::Verb::StopAll);
|
||||||
|
result?;
|
||||||
|
println!("Startup and identity validation passed");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let outcome=update::apply(&update::Params{root,meta_dir:&meta,source:coa_core::pkgsource::Source::Dir(args[2].clone().into()),trusted_key:coa_core::signing::EMBEDDED_PUBLIC_KEY,cancel:Default::default(),resolutions:Default::default(),env:&env,fail_after_ops:None},&|phase,percent|println!("{percent}% {phase}"));
|
||||||
|
let stopped=coa_core::driver::run(root,coa_core::driver::Verb::StopAll);
|
||||||
|
println!("Shutdown after test: {:?}",stopped.map(|r|r.ok));
|
||||||
|
let outcome=outcome?;
|
||||||
|
println!("Final transaction: {:?}",outcome.txn.state);
|
||||||
|
env.ensure_stopped()?;
|
||||||
|
assert_eq!(outcome.txn.state,update::State::Committed);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -93,6 +93,7 @@ pub struct Compatibility {
|
|||||||
pub core_commit: String,
|
pub core_commit: String,
|
||||||
pub module_commits: std::collections::BTreeMap<String, String>,
|
pub module_commits: std::collections::BTreeMap<String, String>,
|
||||||
pub client_patch_version: String,
|
pub client_patch_version: String,
|
||||||
|
/// Sources exercised during release qualification, not an exclusive updater allowlist.
|
||||||
pub source_versions: Vec<String>,
|
pub source_versions: Vec<String>,
|
||||||
#[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
|
#[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
|
||||||
pub source_databases: std::collections::BTreeMap<String, SourceDatabase>,
|
pub source_databases: std::collections::BTreeMap<String, SourceDatabase>,
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ use crate::fsx;
|
|||||||
use crate::manifest::Migration;
|
use crate::manifest::Migration;
|
||||||
|
|
||||||
const LEDGER_SCHEMA: &str = "acore_world";
|
const LEDGER_SCHEMA: &str = "acore_world";
|
||||||
|
const AUTH_LEDGER_SCHEMA: &str = "acore_auth";
|
||||||
const LEDGER_TABLE: &str = "coa_manager_migrations";
|
const LEDGER_TABLE: &str = "coa_manager_migrations";
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
|
||||||
@@ -37,6 +38,10 @@ pub trait Store {
|
|||||||
fn load(&self) -> Result<Vec<LedgerRow>>;
|
fn load(&self) -> Result<Vec<LedgerRow>>;
|
||||||
fn load_readonly(&self) -> Result<Vec<LedgerRow>> { self.load() }
|
fn load_readonly(&self) -> Result<Vec<LedgerRow>> { self.load() }
|
||||||
fn put(&self, row: &LedgerRow) -> Result<()>;
|
fn put(&self, row: &LedgerRow) -> Result<()>;
|
||||||
|
fn put_batch(&self, rows: &[LedgerRow]) -> Result<()> {
|
||||||
|
for row in rows { self.put(row)?; }
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
fn run_file(&self, schema: &str, file: &Path) -> Result<()>;
|
fn run_file(&self, schema: &str, file: &Path) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,59 +53,101 @@ fn ident_ok(s: &str) -> bool {
|
|||||||
!s.is_empty() && s.len() <= 150 && s.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | '-'))
|
!s.is_empty() && s.len() <= 150 && s.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | '-'))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn shared_auth(kind: &str) -> bool {
|
||||||
|
matches!(db::schema_of(kind), Ok("acore_auth"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_ledger(db: &Db, schema: &str) -> Result<()> {
|
||||||
|
db.query(&format!("CREATE TABLE IF NOT EXISTS `{schema}`.`{LEDGER_TABLE}` (`db` VARCHAR(32) NOT NULL, `id` VARCHAR(190) NOT NULL, `sha256` CHAR(64) NOT NULL, `status` VARCHAR(16) NOT NULL, `applied_at` DATETIME NOT NULL, `error` TEXT NULL, `baseline` TINYINT NOT NULL DEFAULT 0, PRIMARY KEY (`db`,`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
impl Store for Db {
|
impl Store for Db {
|
||||||
fn load(&self) -> Result<Vec<LedgerRow>> {
|
fn load(&self) -> Result<Vec<LedgerRow>> {
|
||||||
self.query(&format!(
|
create_ledger(self, LEDGER_SCHEMA)?;
|
||||||
"CREATE TABLE IF NOT EXISTS `{LEDGER_SCHEMA}`.`{LEDGER_TABLE}` (`db` VARCHAR(32) NOT NULL, `id` VARCHAR(190) NOT NULL, `sha256` CHAR(64) NOT NULL, `status` VARCHAR(16) NOT NULL, `applied_at` DATETIME NOT NULL, `error` TEXT NULL, `baseline` TINYINT NOT NULL DEFAULT 0, PRIMARY KEY (`db`,`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
|
create_ledger(self, AUTH_LEDGER_SCHEMA)?;
|
||||||
))?;
|
let rows = self.load_readonly()?;
|
||||||
self.load_readonly()
|
// Preserve already-recorded core history even if a module later replaces its updates table.
|
||||||
|
// A zero checksum remains unknown; importing it does not invent a checksum for old SQL.
|
||||||
|
let selected: std::collections::BTreeMap<_, _> = rows.iter().map(|row| ((row.db.clone(), row.id.to_ascii_lowercase()), row)).collect();
|
||||||
|
let core: Vec<_> = selected.values().filter(|r| r.status == Status::Applied && r.baseline && r.sha256 == "0".repeat(64)
|
||||||
|
&& matches!(r.db.as_str(), "world" | "characters") && ident_ok(&r.id)).map(|r| (*r).clone()).collect();
|
||||||
|
self.put_batch(&core)?;
|
||||||
|
Ok(rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_readonly(&self) -> Result<Vec<LedgerRow>> {
|
fn load_readonly(&self) -> Result<Vec<LedgerRow>> {
|
||||||
let out = if self.tables(LEDGER_SCHEMA)?.iter().any(|name| name == LEDGER_TABLE) {
|
// Resolve physical schemas once, then read all history in two client calls.
|
||||||
self.query(&format!("SELECT `db`,`id`,`sha256`,`status`,`baseline`,COALESCE(HEX(`error`),'') FROM `{LEDGER_SCHEMA}`.`{LEDGER_TABLE}` ORDER BY `db`,`id`;"))?
|
// Using a neutral connection prevents a Wildcard query from rerouting the other realm.
|
||||||
} else { String::new() };
|
let connection = self.clone().for_realm(crate::realms::Mode::Coa);
|
||||||
let mut rows = Vec::new();
|
let world = self.realm_schema("acore_world");
|
||||||
// Files the core's own updater (or whoever prepared this database) already recorded count as applied.
|
let characters = self.realm_schema("acore_characters");
|
||||||
for (kind, schema) in db::SCHEMAS {
|
let other = if self.realm() == crate::realms::Mode::Coa { "acore_world_wildcard" } else { "acore_world" };
|
||||||
if self.tables(schema)?.iter().any(|name| name == "updates") {
|
let schemas: std::collections::BTreeSet<_> = [world, characters, other, AUTH_LEDGER_SCHEMA].into_iter().collect();
|
||||||
let names = self.query(&format!("SELECT name FROM `{schema}`.`updates`;"))?;
|
let catalog = connection.query(&format!("SELECT table_schema,table_name FROM information_schema.tables WHERE table_schema IN ('{}') AND table_name IN ('updates','{LEDGER_TABLE}') AND table_type='BASE TABLE';", schemas.into_iter().collect::<Vec<_>>().join("','")))?;
|
||||||
for name in names.lines().filter(|n| n.ends_with(".sql")) {
|
let available: std::collections::BTreeSet<_> = catalog.lines().filter_map(|line| line.split_once('\t')).collect();
|
||||||
rows.push(LedgerRow { db: kind.into(), id: name.trim_end_matches(".sql").into(), sha256: "0".repeat(64), status: Status::Applied, error: None, baseline: true });
|
let text = |expression: &str| format!("CONVERT({expression} USING utf8mb4) COLLATE utf8mb4_bin");
|
||||||
}
|
let mut queries = Vec::new();
|
||||||
|
for (kind, schema) in [("characters", characters), ("auth", AUTH_LEDGER_SCHEMA), ("world", world)] {
|
||||||
|
if available.contains(&(schema, "updates")) {
|
||||||
|
queries.push(format!("SELECT 'core','{kind}',{},REPEAT('0',64),'applied',1,'' FROM `{schema}`.`updates` WHERE BINARY RIGHT(name,4)='.sql'",text("LEFT(name,CHAR_LENGTH(name)-4)")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for line in out.lines().filter(|l| !l.is_empty()) {
|
for (origin, schema, filter) in [("local",world,""), ("other",other," WHERE `db` IN ('auth','coa-auth','wildcard-auth')"), ("shared",AUTH_LEDGER_SCHEMA,"")] {
|
||||||
let f: Vec<&str> = line.split('\t').collect();
|
if available.contains(&(schema, LEDGER_TABLE)) {
|
||||||
if f.len() < 5 {
|
queries.push(format!("SELECT '{origin}',{},{},{},{},`baseline`,{} FROM `{schema}`.`{LEDGER_TABLE}`{filter}",text("`db`"),text("`id`"),text("`sha256`"),text("`status`"),text("COALESCE(HEX(`error`),'')")));
|
||||||
return Err(Error::Invalid("The database migration history is malformed; no SQL was replayed.".into()));
|
|
||||||
}
|
}
|
||||||
if !matches!(f[3], "applied" | "failed" | "running") || !ident_ok(f[1]) || f[2].len() != 64 || !f[2].chars().all(|c| c.is_ascii_hexdigit()) {
|
}
|
||||||
|
if queries.is_empty() { return Ok(vec![]); }
|
||||||
|
let out = connection.query(&queries.join(" UNION ALL "))?;
|
||||||
|
let (mut rows, mut local, mut auth, mut shared) = (Vec::new(), Vec::new(), Vec::new(), Vec::new());
|
||||||
|
for line in out.lines().filter(|l| !l.is_empty()) {
|
||||||
|
let f: Vec<_> = line.split('\t').collect();
|
||||||
|
if f.len() < 6 { return Err(Error::Invalid("The database migration history is malformed; no SQL was replayed.".into())); }
|
||||||
|
// Unrelated legacy filenames cannot match a valid manifest migration ID.
|
||||||
|
if f[0] == "core" && !ident_ok(f[2]) { continue; }
|
||||||
|
if !matches!(f[4], "applied" | "failed" | "running") || !ident_ok(f[2]) || f[3].len() != 64 || !f[3].chars().all(|c| c.is_ascii_hexdigit()) {
|
||||||
return Err(Error::Invalid("The database migration history is invalid; no SQL was replayed.".into()));
|
return Err(Error::Invalid("The database migration history is invalid; no SQL was replayed.".into()));
|
||||||
}
|
}
|
||||||
let err = f.get(5).filter(|h| !h.is_empty()).and_then(|h| hex::decode(h).ok()).map(|b| String::from_utf8_lossy(&b).into_owned());
|
let error = f.get(6).filter(|h| !h.is_empty()).and_then(|h| hex::decode(h).ok()).map(|b| String::from_utf8_lossy(&b).into_owned());
|
||||||
rows.push(LedgerRow {
|
let row = LedgerRow { db:f[1].into(), id:f[2].into(), sha256:f[3].into(), status:match f[4] { "applied"=>Status::Applied,"running"=>Status::Running,_=>Status::Failed }, baseline:f[5]=="1", error };
|
||||||
db: f[0].into(),
|
match f[0] {
|
||||||
id: f[1].into(),
|
"core" => rows.push(row),
|
||||||
sha256: f[2].into(),
|
"local" if !shared_auth(&row.db) => local.push(row),
|
||||||
status: match f[3] { "applied" => Status::Applied, "running" => Status::Running, _ => Status::Failed },
|
"local" | "other" => auth.push(row),
|
||||||
baseline: f[4] == "1",
|
"shared" => shared.push(row),
|
||||||
error: err,
|
_ => return Err(Error::Invalid("Unknown migration history source.".into())),
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
rows.extend(local);
|
||||||
|
// An interrupted legacy auth execution must not be hidden by another realm's applied row.
|
||||||
|
auth.sort_by_key(|r| (match r.status { Status::Applied=>0,Status::Running=>1,Status::Failed=>2,Status::Pending=>3 },r.sha256!="0".repeat(64)));
|
||||||
|
for mut row in auth { row.db="auth".into(); rows.push(row); }
|
||||||
|
// New auth records live with the shared account database and are authoritative.
|
||||||
|
for mut row in shared {
|
||||||
|
if !shared_auth(&row.db) { return Err(Error::Invalid("The shared authentication migration history is invalid.".into())); }
|
||||||
|
rows.retain(|r| !(shared_auth(&r.db) && r.id.eq_ignore_ascii_case(&row.id)));
|
||||||
|
row.db="auth".into(); rows.push(row);
|
||||||
}
|
}
|
||||||
Ok(rows)
|
Ok(rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put(&self, r: &LedgerRow) -> Result<()> {
|
fn put(&self, row: &LedgerRow) -> Result<()> { self.put_batch(std::slice::from_ref(row)) }
|
||||||
if !ident_ok(&r.id) || !ident_ok(&r.db) || r.sha256.len() != 64 || !r.sha256.chars().all(|c| c.is_ascii_hexdigit()) {
|
|
||||||
return Err(Error::Invalid("migration record has an invalid identifier".into()));
|
fn put_batch(&self, rows: &[LedgerRow]) -> Result<()> {
|
||||||
|
let mut groups = std::collections::BTreeMap::<&str, Vec<String>>::new();
|
||||||
|
for r in rows {
|
||||||
|
if !ident_ok(&r.id) || !ident_ok(&r.db) || r.sha256.len() != 64 || !r.sha256.chars().all(|c| c.is_ascii_hexdigit()) {
|
||||||
|
return Err(Error::Invalid("migration record has an invalid identifier".into()));
|
||||||
|
}
|
||||||
|
let status = match r.status { Status::Applied => "applied", Status::Running => "running", _ => "failed" };
|
||||||
|
let error = r.error.as_deref().map(|e| format!("UNHEX('{}')", hex_of(e))).unwrap_or_else(|| "NULL".into());
|
||||||
|
let (schema, kind) = if shared_auth(&r.db) { (AUTH_LEDGER_SCHEMA, "auth") } else { (LEDGER_SCHEMA, r.db.as_str()) };
|
||||||
|
groups.entry(schema).or_default().push(format!("('{}','{}','{}','{status}',NOW(),{error},{})",kind,r.id,r.sha256,r.baseline as u8));
|
||||||
|
}
|
||||||
|
for (schema, values) in groups {
|
||||||
|
self.query(&format!("REPLACE INTO `{schema}`.`{LEDGER_TABLE}` (`db`,`id`,`sha256`,`status`,`applied_at`,`error`,`baseline`) VALUES {};",values.join(",")))?;
|
||||||
}
|
}
|
||||||
let status = match r.status { Status::Applied => "applied", Status::Running => "running", _ => "failed" };
|
|
||||||
let err = r.error.as_deref().map(|e| format!("UNHEX('{}')", hex_of(e))).unwrap_or_else(|| "NULL".into());
|
|
||||||
self.query(&format!(
|
|
||||||
"REPLACE INTO `{LEDGER_SCHEMA}`.`{LEDGER_TABLE}` (`db`,`id`,`sha256`,`status`,`applied_at`,`error`,`baseline`) VALUES ('{}','{}','{}','{status}',NOW(),{err},{});",
|
|
||||||
r.db, r.id, r.sha256, r.baseline as u8
|
|
||||||
))?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,21 +184,34 @@ pub fn status(store: &dyn Store, list: &[Migration]) -> Result<Vec<Item>> {
|
|||||||
.map(|m| {
|
.map(|m| {
|
||||||
// Manager records are appended after the core's updates table and are authoritative.
|
// Manager records are appended after the core's updates table and are authoritative.
|
||||||
let row = recorded(&rows, m);
|
let row = recorded(&rows, m);
|
||||||
let changed = row.is_some_and(|r| hash_changed(r, m));
|
let history = effective_history(&rows, m);
|
||||||
|
let changed = history.iter().any(|r| hash_changed(r,m));
|
||||||
|
let interrupted = history.iter().find(|r| matches!(r.status, Status::Running | Status::Failed));
|
||||||
Item {
|
Item {
|
||||||
id: m.id.clone(),
|
id: m.id.clone(),
|
||||||
db: m.db.clone(),
|
db: m.db.clone(),
|
||||||
status: if changed || row.is_some_and(|r| r.status == Status::Running) { Status::Failed } else { row.map(|r| r.status).unwrap_or(Status::Pending) },
|
status: if changed || interrupted.is_some() { Status::Failed } else { row.map(|r| r.status).unwrap_or(Status::Pending) },
|
||||||
destructive: m.destructive,
|
destructive: m.destructive,
|
||||||
baseline: row.map(|r| r.baseline).unwrap_or(false),
|
baseline: row.map(|r| r.baseline).unwrap_or(false),
|
||||||
error: if changed { Some("An already applied SQL file changed. Publish a new migration instead of replaying it.".into()) } else { row.and_then(|r| r.error.clone()) },
|
error: if changed { Some("An already applied SQL file changed. Publish a new migration instead of replaying it.".into()) } else { interrupted.and_then(|r| r.error.clone()).or_else(|| row.and_then(|r| r.error.clone())) },
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn recorded<'a>(rows: &'a [LedgerRow], m: &Migration) -> Option<&'a LedgerRow> {
|
fn recorded<'a>(rows: &'a [LedgerRow], m: &Migration) -> Option<&'a LedgerRow> {
|
||||||
rows.iter().rev().find(|r| r.db == m.db && r.id == m.id)
|
rows.iter().rev().find(|r| (r.db == m.db || shared_auth(&r.db) && shared_auth(&m.db)) && r.id.eq_ignore_ascii_case(&m.id))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn effective_history<'a>(rows: &'a [LedgerRow], m: &Migration) -> Vec<&'a LedgerRow> {
|
||||||
|
if shared_auth(&m.db) { rows.iter().filter(|r| shared_auth(&r.db) && r.id.eq_ignore_ascii_case(&m.id)).collect() }
|
||||||
|
else { recorded(rows, m).into_iter().collect() }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn preserve_auth_history(store: &dyn Store, rows: &[LedgerRow], list: &[Migration]) -> Result<()> {
|
||||||
|
let applied: Vec<_> = list.iter().filter(|m| shared_auth(&m.db)).filter_map(|m| recorded(rows,m))
|
||||||
|
.filter(|r| r.status == Status::Applied).cloned().collect();
|
||||||
|
store.put_batch(&applied)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Migrations that were published with different bytes but have the same effect, so a server that applied either
|
/// Migrations that were published with different bytes but have the same effect, so a server that applied either
|
||||||
@@ -166,7 +226,7 @@ const SAME_EFFECT: &[(&str, &[&str])] = &[(
|
|||||||
)];
|
)];
|
||||||
|
|
||||||
fn same_effect(id: &str, a: &str, b: &str) -> bool {
|
fn same_effect(id: &str, a: &str, b: &str) -> bool {
|
||||||
SAME_EFFECT.iter().any(|(known, hashes)| *known == id
|
SAME_EFFECT.iter().any(|(known, hashes)| known.eq_ignore_ascii_case(id)
|
||||||
&& hashes.iter().any(|h| h.eq_ignore_ascii_case(a)) && hashes.iter().any(|h| h.eq_ignore_ascii_case(b)))
|
&& hashes.iter().any(|h| h.eq_ignore_ascii_case(a)) && hashes.iter().any(|h| h.eq_ignore_ascii_case(b)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,8 +243,7 @@ pub fn preflight(store: &dyn Store, list: &[Migration]) -> Result<()> {
|
|||||||
|
|
||||||
pub fn pending_count(store: &dyn Store, list: &[Migration]) -> Result<usize> {
|
pub fn pending_count(store: &dyn Store, list: &[Migration]) -> Result<usize> {
|
||||||
let rows = store.load_readonly()?;
|
let rows = store.load_readonly()?;
|
||||||
check_history(&rows, list)?;
|
Ok(pending_migrations(&rows, list)?.len())
|
||||||
Ok(list.iter().filter(|m| recorded(&rows, m).is_none_or(|r| r.status != Status::Applied)).count())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn verified_file(m: &Migration, dir: &Path) -> Result<(&'static str, std::path::PathBuf, bool)> {
|
fn verified_file(m: &Migration, dir: &Path) -> Result<(&'static str, std::path::PathBuf, bool)> {
|
||||||
@@ -199,22 +258,39 @@ fn verified_file(m: &Migration, dir: &Path) -> Result<(&'static str, std::path::
|
|||||||
|
|
||||||
/// Check every migration artifact, including applied history, before replacing server files.
|
/// Check every migration artifact, including applied history, before replacing server files.
|
||||||
pub fn verify_files(list: &[Migration], dir: &Path) -> Result<()> {
|
pub fn verify_files(list: &[Migration], dir: &Path) -> Result<()> {
|
||||||
|
validate_list(list)?;
|
||||||
|
for m in list { verified_file(m, dir)?; }
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_list(list: &[Migration]) -> Result<()> {
|
||||||
let mut seen = std::collections::BTreeSet::new();
|
let mut seen = std::collections::BTreeSet::new();
|
||||||
for m in list {
|
for m in list {
|
||||||
if !seen.insert((&m.db, &m.id)) { return Err(Error::InvalidManifest(format!("duplicate migration {} ({})", m.id, m.db))); }
|
if !ident_ok(&m.id) { return Err(Error::InvalidManifest(format!("migration id {:?} is not allowed", m.id))); }
|
||||||
verified_file(m, dir)?;
|
let schema = db::schema_of(&m.db)?;
|
||||||
|
if !seen.insert((schema, m.id.to_ascii_lowercase())) {
|
||||||
|
return Err(Error::InvalidManifest(format!("duplicate migration {} ({})", m.id, m.db)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn pending_migrations<'a>(rows: &[LedgerRow], list: &'a [Migration]) -> Result<Vec<&'a Migration>> {
|
||||||
|
check_history(rows, list)?;
|
||||||
|
Ok(list.iter().filter(|m| recorded(rows, m).is_none_or(|r| r.status != Status::Applied)).collect())
|
||||||
|
}
|
||||||
|
|
||||||
fn check_history(rows: &[LedgerRow], list: &[Migration]) -> Result<()> {
|
fn check_history(rows: &[LedgerRow], list: &[Migration]) -> Result<()> {
|
||||||
|
validate_list(list)?;
|
||||||
for m in list {
|
for m in list {
|
||||||
if recorded(rows, m).is_some_and(|r| matches!(r.status, Status::Running | Status::Failed)) {
|
for row in effective_history(rows,m) {
|
||||||
|
if matches!(row.status, Status::Running | Status::Failed) {
|
||||||
return Err(Error::Invalid(format!("Database update {} ({}) failed or was interrupted and may be partially applied. Restore its recovery point before retrying.", m.id, m.db)));
|
return Err(Error::Invalid(format!("Database update {} ({}) failed or was interrupted and may be partially applied. Restore its recovery point before retrying.", m.id, m.db)));
|
||||||
}
|
}
|
||||||
if recorded(rows, m).is_some_and(|r| hash_changed(r, m)) {
|
if hash_changed(row,m) {
|
||||||
return Err(Error::Invalid(format!("Applied database update {} ({}) has a different checksum. A new migration is required; it was not replayed.", m.id, m.db)));
|
return Err(Error::Invalid(format!("Applied database update {} ({}) has a different checksum. A new migration is required; it was not replayed.", m.id, m.db)));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -223,9 +299,11 @@ fn check_history(rows: &[LedgerRow], list: &[Migration]) -> Result<()> {
|
|||||||
/// was brought up to date by hand).
|
/// was brought up to date by hand).
|
||||||
pub fn baseline(store: &dyn Store, list: &[Migration]) -> Result<usize> {
|
pub fn baseline(store: &dyn Store, list: &[Migration]) -> Result<usize> {
|
||||||
let rows = store.load()?;
|
let rows = store.load()?;
|
||||||
|
check_history(&rows,list)?;
|
||||||
|
preserve_auth_history(store,&rows,list)?;
|
||||||
let mut n = 0;
|
let mut n = 0;
|
||||||
for m in list {
|
for m in list {
|
||||||
if rows.iter().any(|r| r.db == m.db && r.id == m.id && r.status == Status::Applied) {
|
if recorded(&rows,m).is_some_and(|r| r.status == Status::Applied) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
store.put(&LedgerRow { db: m.db.clone(), id: m.id.clone(), sha256: m.sha256.clone(), status: Status::Applied, error: None, baseline: true })?;
|
store.put(&LedgerRow { db: m.db.clone(), id: m.id.clone(), sha256: m.sha256.clone(), status: Status::Applied, error: None, baseline: true })?;
|
||||||
@@ -246,8 +324,7 @@ pub struct ApplyReport {
|
|||||||
pub fn apply_pending(store: &dyn Store, list: &[Migration], dir: &Path, snapshot: &dyn Fn() -> Result<String>) -> Result<ApplyReport> {
|
pub fn apply_pending(store: &dyn Store, list: &[Migration], dir: &Path, snapshot: &dyn Fn() -> Result<String>) -> Result<ApplyReport> {
|
||||||
let rows = store.load()?;
|
let rows = store.load()?;
|
||||||
// The list order is the release's order of application (released updates, then pending, then modules).
|
// The list order is the release's order of application (released updates, then pending, then modules).
|
||||||
check_history(&rows, list)?;
|
let todo = pending_migrations(&rows, list)?;
|
||||||
let todo: Vec<&Migration> = list.iter().filter(|m| recorded(&rows, m).is_none_or(|r| r.status != Status::Applied)).collect();
|
|
||||||
tracing::info!(total = list.len(), pending = todo.len(), skipped = list.len() - todo.len(), "database migration plan");
|
tracing::info!(total = list.len(), pending = todo.len(), skipped = list.len() - todo.len(), "database migration plan");
|
||||||
|
|
||||||
// Resolve and verify every file before running anything.
|
// Resolve and verify every file before running anything.
|
||||||
@@ -261,11 +338,13 @@ pub fn apply_pending(store: &dyn Store, list: &[Migration], dir: &Path, snapshot
|
|||||||
|
|
||||||
let mut report = ApplyReport { applied: Vec::new(), failed: None, snapshot: None };
|
let mut report = ApplyReport { applied: Vec::new(), failed: None, snapshot: None };
|
||||||
if files.is_empty() {
|
if files.is_empty() {
|
||||||
|
preserve_auth_history(store,&rows,list)?;
|
||||||
return Ok(report);
|
return Ok(report);
|
||||||
}
|
}
|
||||||
if any_destructive {
|
if any_destructive {
|
||||||
report.snapshot = Some(snapshot().map_err(|e| Error::Invalid(format!("No database update was applied because the safety backup failed: {e}")))?);
|
report.snapshot = Some(snapshot().map_err(|e| Error::Invalid(format!("No database update was applied because the safety backup failed: {e}")))?);
|
||||||
}
|
}
|
||||||
|
preserve_auth_history(store,&rows,list)?;
|
||||||
for (m, schema, path) in files {
|
for (m, schema, path) in files {
|
||||||
tracing::info!(migration = %m.id, database = %m.db, "database migration starting");
|
tracing::info!(migration = %m.id, database = %m.db, "database migration starting");
|
||||||
store.put(&LedgerRow { db: m.db.clone(), id: m.id.clone(), sha256: m.sha256.clone(), status: Status::Running, error: Some("Interrupted SQL must be recovered before replay.".into()), baseline: false })?;
|
store.put(&LedgerRow { db: m.db.clone(), id: m.id.clone(), sha256: m.sha256.clone(), status: Status::Running, error: Some("Interrupted SQL must be recovered before replay.".into()), baseline: false })?;
|
||||||
@@ -368,6 +447,23 @@ mod tests {
|
|||||||
assert!(store.ran.borrow().is_empty());
|
assert!(store.ran.borrow().is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn every_entry_point_rejects_duplicate_sql_before_execution() {
|
||||||
|
let (dir, original) = setup(&[("world", "once", "SELECT 1;\n")]);
|
||||||
|
for alias in ["world", "coa-world"] {
|
||||||
|
let mut list = original.clone();
|
||||||
|
let mut duplicate = original[0].clone();
|
||||||
|
duplicate.db = alias.into();
|
||||||
|
list.push(duplicate);
|
||||||
|
let store = Mem::default();
|
||||||
|
assert!(preflight(&store, &list).is_err());
|
||||||
|
assert!(pending_count(&store, &list).is_err());
|
||||||
|
assert!(apply_pending(&store, &list, dir.path(), &no_snapshot).is_err());
|
||||||
|
assert!(store.ran.borrow().is_empty());
|
||||||
|
assert!(store.rows.borrow().is_empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn staged_files_reject_missing_corrupt_duplicate_and_invalid_migrations() {
|
fn staged_files_reject_missing_corrupt_duplicate_and_invalid_migrations() {
|
||||||
let (dir, list) = setup(&[("characters", "repair", "SELECT 1;\n")]);
|
let (dir, list) = setup(&[("characters", "repair", "SELECT 1;\n")]);
|
||||||
|
|||||||
@@ -129,10 +129,6 @@ pub(crate) fn inventory(root: &Path) -> Result<BTreeMap<String, String>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn copy_verified(source: &Path, target: &Path, files: &BTreeMap<String, String>) -> Result<()> {
|
pub(crate) fn copy_verified(source: &Path, target: &Path, files: &BTreeMap<String, String>) -> Result<()> {
|
||||||
copy_verified_with_progress(source, target, files, &|_| {})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn copy_verified_with_progress(source: &Path, target: &Path, files: &BTreeMap<String, String>, report: &dyn Fn(&str)) -> Result<()> {
|
|
||||||
if target.exists() { return Err(Error::Invalid("Database snapshot destination already exists.".into())); }
|
if target.exists() { return Err(Error::Invalid("Database snapshot destination already exists.".into())); }
|
||||||
let bytes = files.keys().try_fold(0u64, |total, name| -> Result<u64> {
|
let bytes = files.keys().try_fold(0u64, |total, name| -> Result<u64> {
|
||||||
let path = fsx::ensure_within(source, &fsx::safe_join(source, name)?)?;
|
let path = fsx::ensure_within(source, &fsx::safe_join(source, name)?)?;
|
||||||
@@ -142,15 +138,13 @@ fn copy_verified_with_progress(source: &Path, target: &Path, files: &BTreeMap<St
|
|||||||
fs::create_dir_all(parent)?;
|
fs::create_dir_all(parent)?;
|
||||||
fsx::require_space(parent, bytes)?;
|
fsx::require_space(parent, bytes)?;
|
||||||
fs::create_dir(target)?;
|
fs::create_dir(target)?;
|
||||||
for (index, (relative, expected)) in files.iter().enumerate() {
|
for (relative, expected) in files {
|
||||||
if index % 250 == 0 { report(&format!("Copying private server files: {} of {}", index, files.len())); }
|
|
||||||
let source_file = fsx::ensure_within(source, &fsx::safe_join(source, relative)?)?;
|
let source_file = fsx::ensure_within(source, &fsx::safe_join(source, relative)?)?;
|
||||||
let target_file = fsx::ensure_within(target, &fsx::safe_join(target, relative)?)?;
|
let target_file = fsx::ensure_within(target, &fsx::safe_join(target, relative)?)?;
|
||||||
fs::create_dir_all(target_file.parent().unwrap())?;
|
fs::create_dir_all(target_file.parent().unwrap())?;
|
||||||
fs::copy(&source_file, &target_file)?;
|
fs::copy(&source_file, &target_file)?;
|
||||||
if fsx::sha256_file(&target_file)? != *expected { return Err(Error::Invalid(format!("Database snapshot file {relative} failed verification."))); }
|
if fsx::sha256_file(&target_file)? != *expected { return Err(Error::Invalid(format!("Database snapshot file {relative} failed verification."))); }
|
||||||
}
|
}
|
||||||
report("Verifying the complete private file copy");
|
|
||||||
if inventory(target)? != *files { return Err(Error::Invalid("Database snapshot inventory differs.".into())); }
|
if inventory(target)? != *files { return Err(Error::Invalid("Database snapshot inventory differs.".into())); }
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -250,99 +244,6 @@ pub(crate) fn verify(point: &Path, snapshot: &Snapshot) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn rehearsal_copy_with_progress(root: &Path, meta: &Path, point: &Path, snapshot: &Snapshot, report: &dyn Fn(&str)) -> Result<PathBuf> {
|
|
||||||
report("Verifying the database recovery copy before rehearsal");
|
|
||||||
verify(point, snapshot)?;
|
|
||||||
let canonical = fsx::canonicalize_lenient(root)?;
|
|
||||||
let root = canonical.as_path();
|
|
||||||
let destination = meta.join("rehearsals").join(uuid::Uuid::new_v4().simple().to_string()).join("server");
|
|
||||||
let mut files = BTreeMap::new();
|
|
||||||
let mut directories = vec![root.to_path_buf()];
|
|
||||||
while let Some(directory) = directories.pop() {
|
|
||||||
for entry in fs::read_dir(directory)? {
|
|
||||||
let entry = entry?;
|
|
||||||
let raw = entry.path();
|
|
||||||
let relative = raw.strip_prefix(root).map_err(|e| Error::Invalid(e.to_string()))?.to_string_lossy().replace('\\', "/");
|
|
||||||
let lower = relative.to_ascii_lowercase();
|
|
||||||
let first = lower.split('/').next().unwrap();
|
|
||||||
if entry.file_type()?.is_dir() && !relative.contains('/')
|
|
||||||
&& !["core", "data", "scripts", "settings", "runtime", "mysql", ".realms", "bugreport", "extras", "licenses"].contains(&first) { continue; }
|
|
||||||
if lower == "mysql/data" || lower.starts_with("mysql/data-")
|
|
||||||
|| lower.split('/').any(|part| part == ".state")
|
|
||||||
|| lower == "bugreport/reports" || lower == "core/logs" || lower == "mysql/logs" { continue; }
|
|
||||||
let metadata = fs::symlink_metadata(&raw)?;
|
|
||||||
reject_link(&metadata)?;
|
|
||||||
let path = fsx::ensure_within(root, &raw)?;
|
|
||||||
if metadata.is_dir() { directories.push(path); }
|
|
||||||
else if metadata.is_file() {
|
|
||||||
files.insert(relative, fsx::sha256_file(&path)?);
|
|
||||||
if files.len() % 250 == 0 { report(&format!("Preparing the private copy: {} files checked", files.len())); }
|
|
||||||
}
|
|
||||||
else { return Err(Error::Invalid("Unsupported rehearsal file.".into())); }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
copy_verified_with_progress(root, &destination, &files, report)?;
|
|
||||||
report("Restoring MySQL into the private server copy");
|
|
||||||
copy_snapshot(point, snapshot, &destination.join("mysql/data"))?;
|
|
||||||
let (_, mut installed) = crate::registry::MetaDir::open(meta)?;
|
|
||||||
installed.id = uuid::Uuid::new_v4().to_string();
|
|
||||||
installed.server_path = destination.to_string_lossy().into_owned();
|
|
||||||
installed.client_path = None;
|
|
||||||
crate::registry::MetaDir::create(&destination, &installed)?;
|
|
||||||
Ok(destination)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn isolate_connections(root: &Path) -> Result<()> {
|
|
||||||
for relative in ["Settings/worldserver.conf.template", "Settings/authserver.conf.template", "Core/configs/worldserver.conf", "Core/configs/authserver.conf"] {
|
|
||||||
let path = fsx::ensure_within(root, &root.join(relative))?;
|
|
||||||
if !path.is_file() {
|
|
||||||
if relative.ends_with(".template") { return Err(Error::Invalid("The private update copy lacks connection templates.".into())); }
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let mut config = crate::config::parser::ConfFile::parse_bytes(&fs::read(&path)?)?;
|
|
||||||
for key in ["LoginDatabaseInfo", "WorldDatabaseInfo", "CharacterDatabaseInfo"] {
|
|
||||||
if relative.contains("worldserver") || key == "LoginDatabaseInfo" {
|
|
||||||
config.set(key, &format!("\"@{key}@\""), &[]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
config.set("BindIP", "\"127.0.0.1\"", &[]);
|
|
||||||
if relative.contains("worldserver") {
|
|
||||||
config.set("Ra.IP", "\"127.0.0.1\"", &[]);
|
|
||||||
config.set("DataDir", "\"@DATA@\"", &[]);
|
|
||||||
}
|
|
||||||
fsx::atomic_write(&path, config.to_text().as_bytes())?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn unchanged(root: &Path, snapshot: &Snapshot) -> Result<()> {
|
|
||||||
let data = fsx::ensure_within(root, &root.join("mysql/data"))?;
|
|
||||||
if inventory(&data)? != snapshot.files || directories(&data)? != snapshot.directories {
|
|
||||||
return Err(Error::Invalid("The installed database changed during the private rehearsal. Retry after stopping other database access.".into()));
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn validate_connections(templates: &Path, installation: &Path) -> Result<()> {
|
|
||||||
let port = crate::layout::read_ports(installation).mysql.to_string();
|
|
||||||
let (_, password) = crate::db::credentials(installation)?;
|
|
||||||
let realm = crate::realms::state(installation)?.active;
|
|
||||||
for relative in ["Settings/worldserver.conf.template", "Settings/authserver.conf.template"] {
|
|
||||||
let path = fsx::ensure_within(templates, &templates.join(relative))?;
|
|
||||||
let config = crate::config::parser::ConfFile::parse_bytes(&fs::read(path)?)?;
|
|
||||||
for (key, kind) in [("LoginDatabaseInfo", "auth"), ("WorldDatabaseInfo", "world"), ("CharacterDatabaseInfo", "characters")] {
|
|
||||||
if relative.contains("authserver") && kind != "auth" { continue; }
|
|
||||||
let value = config.get(key).ok_or_else(|| Error::Invalid(format!("The update cannot isolate the missing {key} connection.")))?.trim().trim_matches('"');
|
|
||||||
if value == format!("@{key}@") { continue; }
|
|
||||||
let fields: Vec<_> = value.split(';').collect();
|
|
||||||
if fields.len() != 5 || !matches!(fields[0], "127.0.0.1" | "localhost") || fields[1] != port || fields[2] != "acore" || fields[3] != password || fields[4] != realm.schema(kind)? {
|
|
||||||
return Err(Error::Invalid(format!("The {key} connection does not use this repack's bundled database. A private update cannot safely qualify an external database.")));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep the replaced data directory. If interrupted between renames, a retry can install a new verified copy.
|
// Keep the replaced data directory. If interrupted between renames, a retry can install a new verified copy.
|
||||||
pub(crate) fn swap(root: &Path, meta: &Path, point: &Path, snapshot: &Snapshot, fail_after_move: bool) -> Result<PathBuf> {
|
pub(crate) fn swap(root: &Path, meta: &Path, point: &Path, snapshot: &Snapshot, fail_after_move: bool) -> Result<PathBuf> {
|
||||||
verify(point, snapshot)?;
|
verify(point, snapshot)?;
|
||||||
@@ -570,6 +471,46 @@ mod tests {
|
|||||||
};
|
};
|
||||||
let mut server = start();
|
let mut server = start();
|
||||||
db.query("CREATE DATABASE acore_characters; CREATE DATABASE acore_world; CREATE DATABASE acore_auth; CREATE TABLE acore_characters.probe(id INT PRIMARY KEY, note VARCHAR(40)); CREATE TABLE acore_world.probe(id INT NOT NULL DEFAULT 0); INSERT INTO acore_world.probe VALUES(43); CREATE TABLE acore_auth.probe(id INT); INSERT INTO acore_auth.probe VALUES(44); CREATE USER 'fixture_user'@'localhost' IDENTIFIED BY 'fixture'; CREATE TRIGGER acore_characters.probe_insert BEFORE INSERT ON acore_characters.probe FOR EACH ROW SET NEW.note='triggered'; INSERT INTO acore_characters.probe VALUES(42,'original'); CREATE VIEW acore_characters.probe_view AS SELECT c.id,w.id AS world_id FROM acore_characters.probe c CROSS JOIN acore_world.probe w; CREATE PROCEDURE acore_characters.probe_proc() SELECT id FROM acore_characters.probe; CREATE FUNCTION acore_characters.probe_func() RETURNS INT DETERMINISTIC RETURN 73;").unwrap();
|
db.query("CREATE DATABASE acore_characters; CREATE DATABASE acore_world; CREATE DATABASE acore_auth; CREATE TABLE acore_characters.probe(id INT PRIMARY KEY, note VARCHAR(40)); CREATE TABLE acore_world.probe(id INT NOT NULL DEFAULT 0); INSERT INTO acore_world.probe VALUES(43); CREATE TABLE acore_auth.probe(id INT); INSERT INTO acore_auth.probe VALUES(44); CREATE USER 'fixture_user'@'localhost' IDENTIFIED BY 'fixture'; CREATE TRIGGER acore_characters.probe_insert BEFORE INSERT ON acore_characters.probe FOR EACH ROW SET NEW.note='triggered'; INSERT INTO acore_characters.probe VALUES(42,'original'); CREATE VIEW acore_characters.probe_view AS SELECT c.id,w.id AS world_id FROM acore_characters.probe c CROSS JOIN acore_world.probe w; CREATE PROCEDURE acore_characters.probe_proc() SELECT id FROM acore_characters.probe; CREATE FUNCTION acore_characters.probe_func() RETURNS INT DETERMINISTIC RETURN 73;").unwrap();
|
||||||
|
// Shared auth must not run again when the active realm changes.
|
||||||
|
use crate::migrations::Store;
|
||||||
|
db.query("CREATE DATABASE acore_world_wildcard;").unwrap();
|
||||||
|
let migration_dir = root.join("migration-fixture");
|
||||||
|
fs::create_dir_all(migration_dir.join("auth")).unwrap();
|
||||||
|
fs::create_dir_all(migration_dir.join("world")).unwrap();
|
||||||
|
let auth_sql = b"INSERT INTO acore_auth.probe VALUES(45);";
|
||||||
|
fs::write(migration_dir.join("auth/shared_once.sql"), auth_sql).unwrap();
|
||||||
|
let auth_list = vec![crate::manifest::Migration { id:"shared_once".into(), db:"auth".into(), sha256:fsx::sha256_bytes(auth_sql), destructive:false, compatible_sha256:vec![] }];
|
||||||
|
assert_eq!(crate::migrations::apply_pending(&db, &auth_list, &migration_dir, &|| Ok("fixture".into())).unwrap().applied.len(), 1);
|
||||||
|
let wildcard = db.clone().for_realm(crate::realms::Mode::Wildcard);
|
||||||
|
assert_eq!(crate::migrations::pending_count(&wildcard, &auth_list).unwrap(), 0, "auth is shared by both realms");
|
||||||
|
assert!(crate::migrations::apply_pending(&wildcard, &auth_list, &migration_dir, &|| Ok("fixture".into())).unwrap().applied.is_empty());
|
||||||
|
assert_eq!(db.query("SELECT COUNT(*) FROM acore_auth.probe WHERE id=45;").unwrap(), "1");
|
||||||
|
let mut failed = crate::migrations::LedgerRow { db:"auth".into(), id:"shared_once".into(), sha256:auth_list[0].sha256.clone(), status:crate::migrations::Status::Failed, error:Some("interrupted fixture".into()), baseline:false };
|
||||||
|
db.put(&failed).unwrap();
|
||||||
|
assert!(crate::migrations::pending_count(&wildcard, &auth_list).is_err());
|
||||||
|
failed.status = crate::migrations::Status::Applied; failed.error = None; db.put(&failed).unwrap();
|
||||||
|
|
||||||
|
|
||||||
|
// Legacy per-world auth records are adopted without replaying their SQL.
|
||||||
|
let legacy_sql = b"INSERT INTO acore_auth.probe VALUES(46);";
|
||||||
|
fs::write(migration_dir.join("auth/legacy_auth.sql"), legacy_sql).unwrap();
|
||||||
|
db.query(&format!("INSERT INTO acore_auth.probe VALUES(46); REPLACE INTO acore_world.coa_manager_migrations VALUES('auth','legacy_auth','{}','applied',NOW(),NULL,0);", fsx::sha256_bytes(legacy_sql))).unwrap();
|
||||||
|
let legacy_auth = vec![crate::manifest::Migration { id:"legacy_auth".into(), db:"auth".into(), sha256:fsx::sha256_bytes(legacy_sql), destructive:false, compatible_sha256:vec![] }];
|
||||||
|
assert_eq!(crate::migrations::pending_count(&wildcard, &legacy_auth).unwrap(), 0);
|
||||||
|
assert!(crate::migrations::apply_pending(&wildcard, &legacy_auth, &migration_dir, &|| Ok("fixture".into())).unwrap().applied.is_empty());
|
||||||
|
assert_eq!(db.query("SELECT COUNT(*) FROM acore_auth.probe WHERE id=46;").unwrap(), "1");
|
||||||
|
|
||||||
|
// A core history cleanup cannot make previously recorded SQL pending again.
|
||||||
|
db.query("CREATE TABLE acore_world.updates(name VARCHAR(150) PRIMARY KEY) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; INSERT INTO acore_world.updates VALUES('LEGACY_WORLD.sql'); INSERT INTO acore_world.probe VALUES(99);").unwrap();
|
||||||
|
db.load().unwrap();
|
||||||
|
db.query("DROP TABLE acore_world.updates;").unwrap();
|
||||||
|
let legacy_world_sql = b"INSERT INTO acore_world.probe VALUES(99);";
|
||||||
|
fs::write(migration_dir.join("world/legacy_world.sql"), legacy_world_sql).unwrap();
|
||||||
|
let legacy_world = vec![crate::manifest::Migration { id:"legacy_world".into(), db:"world".into(), sha256:fsx::sha256_bytes(legacy_world_sql), destructive:false, compatible_sha256:vec![] }];
|
||||||
|
assert_eq!(crate::migrations::pending_count(&db, &legacy_world).unwrap(), 0);
|
||||||
|
assert!(crate::migrations::apply_pending(&db, &legacy_world, &migration_dir, &|| Ok("fixture".into())).unwrap().applied.is_empty());
|
||||||
|
assert_eq!(db.query("SELECT COUNT(*) FROM acore_world.probe WHERE id=99;").unwrap(), "1");
|
||||||
|
db.query("DELETE FROM acore_world.probe WHERE id=99;").unwrap();
|
||||||
assert_eq!(db.recovery_objects("acore_characters").unwrap().len(), 4);
|
assert_eq!(db.recovery_objects("acore_characters").unwrap().len(), 4);
|
||||||
db.query("CREATE TABLE acore_characters.coa_squid_pending(path VARCHAR(255), sha256 CHAR(64)); INSERT INTO acore_characters.coa_squid_pending VALUES('playerbots/base/ai_playerbot_texts.sql', REPEAT('a',64));").unwrap();
|
db.query("CREATE TABLE acore_characters.coa_squid_pending(path VARCHAR(255), sha256 CHAR(64)); INSERT INTO acore_characters.coa_squid_pending VALUES('playerbots/base/ai_playerbot_texts.sql', REPEAT('a',64));").unwrap();
|
||||||
let squid_problems = crate::squid::database_problems(&root, crate::realms::Mode::Coa, &|sql| {
|
let squid_problems = crate::squid::database_problems(&root, crate::realms::Mode::Coa, &|sql| {
|
||||||
@@ -660,45 +601,10 @@ mod tests {
|
|||||||
fsx::atomic_write(&archive_path, b"damaged").unwrap();
|
fsx::atomic_write(&archive_path, b"damaged").unwrap();
|
||||||
assert!(verify(&point, &snapshot).is_err());
|
assert!(verify(&point, &snapshot).is_err());
|
||||||
}
|
}
|
||||||
#[test]
|
|
||||||
fn private_rehearsals_reject_external_database_connections() {
|
|
||||||
let (_temp, root, _meta, _point, _snapshot) = fixture();
|
|
||||||
fsx::atomic_write_json(&root.join("Settings/repack.json"), &serde_json::json!({"mysqlPort":3307})).unwrap();
|
|
||||||
fsx::atomic_write_json(&root.join("Settings/database.json"), &serde_json::json!({"rootPassword":"test-root","appPassword":"test-app"})).unwrap();
|
|
||||||
fsx::atomic_write(&root.join("Settings/worldserver.conf.template"), b"LoginDatabaseInfo = \"@LoginDatabaseInfo@\"\nWorldDatabaseInfo = \"@WorldDatabaseInfo@\"\nCharacterDatabaseInfo = \"@CharacterDatabaseInfo@\"\n").unwrap();
|
|
||||||
fsx::atomic_write(&root.join("Settings/authserver.conf.template"), b"LoginDatabaseInfo = \"127.0.0.1;3307;acore;test-app;acore_auth\"\n").unwrap();
|
|
||||||
validate_connections(&root, &root).unwrap();
|
|
||||||
fsx::atomic_write(&root.join("Settings/authserver.conf.template"), b"LoginDatabaseInfo = \"192.0.2.1;3307;acore;test-app;acore_auth\"\n").unwrap();
|
|
||||||
let error = validate_connections(&root, &root).unwrap_err().to_string();
|
|
||||||
assert!(error.contains("external database"));
|
|
||||||
assert!(!error.contains("test-app"));
|
|
||||||
}
|
|
||||||
#[test]
|
|
||||||
fn a_changed_live_database_invalidates_the_rehearsal() {
|
|
||||||
let (_temp, root, _meta, _point, snapshot) = fixture();
|
|
||||||
assert!(unchanged(&root, &snapshot).is_err());
|
|
||||||
fsx::atomic_write(&root.join("mysql/data/characters.ibd"), b"saved").unwrap();
|
|
||||||
fs::create_dir_all(root.join("mysql/data/empty_schema")).unwrap();
|
|
||||||
unchanged(&root, &snapshot).unwrap();
|
|
||||||
fsx::atomic_write(&root.join("mysql/data/extra.ibd"), b"new data").unwrap();
|
|
||||||
assert!(unchanged(&root, &snapshot).is_err());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn rehearsal_copy_succeeds_with_nested_directories_and_unnormalized_root() {
|
|
||||||
let (_temp, root, meta, point, snapshot) = fixture();
|
|
||||||
let meta_obj = crate::registry::InstallMeta::new(crate::registry::InstallKind::New, &root);
|
|
||||||
fsx::atomic_write_json(&meta.join("install.json"), &meta_obj).unwrap();
|
|
||||||
fsx::atomic_write(&root.join("Core/worldserver.exe"), b"exe").unwrap();
|
|
||||||
fsx::atomic_write(&root.join("Core/configs/worldserver.conf"), b"conf").unwrap();
|
|
||||||
fsx::atomic_write(&root.join("Settings/repack.json"), b"{}").unwrap();
|
|
||||||
let phases = std::cell::RefCell::new(Vec::new());
|
|
||||||
let dest = rehearsal_copy_with_progress(&root, &meta, &point, &snapshot, &|phase| phases.borrow_mut().push(phase.to_string())).unwrap();
|
|
||||||
assert_eq!(fs::read(dest.join("Core/configs/worldserver.conf")).unwrap(), b"conf");
|
|
||||||
assert_eq!(fs::read(dest.join("mysql/data/characters.ibd")).unwrap(), b"saved");
|
|
||||||
let phases = phases.into_inner();
|
|
||||||
assert!(phases[0].contains("Verifying the database"));
|
|
||||||
assert!(phases.iter().any(|phase| phase.contains("Copying private server files")));
|
|
||||||
assert!(phases.last().unwrap().contains("Restoring MySQL"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -348,7 +348,8 @@ AC>";
|
|||||||
let (port, _h) = fake_ra("Account not found.");
|
let (port, _h) = fake_ra("Account not found.");
|
||||||
let mut ra = Ra::connect_to(port, "u", "p").unwrap();
|
let mut ra = Ra::connect_to(port, "u", "p").unwrap();
|
||||||
assert!(ra.set_account_password("x", "newpass9").is_err(), "the name is validated before anything is sent");
|
assert!(ra.set_account_password("x", "newpass9").is_err(), "the name is validated before anything is sent");
|
||||||
assert!(ra.set_account_password("Nobody", "newpass9").unwrap_err().to_string().contains("not found"));
|
let error = ra.set_account_password("Nobody", "newpass9").unwrap_err().to_string();
|
||||||
|
assert!(error.contains("not found"), "Unexpected console error: {error}");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -14,8 +14,6 @@ impl crate::update::Env for QualificationEnv<'_> {
|
|||||||
fn preflight(&self, manifest: &crate::manifest::Manifest) -> Result<()> { self.0.preflight(manifest) }
|
fn preflight(&self, manifest: &crate::manifest::Manifest) -> Result<()> { self.0.preflight(manifest) }
|
||||||
fn snapshot(&self) -> Result<String> { self.0.snapshot() }
|
fn snapshot(&self) -> Result<String> { self.0.snapshot() }
|
||||||
fn verify_snapshot(&self, id: &str) -> Result<()> { self.0.verify_snapshot(id) }
|
fn verify_snapshot(&self, id: &str) -> Result<()> { self.0.verify_snapshot(id) }
|
||||||
fn rehearse(&self, manifest: &crate::manifest::Manifest, tree: &Path, items: &[crate::update::PlanItem], point: &str) -> Result<()> { self.0.rehearse(manifest, tree, items, point) }
|
|
||||||
fn rehearse_with_progress(&self, manifest: &crate::manifest::Manifest, tree: &Path, items: &[crate::update::PlanItem], point: &str, report: &dyn Fn(&str, u8)) -> Result<()> { self.0.rehearse_with_progress(manifest, tree, items, point, report) }
|
|
||||||
fn restore_snapshot(&self, id: &str) -> Result<()> { self.0.restore_snapshot(id) }
|
fn restore_snapshot(&self, id: &str) -> Result<()> { self.0.restore_snapshot(id) }
|
||||||
fn migrate(&self, manifest: &crate::manifest::Manifest, staged: &Path) -> Result<crate::migrations::ApplyReport> { self.0.migrate(manifest, staged) }
|
fn migrate(&self, manifest: &crate::manifest::Manifest, staged: &Path) -> Result<crate::migrations::ApplyReport> { self.0.migrate(manifest, staged) }
|
||||||
fn validate(&self) -> Result<()> { self.0.validate() }
|
fn validate(&self) -> Result<()> { self.0.validate() }
|
||||||
@@ -87,7 +85,7 @@ pub fn qualify_upgrade(base: &Path, source: &Path, candidate: &Path, fixture: &P
|
|||||||
"schema": 1, "result": "passed", "fromVersion": source_manifest.version,
|
"schema": 1, "result": "passed", "fromVersion": source_manifest.version,
|
||||||
"toVersion": candidate_manifest.version, "fromManifestSha256": fsx::sha256_bytes(&source_bytes),
|
"toVersion": candidate_manifest.version, "fromManifestSha256": fsx::sha256_bytes(&source_bytes),
|
||||||
"fromSchemaSha256": source_manifest.files.iter().find(|file| file.path == schema_check::CONTRACT).map(|file| &file.sha256),
|
"fromSchemaSha256": source_manifest.files.iter().find(|file| file.path == schema_check::CONTRACT).map(|file| &file.sha256),
|
||||||
"checks": ["manager-transaction", "private-user-database-rehearsal", "database-schema", "private-auth-world-startup", "existing-identities", "user-file-preservation"],
|
"checks": ["manager-transaction", "recovery-backed-database-update", "database-schema", "private-auth-world-startup", "existing-identities", "user-file-preservation"],
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+84
-188
@@ -92,9 +92,6 @@ pub trait Env {
|
|||||||
fn ensure_stopped(&self) -> Result<()>;
|
fn ensure_stopped(&self) -> Result<()>;
|
||||||
fn preflight(&self, _manifest: &Manifest) -> Result<()> { Ok(()) }
|
fn preflight(&self, _manifest: &Manifest) -> Result<()> { Ok(()) }
|
||||||
fn verify_snapshot(&self, _id: &str) -> Result<()> { Ok(()) }
|
fn verify_snapshot(&self, _id: &str) -> Result<()> { Ok(()) }
|
||||||
fn rehearse(&self, _manifest: &Manifest, _tree: &Path, _items: &[PlanItem], _point: &str) -> Result<()> { Ok(()) }
|
|
||||||
fn rehearse_with_progress(&self, manifest: &Manifest, tree: &Path, items: &[PlanItem], point: &str, _report: &dyn Fn(&str, u8)) -> Result<()> { self.rehearse(manifest, tree, items, point) }
|
|
||||||
/// Create a recovery point for the databases and configuration; returns its id.
|
|
||||||
fn snapshot(&self) -> Result<String>;
|
fn snapshot(&self) -> Result<String>;
|
||||||
fn restore_snapshot(&self, id: &str) -> Result<()>;
|
fn restore_snapshot(&self, id: &str) -> Result<()>;
|
||||||
fn migrate(&self, manifest: &Manifest, staged_migrations: &Path) -> Result<ApplyReport>;
|
fn migrate(&self, manifest: &Manifest, staged_migrations: &Path) -> Result<ApplyReport>;
|
||||||
@@ -339,7 +336,7 @@ pub fn preview_with(root: &Path, meta: &InstallMeta, source: &Source, trusted_ke
|
|||||||
let meta_dir = crate::registry::metadata_dir_for(root)?;
|
let meta_dir = crate::registry::metadata_dir_for(root)?;
|
||||||
let observed_version = MetaDir::open(&meta_dir)?.1.core.version;
|
let observed_version = MetaDir::open(&meta_dir)?.1.core.version;
|
||||||
let (m, _) = fetch_manifest(source, trusted_key)?;
|
let (m, _) = fetch_manifest(source, trusted_key)?;
|
||||||
let _lock = operation_lock_for(&meta_dir, "checking updates", false)?;
|
let _lock = operation_lock_for(&meta_dir, "checking updates", true)?;
|
||||||
ensure_recovered(&meta_dir)?;
|
ensure_recovered(&meta_dir)?;
|
||||||
let (_, current_meta) = MetaDir::open(&meta_dir)?;
|
let (_, current_meta) = MetaDir::open(&meta_dir)?;
|
||||||
let meta = if current_meta.core.version != observed_version { ¤t_meta } else { meta };
|
let meta = if current_meta.core.version != observed_version { ¤t_meta } else { meta };
|
||||||
@@ -347,7 +344,7 @@ pub fn preview_with(root: &Path, meta: &InstallMeta, source: &Source, trusted_ke
|
|||||||
reject_downgrade(meta, &m)?;
|
reject_downgrade(meta, &m)?;
|
||||||
let mut items = plan(root, meta, &m, resolutions, None)?;
|
let mut items = plan(root, meta, &m, resolutions, None)?;
|
||||||
if items.iter().any(|item| item.path == "Scripts/manage.py" && item.action != Action::Skip)
|
if items.iter().any(|item| item.path == "Scripts/manage.py" && item.action != Action::Skip)
|
||||||
&& launcher_already_integrated(root, &meta_dir, meta, &m, source)? {
|
&& launcher_already_integrated(root, meta, &m)? {
|
||||||
if let Some(item) = items.iter_mut().find(|item| item.path == "Scripts/manage.py") {
|
if let Some(item) = items.iter_mut().find(|item| item.path == "Scripts/manage.py") {
|
||||||
item.action = Action::Skip;
|
item.action = Action::Skip;
|
||||||
item.reason = Some("already up to date with Manager integration".into());
|
item.reason = Some("already up to date with Manager integration".into());
|
||||||
@@ -377,27 +374,11 @@ pub fn preview_with(root: &Path, meta: &InstallMeta, source: &Source, trusted_ke
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn launcher_already_integrated(root: &Path, meta_dir: &Path, meta: &InstallMeta, manifest: &Manifest, source: &Source) -> Result<bool> {
|
fn launcher_already_integrated(root: &Path, meta: &InstallMeta, manifest: &Manifest) -> Result<bool> {
|
||||||
let Some(file) = manifest.files.iter().find(|file| file.path == "Scripts/manage.py") else { return Ok(false); };
|
let Some(file) = manifest.files.iter().find(|file| file.path == "Scripts/manage.py") else { return Ok(false); };
|
||||||
let actual = sha_if_exists(&root.join(&file.path));
|
let Ok(actual) = fs::read(root.join(&file.path)) else { return Ok(false); };
|
||||||
if actual.as_ref() != meta.original_hashes.get(&file.path) || actual.is_none() { return Ok(false); }
|
if meta.original_hashes.get(&file.path) != Some(&fsx::sha256_bytes(&actual)) { return Ok(false); }
|
||||||
let cache = meta_dir.join("manifests").join(format!("launcher-{}.py", file.sha256));
|
Ok(crate::driver::launcher_matches_recorded(&file.sha256, &actual))
|
||||||
let signed = match fs::read(&cache) {
|
|
||||||
Ok(bytes) if fsx::sha256_bytes(&bytes) == file.sha256 => bytes,
|
|
||||||
_ => {
|
|
||||||
let temp = tempfile::tempdir()?;
|
|
||||||
let parts = fetch_parts(source, manifest, &temp.path().join("download"), &Cancel::default(), &|_, _| {})?;
|
|
||||||
package::extract(&parts, manifest, &temp.path().join("tree"), &|_, _| {})?;
|
|
||||||
let bytes = fs::read(temp.path().join("tree").join(&file.path))?;
|
|
||||||
if fsx::sha256_bytes(&bytes) != file.sha256 { return Err(Error::Invalid("The signed launcher failed verification.".into())); }
|
|
||||||
// This derived cache is optional; failure to save it does not change the server.
|
|
||||||
let _ = fsx::atomic_write(&cache, &bytes);
|
|
||||||
bytes
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(std::str::from_utf8(&signed).ok().is_some_and(|signed| {
|
|
||||||
fs::read(root.join(&file.path)).ok().is_some_and(|actual| crate::driver::launcher_matches(signed, &actual))
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reject_downgrade(meta: &InstallMeta, manifest: &Manifest) -> Result<()> {
|
fn reject_downgrade(meta: &InstallMeta, manifest: &Manifest) -> Result<()> {
|
||||||
@@ -406,8 +387,10 @@ fn reject_downgrade(meta: &InstallMeta, manifest: &Manifest) -> Result<()> {
|
|||||||
if c.platform != platform {
|
if c.platform != platform {
|
||||||
return Err(Error::Invalid(format!("This package targets {} rather than {platform}.", c.platform)));
|
return Err(Error::Invalid(format!("This package targets {} rather than {platform}.", c.platform)));
|
||||||
}
|
}
|
||||||
if meta.core.version.as_ref().is_none_or(|version| version != &manifest.version && !c.source_versions.contains(version)) {
|
// The source list records release qualification, not an exclusive upgrade route.
|
||||||
return Err(Error::Invalid(format!("Installed server {} is outside the compatibility matrix for {}. Tested source versions: {}.", meta.core.version.as_deref().unwrap_or("unknown"), manifest.version, c.source_versions.join(", "))));
|
// Database history and target validation decide whether the update can complete.
|
||||||
|
if meta.core.version.as_deref().and_then(crate::manifest::parse_version).is_none() {
|
||||||
|
return Err(Error::Invalid("The installed server version is unknown. An imported repack needs a verified starting state before updating.".into()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(installed) = meta.core.version.as_deref() {
|
if let Some(installed) = meta.core.version.as_deref() {
|
||||||
@@ -528,14 +511,6 @@ pub fn apply(p: &Params, report: &dyn Fn(&str, u8)) -> Result<Outcome> {
|
|||||||
let point_meta = crate::backup::point_json(meta_dir, txn.recovery_point.as_deref().unwrap())?;
|
let point_meta = crate::backup::point_json(meta_dir, txn.recovery_point.as_deref().unwrap())?;
|
||||||
if point_meta.is_file() { txn.recovery_hashes.insert("@point".into(), fsx::sha256_file(&point_meta)?); }
|
if point_meta.is_file() { txn.recovery_hashes.insert("@point".into(), fsx::sha256_file(&point_meta)?); }
|
||||||
save(meta_dir, &txn)?;
|
save(meta_dir, &txn)?;
|
||||||
step(report, "Testing the update on a private copy of your server", 54);
|
|
||||||
if let Err(error) = p.env.rehearse_with_progress(&m, &tree, &items, txn.recovery_point.as_deref().unwrap(), report) {
|
|
||||||
txn.state = State::Failed;
|
|
||||||
txn.message = Some(format!("The private update rehearsal failed: {error}. The installed server was not updated."));
|
|
||||||
save(meta_dir, &txn)?;
|
|
||||||
return Err(Error::Invalid(txn.message.unwrap()));
|
|
||||||
}
|
|
||||||
|
|
||||||
txn.ops = items
|
txn.ops = items
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|i| i.action != Action::Skip)
|
.filter(|i| i.action != Action::Skip)
|
||||||
@@ -923,7 +898,7 @@ impl RepackEnv<'_> {
|
|||||||
if realms.wildcard_created { modes.push(if realms.active == crate::realms::Mode::Coa { crate::realms::Mode::Wildcard } else { crate::realms::Mode::Coa }); }
|
if realms.wildcard_created { modes.push(if realms.active == crate::realms::Mode::Coa { crate::realms::Mode::Wildcard } else { crate::realms::Mode::Coa }); }
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
for (index, mode) in modes.into_iter().enumerate() {
|
for (index, mode) in modes.into_iter().enumerate() {
|
||||||
let list: Vec<_> = manifest.migrations.iter().filter(|m| index == 0 || m.db != "auth").cloned().collect();
|
let list: Vec<_> = manifest.migrations.iter().filter(|m| index == 0 || !crate::migrations::shared_auth(&m.db)).cloned().collect();
|
||||||
count += crate::migrations::pending_count(&db.clone().for_realm(mode), &list)?;
|
count += crate::migrations::pending_count(&db.clone().for_realm(mode), &list)?;
|
||||||
}
|
}
|
||||||
Ok(count)
|
Ok(count)
|
||||||
@@ -989,85 +964,6 @@ impl Env for RepackEnv<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn automatic_rollback(&self) -> bool { true }
|
fn automatic_rollback(&self) -> bool { true }
|
||||||
fn rehearse(&self, manifest: &Manifest, tree: &Path, items: &[PlanItem], id: &str) -> Result<()> {
|
|
||||||
self.rehearse_with_progress(manifest, tree, items, id, &|_, _| {})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn rehearse_with_progress(&self, manifest: &Manifest, tree: &Path, items: &[PlanItem], id: &str, report: &dyn Fn(&str, u8)) -> Result<()> {
|
|
||||||
if items.iter().any(|item| item.action != Action::Skip && item.path.to_ascii_lowercase().starts_with("mysql/")) {
|
|
||||||
return Err(Error::Invalid("A server update cannot replace MySQL while using a cold recovery copy; a separate database engine upgrade is required.".into()));
|
|
||||||
}
|
|
||||||
let point = crate::backup::get(self.meta_dir, id)?;
|
|
||||||
let Some(snapshot) = &point.mysql_snapshot else {
|
|
||||||
// Docker uses its separate experimental release channel until its clone runner is qualified.
|
|
||||||
if crate::docker::is_docker(self.root) { return Ok(()); }
|
|
||||||
return Err(Error::Invalid("This installation has no complete MySQL recovery copy for an update rehearsal.".into()));
|
|
||||||
};
|
|
||||||
crate::mysql_snapshot::validate_connections(self.root, self.root)?;
|
|
||||||
crate::mysql_snapshot::stop(self.root)?;
|
|
||||||
let point_dir = crate::backup::point_json(self.meta_dir, id)?.parent().unwrap().to_path_buf();
|
|
||||||
let fixture = crate::mysql_snapshot::rehearsal_copy_with_progress(self.root, self.meta_dir, &point_dir, snapshot, &|phase| step(report, phase, 54))?;
|
|
||||||
let metadata = crate::registry::metadata_dir_for(&fixture)?;
|
|
||||||
let env = RepackEnv { root: &fixture, meta_dir: &metadata };
|
|
||||||
let result = crate::install::with_scratch_ports(&fixture, || {
|
|
||||||
let mut txn = Txn { id: "rehearsal".into(), state: State::Applying, from_version: None,
|
|
||||||
to_version: manifest.version.clone(), recovery_point: None, databases_started: false,
|
|
||||||
ops: items.iter().filter(|item| item.action != Action::Skip).map(|item| Op {
|
|
||||||
path: item.path.clone(), action: item.action, reason: item.reason.clone(),
|
|
||||||
new_sha256: manifest.files.iter().find(|file| file.path == item.path).unwrap().sha256.clone(),
|
|
||||||
had_previous: fsx::safe_join(&fixture, &item.path).is_ok_and(|path| path.is_file()), started:false, done:false,
|
|
||||||
}).collect(), message:None, recovery_hashes:BTreeMap::new() };
|
|
||||||
let before = txn_dir(&metadata, &txn.id)?.join("before");
|
|
||||||
fs::create_dir_all(&before)?;
|
|
||||||
step(report, "Applying update files to the private server copy", 55);
|
|
||||||
apply_ops(&fixture, tree, &before, &manifest.files, &mut txn, &metadata, None)?;
|
|
||||||
crate::mysql_snapshot::validate_connections(&fixture, self.root)?;
|
|
||||||
crate::mysql_snapshot::isolate_connections(&fixture)?;
|
|
||||||
step(report, "Testing database updates on the private server copy", 56);
|
|
||||||
let identities = crate::backup::with_database(&fixture, |db| {
|
|
||||||
let identities = database_identities(db)?;
|
|
||||||
let realms = crate::realms::state(&fixture)?;
|
|
||||||
let mut modes = vec![realms.active];
|
|
||||||
if realms.wildcard_created { modes.push(if realms.active == crate::realms::Mode::Coa { crate::realms::Mode::Wildcard } else { crate::realms::Mode::Coa }); }
|
|
||||||
for (index, mode) in modes.into_iter().enumerate() {
|
|
||||||
let db = db.clone().for_realm(mode);
|
|
||||||
crate::schema_check::repair_missing_defaults(&db, &fixture, manifest.files.iter().find(|file| file.path == crate::schema_check::CONTRACT).map(|file| file.sha256.as_str()))?;
|
|
||||||
let list: Vec<_> = manifest.migrations.iter().filter(|migration| index == 0 || migration.db != "auth").cloned().collect();
|
|
||||||
let report = crate::migrations::apply_pending(&db, &list, &tree.join("_migrations"), &|| Ok("private rehearsal".into()))?;
|
|
||||||
if let Some((migration, error)) = report.failed { return Err(Error::Invalid(format!("Rehearsal migration {migration} failed: {error}"))); }
|
|
||||||
crate::schema_check::repair_missing_defaults(&db, &fixture, manifest.files.iter().find(|file| file.path == crate::schema_check::CONTRACT).map(|file| file.sha256.as_str()))?;
|
|
||||||
}
|
|
||||||
verify_database_identities(db, &identities)?;
|
|
||||||
Ok(identities)
|
|
||||||
})?;
|
|
||||||
step(report, "Testing startup of the private server copy", 57);
|
|
||||||
env.validate()?;
|
|
||||||
crate::backup::with_database(&fixture, |db| verify_database_identities(db, &identities))?;
|
|
||||||
env.ensure_stopped()
|
|
||||||
});
|
|
||||||
step(report, "Stopping the private server copy and verifying the installed database", 57);
|
|
||||||
let stopped = crate::mysql_snapshot::stop(&fixture);
|
|
||||||
let live_state = if result.is_ok() { crate::mysql_snapshot::stop(self.root).and_then(|()| crate::mysql_snapshot::unchanged(self.root, snapshot)) } else { Ok(()) };
|
|
||||||
let logs: BTreeMap<_, _> = ["Core/Logs/auth-console.log", "Core/Logs/world-console.log", "Core/Logs/supervisor.log", "mysql/logs/mysql-error.log"].into_iter()
|
|
||||||
.map(|path| (path, crate::diag::redact(&crate::health::tail(&fixture.join(path), 32768)))).collect();
|
|
||||||
let mut schema_reports = Vec::<serde_json::Value>::new();
|
|
||||||
if let Ok(entries) = fs::read_dir(metadata.join("diagnostics")) {
|
|
||||||
for entry in entries.flatten().filter(|entry| entry.file_name().to_string_lossy().starts_with("schema-validation-")) {
|
|
||||||
let path = fsx::ensure_within(&metadata, &entry.path())?;
|
|
||||||
if fs::metadata(&path)?.len() <= 4 * 1024 * 1024 { schema_reports.push(fsx::read_json(&path)?); }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let report = serde_json::json!({"schema":1,"checkedAt":chrono::Utc::now().to_rfc3339(),"toVersion":manifest.version,"recoveryPoint":id,
|
|
||||||
"result":if result.is_ok() && stopped.is_ok() && live_state.is_ok() {"passed"} else {"failed"}, "error":result.as_ref().err().map(ToString::to_string), "stopError":stopped.as_ref().err().map(ToString::to_string), "liveStateError":live_state.as_ref().err().map(ToString::to_string), "logs":logs,"schemaReports":schema_reports,"fixture":fixture});
|
|
||||||
let report_path = self.meta_dir.join("diagnostics").join(format!("database-rehearsal-{}.json", uuid::Uuid::new_v4()));
|
|
||||||
fsx::atomic_write_json(&report_path, &report)?;
|
|
||||||
stopped?;
|
|
||||||
let parent = fixture.parent().unwrap();
|
|
||||||
let owned = fsx::ensure_within(&self.meta_dir.join("rehearsals"), parent)?;
|
|
||||||
fs::remove_dir_all(owned)?;
|
|
||||||
live_state?;
|
|
||||||
result
|
|
||||||
}
|
|
||||||
fn preflight(&self, manifest: &Manifest) -> Result<()> {
|
fn preflight(&self, manifest: &Manifest) -> Result<()> {
|
||||||
crate::mysql_snapshot::stop_previous_rehearsals(self.meta_dir)?;
|
crate::mysql_snapshot::stop_previous_rehearsals(self.meta_dir)?;
|
||||||
if let Some(compatibility) = &manifest.compatibility {
|
if let Some(compatibility) = &manifest.compatibility {
|
||||||
@@ -1087,7 +983,7 @@ impl Env for RepackEnv<'_> {
|
|||||||
if crate::docker::is_docker(self.root) {
|
if crate::docker::is_docker(self.root) {
|
||||||
return Err(Error::Invalid(format!("The installed {} database does not match its upgrade source: {}.{}: {}", mode.name(), problem.database, problem.table, problem.detail)));
|
return Err(Error::Invalid(format!("The installed {} database does not match its upgrade source: {}.{}: {}", mode.name(), problem.database, problem.table, problem.detail)));
|
||||||
}
|
}
|
||||||
tracing::warn!(realm = mode.name(), table = %problem.table, "Installed schema differs; the private candidate rehearsal must qualify this database");
|
tracing::warn!(realm = mode.name(), table = %problem.table, "Installed schema differs; pending migrations and target validation will run with a full recovery point");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -1142,43 +1038,10 @@ impl Env for RepackEnv<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn migrate(&self, manifest: &Manifest, staged: &Path) -> Result<ApplyReport> {
|
fn migrate(&self, manifest: &Manifest, staged: &Path) -> Result<ApplyReport> {
|
||||||
let root = self.root;
|
crate::backup::with_database(self.root, |db| {
|
||||||
crate::backup::with_database(root, |db| {
|
let identities = database_identities(db)?;
|
||||||
let realms = crate::realms::state(root)?;
|
let result = migrate_databases(db, self.root, manifest, staged, &|| self.migration_snapshot())?;
|
||||||
if let Some(contract) = manifest.files.iter().find(|file| file.path == crate::schema_check::CONTRACT) {
|
if result.failed.is_none() { verify_database_identities(db, &identities)?; }
|
||||||
self.migration_snapshot()?;
|
|
||||||
crate::schema_check::repair_missing_defaults(db, root, Some(&contract.sha256))?;
|
|
||||||
}
|
|
||||||
let mut result = crate::migrations::apply_pending(db, &manifest.migrations, staged, &|| {
|
|
||||||
self.migration_snapshot()
|
|
||||||
})?;
|
|
||||||
if realms.wildcard_created && result.failed.is_none() {
|
|
||||||
let other = if realms.active == crate::realms::Mode::Coa { crate::realms::Mode::Wildcard } else { crate::realms::Mode::Coa };
|
|
||||||
let other_db = db.clone().for_realm(other);
|
|
||||||
crate::schema_check::repair_missing_defaults(&other_db, root, manifest.files.iter().find(|file| file.path == crate::schema_check::CONTRACT).map(|file| file.sha256.as_str()))?;
|
|
||||||
let shared_snapshot = result.snapshot.clone();
|
|
||||||
let migrations: Vec<_> = manifest.migrations.iter().filter(|m| m.db != "auth").cloned().collect();
|
|
||||||
let extra = crate::migrations::apply_pending(&other_db, &migrations, staged, &|| {
|
|
||||||
if let Some(id) = &shared_snapshot { return Ok(id.clone()); }
|
|
||||||
self.migration_snapshot()
|
|
||||||
})?;
|
|
||||||
result.applied.extend(extra.applied);
|
|
||||||
result.failed = extra.failed;
|
|
||||||
if result.snapshot.is_none() { result.snapshot = extra.snapshot; }
|
|
||||||
if result.failed.is_none() {
|
|
||||||
crate::schema_check::repair_missing_defaults(&other_db, root, manifest.files.iter().find(|file| file.path == crate::schema_check::CONTRACT).map(|file| file.sha256.as_str()))?;
|
|
||||||
if let Some(p) = crate::schema_check::check_with_report(&other_db, root)?.first() {
|
|
||||||
return Err(Error::Invalid(format!("Database validation failed on {}: {}.{}: {}", other.name(), p.table, p.column, p.detail)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if result.failed.is_none() {
|
|
||||||
crate::schema_check::repair_missing_defaults(db, root, manifest.files.iter().find(|file| file.path == crate::schema_check::CONTRACT).map(|file| file.sha256.as_str()))?;
|
|
||||||
let problems = crate::schema_check::check_with_report(db, root)?;
|
|
||||||
if let Some(p) = problems.first() {
|
|
||||||
return Err(Error::Invalid(format!("Database validation failed: {}.{}.{}: {} ({} problems).", p.database, p.table, p.column, p.detail, problems.len())));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -1194,23 +1057,24 @@ impl Env for RepackEnv<'_> {
|
|||||||
|
|
||||||
impl RepackEnv<'_> {
|
impl RepackEnv<'_> {
|
||||||
fn validate_server(&self, check_schema: bool) -> Result<()> {
|
fn validate_server(&self, check_schema: bool) -> Result<()> {
|
||||||
let stopped = crate::driver::run(self.root, crate::driver::Verb::StopAll)?;
|
crate::mysql_snapshot::stop(self.root)?;
|
||||||
if !stopped.ok { return Err(Error::Invalid("Could not stop the server before isolated validation.".into())); }
|
|
||||||
self.ensure_stopped()?;
|
self.ensure_stopped()?;
|
||||||
self.recover_validation()?;
|
self.recover_validation()?;
|
||||||
let result = (|| -> Result<()> {
|
let result = (|| -> Result<()> {
|
||||||
crate::update_isolation::begin(self.root, self.meta_dir)?;
|
crate::update_isolation::begin(self.root, self.meta_dir)?;
|
||||||
use crate::process::{observe, ServiceState};
|
use crate::process::{observe, ServiceState};
|
||||||
if check_schema { crate::backup::with_database(self.root, |db| {
|
let identities = crate::backup::with_database(self.root, |db| {
|
||||||
let mut modes = vec![crate::realms::Mode::Coa];
|
if check_schema {
|
||||||
if crate::realms::state(self.root)?.wildcard_created { modes.push(crate::realms::Mode::Wildcard); }
|
let mut modes = vec![crate::realms::Mode::Coa];
|
||||||
for mode in modes {
|
if crate::realms::state(self.root)?.wildcard_created { modes.push(crate::realms::Mode::Wildcard); }
|
||||||
if let Some(p) = crate::schema_check::check_with_report(&db.clone().for_realm(mode), self.root)?.first() {
|
for mode in modes {
|
||||||
return Err(Error::Invalid(format!("Database validation failed on {}: {}.{}: {}", mode.name(), p.database, p.table, p.detail)));
|
if let Some(p) = crate::schema_check::check_with_report(&db.clone().for_realm(mode), self.root)?.first() {
|
||||||
|
return Err(Error::Invalid(format!("Database validation failed on {}: {}.{}: {}", mode.name(), p.database, p.table, p.detail)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
database_identities(db)
|
||||||
})?; }
|
})?;
|
||||||
// The check start is the first start of the new build: give the module configs the settings the update added
|
// The check start is the first start of the new build: give the module configs the settings the update added
|
||||||
// (as a normal start does), or the server logs a "missing property" line for every one of them.
|
// (as a normal start does), or the server logs a "missing property" line for every one of them.
|
||||||
match crate::registry::MetaDir::open(self.meta_dir) {
|
match crate::registry::MetaDir::open(self.meta_dir) {
|
||||||
@@ -1248,6 +1112,7 @@ impl RepackEnv<'_> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
if healthy {
|
if healthy {
|
||||||
|
crate::backup::with_database(self.root, |db| verify_database_identities(db, &identities))?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
let cause = crate::driver::startup_failure(self.root, &started);
|
let cause = crate::driver::startup_failure(self.root, &started);
|
||||||
@@ -1255,10 +1120,7 @@ impl RepackEnv<'_> {
|
|||||||
let _ = crate::driver::run(self.root, crate::driver::Verb::StopAll);
|
let _ = crate::driver::run(self.root, crate::driver::Verb::StopAll);
|
||||||
Err(Error::Invalid(cause))
|
Err(Error::Invalid(cause))
|
||||||
})();
|
})();
|
||||||
let stopped = crate::driver::run(self.root, crate::driver::Verb::StopAll)?;
|
crate::mysql_snapshot::stop(self.root)?;
|
||||||
if !stopped.ok {
|
|
||||||
return Err(Error::Invalid("Isolated validation could not stop the server; recovery configurations were preserved.".into()));
|
|
||||||
}
|
|
||||||
self.ensure_stopped()?;
|
self.ensure_stopped()?;
|
||||||
self.recover_validation()?;
|
self.recover_validation()?;
|
||||||
result
|
result
|
||||||
@@ -1266,6 +1128,40 @@ impl RepackEnv<'_> {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// One migration pass for both realms, with shared history checks and one recovery point.
|
||||||
|
fn migrate_databases(db: &crate::db::Db, root: &Path, manifest: &Manifest, staged: &Path, snapshot: &dyn Fn() -> Result<String>) -> Result<ApplyReport> {
|
||||||
|
let realms = crate::realms::state(root)?;
|
||||||
|
let mut modes = vec![realms.active];
|
||||||
|
if realms.wildcard_created {
|
||||||
|
modes.push(if realms.active == crate::realms::Mode::Coa { crate::realms::Mode::Wildcard } else { crate::realms::Mode::Coa });
|
||||||
|
}
|
||||||
|
// Reject a broken history in either realm before changing the first one.
|
||||||
|
for (index, mode) in modes.iter().enumerate() {
|
||||||
|
let list: Vec<_> = manifest.migrations.iter().filter(|m| index == 0 || !crate::migrations::shared_auth(&m.db)).cloned().collect();
|
||||||
|
crate::migrations::preflight(&db.clone().for_realm(*mode), &list)?;
|
||||||
|
}
|
||||||
|
let contract = manifest.files.iter().find(|file| file.path == crate::schema_check::CONTRACT).map(|file| file.sha256.as_str());
|
||||||
|
let mut result = ApplyReport { applied: vec![], failed: None, snapshot: None };
|
||||||
|
if contract.is_some() { result.snapshot = Some(snapshot()?); }
|
||||||
|
for (index, mode) in modes.into_iter().enumerate() {
|
||||||
|
let db = db.clone().for_realm(mode);
|
||||||
|
let list: Vec<_> = manifest.migrations.iter().filter(|m| index == 0 || !crate::migrations::shared_auth(&m.db)).cloned().collect();
|
||||||
|
crate::schema_check::repair_missing_defaults(&db, root, contract)?;
|
||||||
|
let extra = crate::migrations::apply_pending(&db, &list, staged, &|| {
|
||||||
|
if let Some(id) = &result.snapshot { Ok(id.clone()) } else { snapshot() }
|
||||||
|
})?;
|
||||||
|
result.applied.extend(extra.applied);
|
||||||
|
result.failed = extra.failed;
|
||||||
|
if result.snapshot.is_none() { result.snapshot = extra.snapshot; }
|
||||||
|
if result.failed.is_some() { return Ok(result); }
|
||||||
|
crate::schema_check::repair_missing_defaults(&db, root, contract)?;
|
||||||
|
if let Some(p) = crate::schema_check::check_with_report(&db, root)?.first() {
|
||||||
|
return Err(Error::Invalid(format!("Database validation failed on {}: {}.{}: {}", mode.name(), p.table, p.column, p.detail)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
fn database_identities(db: &crate::db::Db) -> Result<BTreeMap<String, std::collections::BTreeSet<String>>> {
|
fn database_identities(db: &crate::db::Db) -> Result<BTreeMap<String, std::collections::BTreeSet<String>>> {
|
||||||
let db = db.clone().for_realm(crate::realms::Mode::Coa);
|
let db = db.clone().for_realm(crate::realms::Mode::Coa);
|
||||||
let mut identities = BTreeMap::new();
|
let mut identities = BTreeMap::new();
|
||||||
@@ -1285,7 +1181,7 @@ fn verify_database_identities(db: &crate::db::Db, before: &BTreeMap<String, std:
|
|||||||
let after = database_identities(db)?;
|
let after = database_identities(db)?;
|
||||||
for (schema, expected) in before {
|
for (schema, expected) in before {
|
||||||
if after.get(schema).is_none_or(|actual| !expected.is_subset(actual)) {
|
if after.get(schema).is_none_or(|actual| !expected.is_subset(actual)) {
|
||||||
return Err(Error::Invalid(format!("The private update changed or removed existing character or account identities in {schema}.")));
|
return Err(Error::Invalid(format!("The update changed or removed existing character or account identities in {schema}.")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -1313,19 +1209,15 @@ mod tests {
|
|||||||
migrate_fail: Cell<bool>,
|
migrate_fail: Cell<bool>,
|
||||||
preflight_fail: Cell<bool>,
|
preflight_fail: Cell<bool>,
|
||||||
restore_fail: Cell<bool>,
|
restore_fail: Cell<bool>,
|
||||||
rehearsal_fail: Cell<bool>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Fake {
|
impl Fake {
|
||||||
fn ok() -> Fake {
|
fn ok() -> Fake {
|
||||||
Fake { activation_expected: RefCell::new(None), activation_failed: Cell::new(false), activated: Cell::new(false), automatic_rollback: Cell::new(false), recovery_healthy: Cell::new(true), stopped: Cell::new(true), healthy: Cell::new(true), snapshot_ok: Cell::new(true), calls: Default::default(), migrate_fail: Cell::new(false), preflight_fail: Cell::new(false), restore_fail: Cell::new(false), rehearsal_fail: Cell::new(false) }
|
Fake { activation_expected: RefCell::new(None), activation_failed: Cell::new(false), activated: Cell::new(false), automatic_rollback: Cell::new(false), recovery_healthy: Cell::new(true), stopped: Cell::new(true), healthy: Cell::new(true), snapshot_ok: Cell::new(true), calls: Default::default(), migrate_fail: Cell::new(false), preflight_fail: Cell::new(false), restore_fail: Cell::new(false) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Env for Fake {
|
impl Env for Fake {
|
||||||
fn rehearse(&self, _: &Manifest, _: &Path, _: &[PlanItem], _: &str) -> Result<()> {
|
|
||||||
if self.rehearsal_fail.get() { Err(Error::Invalid("SQL failed in the private copy".into())) } else { Ok(()) }
|
|
||||||
}
|
|
||||||
fn activate(&self) -> Result<()> {
|
fn activate(&self) -> Result<()> {
|
||||||
if let Some((meta, version)) = self.activation_expected.borrow().as_ref() {
|
if let Some((meta, version)) = self.activation_expected.borrow().as_ref() {
|
||||||
ensure_recovered(meta)?;
|
ensure_recovered(meta)?;
|
||||||
@@ -1584,6 +1476,8 @@ mod tests {
|
|||||||
let (_, mut meta) = MetaDir::open(&w.meta).unwrap();
|
let (_, mut meta) = MetaDir::open(&w.meta).unwrap();
|
||||||
meta.original_hashes.insert("Scripts/manage.py".into(), fsx::sha256_bytes(integrated.as_bytes()));
|
meta.original_hashes.insert("Scripts/manage.py".into(), fsx::sha256_bytes(integrated.as_bytes()));
|
||||||
fsx::atomic_write_json(&w.meta.join("install.json"), &meta).unwrap();
|
fsx::atomic_write_json(&w.meta.join("install.json"), &meta).unwrap();
|
||||||
|
let original = fetch_manifest(&Source::Dir(w.pkg.clone()), &w.key).unwrap().0;
|
||||||
|
for part in &original.archive.as_ref().unwrap().parts { fs::remove_file(w.pkg.join(&part.name)).unwrap(); }
|
||||||
let p = preview(&w.root, &meta, &Source::Dir(w.pkg.clone()), &w.key, &BTreeMap::new()).unwrap();
|
let p = preview(&w.root, &meta, &Source::Dir(w.pkg.clone()), &w.key, &BTreeMap::new()).unwrap();
|
||||||
assert_eq!(p.items.iter().find(|item| item.path == "Scripts/manage.py").unwrap().action, Action::Skip);
|
assert_eq!(p.items.iter().find(|item| item.path == "Scripts/manage.py").unwrap().action, Action::Skip);
|
||||||
let manifest = fetch_manifest(&Source::Dir(w.pkg.clone()), &w.key).unwrap().0;
|
let manifest = fetch_manifest(&Source::Dir(w.pkg.clone()), &w.key).unwrap().0;
|
||||||
@@ -1591,7 +1485,7 @@ mod tests {
|
|||||||
assert_eq!(applied_plan.iter().find(|item| item.path == "Scripts/manage.py").unwrap().action, Action::Skip);
|
assert_eq!(applied_plan.iter().find(|item| item.path == "Scripts/manage.py").unwrap().action, Action::Skip);
|
||||||
let mut changed = fetch_manifest(&Source::Dir(w.pkg.clone()), &w.key).unwrap().0;
|
let mut changed = fetch_manifest(&Source::Dir(w.pkg.clone()), &w.key).unwrap().0;
|
||||||
changed.files.iter_mut().find(|file| file.path == "Scripts/manage.py").unwrap().sha256 = fsx::sha256_bytes(b"new launcher");
|
changed.files.iter_mut().find(|file| file.path == "Scripts/manage.py").unwrap().sha256 = fsx::sha256_bytes(b"new launcher");
|
||||||
assert!(launcher_already_integrated(&w.root, &w.meta, &meta, &changed, &Source::Dir(w.pkg.clone())).is_err());
|
assert!(!launcher_already_integrated(&w.root, &meta, &changed).unwrap());
|
||||||
write(&w.root, "Scripts/manage.py", format!("{integrated}\n# user edit\n").as_bytes());
|
write(&w.root, "Scripts/manage.py", format!("{integrated}\n# user edit\n").as_bytes());
|
||||||
// Same version: the owner's edit is not an update (Repair lists and restores it).
|
// Same version: the owner's edit is not an update (Repair lists and restores it).
|
||||||
let p = preview(&w.root, &meta, &Source::Dir(w.pkg.clone()), &w.key, &BTreeMap::new()).unwrap();
|
let p = preview(&w.root, &meta, &Source::Dir(w.pkg.clone()), &w.key, &BTreeMap::new()).unwrap();
|
||||||
@@ -1738,7 +1632,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn incompatible_source_or_platform_is_rejected_without_touching_installation() {
|
fn unlisted_source_is_allowed_but_unknown_versions_and_other_platforms_are_rejected() {
|
||||||
let w = world(&[], false);
|
let w = world(&[], false);
|
||||||
let (_, meta) = MetaDir::open(&w.meta).unwrap();
|
let (_, meta) = MetaDir::open(&w.meta).unwrap();
|
||||||
let mut manifest = Manifest::parse(&fs::read(w.pkg.join("manifest.json")).unwrap()).unwrap();
|
let mut manifest = Manifest::parse(&fs::read(w.pkg.join("manifest.json")).unwrap()).unwrap();
|
||||||
@@ -1751,7 +1645,12 @@ mod tests {
|
|||||||
manifest.validate().unwrap();
|
manifest.validate().unwrap();
|
||||||
validate_candidate(&meta, &manifest).unwrap();
|
validate_candidate(&meta, &manifest).unwrap();
|
||||||
manifest.compatibility.as_mut().unwrap().source_versions = vec!["0.9.0".into()];
|
manifest.compatibility.as_mut().unwrap().source_versions = vec!["0.9.0".into()];
|
||||||
assert!(validate_candidate(&meta, &manifest).unwrap_err().to_string().contains("compatibility matrix"));
|
validate_candidate(&meta, &manifest).unwrap();
|
||||||
|
let mut unknown = meta.clone();
|
||||||
|
unknown.core.version = None;
|
||||||
|
assert!(validate_candidate(&unknown, &manifest).unwrap_err().to_string().contains("unknown"));
|
||||||
|
unknown.core.version = Some("unverified-repack".into());
|
||||||
|
assert!(validate_candidate(&unknown, &manifest).is_err());
|
||||||
let mut already_updated = meta.clone();
|
let mut already_updated = meta.clone();
|
||||||
already_updated.core.version = Some(manifest.version.clone());
|
already_updated.core.version = Some(manifest.version.clone());
|
||||||
validate_candidate(&already_updated, &manifest).unwrap();
|
validate_candidate(&already_updated, &manifest).unwrap();
|
||||||
@@ -2072,20 +1971,17 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn failed_rehearsal_preserves_installed_files_version_and_recovery_point() {
|
fn update_saves_recovery_then_migrates_and_validates_once() {
|
||||||
let w = world(&[], true);
|
let w = world(&[], true);
|
||||||
let env = Fake::ok();
|
let env = Fake::ok();
|
||||||
env.rehearsal_fail.set(true);
|
let result = run(&w, &env, BTreeMap::new(), None).unwrap();
|
||||||
let original = fsx::sha256_file(&w.meta.join("install.json")).unwrap();
|
assert_eq!(result.txn.state, State::Committed);
|
||||||
let error = run(&w, &env, BTreeMap::new(), None).unwrap_err();
|
let calls = env.calls.borrow();
|
||||||
assert!(error.to_string().contains("installed server was not updated"));
|
assert_eq!(calls.iter().filter(|call| **call == "snapshot").count(), 1);
|
||||||
assert_eq!(read(&w, "Core/worldserver.exe"), b"world-v1");
|
assert_eq!(calls.iter().filter(|call| **call == "migrate").count(), 1);
|
||||||
assert_eq!(fsx::sha256_file(&w.meta.join("install.json")).unwrap(), original);
|
assert_eq!(calls.iter().filter(|call| **call == "validate").count(), 1);
|
||||||
assert_eq!(&*env.calls.borrow(), &["stop", "snapshot"]);
|
assert!(calls.iter().position(|call| *call == "snapshot") < calls.iter().position(|call| *call == "migrate"));
|
||||||
assert!(pending_checked(&w.meta).unwrap().is_none());
|
assert!(!w.meta.join("rehearsals").exists());
|
||||||
let journal = journals(&w.meta).unwrap().remove(0);
|
|
||||||
assert_eq!(journal.recovery_point.as_deref(), Some("rp-1"));
|
|
||||||
assert!(!journal.databases_started && journal.ops.is_empty());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -1,12 +1,9 @@
|
|||||||
use std::collections::BTreeSet;
|
use crate::{manifest::Manifest, pkgsource::{self, Source}, registry::InstallMeta, update, Result};
|
||||||
|
|
||||||
use crate::{fsx, manifest::Manifest, pkgsource::{self, Source}, registry::InstallMeta, update, Error, Result};
|
|
||||||
|
|
||||||
const RELEASES: &str = "https://github.com/Corfirean/coa-server-build/releases/download/server-";
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Selection {
|
pub struct Selection {
|
||||||
pub source: Source,
|
pub source: Source,
|
||||||
|
// Retained for the frontend response shape; direct updates never offer a bridge.
|
||||||
pub latest_version: Option<String>,
|
pub latest_version: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16,48 +13,8 @@ pub fn select(meta: &InstallMeta, source: Source, key: &str) -> Result<Selection
|
|||||||
|
|
||||||
fn select_with(meta: &InstallMeta, source: Source, fetch: &mut impl FnMut(&Source) -> Result<(Manifest, Vec<u8>)>) -> Result<Selection> {
|
fn select_with(meta: &InstallMeta, source: Source, fetch: &mut impl FnMut(&Source) -> Result<(Manifest, Vec<u8>)>) -> Result<Selection> {
|
||||||
let (manifest, _) = fetch(&source)?;
|
let (manifest, _) = fetch(&source)?;
|
||||||
if update::validate_candidate(meta, &manifest).is_ok() {
|
update::validate_candidate(meta, &manifest)?;
|
||||||
return Ok(Selection { source, latest_version: None });
|
Ok(Selection { source, latest_version: None })
|
||||||
}
|
|
||||||
let original = update::validate_candidate(meta, &manifest).unwrap_err();
|
|
||||||
if !matches!(&source, Source::Url(url) if url.trim_end_matches('/') == format!("{RELEASES}{}", manifest.version)) {
|
|
||||||
return Err(original);
|
|
||||||
}
|
|
||||||
let Some(installed) = meta.core.version.as_deref().and_then(crate::manifest::parse_version) else { return Err(original); };
|
|
||||||
if manifest.kind != crate::manifest::Kind::Update
|
|
||||||
|| manifest.compatibility.as_ref().is_none_or(|c| c.platform != if cfg!(windows) { "windows-x86_64" } else { "linux-x86_64" })
|
|
||||||
|| !manifest.compatible_with_manager(crate::MANAGER_VERSION)
|
|
||||||
|| crate::manifest::parse_version(&manifest.version).is_none_or(|version| version <= installed) {
|
|
||||||
return Err(original);
|
|
||||||
}
|
|
||||||
let mut visited = BTreeSet::new();
|
|
||||||
let Some(source) = find(meta, &manifest, fetch, &mut visited, 0)? else { return Err(original); };
|
|
||||||
Ok(Selection { source, latest_version: Some(manifest.version) })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find(meta: &InstallMeta, manifest: &Manifest, fetch: &mut impl FnMut(&Source) -> Result<(Manifest, Vec<u8>)>, visited: &mut BTreeSet<String>, depth: usize) -> Result<Option<Source>> {
|
|
||||||
if depth >= 8 || visited.len() >= 32 { return Ok(None); }
|
|
||||||
let Some(compatibility) = &manifest.compatibility else { return Ok(None); };
|
|
||||||
let Some(installed) = meta.core.version.as_deref().and_then(crate::manifest::parse_version) else { return Ok(None); };
|
|
||||||
let target = crate::manifest::parse_version(&manifest.version).unwrap();
|
|
||||||
let mut versions: Vec<_> = compatibility.source_versions.iter().filter_map(|version| {
|
|
||||||
crate::manifest::parse_version(version).filter(|number| *number > installed && *number < target).map(|number| (number, version))
|
|
||||||
}).collect();
|
|
||||||
versions.sort_by(|left, right| right.0.cmp(&left.0));
|
|
||||||
for (_, version) in versions {
|
|
||||||
let Some(database) = compatibility.source_databases.get(version) else { continue; };
|
|
||||||
if !visited.insert(version.clone()) || visited.len() > 32 { continue; }
|
|
||||||
let source = Source::Url(format!("{RELEASES}{version}"));
|
|
||||||
let (bridge, bytes) = fetch(&source)?;
|
|
||||||
if bridge.version != *version || fsx::sha256_bytes(&bytes) != database.manifest_sha256 {
|
|
||||||
return Err(Error::InvalidManifest("The intermediate update differs from the signed compatibility matrix.".into()));
|
|
||||||
}
|
|
||||||
if update::validate_candidate(meta, &bridge).is_ok() { return Ok(Some(source)); }
|
|
||||||
if bridge.kind != crate::manifest::Kind::Update || !bridge.compatible_with_manager(crate::MANAGER_VERSION)
|
|
||||||
|| bridge.compatibility.as_ref().is_none_or(|c| c.platform != compatibility.platform) { continue; }
|
|
||||||
if let Some(source) = find(meta, &bridge, fetch, visited, depth + 1)? { return Ok(Some(source)); }
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -67,12 +24,12 @@ mod tests {
|
|||||||
use ed25519_dalek::{Signer, SigningKey};
|
use ed25519_dalek::{Signer, SigningKey};
|
||||||
use std::{fs, path::Path};
|
use std::{fs, path::Path};
|
||||||
|
|
||||||
fn package(version: &str, from: &str, digest: &str) -> serde_json::Value {
|
fn package() -> serde_json::Value {
|
||||||
serde_json::json!({"schema":1,"kind":"update","version":version,"core":{"commit":"a".repeat(40)},
|
serde_json::json!({"schema":1,"kind":"update","version":"0.261015.58","core":{"commit":"a".repeat(40)},
|
||||||
"builtAt":"test","minManagerVersion":"0.6.16","files":[],"migrations":[],"archive":null,
|
"builtAt":"test","minManagerVersion":"0.6.16","files":[],"migrations":[],"archive":null,
|
||||||
"compatibility":{"schema":1,"platform":if cfg!(windows) {"windows-x86_64"} else {"linux-x86_64"},
|
"compatibility":{"schema":1,"platform":if cfg!(windows) {"windows-x86_64"} else {"linux-x86_64"},
|
||||||
"coreCommit":"a".repeat(40),"moduleCommits":{"squid":"b".repeat(40)},"clientPatchVersion":"1.7",
|
"coreCommit":"a".repeat(40),"moduleCommits":{"squid":"b".repeat(40)},"clientPatchVersion":"1.7",
|
||||||
"sourceVersions":[from],"sourceDatabases":{from:{"manifestSha256":digest,"schemaSha256":null}}}})
|
"sourceVersions":["0.261013.54"],"sourceDatabases":{"0.261013.54":{"manifestSha256":"c".repeat(64),"schemaSha256":null}}}})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn meta(version: &str) -> InstallMeta {
|
fn meta(version: &str) -> InstallMeta {
|
||||||
@@ -81,100 +38,56 @@ mod tests {
|
|||||||
meta
|
meta
|
||||||
}
|
}
|
||||||
|
|
||||||
fn signed_fixture(values: &[serde_json::Value], tamper: Option<&str>) -> (tempfile::TempDir, String) {
|
fn fixture(value: &serde_json::Value, tamper: bool) -> (tempfile::TempDir, String) {
|
||||||
let folder = tempfile::tempdir().unwrap();
|
let folder = tempfile::tempdir().unwrap();
|
||||||
let signing = SigningKey::from_bytes(&[37; 32]);
|
let signing = SigningKey::from_bytes(&[37; 32]);
|
||||||
for value in values {
|
let bytes = serde_json::to_vec(value).unwrap();
|
||||||
let version = value["version"].as_str().unwrap();
|
fs::write(folder.path().join("manifest.json.sig"), STANDARD.encode(signing.sign(&bytes).to_bytes())).unwrap();
|
||||||
let dir = folder.path().join(version);
|
fs::write(folder.path().join("manifest.json"), if tamper { b"tampered".as_slice() } else { &bytes }).unwrap();
|
||||||
fs::create_dir(&dir).unwrap();
|
|
||||||
let bytes = serde_json::to_vec(value).unwrap();
|
|
||||||
fs::write(dir.join("manifest.json.sig"), STANDARD.encode(signing.sign(&bytes).to_bytes())).unwrap();
|
|
||||||
fs::write(dir.join("manifest.json"), &bytes).unwrap();
|
|
||||||
if tamper == Some(version) { fs::write(dir.join("manifest.json"), b"tampered").unwrap(); }
|
|
||||||
}
|
|
||||||
(folder, STANDARD.encode(signing.verifying_key().to_bytes()))
|
(folder, STANDARD.encode(signing.verifying_key().to_bytes()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn select_fixture(values: &[serde_json::Value], installed: &str, tamper: Option<&str>) -> Result<Selection> {
|
|
||||||
let (folder, key) = signed_fixture(values, tamper);
|
|
||||||
let latest = values.last().unwrap()["version"].as_str().unwrap();
|
|
||||||
select_with(&meta(installed), Source::Url(format!("{RELEASES}{latest}")), &mut |source| {
|
|
||||||
let Source::Url(url) = source else { unreachable!() };
|
|
||||||
let version = url.strip_prefix(RELEASES).unwrap();
|
|
||||||
pkgsource::fetch_manifest(&Source::Dir(folder.path().join(version)), &key)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn pair() -> Vec<serde_json::Value> {
|
|
||||||
let bridge = package("1.1.0", "1.0.0", &"c".repeat(64));
|
|
||||||
let digest = fsx::sha256_bytes(&serde_json::to_vec(&bridge).unwrap());
|
|
||||||
vec![bridge, package("1.2.0", "1.1.0", &digest)]
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn finds_a_signed_intermediate_without_relaxing_candidate_validation() {
|
fn every_known_older_version_selects_the_latest_signed_package_once() {
|
||||||
let selection = select_fixture(&pair(), "1.0.0", None).unwrap();
|
for installed in ["0.2.0", "0.261004.20", "0.261005.0", "0.261007.24", "0.261011.800006", "0.261013.54", "0.261015.58"] {
|
||||||
assert!(matches!(selection.source, Source::Url(url) if url.ends_with("1.1.0")));
|
let (folder, key) = fixture(&package(), false);
|
||||||
assert_eq!(selection.latest_version.as_deref(), Some("1.2.0"));
|
for kind in [crate::registry::InstallKind::New, crate::registry::InstallKind::Imported] {
|
||||||
let latest = Manifest::parse(&serde_json::to_vec(pair().last().unwrap()).unwrap()).unwrap();
|
let mut installation = meta(installed);
|
||||||
assert!(update::validate_candidate(&meta("1.0.0"), &latest).is_err());
|
installation.kind = kind;
|
||||||
}
|
let mut calls = 0;
|
||||||
|
let selected = select_with(&installation, Source::Dir(folder.path().into()), &mut |source| {
|
||||||
#[test]
|
calls += 1;
|
||||||
fn direct_updates_and_current_versions_do_not_request_an_intermediate() {
|
pkgsource::fetch_manifest(source, &key)
|
||||||
for installed in ["1.1.0", "1.2.0"] {
|
}).unwrap();
|
||||||
let selection = select_fixture(&pair(), installed, None).unwrap();
|
assert!(matches!(selected.source, Source::Dir(path) if path == folder.path()));
|
||||||
assert!(selection.latest_version.is_none());
|
assert!(selected.latest_version.is_none());
|
||||||
}
|
assert_eq!(calls, 1, "no intermediate package fetches for {installed}");
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn rejects_tampered_or_replaced_intermediate_manifests() {
|
|
||||||
assert!(select_fixture(&pair(), "1.0.0", Some("1.1.0")).is_err());
|
|
||||||
let mut values = pair();
|
|
||||||
values[0]["builtAt"] = "replaced".into();
|
|
||||||
assert!(select_fixture(&values, "1.0.0", None).unwrap_err().to_string().contains("differs"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn follows_multiple_tested_edges_and_rejects_missing_paths() {
|
|
||||||
let mut values = pair();
|
|
||||||
let digest = fsx::sha256_bytes(&serde_json::to_vec(values.last().unwrap()).unwrap());
|
|
||||||
values.push(package("1.3.0", "1.2.0", &digest));
|
|
||||||
let selection = select_fixture(&values, "1.0.0", None).unwrap();
|
|
||||||
assert!(matches!(selection.source, Source::Url(url) if url.ends_with("1.1.0")));
|
|
||||||
assert!(select_fixture(&values, "0.9.0", None).is_err());
|
|
||||||
assert!(select_fixture(&values, "2.0.0", None).is_err());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn never_routes_custom_sources_or_unknown_installations() {
|
|
||||||
let values = pair();
|
|
||||||
let (folder, key) = signed_fixture(&values, None);
|
|
||||||
let mut calls = 0;
|
|
||||||
let result = select_with(&meta("1.0.0"), Source::Dir(folder.path().join("1.2.0")), &mut |source| {
|
|
||||||
calls += 1;
|
|
||||||
pkgsource::fetch_manifest(source, &key)
|
|
||||||
});
|
|
||||||
assert!(result.is_err());
|
|
||||||
assert_eq!(calls, 1);
|
|
||||||
assert!(select_fixture(&values, "unknown", None).is_err());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn rejects_incompatible_platforms_and_manager_versions() {
|
|
||||||
for index in [0, 1] {
|
|
||||||
for (field, value) in [("minManagerVersion", "99.0.0"), ("platform", "other-platform")] {
|
|
||||||
let mut values = pair();
|
|
||||||
if field == "platform" { values[index]["compatibility"][field] = value.into(); }
|
|
||||||
else { values[index][field] = value.into(); }
|
|
||||||
if index == 0 {
|
|
||||||
let digest = fsx::sha256_bytes(&serde_json::to_vec(&values[0]).unwrap());
|
|
||||||
values[1]["compatibility"]["sourceDatabases"]["1.1.0"]["manifestSha256"] = digest.into();
|
|
||||||
}
|
|
||||||
assert!(select_fixture(&values, "1.0.0", None).is_err());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unknown_versions_and_downgrades_remain_blocked() {
|
||||||
|
let (folder, key) = fixture(&package(), false);
|
||||||
|
for installed in ["unknown", "a65be06ec62f4", "0.261016.59"] {
|
||||||
|
assert!(select(&meta(installed), Source::Dir(folder.path().into()), &key).is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn signed_target_is_required_even_for_unlisted_old_versions() {
|
||||||
|
let (folder, key) = fixture(&package(), true);
|
||||||
|
assert!(select(&meta("0.261004.20"), Source::Dir(folder.path().into()), &key).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn platform_and_manager_requirements_are_still_enforced() {
|
||||||
|
for (field, value) in [("minManagerVersion", "99.0.0"), ("platform", "other-platform")] {
|
||||||
|
let mut target = package();
|
||||||
|
if field == "platform" { target["compatibility"][field] = value.into(); }
|
||||||
|
else { target[field] = value.into(); }
|
||||||
|
let (folder, key) = fixture(&target, false);
|
||||||
|
assert!(select(&meta("0.261004.20"), Source::Dir(folder.path().into()), &key).is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
# Update workflow
|
||||||
|
|
||||||
|
The Manager selects the latest signed package directly for any known older server version. The source-version list records release qualification; it is not an exclusive allowlist and does not prove that a particular installed database can apply the package. Unknown imported versions, downgrades, incompatible platforms and newer Manager requirements remain blocked.
|
||||||
|
|
||||||
|
The updater verifies the signed manifest and archive, checks file changes and database history, stops the server, and saves one complete recovery point. It then replaces files, applies pending SQL once, verifies the target schema and starts the updated server once for validation. Shared authentication migrations run once; both realm histories are checked before changing either. Account and character identities are compared before and after migration and startup.
|
||||||
|
|
||||||
|
Account migration history is stored in the shared account database. Read-only checks recognize legacy auth records from both realms. Write operations adopt existing records and preserve already-applied core history; unknown legacy checksums stay unknown. History is read in two batched client calls, with explicit physical schemas and consistent text collations. Use the current Manager for subsequent database operations.
|
||||||
|
|
||||||
|
Applied migrations are skipped; changed checksums and interrupted/failed records stop the update. Duplicate SQL identifiers for the same schema are rejected before execution. SQL or startup failure restores files and databases together. If recovery fails, startup remains blocked and the recovery artifacts remain available.
|
||||||
|
|
||||||
|
The per-update nested server rehearsal and intermediate-package routing have been removed. Full-copy upgrade/startup testing belongs to release qualification, on independent disposable installations. Cleanup of interrupted rehearsals from older Manager versions remains for compatibility. Signed packages, recovery verification, configuration merge and startup validation stay enabled. No migration guard is bypassed and no unexecuted SQL is marked applied. Retired source archives do not prevent a direct update, but the database history must still be consistent and target checks must pass.
|
||||||
|
|
||||||
|
## Verify with an actual installation copy
|
||||||
|
|
||||||
|
`cargo run -p coa-core --example upgrade_fixture -- <copy-root> <signed-package-directory>` exercises the production updater, including recovery copies and startup. It refuses mutation unless the directory name starts with `coa-schema-fixture-real` and contains `.coa-update-fixture`. Prepare a disposable copy from a verified recovery point, with separate ports, metadata and no client path. Never point it at a working installation.
|
||||||
|
|
||||||
|
`migration_plan <copy-root> <signed-manifest>` reads database history without creating a migration ledger. On a separate marked `update-database-diagnosis-*` copy, `--apply <migration-directory>` exercises the production database phase. `--sql-only <migration-directory>` verifies and applies the signed SQL list for both realms and compares account/character identities. SQL-only success is not full package/startup qualification and does not verify the target schema contract.
|
||||||
|
|
||||||
|
Record package signatures, exact source recovery point, actual result, repeat-run behavior and unavailable stages. Do not label a route universally tested based on a version list or SQL-only success.
|
||||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "coa-server-manager",
|
"name": "coa-server-manager",
|
||||||
"version": "0.6.18",
|
"version": "0.6.21",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "coa-server-manager",
|
"name": "coa-server-manager",
|
||||||
"version": "0.6.18",
|
"version": "0.6.21",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tauri-apps/api": "^2.12.0",
|
"@tauri-apps/api": "^2.12.0",
|
||||||
"@tauri-apps/plugin-dialog": "^2.8.0",
|
"@tauri-apps/plugin-dialog": "^2.8.0",
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "coa-server-manager",
|
"name": "coa-server-manager",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "0.6.18",
|
"version": "0.6.21",
|
||||||
"license": "AGPL-3.0-only",
|
"license": "AGPL-3.0-only",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"$schema": "https://schema.tauri.app/config/2",
|
"$schema": "https://schema.tauri.app/config/2",
|
||||||
"productName": "CoA Server Manager",
|
"productName": "CoA Server Manager",
|
||||||
"version": "0.6.18",
|
"version": "0.6.21",
|
||||||
"identifier": "com.coa.servermanager",
|
"identifier": "com.coa.servermanager",
|
||||||
"build": {
|
"build": {
|
||||||
"beforeDevCommand": "npm run dev",
|
"beforeDevCommand": "npm run dev",
|
||||||
|
|||||||
@@ -430,7 +430,7 @@ const en = {
|
|||||||
"upd.folderHint": "Update package folder (leave empty to use the official source)",
|
"upd.folderHint": "Update package folder (leave empty to use the official source)",
|
||||||
"upd.available": "Version {to} is available",
|
"upd.available": "Version {to} is available",
|
||||||
"upd.availableFrom": "Version {to} is available (you have {from})",
|
"upd.availableFrom": "Version {to} is available (you have {from})",
|
||||||
"upd.intermediate": "Update to {to} first, then check again to continue to {latest}. Each step uses a tested upgrade path.",
|
"upd.intermediate": "Update to {to} first, then check again to continue to {latest}. The package supports this version sequence; your database is checked when you update.",
|
||||||
"upd.summary": "Download {size} · {files} file(s) change",
|
"upd.summary": "Download {size} · {files} file(s) change",
|
||||||
"upd.summaryDb": "Download {size} · {files} file(s) change · {db} database update(s)",
|
"upd.summaryDb": "Download {size} · {files} file(s) change · {db} database update(s)",
|
||||||
"upd.conflict": "This file was modified outside CoA Server Manager.",
|
"upd.conflict": "This file was modified outside CoA Server Manager.",
|
||||||
|
|||||||
@@ -412,7 +412,7 @@ const ru: Partial<Record<keyof typeof en, string>> = {
|
|||||||
"upd.folderHint": "Папка с пакетом обновления (оставьте пустым, чтобы использовать официальный источник)",
|
"upd.folderHint": "Папка с пакетом обновления (оставьте пустым, чтобы использовать официальный источник)",
|
||||||
"upd.available": "Доступна версия {to}",
|
"upd.available": "Доступна версия {to}",
|
||||||
"upd.availableFrom": "Доступна версия {to} (у вас {from})",
|
"upd.availableFrom": "Доступна версия {to} (у вас {from})",
|
||||||
"upd.intermediate": "Сначала обновитесь до {to}, затем проверьте обновления ещё раз для перехода на {latest}. Каждый этап использует проверенный путь обновления.",
|
"upd.intermediate": "Сначала обновитесь до {to}, затем проверьте обновления ещё раз для перехода на {latest}. Пакет поддерживает эту последовательность версий; состояние вашей базы проверяется при обновлении.",
|
||||||
"upd.summary": "Загрузка {size} · изменится файлов: {files}",
|
"upd.summary": "Загрузка {size} · изменится файлов: {files}",
|
||||||
"upd.summaryDb": "Загрузка {size} · изменится файлов: {files} · обновлений базы данных: {db}",
|
"upd.summaryDb": "Загрузка {size} · изменится файлов: {files} · обновлений базы данных: {db}",
|
||||||
"upd.conflict": "Этот файл был изменён вне CoA Server Manager.",
|
"upd.conflict": "Этот файл был изменён вне CoA Server Manager.",
|
||||||
|
|||||||
Reference in new issue
Block a user