feat: Sandbox-Limits in config.toml und Image cb-python
Speicher-, CPU- und Runtime-Angabe in config.toml, Standard podman, 2g, 2. Containerfile für cb-python (Python 3.12, pytest). Echter Sandbox-Test mit cb-python:latest grün. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
//! mit Standardwerten angelegt. Pfade in der Datei sind optional; ohne Angabe
|
||||
//! gilt der Standardordner der App bzw. `<datenordner>/tasks`.
|
||||
|
||||
use crate::sandbox::Limits;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
@ -11,13 +12,32 @@ use std::path::{Path, PathBuf};
|
||||
pub const CONFIG_FILE_NAME: &str = "config.toml";
|
||||
|
||||
/// Rohe Konfiguration, so wie sie in der Datei steht.
|
||||
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct Config {
|
||||
/// Datenordner (Ergebnisse, Artefakte, Datenbank). Leer = Standard der App.
|
||||
pub data_dir: Option<PathBuf>,
|
||||
/// Aufgabenordner. Leer = `<datenordner>/tasks`.
|
||||
pub tasks_dir: Option<PathBuf>,
|
||||
/// Container-Programm für die Sandbox (Podman-CLI).
|
||||
pub container_runtime: String,
|
||||
/// Speicherlimit pro Lauf, Podman-Schreibweise.
|
||||
pub sandbox_memory: String,
|
||||
/// CPU-Anteil pro Lauf.
|
||||
pub sandbox_cpus: String,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
let limits = Limits::default();
|
||||
Config {
|
||||
data_dir: None,
|
||||
tasks_dir: None,
|
||||
container_runtime: "podman".into(),
|
||||
sandbox_memory: limits.memory,
|
||||
sandbox_cpus: limits.cpus,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Aufgelöste Pfade, die die App tatsächlich verwendet.
|
||||
@ -34,6 +54,14 @@ impl Config {
|
||||
toml::from_str(text).map_err(|e| format!("config.toml ist ungültig: {e}"))
|
||||
}
|
||||
|
||||
/// Ressourcenlimits der Sandbox aus der Konfiguration.
|
||||
pub fn sandbox_limits(&self) -> Limits {
|
||||
Limits {
|
||||
memory: self.sandbox_memory.clone(),
|
||||
cpus: self.sandbox_cpus.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Löst die Pfade auf. `default_data_dir` ist der Standardordner der App.
|
||||
pub fn resolve(&self, config_file: &Path, default_data_dir: &Path) -> Paths {
|
||||
let data_dir = self
|
||||
@ -54,7 +82,7 @@ impl Config {
|
||||
|
||||
/// Lädt die Konfiguration aus `config_dir/config.toml`, legt die Datei bei Bedarf
|
||||
/// mit Standardwerten an und stellt sicher, dass der Datenordner existiert.
|
||||
pub fn load_or_create(config_dir: &Path, default_data_dir: &Path) -> Result<Paths, String> {
|
||||
pub fn load_or_create(config_dir: &Path, default_data_dir: &Path) -> Result<(Paths, Config), String> {
|
||||
fs::create_dir_all(config_dir)
|
||||
.map_err(|e| format!("Konfigurationsordner {} nicht anlegbar: {e}", config_dir.display()))?;
|
||||
|
||||
@ -75,7 +103,7 @@ pub fn load_or_create(config_dir: &Path, default_data_dir: &Path) -> Result<Path
|
||||
let paths = config.resolve(&config_file, default_data_dir);
|
||||
fs::create_dir_all(&paths.data_dir)
|
||||
.map_err(|e| format!("Datenordner {} nicht anlegbar: {e}", paths.data_dir.display()))?;
|
||||
Ok(paths)
|
||||
Ok((paths, config))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -95,6 +123,13 @@ mod tests {
|
||||
assert_eq!(c.tasks_dir, Some(PathBuf::from("/x/aufgaben")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sandbox_limits_werden_gelesen() {
|
||||
let c = Config::parse("sandbox_memory = \"4g\"\nsandbox_cpus = \"1\"\n").unwrap();
|
||||
assert_eq!(c.sandbox_limits(), Limits { memory: "4g".into(), cpus: "1".into() });
|
||||
assert_eq!(c.container_runtime, "podman");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unbekannter_schluessel_ist_fehler() {
|
||||
assert!(Config::parse("unbekannt = 1\n").is_err());
|
||||
@ -122,7 +157,7 @@ mod tests {
|
||||
let cfg_dir = base.join("cfg");
|
||||
let data_dir = base.join("data");
|
||||
|
||||
let paths = load_or_create(&cfg_dir, &data_dir).unwrap();
|
||||
let (paths, _) = load_or_create(&cfg_dir, &data_dir).unwrap();
|
||||
assert!(cfg_dir.join(CONFIG_FILE_NAME).exists());
|
||||
assert!(data_dir.is_dir());
|
||||
assert_eq!(paths.tasks_dir, data_dir.join("tasks"));
|
||||
|
||||
@ -31,8 +31,9 @@ pub fn run() {
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.map_err(|e| format!("Datenordner nicht ermittelbar: {e}"))?;
|
||||
let paths = config::load_or_create(&config_dir, &default_data_dir)?;
|
||||
let (paths, cfg) = config::load_or_create(&config_dir, &default_data_dir)?;
|
||||
app.manage(AppPaths(paths));
|
||||
app.manage(cfg);
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![get_paths, load_tasks])
|
||||
|
||||
Reference in New Issue
Block a user