mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-19 11:25:56 +00:00
feat: install a new Docker server from the install screen (Linux)
The install commands ask coa_core::platform::flavor() once and hand a Docker install to src-tauri/src/docker_install.rs; the repack arm of each command is untouched (an early return and an extra optional parameter are all that changed in lib.rs). New command install_environment tells the screen the kind of server, the suggested folder (~/CoaServer) and why Docker cannot be used, if it cannot. On Linux the install screen shows the game data folder (used in place, read-only) and the server package (a folder or an address; there is no default address until a signed Linux package is published), and disables Install until both are given and Docker works. It shows Docker's own message with a "Check again" button when Docker is missing, stopped or not allowed for the user. The Windows screen is unchanged (checked side by side in the browser mock). A debug build (tauri dev) accepts another package key named in COA_DEV_TRUSTED_KEY, to try the installer on a package signed with a throwaway key; a release build always uses the embedded key. New text is English only for now (other languages fall back to it).
This commit is contained in:
1 parent
58c1e1aee0
commit
45a699d2ca
9 files changed
+276
-16
No files matched your search
@@ -0,0 +1,125 @@
|
||||
//! The Linux (Docker) side of the install commands. `lib.rs` asks `coa_core::platform::flavor()` once per command and
|
||||
//! calls this module for a Docker install; the code for a repack install stays where it was and is not changed by what
|
||||
//! happens here.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use coa_core::docker::{self, install as core_install, SystemDocker};
|
||||
use coa_core::download::Cancel;
|
||||
use coa_core::install::{Preflight, Source};
|
||||
use coa_core::registry::Registry;
|
||||
use coa_core::{Error, Result};
|
||||
use serde::Serialize;
|
||||
use tauri::{AppHandle, Emitter, State};
|
||||
|
||||
use super::{blocking, data_dir, summary, AppState, InstallRequirements, ServerSummary, UiError};
|
||||
|
||||
/// What the install screen needs to know about this computer before it asks anything.
|
||||
#[derive(Serialize)]
|
||||
pub struct Environment {
|
||||
pub flavor: coa_core::platform::Flavor,
|
||||
pub default_dir: String,
|
||||
/// Why Docker cannot be used (not installed, not running, no permission), in Docker's own words; none when it works.
|
||||
pub docker_problem: Option<String>,
|
||||
}
|
||||
|
||||
pub fn default_dir() -> String {
|
||||
default_dir_in(std::env::var("HOME").ok().as_deref())
|
||||
}
|
||||
|
||||
fn default_dir_in(home: Option<&str>) -> String {
|
||||
match home.map(|h| h.trim_end_matches('/')).filter(|h| !h.is_empty()) {
|
||||
Some(h) => format!("{h}/CoaServer"),
|
||||
None => "/srv/coa".into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn docker_problem() -> Option<String> {
|
||||
docker::check_docker(&SystemDocker).err().map(|e| e.to_string())
|
||||
}
|
||||
|
||||
pub fn environment() -> Environment {
|
||||
Environment { flavor: coa_core::platform::flavor(), default_dir: default_dir(), docker_problem: docker_problem() }
|
||||
}
|
||||
|
||||
pub fn preflight(dest: &str, data: Option<String>, needed: Option<u64>, registry: &Registry) -> Preflight {
|
||||
// The size comes from the signed package list when the screen already has it; it is checked again at install time.
|
||||
core_install::preflight(Path::new(dest), Path::new(data.as_deref().unwrap_or("")), needed.unwrap_or(1024 * 1024 * 1024), registry)
|
||||
}
|
||||
|
||||
/// The Linux package has no default address yet: it is published only after the maintainer has signed it.
|
||||
pub fn package_source(custom: Option<String>) -> Result<Source> {
|
||||
let pick = custom.filter(|s| !s.trim().is_empty()).or_else(|| std::env::var("COA_PACKAGE_SOURCE").ok().filter(|s| !s.trim().is_empty()));
|
||||
match pick {
|
||||
Some(p) if p.to_ascii_lowercase().starts_with("http") => Ok(Source::Url(p)),
|
||||
Some(p) => Ok(Source::Dir(PathBuf::from(p))),
|
||||
None => Err(Error::Invalid("Choose the Linux server package (a folder or an address): none is published yet.".into())),
|
||||
}
|
||||
}
|
||||
|
||||
/// The key a Linux package must be signed with. A debug build (`tauri dev`) may name another one in `COA_DEV_TRUSTED_KEY`,
|
||||
/// to try the installer on a package signed with a throwaway key; a release build always uses the embedded key.
|
||||
pub fn trusted_key() -> String {
|
||||
#[cfg(debug_assertions)]
|
||||
if let Ok(k) = std::env::var("COA_DEV_TRUSTED_KEY") {
|
||||
if !k.trim().is_empty() {
|
||||
return k.trim().to_string();
|
||||
}
|
||||
}
|
||||
coa_core::signing::EMBEDDED_PUBLIC_KEY.to_string()
|
||||
}
|
||||
|
||||
pub async fn requirements(package: Option<String>) -> std::result::Result<InstallRequirements, UiError> {
|
||||
blocking(move || {
|
||||
let (m, _) = coa_core::pkgsource::fetch_manifest(&package_source(package)?, &trusted_key())?;
|
||||
let archive = m.archive.ok_or_else(|| Error::InvalidManifest("no archive".into()))?;
|
||||
Ok(InstallRequirements { download_bytes: archive.parts.iter().map(|p| p.size).sum(), unpacked_bytes: archive.unpacked_size, version: m.version })
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn install(app: AppHandle, state: State<'_, AppState>, dest: String, package: Option<String>, data: Option<String>) -> std::result::Result<ServerSummary, UiError> {
|
||||
let source = package_source(package)?;
|
||||
let data_dir_path = PathBuf::from(data.filter(|d| !d.trim().is_empty()).ok_or_else(|| Error::Invalid("Choose the folder that holds the game data.".into()))?);
|
||||
let cancel = Cancel::default();
|
||||
*state.install_cancel.lock().map_err(|_| Error::Invalid("state poisoned".into()))? = Some(cancel.clone());
|
||||
let registry = Registry::at(data_dir().join("installs.json"));
|
||||
let dest_path = PathBuf::from(dest);
|
||||
let key = trusted_key();
|
||||
let result = tauri::async_runtime::spawn_blocking(move || {
|
||||
core_install::install(
|
||||
&core_install::Params { source, dest: dest_path, data_dir: data_dir_path, trusted_key: &key, registry: ®istry, cancel },
|
||||
&|step| {
|
||||
let _ = app.emit("install-progress", step);
|
||||
},
|
||||
)
|
||||
})
|
||||
.await;
|
||||
if let Ok(mut c) = state.install_cancel.lock() {
|
||||
*c = None;
|
||||
}
|
||||
let done = result.map_err(|e| Error::Invalid(e.to_string()))??;
|
||||
Ok(summary(done.id, PathBuf::from(done.path)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_default_folder_is_in_the_home_folder() {
|
||||
assert_eq!(default_dir_in(Some("/home/ana")), "/home/ana/CoaServer");
|
||||
assert_eq!(default_dir_in(Some("/home/ana/")), "/home/ana/CoaServer");
|
||||
assert_eq!(default_dir_in(None), "/srv/coa");
|
||||
assert_eq!(default_dir_in(Some("")), "/srv/coa");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_linux_package_must_be_named_there_is_no_default() {
|
||||
std::env::remove_var("COA_PACKAGE_SOURCE");
|
||||
assert!(package_source(None).is_err());
|
||||
assert!(package_source(Some(" ".into())).is_err());
|
||||
assert!(matches!(package_source(Some("/tmp/pkg".into())).unwrap(), Source::Dir(_)));
|
||||
assert!(matches!(package_source(Some("https://example.org/pkg".into())).unwrap(), Source::Url(_)));
|
||||
}
|
||||
}
|
||||
+25
-2
@@ -5,11 +5,14 @@ use std::collections::HashSet;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Mutex;
|
||||
|
||||
mod docker_install;
|
||||
|
||||
use coa_core::backup::{self, Kind, RecoveryPoint, Trigger, VerifyReport};
|
||||
use coa_core::config::{self, Scope, SettingsView};
|
||||
use coa_core::download::Cancel;
|
||||
use coa_core::driver::{self, DriverOutcome, Verb};
|
||||
use coa_core::install::{self, Preflight, Source};
|
||||
use coa_core::platform::{self, Flavor};
|
||||
use coa_core::ra::Ra;
|
||||
use coa_core::update::{self, Resolution};
|
||||
use coa_core::error::UiError;
|
||||
@@ -105,9 +108,19 @@ fn summary(id: String, path: PathBuf) -> ServerSummary {
|
||||
|
||||
#[tauri::command]
|
||||
fn default_install_dir() -> String {
|
||||
if platform::flavor() == Flavor::Docker {
|
||||
return docker_install::default_dir();
|
||||
}
|
||||
"C:\\Games\\CoA Server".into()
|
||||
}
|
||||
|
||||
/// What the install screen needs to know about this computer: the kind of server it installs, the suggested folder, and
|
||||
/// whether Docker can be used.
|
||||
#[tauri::command]
|
||||
async fn install_environment() -> docker_install::Environment {
|
||||
tauri::async_runtime::spawn_blocking(docker_install::environment).await.unwrap_or_else(|_| docker_install::Environment { flavor: platform::flavor(), default_dir: default_install_dir(), docker_problem: None })
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn scan_server(path: String) -> std::result::Result<ScanReport, UiError> {
|
||||
tauri::async_runtime::spawn_blocking(move || layout::scan(std::path::Path::new(&path)))
|
||||
@@ -450,7 +463,10 @@ async fn restore_backup_database(state: State<'_, AppState>, id: String, backup_
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn install_preflight(state: State<'_, AppState>, dest: String, needed: Option<u64>) -> Preflight {
|
||||
fn install_preflight(state: State<'_, AppState>, dest: String, needed: Option<u64>, game_data: Option<String>) -> Preflight {
|
||||
if platform::flavor() == Flavor::Docker {
|
||||
return docker_install::preflight(&dest, game_data, needed, &state.registry);
|
||||
}
|
||||
// `needed` is the real size from the signed package list when the screen already has it; the real size is checked
|
||||
// again at install time either way.
|
||||
install::preflight(std::path::Path::new(&dest), needed.unwrap_or(6 * 1024 * 1024 * 1024), &state.registry)
|
||||
@@ -468,6 +484,9 @@ struct InstallRequirements {
|
||||
/// How big the server is, read from the package's signed list before anything is downloaded.
|
||||
#[tauri::command]
|
||||
async fn install_requirements(package: Option<String>) -> std::result::Result<InstallRequirements, UiError> {
|
||||
if platform::flavor() == Flavor::Docker {
|
||||
return docker_install::requirements(package).await;
|
||||
}
|
||||
blocking(move || {
|
||||
let (m, _) = coa_core::pkgsource::fetch_manifest(&package_source(package), coa_core::signing::EMBEDDED_PUBLIC_KEY)?;
|
||||
let archive = m.archive.ok_or_else(|| Error::InvalidManifest("no archive".into()))?;
|
||||
@@ -477,7 +496,10 @@ async fn install_requirements(package: Option<String>) -> std::result::Result<In
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn install_new(app: AppHandle, state: State<'_, AppState>, dest: String, package: Option<String>) -> std::result::Result<ServerSummary, UiError> {
|
||||
async fn install_new(app: AppHandle, state: State<'_, AppState>, dest: String, package: Option<String>, game_data: Option<String>) -> std::result::Result<ServerSummary, UiError> {
|
||||
if platform::flavor() == Flavor::Docker {
|
||||
return docker_install::install(app, state, dest, package, game_data).await;
|
||||
}
|
||||
let cancel = Cancel::default();
|
||||
*state.install_cancel.lock().map_err(|_| Error::Invalid("state poisoned".into()))? = Some(cancel.clone());
|
||||
let source = package_source(package);
|
||||
@@ -1590,6 +1612,7 @@ pub fn run() {
|
||||
.manage(AppState { registry: Registry::at(dir.join("installs.json")), busy: Mutex::new(HashSet::new()), install_cancel: Mutex::new(None), client_cancel: Mutex::new(None) })
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
default_install_dir,
|
||||
install_environment,
|
||||
scan_server,
|
||||
add_server,
|
||||
list_servers,
|
||||
|
||||
Reference in new issue
Block a user