parse resource files

This commit is contained in:
mbecker20
2024-03-24 03:51:07 -07:00
parent 475f0438f9
commit 2bea16d003
8 changed files with 145 additions and 17 deletions
View File
+19
View File
@@ -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"
+13
View File
@@ -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"
+13
View File
@@ -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
+3 -5
View File
@@ -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<Stage>,
#[serde(rename = "stage")]
pub stages: Vec<Stage>,
}
#[derive(Debug, Clone, Deserialize)]
+7 -4
View File
@@ -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?
}
}
+80 -6
View File
@@ -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<Server>,
#[serde(default, rename = "build")]
builds: Vec<Build>,
#[serde(default, rename = "deployment")]
deployments: Vec<Deployment>,
// #[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<ResourceFile> {
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::<ResourceFile>(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"))
}
}
+10 -2
View File
@@ -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"
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"