From 2bea16d00352f558fed786a9e43f0692515de997 Mon Sep 17 00:00:00 2001 From: mbecker20 Date: Sun, 24 Mar 2024 03:51:07 -0700 Subject: [PATCH] parse resource files --- bin/monrun/exec/exec1.toml | 0 bin/monrun/resources/builds.toml | 19 ++++++ bin/monrun/resources/deployments.toml | 13 ++++ bin/monrun/resources/servers.toml | 13 ++++ bin/monrun/src/execution.rs | 8 +-- bin/monrun/src/main.rs | 11 ++-- bin/monrun/src/resource.rs | 86 +++++++++++++++++++++++++-- runfile.toml | 12 +++- 8 files changed, 145 insertions(+), 17 deletions(-) create mode 100644 bin/monrun/exec/exec1.toml create mode 100644 bin/monrun/resources/builds.toml create mode 100644 bin/monrun/resources/deployments.toml create mode 100644 bin/monrun/resources/servers.toml diff --git a/bin/monrun/exec/exec1.toml b/bin/monrun/exec/exec1.toml new file mode 100644 index 000000000..e69de29bb diff --git a/bin/monrun/resources/builds.toml b/bin/monrun/resources/builds.toml new file mode 100644 index 000000000..87f91854e --- /dev/null +++ b/bin/monrun/resources/builds.toml @@ -0,0 +1,19 @@ +[[build]] +name = "monitor-core-v1" +description = "the prod build for monitor" +tags = ["monitor", "prod"] +config.repo = "mbecker20/monitor" +config.branch = "next" +config.build_path = "." +config.dockerfile_path = "Dockerfile" +config.docker_account = "mbecker2020" + +[[build]] +name = "monitor-core-v1-dev" +description = "the dev build for monitor" +tags = ["monitor", "dev"] +config.repo = "mbecker20/monitor" +config.branch = "next" +config.build_path = "." +config.dockerfile_path = "Dockerfile" +config.docker_account = "mbecker2020" \ No newline at end of file diff --git a/bin/monrun/resources/deployments.toml b/bin/monrun/resources/deployments.toml new file mode 100644 index 000000000..454bbc954 --- /dev/null +++ b/bin/monrun/resources/deployments.toml @@ -0,0 +1,13 @@ +[[deployment]] +name = "monitor-core-v1-dev" +description = "v1 dev core api" +tags = ["mogh", "dev"] +config.server_id = "mogh-server" + +[[deployment.config.environment]] +variable = "TEST_1" +value = "VALUE" + +[[deployment.config.environment]] +variable = "TEST_2" +value = "VALUE" \ No newline at end of file diff --git a/bin/monrun/resources/servers.toml b/bin/monrun/resources/servers.toml new file mode 100644 index 000000000..cc5ebf769 --- /dev/null +++ b/bin/monrun/resources/servers.toml @@ -0,0 +1,13 @@ +[[server]] +name = "mogh-server" +description = "main mogh server" +tags = ["mogh", "prod"] +config.address = "http://localhost:8001" +config.region = "us-east-2" + +[[server]] +name = "testo" +description = "test server 1" +tags = ["mogh", "test"] +config.address = "" +config.enabled = false \ No newline at end of file diff --git a/bin/monrun/src/execution.rs b/bin/monrun/src/execution.rs index 4f251d11f..f03c0ad4e 100644 --- a/bin/monrun/src/execution.rs +++ b/bin/monrun/src/execution.rs @@ -12,10 +12,7 @@ use crate::{ }; pub async fn run_execution(path: &Path) -> anyhow::Result<()> { - let ExecutionFile { - name, - stage: stages, - } = crate::parse_toml_file(path)?; + let ExecutionFile { name, stages } = crate::parse_toml_file(path)?; info!("EXECUTION: {name}"); info!("path: {path:?}"); @@ -36,7 +33,8 @@ pub async fn run_execution(path: &Path) -> anyhow::Result<()> { #[derive(Debug, Clone, Deserialize)] pub struct ExecutionFile { pub name: String, - pub stage: Vec, + #[serde(rename = "stage")] + pub stages: Vec, } #[derive(Debug, Clone, Deserialize)] diff --git a/bin/monrun/src/main.rs b/bin/monrun/src/main.rs index c88998415..fd14c39ad 100644 --- a/bin/monrun/src/main.rs +++ b/bin/monrun/src/main.rs @@ -1,7 +1,7 @@ #[macro_use] extern crate tracing; -use std::{io::Read, path::PathBuf, sync::OnceLock}; +use std::{io::Read, path::PathBuf, str::FromStr, sync::OnceLock}; use anyhow::Context; use clap::{Parser, Subcommand}; @@ -32,8 +32,10 @@ enum Command { Resource { /// The resource action to take action: resource::SyncDirection, - /// The path of the resource file - path: PathBuf, + /// The path of the resource folder / file + /// Folder paths will recursively incorporate all the resources it finds under the folder + #[arg(default_value_t = String::from("./resources"))] + path: String, }, /// Runs execution files Exec { @@ -71,7 +73,8 @@ async fn main() -> anyhow::Result<()> { match &cli_args().command { Command::Exec { path } => execution::run_execution(path).await?, Command::Resource { action, path } => { - info!("got RESOURCE! action: {action:?} | path: {path:?}") + resource::run_resource(*action, &PathBuf::from_str(path)?) + .await? } } diff --git a/bin/monrun/src/resource.rs b/bin/monrun/src/resource.rs index 458c84e9c..ad352ef7c 100644 --- a/bin/monrun/src/resource.rs +++ b/bin/monrun/src/resource.rs @@ -1,12 +1,38 @@ +use std::{fs, path::Path}; + +use anyhow::{anyhow, Context}; use clap::ValueEnum; +use monitor_client::entities::{ + build::Build, deployment::Deployment, server::Server, +}; +use serde::Deserialize; + +pub async fn run_resource( + action: SyncDirection, + path: &Path, +) -> anyhow::Result<()> { + info!("action: {action:?} | path: {path:?}"); + + let resources = read_resources(path)?; + + println!("{resources:#?}"); + + Ok(()) +} /// Specifies resources to sync on monitor -pub struct ResourceFile { - pub builders: (), - pub servers: (), - pub builds: (), - pub deployments: (), - pub repos: (), +#[derive(Debug, Clone, Default, Deserialize)] +struct ResourceFile { + #[serde(default, rename = "server")] + servers: Vec, + #[serde(default, rename = "build")] + builds: Vec, + #[serde(default, rename = "deployment")] + deployments: Vec, + // #[serde(rename = "builder")] + // builders: (), + // #[serde(rename = "repo")] + // repos: (), } #[derive(Debug, Clone, Copy, ValueEnum)] @@ -16,3 +42,51 @@ pub enum SyncDirection { /// Takes down / deletes resources Down, } + +fn read_resources(path: &Path) -> anyhow::Result { + let mut res = ResourceFile::default(); + read_resources_recursive(path, &mut res)?; + Ok(res) +} + +fn read_resources_recursive( + path: &Path, + resources: &mut ResourceFile, +) -> anyhow::Result<()> { + let res = + fs::metadata(path).context("failed to get path metadata")?; + if res.is_file() { + if !path + .extension() + .map(|ext| ext == "toml") + .unwrap_or_default() + { + return Ok(()); + } + let more = match crate::parse_toml_file::(path) { + Ok(res) => res, + Err(e) => { + warn!("failed to parse {:?}. skipping file | {e:#}", path); + return Ok(()); + } + }; + info!("adding resources from {path:?}"); + resources.servers.extend(more.servers); + resources.builds.extend(more.builds); + resources.deployments.extend(more.deployments); + Ok(()) + } else if res.is_dir() { + let directory = fs::read_dir(path) + .context("failed to read directory contents")?; + for entry in directory.into_iter().flatten() { + if let Err(e) = + read_resources_recursive(&entry.path(), resources) + { + warn!("failed to read additional resources at path | {e:#}"); + } + } + Ok(()) + } else { + Err(anyhow!("resources path is neither file nor directory")) + } +} diff --git a/runfile.toml b/runfile.toml index ecbd4f63c..50f7d5003 100644 --- a/runfile.toml +++ b/runfile.toml @@ -3,6 +3,14 @@ description = "generates typescript types for the ts client" path = "." cmd = "node ./client/ts/generate_types.mjs" -[try-monrun] +[monrun-help] path = "bin/monrun" -cmd = "cargo run -- resource down ./resources/deployments.toml" \ No newline at end of file +cmd = "cargo run -- resource --help" + +[monrun-exec] +path = "bin/monrun" +cmd = "cargo run -- exec --help" + +[monrun-resource] +path = "bin/monrun" +cmd = "cargo run -- resource up" \ No newline at end of file