From 35aedf1d5b07a12ce675a2e18682ef73f6f19a63 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Thu, 1 Oct 2026 02:21:39 +0200 Subject: [PATCH] feat: Configs are captured in a string first --- src/cluster.rs | 31 +++++++++++++++++++++---------- src/lib.rs | 13 +++++++++++++ src/main.rs | 18 +++--------------- src/node.rs | 24 +++++++++++++++++------- 4 files changed, 54 insertions(+), 32 deletions(-) diff --git a/src/cluster.rs b/src/cluster.rs index bd3c670..4b02681 100644 --- a/src/cluster.rs +++ b/src/cluster.rs @@ -15,7 +15,7 @@ use tokio::process::Command; use crate::environment::PathEnvironment; use crate::node::{Node, OptionalNodeDeserialize}; use crate::patch::Patches; -use crate::{get_configs_path, get_talos_path}; +use crate::{get_configs_path, get_talos_path, run_command}; #[optional_struct] #[derive(Debug, Deserialize, JsonSchema, Clone)] @@ -174,9 +174,7 @@ impl Cluster { &self.nodes } - pub fn talosctl_gen_config_command(&self) -> Command { - let path = get_configs_path().join(&self.name).join("talosconfig"); - + pub async fn talosconfig(&self) -> String { let mut command = Command::new("talosctl"); command.args([ "gen", @@ -188,13 +186,13 @@ impl Cluster { "--output-types", "talosconfig", "-o", - path.to_str().expect("Path should be valid utf-8"), + "-", ]); - command + run_command(command).await } - pub fn talosctl_add_endpoint_command(&self) -> Command { + async fn talosconfig_add_endpoint(&self) { let path = get_configs_path().join(&self.name).join("talosconfig"); let mut command = Command::new("talosctl"); @@ -206,10 +204,23 @@ impl Cluster { &self.control_plane_ip.to_string(), ]); - command + run_command(command).await; } - pub fn talosctl_merge_command(&self) -> Command { + pub async fn talosconfig_write(&self) { + let config = self.talosconfig().await; + + let path = get_configs_path().join(&self.name).join("talosconfig"); + tokio::fs::create_dir_all(path.parent().expect("talosconfig should have parent")) + .await + .unwrap(); + + tokio::fs::write(path, config).await.unwrap(); + + self.talosconfig_add_endpoint().await + } + + pub async fn talosconfig_merge(&self) { let cluster_path = get_configs_path().join(&self.name).join("talosconfig"); let path = get_configs_path().join("talosconfig"); @@ -222,7 +233,7 @@ impl Cluster { cluster_path.to_str().expect("Path should be valid utf-8"), ]); - command + run_command(command).await; } } diff --git a/src/lib.rs b/src/lib.rs index 549d4b1..ce4e59a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,8 @@ use std::path::{Path, PathBuf}; use std::sync::OnceLock; +use tokio::process::Command; + pub mod cluster; pub mod environment; pub mod node; @@ -29,3 +31,14 @@ pub fn get_talos_path() -> PathBuf { pub fn get_configs_path() -> PathBuf { get_repo_path().join("configs") } + +pub async fn run_command(mut command: Command) -> String { + let output = command.output().await.unwrap(); + if !output.stderr.is_empty() { + println!("{}", String::from_utf8_lossy(&output.stderr).trim_end()); + } + + String::from_utf8_lossy(&output.stdout) + .trim_end() + .to_owned() +} diff --git a/src/main.rs b/src/main.rs index 4cbb473..6cd6231 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,6 @@ use futures::StreamExt; use futures::future::join_all; use minijinja::context; use thiserror::Error; -use tokio::process::Command; use crate::cli::{Cli, Commands, GlobalOpts}; @@ -21,16 +20,6 @@ enum Error { NoClustersFound, } -async fn run_command(mut command: Command) { - let output = command.output().await.unwrap(); - if !output.stdout.is_empty() { - println!("{}", String::from_utf8_lossy(&output.stdout).trim_end()); - } - if !output.stderr.is_empty() { - println!("{}", String::from_utf8_lossy(&output.stderr).trim_end()); - } -} - async fn generate(opts: &GlobalOpts) -> Result<(), Error> { set_repo_path(&opts.repo); @@ -74,22 +63,21 @@ async fn generate(opts: &GlobalOpts) -> Result<(), Error> { join_all(cluster.nodes().iter().map(|node| { let cluster = cluster.clone(); async move { - run_command(node.talosctl_gen_config_command(&cluster)).await; + node.config_write(&cluster).await; } })) .await; // Run these commands in parallel with the the node commands // NOTE: These do need to happen in the correct order - run_command(cluster.talosctl_gen_config_command()).await; - run_command(cluster.talosctl_add_endpoint_command()).await; + cluster.talosconfig_write().await; })) .await; // Merge all the cluster talosconfig files into a single file // Since this all modifies the same output file this has to happen sequentially for cluster in clusters { - run_command(cluster.talosctl_merge_command()).await; + cluster.talosconfig_merge().await; } Ok(()) diff --git a/src/node.rs b/src/node.rs index e4539da..015e0b8 100644 --- a/src/node.rs +++ b/src/node.rs @@ -10,7 +10,7 @@ use crate::cluster::{Base, Cluster}; use crate::patch::{OptionalPatches, OptionalPatchesString, Patches}; use crate::schematic::Schematic; use crate::secret::Secret; -use crate::{get_configs_path, get_talos_path}; +use crate::{get_configs_path, get_talos_path, run_command}; #[derive(Debug, Serialize, Deserialize, JsonSchema, Clone, Copy, PartialEq, Eq)] #[serde(rename_all = "camelCase")] @@ -177,10 +177,7 @@ impl Node { Node { patches, ..node } } - pub fn talosctl_gen_config_command(&self, cluster: &Cluster) -> Command { - let mut path = get_configs_path().join(&cluster.name).join(&self.hostname); - path.add_extension("yaml"); - + pub async fn config(&self, cluster: &Cluster) -> String { let mut command = Command::new("talosctl"); command.args([ "gen", @@ -204,7 +201,7 @@ impl Node { "--with-docs=false", "--with-examples=false", "-o", - path.to_str().expect("Path should be valid utf-8"), + "-", ]); for patch in &self.patches.all { @@ -218,7 +215,20 @@ impl Node { ]); } - command + run_command(command).await + } + + pub async fn config_write(&self, cluster: &Cluster) { + let config = self.config(cluster).await; + + let mut path = get_configs_path().join(&cluster.name).join(&self.hostname); + path.add_extension("yaml"); + + tokio::fs::create_dir_all(path.parent().expect("config should have parent")) + .await + .unwrap(); + + tokio::fs::write(path, config).await.unwrap(); } }