Serve command #27

Merged
Dreaded_X merged 20 commits from feature/tftp_side into main 2026-10-21 05:15:47 +00:00
4 changed files with 54 additions and 32 deletions
Showing only changes of commit 35aedf1d5b - Show all commits
+21 -10
View File
@@ -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;
}
}
+13
View File
@@ -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()
}
+3 -15
View File
@@ -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(())
+17 -7
View File
@@ -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();
}
}