From 481d59e963673fa64e66d043f01ab06ec4579fd0 Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Tue, 23 Jul 2024 23:23:44 +0200 Subject: [PATCH] feat: add cli interface to change config --- Cargo.lock | 32 ++++++++++++++++++++++++++++++++ Cargo.toml | 1 + README.md | 2 +- TODO.md | 11 +++++++---- src/main.rs | 21 ++++++++++++++++++--- 5 files changed, 59 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ef4e615..2f45a3e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -115,6 +115,37 @@ version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" +[[package]] +name = "argh" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7af5ba06967ff7214ce4c7419c7d185be7ecd6cc4965a8f6e1d8ce0398aad219" +dependencies = [ + "argh_derive", + "argh_shared", +] + +[[package]] +name = "argh_derive" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56df0aeedf6b7a2fc67d06db35b09684c3e8da0c95f8f27685cb17e08413d87a" +dependencies = [ + "argh_shared", + "proc-macro2", + "quote", + "syn 2.0.72", +] + +[[package]] +name = "argh_shared" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5693f39141bda5760ecc4111ab08da40565d1771038c4a0250f03457ec707531" +dependencies = [ + "serde", +] + [[package]] name = "async-trait" version = "0.1.81" @@ -146,6 +177,7 @@ name = "autotasker" version = "0.1.0" dependencies = [ "anyhow", + "argh", "axum", "chrono", "clap", diff --git a/Cargo.toml b/Cargo.toml index 960f657..101dbdf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,4 +20,5 @@ log = "0.4.22" env_logger = "0.11.3" tower-http = { version = "0.5.2", features = ["fs"] } tokio-cron-scheduler = "0.10.2" +argh = "0.1.12" diff --git a/README.md b/README.md index c80b8ea..3679681 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Unix-like - [x] List the tasks availables. - [x] Run tasks in background. - [x] Store the logs and tasks runs in a sqlite DB. -- [ ] Schedule tasks (CRON-like). +- [x] Schedule tasks (CRON-like). - [ ] OpenMetrics exporter to alert when a task failed. - [ ] External alerting when a task failed. - [ ] Run task via webhook, with a webhook token. diff --git a/TODO.md b/TODO.md index dd8c799..623bd73 100644 --- a/TODO.md +++ b/TODO.md @@ -2,12 +2,12 @@ ## TODO -- [x] Implement basic scheduler -- [x] Add basic CSS -- [ ] Implement basic auth with OAuth2 +- [ ] Implement support for webhook and debouncing of webhook +- [ ] Implement basic auth with HTTP basic auth (to trigger and see logs only) +- [ ] add CSS badge and color code on job status - [ ] Validating config file - validate schedule CRON syntax -- [ ] Load config file from `/etc/` +- [ ] Implement basic auth with OAuth2 - [ ] Add `Dockerfile` and docker-compose example - [ ] Add CI/CD to build docker image - [ ] Add configuration to limit the logs head and tail @@ -30,3 +30,6 @@ - [x] setup sqlite - [x] store of task run logs in sqlite +- [x] Implement basic scheduler +- [x] Add basic CSS +- [x] Load config file from `/etc/` diff --git a/src/main.rs b/src/main.rs index e77146d..12212e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod models; mod executor; mod scheduler; +use argh::FromArgs; use log::info; use anyhow::{anyhow, Context, Result}; use axum::routing::get; @@ -27,8 +28,16 @@ pub struct AppState { templating_env: Environment<'static> } -fn get_config() -> Result { - let inp_def_yaml = fs::read_to_string("./config.yaml") +#[derive(Debug, FromArgs)] +/// Autotasker daemon +struct CliFlags { + /// path to YAML config file to use to configure autotasker + #[argh(option)] + config: Option, +} + +fn get_config(path: String) -> Result { + let inp_def_yaml = fs::read_to_string(path) .expect("Should have been able to read the the config file"); serde_yaml::from_str(&inp_def_yaml) @@ -60,6 +69,7 @@ fn build_templating_env() -> Environment<'static> { #[tokio::main] async fn main() -> Result<()> { + let flags: CliFlags = argh::from_env(); env_logger::init(); info!("Starting autotasker"); @@ -68,7 +78,12 @@ async fn main() -> Result<()> { // start channel to talk to executor daemon let (tx, rx) = mpsc::channel::(32); - let config: Config = get_config().expect("Cannot get config"); + let config_path = match flags.config { + Some(v) => v, + None => "/etc/autotasker/config.yaml".to_string() + }; + info!("Using config file at {}", &config_path); + let config: Config = get_config(config_path).expect("Cannot get config"); let state = AppState { config,