feat: Configs are captured in a string first

This commit is contained in:
2026-10-04 05:53:15 +02:00
parent 1ad7a928ae
commit 35aedf1d5b
4 changed files with 54 additions and 32 deletions
+21 -10
View File
@@ -15,7 +15,7 @@ use tokio::process::Command;
use crate::environment::PathEnvironment; use crate::environment::PathEnvironment;
use crate::node::{Node, OptionalNodeDeserialize}; use crate::node::{Node, OptionalNodeDeserialize};
use crate::patch::Patches; use crate::patch::Patches;
use crate::{get_configs_path, get_talos_path}; use crate::{get_configs_path, get_talos_path, run_command};
#[optional_struct] #[optional_struct]
#[derive(Debug, Deserialize, JsonSchema, Clone)] #[derive(Debug, Deserialize, JsonSchema, Clone)]
@@ -174,9 +174,7 @@ impl Cluster {
&self.nodes &self.nodes
} }
pub fn talosctl_gen_config_command(&self) -> Command { pub async fn talosconfig(&self) -> String {
let path = get_configs_path().join(&self.name).join("talosconfig");
let mut command = Command::new("talosctl"); let mut command = Command::new("talosctl");
command.args([ command.args([
"gen", "gen",
@@ -188,13 +186,13 @@ impl Cluster {
"--output-types", "--output-types",
"talosconfig", "talosconfig",
"-o", "-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 path = get_configs_path().join(&self.name).join("talosconfig");
let mut command = Command::new("talosctl"); let mut command = Command::new("talosctl");
@@ -206,10 +204,23 @@ impl Cluster {
&self.control_plane_ip.to_string(), &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 cluster_path = get_configs_path().join(&self.name).join("talosconfig");
let path = get_configs_path().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"), cluster_path.to_str().expect("Path should be valid utf-8"),
]); ]);
command run_command(command).await;
} }
} }
+13
View File
@@ -2,6 +2,8 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::OnceLock; use std::sync::OnceLock;
use tokio::process::Command;
pub mod cluster; pub mod cluster;
pub mod environment; pub mod environment;
pub mod node; pub mod node;
@@ -29,3 +31,14 @@ pub fn get_talos_path() -> PathBuf {
pub fn get_configs_path() -> PathBuf { pub fn get_configs_path() -> PathBuf {
get_repo_path().join("configs") 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()
}
+3 -15
View File
@@ -11,7 +11,6 @@ use futures::StreamExt;
use futures::future::join_all; use futures::future::join_all;
use minijinja::context; use minijinja::context;
use thiserror::Error; use thiserror::Error;
use tokio::process::Command;
use crate::cli::{Cli, Commands, GlobalOpts}; use crate::cli::{Cli, Commands, GlobalOpts};
@@ -21,16 +20,6 @@ enum Error {
NoClustersFound, 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> { async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
set_repo_path(&opts.repo); set_repo_path(&opts.repo);
@@ -74,22 +63,21 @@ async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
join_all(cluster.nodes().iter().map(|node| { join_all(cluster.nodes().iter().map(|node| {
let cluster = cluster.clone(); let cluster = cluster.clone();
async move { async move {
run_command(node.talosctl_gen_config_command(&cluster)).await; node.config_write(&cluster).await;
} }
})) }))
.await; .await;
// Run these commands in parallel with the the node commands // Run these commands in parallel with the the node commands
// NOTE: These do need to happen in the correct order // NOTE: These do need to happen in the correct order
run_command(cluster.talosctl_gen_config_command()).await; cluster.talosconfig_write().await;
run_command(cluster.talosctl_add_endpoint_command()).await;
})) }))
.await; .await;
// Merge all the cluster talosconfig files into a single file // Merge all the cluster talosconfig files into a single file
// Since this all modifies the same output file this has to happen sequentially // Since this all modifies the same output file this has to happen sequentially
for cluster in clusters { for cluster in clusters {
run_command(cluster.talosctl_merge_command()).await; cluster.talosconfig_merge().await;
} }
Ok(()) Ok(())
+17 -7
View File
@@ -10,7 +10,7 @@ use crate::cluster::{Base, Cluster};
use crate::patch::{OptionalPatches, OptionalPatchesString, Patches}; use crate::patch::{OptionalPatches, OptionalPatchesString, Patches};
use crate::schematic::Schematic; use crate::schematic::Schematic;
use crate::secret::Secret; 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)] #[derive(Debug, Serialize, Deserialize, JsonSchema, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@@ -177,10 +177,7 @@ impl Node {
Node { patches, ..node } Node { patches, ..node }
} }
pub fn talosctl_gen_config_command(&self, cluster: &Cluster) -> Command { pub async fn config(&self, cluster: &Cluster) -> String {
let mut path = get_configs_path().join(&cluster.name).join(&self.hostname);
path.add_extension("yaml");
let mut command = Command::new("talosctl"); let mut command = Command::new("talosctl");
command.args([ command.args([
"gen", "gen",
@@ -204,7 +201,7 @@ impl Node {
"--with-docs=false", "--with-docs=false",
"--with-examples=false", "--with-examples=false",
"-o", "-o",
path.to_str().expect("Path should be valid utf-8"), "-",
]); ]);
for patch in &self.patches.all { 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();
} }
} }