feat: Make application async
This commit is contained in:
+28
-19
@@ -1,13 +1,15 @@
|
||||
use std::net::Ipv4Addr;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
use std::str::FromStr;
|
||||
|
||||
use async_walkdir::WalkDir;
|
||||
use futures::StreamExt;
|
||||
use futures::stream::FuturesUnordered;
|
||||
use minijinja::Environment;
|
||||
use optional_struct::{Applicable, optional_struct};
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use walkdir::WalkDir;
|
||||
use tokio::process::Command;
|
||||
|
||||
use crate::environment::PathEnvironment;
|
||||
use crate::node::{Node, OptionalNodeDeserialize};
|
||||
@@ -68,7 +70,7 @@ struct ClusterDeserialize {
|
||||
base: Base,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Cluster {
|
||||
pub(crate) name: String,
|
||||
@@ -80,11 +82,11 @@ pub struct Cluster {
|
||||
}
|
||||
|
||||
impl Cluster {
|
||||
pub(crate) fn get(cluster_name: &str, env: &Environment) -> Self {
|
||||
pub(crate) async fn get(cluster_name: &str, env: &Environment<'static>) -> Self {
|
||||
let path = get_talos_path().join("clusters").join("default.yaml");
|
||||
|
||||
let default: OptionalClusterDeserialize = if path.exists() {
|
||||
let content = std::fs::read_to_string(path).unwrap();
|
||||
let content = tokio::fs::read_to_string(path).await.unwrap();
|
||||
serde_yaml::from_str(&content).unwrap()
|
||||
} else {
|
||||
Default::default()
|
||||
@@ -92,7 +94,7 @@ impl Cluster {
|
||||
|
||||
let mut path = get_talos_path().join("clusters").join(cluster_name);
|
||||
path.add_extension("yaml");
|
||||
let content = std::fs::read_to_string(path).unwrap();
|
||||
let content = tokio::fs::read_to_string(path).await.unwrap();
|
||||
|
||||
let mut cluster: OptionalClusterDeserialize = serde_yaml::from_str(&content).unwrap();
|
||||
|
||||
@@ -122,32 +124,39 @@ impl Cluster {
|
||||
|
||||
let nodes = nodes
|
||||
.iter()
|
||||
.map(|name| Node::get(name, env, &cluster, &default, &base))
|
||||
.collect();
|
||||
.map(|name| async { Node::get(name, env, &cluster, &default, &base).await });
|
||||
|
||||
let nodes = FuturesUnordered::from_iter(nodes).collect().await;
|
||||
|
||||
Self { nodes, ..cluster }
|
||||
}
|
||||
|
||||
pub fn get_all() -> Vec<Cluster> {
|
||||
pub async fn get_all() -> Vec<Cluster> {
|
||||
let env = PathEnvironment::new_patches();
|
||||
|
||||
WalkDir::new(get_talos_path().join("clusters"))
|
||||
.into_iter()
|
||||
.filter_map(|e| e.ok())
|
||||
.filter(|e| e.metadata().unwrap().is_file())
|
||||
.filter_map(|e| async { e.ok() })
|
||||
.filter(|e| {
|
||||
let e = e.clone();
|
||||
async move { e.metadata().await.unwrap().is_file() }
|
||||
})
|
||||
.filter_map(|e| {
|
||||
let mut path = PathBuf::from_str(&e.file_name().to_string_lossy()).unwrap();
|
||||
path.set_extension("");
|
||||
let env = env.clone();
|
||||
async move {
|
||||
let mut path = PathBuf::from_str(&e.file_name().to_string_lossy()).unwrap();
|
||||
path.set_extension("");
|
||||
|
||||
let name = path.to_string_lossy().to_string();
|
||||
let name = path.to_string_lossy().to_string();
|
||||
|
||||
if name == "default" {
|
||||
return None;
|
||||
if name == "default" {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Cluster::get(&name, &env).await)
|
||||
}
|
||||
|
||||
Some(Cluster::get(&name, &env))
|
||||
})
|
||||
.collect()
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn nodes(&self) -> &[Node] {
|
||||
|
||||
Reference in New Issue
Block a user