mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-25 11:25:56 +00:00
feat: validate a release and export its baseline on Linux
The signed base package holds the MySQL data directory of a Windows repack. On a Linux computer extract-schema-base now turns it into a Docker server folder whose database container opens that directory (mysqlData in docker.json), so the release SQL, the schema contract and export-baseline run with the Docker installation code. Windows is unchanged: it still bootstraps the repack's own database program.
This commit is contained in:
1 parent
fac6ce469a
commit
7a3fefaafd
6 files changed
+129
-8
No files matched your search
@@ -0,0 +1,69 @@
|
|||||||
|
//! The disposable database a release is validated against, on a Linux computer.
|
||||||
|
//!
|
||||||
|
//! A release is checked by applying its database changes to the database of the signed base package. The base carries the
|
||||||
|
//! MySQL data directory of a Windows repack, and a MySQL 8.4 server in Docker opens it as it is (with the repack's table
|
||||||
|
//! name setting). So the fixture is a Docker server folder whose database container uses that directory in place of a
|
||||||
|
//! volume; everything else (start, SQL, schema capture, dumps) is the code of a Docker installation.
|
||||||
|
|
||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use super::{Config, Docker, MARKER, MYSQL_IMAGE};
|
||||||
|
use crate::driver::{self, Verb};
|
||||||
|
use crate::error::{Error, Result};
|
||||||
|
use crate::{fsx, install, package};
|
||||||
|
|
||||||
|
/// Turn an extracted base package (`mysql/data`, `Settings/`) into a Docker server folder and start its database.
|
||||||
|
pub fn prepare(root: &Path) -> Result<()> {
|
||||||
|
prepare_with(&super::SystemDocker, root)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn prepare_with(d: &dyn Docker, root: &Path) -> Result<()> {
|
||||||
|
let boot = root.join(package::BOOTSTRAP_CREDENTIALS);
|
||||||
|
if !boot.is_file() {
|
||||||
|
return Err(Error::Invalid("The package has no database bootstrap information.".into()));
|
||||||
|
}
|
||||||
|
let secrets: serde_json::Value = fsx::read_json(&boot)?;
|
||||||
|
let root_pw = secrets["rootPassword"].as_str().filter(|p| p.chars().all(|c| c.is_ascii_hexdigit()) && !p.is_empty())
|
||||||
|
.ok_or_else(|| Error::Invalid("The database bootstrap information is not usable.".into()))?.to_string();
|
||||||
|
fs::copy(&boot, root.join("Settings/database.json"))?;
|
||||||
|
let data = fs::canonicalize(root.join("mysql/data"))
|
||||||
|
.map_err(|_| Error::Invalid("The package has no database data directory.".into()))?;
|
||||||
|
let cfg = Config {
|
||||||
|
project: format!("schema-{}", install::random_hex(8)),
|
||||||
|
bind_address: "127.0.0.1".into(),
|
||||||
|
mysql_image: MYSQL_IMAGE.into(),
|
||||||
|
data_dir: None,
|
||||||
|
mysql_data: Some(data.to_string_lossy().into_owned()),
|
||||||
|
};
|
||||||
|
fsx::atomic_write_json(&root.join(MARKER), &cfg)?;
|
||||||
|
|
||||||
|
let started = driver::run(root, Verb::StartMysql)?;
|
||||||
|
if !started.ok {
|
||||||
|
return Err(Error::Invalid(started.human.map(|h| h.message.to_string()).unwrap_or_else(|| "The database could not be started.".into())));
|
||||||
|
}
|
||||||
|
// The repack's administrator account only exists for connections from the server itself (the socket); the Manager
|
||||||
|
// connects over TCP. The fixture is thrown away, so the account is simply added.
|
||||||
|
let sql = format!("CREATE USER IF NOT EXISTS 'root'@'127.0.0.1' IDENTIFIED BY '{root_pw}'; GRANT ALL ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;");
|
||||||
|
let mut call = super::Call::new(&["exec", "-i", "-e", "MYSQL_PWD", &cfg.names().db, "mysql", "--user=root"], std::time::Duration::from_secs(60));
|
||||||
|
call.env = vec![("MYSQL_PWD".into(), root_pw)];
|
||||||
|
call.stdin = Some(sql.as_bytes());
|
||||||
|
let o = d.run(&call)?;
|
||||||
|
if !o.ok() {
|
||||||
|
return Err(Error::Invalid(format!("The fixture database could not be opened to the Manager: {}", o.text())));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_package_without_bootstrap_information_is_refused_before_anything_starts() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let err = prepare(dir.path()).unwrap_err();
|
||||||
|
assert!(err.to_string().contains("bootstrap"), "{err}");
|
||||||
|
assert!(!dir.path().join(MARKER).exists());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -146,7 +146,7 @@ fn write_settings(root: &Path, data_dir: &Path) -> Result<Settings> {
|
|||||||
let (auth, world, ra) = (pick_port(DEFAULT_PORTS.0)?, pick_port(DEFAULT_PORTS.1)?, pick_port(DEFAULT_PORTS.2)?);
|
let (auth, world, ra) = (pick_port(DEFAULT_PORTS.0)?, pick_port(DEFAULT_PORTS.1)?, pick_port(DEFAULT_PORTS.2)?);
|
||||||
let (root_password, app_password) = (random_hex(48), random_hex(48));
|
let (root_password, app_password) = (random_hex(48), random_hex(48));
|
||||||
fs::create_dir_all(root.join("Settings"))?;
|
fs::create_dir_all(root.join("Settings"))?;
|
||||||
let docker = Config { project, bind_address: "127.0.0.1".into(), mysql_image: super::MYSQL_IMAGE.into(), data_dir: Some(data_dir.to_string_lossy().into_owned()) };
|
let docker = Config { project, bind_address: "127.0.0.1".into(), mysql_image: super::MYSQL_IMAGE.into(), data_dir: Some(data_dir.to_string_lossy().into_owned()), mysql_data: None };
|
||||||
fsx::atomic_write_json(&root.join(MARKER), &docker)?;
|
fsx::atomic_write_json(&root.join(MARKER), &docker)?;
|
||||||
// Same file and keys as a repack, so the console, the ports and the settings screens work unchanged. The database is
|
// Same file and keys as a repack, so the console, the ports and the settings screens work unchanged. The database is
|
||||||
// not published on the host; its port is only there to fill the key.
|
// not published on the host; its port is only there to fill the key.
|
||||||
|
|||||||
@@ -210,7 +210,7 @@ fn start(d: &dyn Docker, root: &Path, cfg: &Config, with_game: bool, log: &mut L
|
|||||||
if !state.get(&n.db).is_some_and(Container::running) {
|
if !state.get(&n.db).is_some_and(Container::running) {
|
||||||
remove(d, &n.db);
|
remove(d, &n.db);
|
||||||
let mut call = Call::new(&[], Duration::from_secs(120));
|
let mut call = Call::new(&[], Duration::from_secs(120));
|
||||||
call.args = db_args(cfg, &n);
|
call.args = db_args(cfg, &n, owner_of(root).as_deref());
|
||||||
call.env = vec![("MYSQL_ROOT_PASSWORD".into(), secrets.root.clone())];
|
call.env = vec![("MYSQL_ROOT_PASSWORD".into(), secrets.root.clone())];
|
||||||
let o = d.run(&call).or_else(|e| fail(ErrorCode::DockerUnavailable, e.to_string()))?;
|
let o = d.run(&call).or_else(|e| fail(ErrorCode::DockerUnavailable, e.to_string()))?;
|
||||||
if !o.ok() {
|
if !o.ok() {
|
||||||
@@ -333,12 +333,22 @@ fn connect_ip(cfg: &Config) -> IpAddr {
|
|||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------- argument lists
|
// ------------------------------------------------------------------------------------------------- argument lists
|
||||||
|
|
||||||
fn db_args(cfg: &Config, n: &Names) -> Vec<String> {
|
fn db_args(cfg: &Config, n: &Names, owner: Option<&str>) -> Vec<String> {
|
||||||
let mut a: Vec<String> = ["run", "--detach", "--name"].iter().map(|s| s.to_string()).collect();
|
let mut a: Vec<String> = ["run", "--detach", "--name"].iter().map(|s| s.to_string()).collect();
|
||||||
a.push(n.db.clone());
|
a.push(n.db.clone());
|
||||||
a.extend(["--network".into(), n.network.clone(), "--network-alias".into(), "db".into()]);
|
a.extend(["--network".into(), n.network.clone(), "--network-alias".into(), "db".into()]);
|
||||||
a.extend(["--label".into(), format!("coa.project={}", cfg.project)]);
|
a.extend(["--label".into(), format!("coa.project={}", cfg.project)]);
|
||||||
a.extend(["--volume".into(), format!("{}:/var/lib/mysql", n.volume)]);
|
match &cfg.mysql_data {
|
||||||
|
Some(dir) => {
|
||||||
|
// A data directory from a Windows repack: it is written by the person who owns the folder, and its table names
|
||||||
|
// were folded to lower case (the repack's setting), which the server must be told because a Linux one does not.
|
||||||
|
a.extend(["--volume".into(), format!("{dir}:/var/lib/mysql")]);
|
||||||
|
if let Some(o) = owner {
|
||||||
|
a.extend(["--user".into(), o.into()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => a.extend(["--volume".into(), format!("{}:/var/lib/mysql", n.volume)]),
|
||||||
|
}
|
||||||
// The password comes from the environment of the docker client, not from this command line.
|
// The password comes from the environment of the docker client, not from this command line.
|
||||||
a.extend(["--env".into(), "MYSQL_ROOT_PASSWORD".into()]);
|
a.extend(["--env".into(), "MYSQL_ROOT_PASSWORD".into()]);
|
||||||
// "mysqladmin ping" succeeds as soon as the server answers, with or without a login. It must go over TCP, like
|
// "mysqladmin ping" succeeds as soon as the server answers, with or without a login. It must go over TCP, like
|
||||||
@@ -349,6 +359,9 @@ fn db_args(cfg: &Config, n: &Names) -> Vec<String> {
|
|||||||
a.extend(["--health-interval".into(), "5s".into(), "--health-timeout".into(), "5s".into(), "--health-retries".into(), "40".into()]);
|
a.extend(["--health-interval".into(), "5s".into(), "--health-timeout".into(), "5s".into(), "--health-retries".into(), "40".into()]);
|
||||||
a.extend(["--stop-timeout".into(), "60".into()]);
|
a.extend(["--stop-timeout".into(), "60".into()]);
|
||||||
a.push(cfg.mysql_image.clone());
|
a.push(cfg.mysql_image.clone());
|
||||||
|
if cfg.mysql_data.is_some() {
|
||||||
|
a.push("--lower-case-table-names=1".into());
|
||||||
|
}
|
||||||
a
|
a
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -698,6 +711,26 @@ mod tests {
|
|||||||
assert!(runs[1].args.windows(2).any(|w| w == ["--env", "AC_WORLD_DATABASE_INFO"]), "name only, the value is in the environment");
|
assert!(runs[1].args.windows(2).any(|w| w == ["--env", "AC_WORLD_DATABASE_INFO"]), "name only, the value is in the environment");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_release_fixture_runs_its_database_on_the_package_data_directory_not_on_a_volume() {
|
||||||
|
let (_d, root) = server("t1");
|
||||||
|
fs::write(root.join("Settings/docker.json"), r#"{"project":"t1","mysqlData":"/work/fixture/mysql/data"}"#).unwrap();
|
||||||
|
let sim = Sim::new();
|
||||||
|
assert!(run_with(&sim, &root, Verb::StartMysql).unwrap().ok);
|
||||||
|
let db = &sim.calls_of("run")[0].args;
|
||||||
|
assert!(db.windows(2).any(|w| w == ["--volume", "/work/fixture/mysql/data:/var/lib/mysql"]), "{db:?}");
|
||||||
|
assert!(!db.iter().any(|a| a.starts_with("coa-t1-db:")), "no named volume: {db:?}");
|
||||||
|
assert!(db.iter().any(|a| a == "--user"), "the folder is written by its owner");
|
||||||
|
assert_eq!(db.last().map(String::as_str), Some("--lower-case-table-names=1"), "a server option, so after the image name");
|
||||||
|
// An installation never gets any of it.
|
||||||
|
let (_d2, root2) = server("t2");
|
||||||
|
let sim2 = Sim::new();
|
||||||
|
assert!(run_with(&sim2, &root2, Verb::StartMysql).unwrap().ok);
|
||||||
|
let plain = &sim2.calls_of("run")[0].args;
|
||||||
|
assert!(plain.windows(2).any(|w| w == ["--volume", "coa-t2-db:/var/lib/mysql"]), "{plain:?}");
|
||||||
|
assert!(!plain.iter().any(|a| a == "--lower-case-table-names=1" || a == "--user"));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn the_database_is_only_healthy_once_it_takes_tcp_connections() {
|
fn the_database_is_only_healthy_once_it_takes_tcp_connections() {
|
||||||
let (_d, root) = server("t1");
|
let (_d, root) = server("t1");
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
mod cli;
|
mod cli;
|
||||||
|
pub mod fixture;
|
||||||
pub mod install;
|
pub mod install;
|
||||||
mod lifecycle;
|
mod lifecycle;
|
||||||
|
|
||||||
@@ -63,6 +64,10 @@ pub struct Config {
|
|||||||
/// mounted read-only, so one copy can serve several servers.
|
/// mounted read-only, so one copy can serve several servers.
|
||||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
pub data_dir: Option<String>,
|
pub data_dir: Option<String>,
|
||||||
|
/// A MySQL data directory the database container uses in place of its volume. Only for the disposable fixture that
|
||||||
|
/// validates a release (the data directory of the signed base package); an installation never sets it.
|
||||||
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
|
pub mysql_data: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct Names {
|
pub(crate) struct Names {
|
||||||
@@ -96,10 +101,10 @@ impl Config {
|
|||||||
if self.bind_address.parse::<IpAddr>().is_err() {
|
if self.bind_address.parse::<IpAddr>().is_err() {
|
||||||
return Err(Error::Invalid(format!("{} is not an IP address.", self.bind_address)));
|
return Err(Error::Invalid(format!("{} is not an IP address.", self.bind_address)));
|
||||||
}
|
}
|
||||||
if let Some(d) = &self.data_dir {
|
for d in [&self.data_dir, &self.mysql_data].into_iter().flatten() {
|
||||||
// The folder goes into a `--volume host:container:ro` option, where a colon would be read as a separator.
|
// The folder goes into a `--volume host:container` option, where a colon would be read as a separator.
|
||||||
if !Path::new(d).is_absolute() || d.contains(':') {
|
if !Path::new(d).is_absolute() || d.contains(':') {
|
||||||
return Err(Error::Invalid("The game data folder must be a full path without a colon.".into()));
|
return Err(Error::Invalid("A Docker folder must be a full path without a colon.".into()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let image_ok = !self.mysql_image.is_empty() && self.mysql_image.chars().all(|c| c.is_ascii_alphanumeric() || "._/:@-".contains(c));
|
let image_ok = !self.mysql_image.is_empty() && self.mysql_image.chars().all(|c| c.is_ascii_alphanumeric() || "._/:@-".contains(c));
|
||||||
|
|||||||
@@ -38,7 +38,12 @@ pub fn extract_base(source: &Path, fixture: &Path, trusted_key: &str) -> Result<
|
|||||||
["mysql/", "settings/", "runtime/", "scripts/", "core/", "bugreport/"].iter().any(|prefix| path.starts_with(prefix))
|
["mysql/", "settings/", "runtime/", "scripts/", "core/", "bugreport/"].iter().any(|prefix| path.starts_with(prefix))
|
||||||
}, &|_, _| {})?;
|
}, &|_, _| {})?;
|
||||||
fsx::atomic_write(&fixture.join(MARKER), b"disposable release-schema fixture")?;
|
fsx::atomic_write(&fixture.join(MARKER), b"disposable release-schema fixture")?;
|
||||||
install::bootstrap_database(fixture)
|
// A Linux computer has no Windows database program: the fixture is a Docker server over the package's data directory.
|
||||||
|
if cfg!(windows) {
|
||||||
|
install::bootstrap_database(fixture)
|
||||||
|
} else {
|
||||||
|
crate::docker::fixture::prepare(fixture)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn capture_release(fixture: &Path, tree: &Path, sql: &[SqlFile]) -> Result<()> {
|
pub fn capture_release(fixture: &Path, tree: &Path, sql: &[SqlFile]) -> Result<()> {
|
||||||
|
|||||||
@@ -392,6 +392,15 @@ hand over to `coa_core::docker`; nothing else needed to change for start, stop,
|
|||||||
so replaying the SQL files from the base scripts fails, in any order. `coa-release export-baseline --server DIR --out DIR`
|
so replaying the SQL files from the base scripts fails, in any order. `coa-release export-baseline --server DIR --out DIR`
|
||||||
produces the dumps from a prepared server (repack or Docker). The game data stays where the person has it
|
produces the dumps from a prepared server (repack or Docker). The game data stays where the person has it
|
||||||
(`dataDir` in `docker.json`, mounted read-only). A failure removes the containers, the volume and the staging folder.
|
(`dataDir` in `docker.json`, mounted read-only). A failure removes the containers, the volume and the staging folder.
|
||||||
|
* **Validating a release on Linux** (`docker/fixture.rs`, used by `coa-release extract-schema-base` and `schema-contract` where
|
||||||
|
there is no Windows database program): the signed base package carries the MySQL data directory of a Windows repack, and a
|
||||||
|
MySQL 8.4 container opens it as it is (`--lower-case-table-names=1`, the repack's setting). The fixture is a Docker server
|
||||||
|
folder (`docker.json` with `mysqlData`: the database container uses that directory in place of a volume; an installation
|
||||||
|
never sets it), plus a `root` account for TCP, which the repack only has for its socket. The release SQL is then applied and the
|
||||||
|
schema contract captured by the same code as on Windows, and `export-baseline` dumps the result: that is how a Linux build
|
||||||
|
produces `Database/baseline` without anyone's live database. The migration checksums recorded in the base are those of
|
||||||
|
files checked out on Windows (CRLF line endings, `core.autocrlf`): the SQL of the core, the modules and SQUID must be
|
||||||
|
checked out the same way for the checksums to match, or every applied migration looks edited.
|
||||||
* **Not done yet**: wiring the installer into the screens, downloading the game data, updates, port conflict detection,
|
* **Not done yet**: wiring the installer into the screens, downloading the game data, updates, port conflict detection,
|
||||||
the firewall and exposure checks, the Wildcard realm profiles, and the client under Wine/Proton.
|
the firewall and exposure checks, the Wildcard realm profiles, and the client under Wine/Proton.
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user