From 23ae5613c7fb62ec85f0e2454f9ac8cfc84f4412 Mon Sep 17 00:00:00 2001 From: Joscha Date: Wed, 9 Aug 2023 15:19:03 +0200 Subject: [PATCH] Add runner config options --- Cargo.lock | 11 ++++++++ Cargo.toml | 1 + src/config.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index b6bfd76..5109cfe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -821,6 +821,16 @@ dependencies = [ "version_check", ] +[[package]] +name = "gethostname" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0176e0459c2e4a1fe232f984bca6890e681076abb9934f6cea7c326f3fc47818" +dependencies = [ + "libc", + "windows-targets", +] + [[package]] name = "getrandom" version = "0.2.10" @@ -2795,6 +2805,7 @@ dependencies = [ "clap", "directories", "futures", + "gethostname", "gix", "humantime", "humantime-serde", diff --git a/Cargo.toml b/Cargo.toml index 814687a..013b3a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ axum = { version = "0.6.19", features = ["macros"] } clap = { version = "4.3.19", features = ["derive", "deprecated"] } directories = "5.0.1" futures = "0.3.28" +gethostname = "0.4.3" humantime = "2.1.0" humantime-serde = "1.1.1" mime_guess = "2.0.4" diff --git a/src/config.rs b/src/config.rs index 6072fc4..42f005d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,7 @@ //! Configuration from a file. use std::{ + collections::HashMap, fs, io::ErrorKind, net::SocketAddr, @@ -32,6 +33,10 @@ mod default { pub fn repo_update_delay() -> Duration { Duration::from_secs(60) } + + pub fn runner_ping_delay() -> Duration { + Duration::from_secs(10) + } } #[derive(Debug, Deserialize)] @@ -67,10 +72,38 @@ impl Default for Repo { } } +#[derive(Debug, Deserialize)] +struct RunnerServer { + url: String, + token: String, +} + +#[derive(Debug, Deserialize)] +struct Runner { + name: Option, + #[serde(default = "default::runner_ping_delay", with = "humantime_serde")] + ping_delay: Duration, + servers: HashMap, +} + +impl Default for Runner { + fn default() -> Self { + Self { + name: None, + ping_delay: default::runner_ping_delay(), + servers: HashMap::new(), + } + } +} + #[derive(Debug, Default, Deserialize)] struct ConfigFile { - repo: Repo, + #[serde(default)] web: Web, + #[serde(default)] + repo: Repo, + #[serde(default)] + runner: Runner, } impl ConfigFile { @@ -115,6 +148,35 @@ impl ConfigFile { Ok("unnamed repo".to_string()) } + + fn runner_name(&self) -> String { + if let Some(name) = &self.runner.name { + return name.clone(); + } + + gethostname::gethostname().to_string_lossy().to_string() + } + + fn runner_servers(&self) -> HashMap { + self.runner + .servers + .iter() + .map(|(name, server)| { + let url = server + .url + .strip_suffix('/') + .unwrap_or(&server.url) + .to_string(); + let token = server.token.to_string(); + (name.to_string(), RunnerServerConfig { url, token }) + }) + .collect() + } +} + +pub struct RunnerServerConfig { + pub url: String, + pub token: String, } pub struct Config { @@ -122,6 +184,9 @@ pub struct Config { pub web_address: SocketAddr, pub repo_name: String, pub repo_update_delay: Duration, + pub runner_name: String, + pub runner_ping_delay: Duration, + pub runner_servers: HashMap, } impl Config { @@ -144,12 +209,17 @@ impl Config { let web_base = config_file.web_base(); let repo_name = config_file.repo_name(args)?; + let runner_name = config_file.runner_name(); + let runner_servers = config_file.runner_servers(); Ok(Self { web_base, web_address: config_file.web.address, repo_name, repo_update_delay: config_file.repo.update_delay, + runner_name, + runner_ping_delay: config_file.runner.ping_delay, + runner_servers, }) } }