13 Commits
Author SHA1 Message Date
Dreaded_X eaa925025d feat: Migrate to serde-saphyr
Committed / committed (pull_request) Successful in 1m9s
CI / cargo shear (pull_request) Successful in 6m12s
CI / prek (pull_request) Successful in 40m8s
CI / cargo shear (push) Successful in 8m30s
Audit / cargo audit (push) Failing after 13m17s
Release-plz / Release-plz Release (push) Successful in 5m15s
Release-plz / Release-plz PR (push) Failing after 5m34s
CI / prek (push) Successful in 22m43s
Fixes: #30
2026-10-21 08:09:25 +02:00
Dreaded_X 65ed7ab1d2 chore: Fix release-plz config
CI / cargo shear (push) Successful in 8m18s
Committed / committed (pull_request) Successful in 2m11s
CI / cargo shear (pull_request) Successful in 6m11s
CI / prek (pull_request) Successful in 30m45s
Release-plz / Release-plz Release (push) Successful in 4m45s
Release-plz / Release-plz PR (push) Failing after 6m25s
CI / prek (push) Successful in 27m25s
2026-10-21 07:52:48 +02:00
Dreaded_X 8e9c461a45 chore(deps): Upgrade all deps
Committed / committed (pull_request) Successful in 1m4s
CI / cargo shear (pull_request) Successful in 4m19s
CI / prek (pull_request) Successful in 19m18s
CI / cargo shear (push) Successful in 7m46s
Audit / cargo audit (push) Failing after 12m52s
Release-plz / Release-plz Release (push) Failing after 3m22s
Release-plz / Release-plz PR (push) Failing after 14m19s
CI / prek (push) Successful in 32m52s
2026-10-21 07:30:51 +02:00
Dreaded_X c183f1a5f1 feat: Enable HTTPS in iPXE
CI / cargo shear (pull_request) Successful in 7m29s
Committed / committed (pull_request) Successful in 2m17s
CI / prek (pull_request) Successful in 16m52s
CI / cargo shear (push) Successful in 8m39s
Audit / cargo audit (push) Failing after 12m22s
Release-plz / Release-plz PR (push) Failing after 7m6s
Release-plz / Release-plz Release (push) Failing after 4m58s
CI / prek (push) Successful in 29m28s
2026-10-21 06:53:05 +02:00
Dreaded_X f9f0be1e92 feat: Rework iPXE build script to allow for patching 2026-10-21 06:53:04 +02:00
Dreaded_X b9251ee5dc refactor: Improve async
Committed / committed (pull_request) Successful in 20s
CI / cargo shear (pull_request) Successful in 6m4s
CI / prek (pull_request) Failing after 7m3s
2026-10-20 03:45:31 +02:00
Dreaded_X 427ec6f61d feat: Reuse compiled regex
Committed / committed (pull_request) Successful in 26s
CI / cargo shear (pull_request) Successful in 5m12s
CI / prek (pull_request) Failing after 6m43s
2026-10-20 03:11:27 +02:00
Dreaded_X 8149ffbc37 feat: Include boot.ipxe script 2026-10-20 02:59:13 +02:00
Dreaded_X 620f36d0bb feat: Added http server that serve the same files 2026-10-20 02:56:39 +02:00
Dreaded_X a5594c768d feat: Make bind ip configurable through cli 2026-10-15 04:46:48 +02:00
Dreaded_X 5a5df6bfc3 feat!: Pass connected to ip to serve callback 2026-10-15 03:43:37 +02:00
Dreaded_X 5552013eeb feat: Bind to address that was connected to 2026-10-15 03:40:20 +02:00
Dreaded_X cc84f13ed7 feat: Use fixed size buffer for receiving packet 2026-10-15 03:32:22 +02:00
24 changed files with 2011 additions and 320 deletions
-3
View File
@@ -1,3 +0,0 @@
[submodule "ipxe/ipxe"]
path = ipxe/ipxe
url = git@github.com:rkubes/ipxe
+9
View File
@@ -1,5 +1,14 @@
[workspace] [workspace]
pr_name = "chore(release): Release v{{ version }}" pr_name = "chore(release): Release v{{ version }}"
changelog_path = "./CHANGELOG.md"
[[package]]
name = "crete"
changelog_include = ["ipxe"]
[[package]]
name = "ipxe"
release = false
[[package]] [[package]]
name = "xtask" name = "xtask"
Generated
+1597 -113
View File
File diff suppressed because it is too large Load Diff
+8 -6
View File
@@ -10,8 +10,8 @@ license = "GPL-3.0-or-later"
members = ["ipxe", "xtask"] members = ["ipxe", "xtask"]
[workspace.dependencies] [workspace.dependencies]
schemars = { version = "1.2.2", features = ["semver1"] } schemars = { version = "1.2.3", features = ["semver1"] }
serde_json = "1.0.151" serde_json = "1.0.154"
[dependencies] [dependencies]
async-walkdir = "2.2.0" async-walkdir = "2.2.0"
@@ -19,8 +19,8 @@ byteorder = "1.5.0"
clap = { version = "4.6.7", features = ["derive"] } clap = { version = "4.6.7", features = ["derive"] }
clap_complete = "4.6.11" clap_complete = "4.6.11"
futures = "0.3.34" futures = "0.3.34"
gix-discover = { version = "0.56.0", features = ["sha1"] } gix-discover = { version = "0.57.0", features = ["sha1"] }
minijinja = { version = "2.24.0", features = ["json", "loader"] } minijinja = { version = "3.0.1", features = ["json", "serde"] }
netascii = "0.1.0" netascii = "0.1.0"
num_enum = "0.7.6" num_enum = "0.7.6"
optional_struct = "0.5.2" optional_struct = "0.5.2"
@@ -34,10 +34,12 @@ serde = { version = "1.0.229", features = ["derive"] }
serde_json = { workspace = true } serde_json = { workspace = true }
serde_yaml = "0.9.34" serde_yaml = "0.9.34"
thiserror = "2.0.21" thiserror = "2.0.21"
tokio = { version = "1.52.1", features = ["full"] } tokio = { version = "1.54.0", features = ["full"] }
ipxe = { path = "./ipxe" } ipxe = { path = "./ipxe" }
axum = { version = "0.8.9", features = ["macros"] } axum = { version = "0.8.9", features = ["macros"] }
udp_sas_async = { version = "0.2.0", features = ["async_tokio"] } socket2 = "0.6.5"
socket-pktinfo = "0.4.1"
serde-saphyr = "2.0.0"
[lib] [lib]
test = false test = false
+7 -1
View File
@@ -4,7 +4,13 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[build-dependencies] [build-dependencies]
build-print = "1.0.1" diffy = "0.5.2"
gix = { version = "0.89.0", default-features = false, features = [
"blocking-network-client",
"worktree-mutation",
"sha1",
"blocking-http-transport-reqwest-rust-tls",
] }
[dependencies] [dependencies]
include_dir = "0.7.4" include_dir = "0.7.4"
+151 -29
View File
@@ -1,58 +1,180 @@
use std::env; use std::collections::HashMap;
use std::path::PathBuf; use std::path::{Path, PathBuf};
use std::process::{Command, Stdio}; use std::process::Command;
use std::sync::atomic::AtomicBool;
use build_print::error; use diffy::apply;
use diffy::patch_set::{FileOperation, ParseOptions, PatchSet};
use gix::Repository;
use gix::progress::Discard;
const URL: &str = "https://github.com/ipxe/ipxe";
const REVISION: &str = "2fa08affe9a1a1422766f54b2fb045234b571f06";
const FILES: &[&str] = &["bin/ipxe.pxe", "bin-x86_64-efi/ipxe.efi"];
const SRC_DIR: &str = "src";
fn main() { fn main() {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()) let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.canonicalize() .canonicalize()
.unwrap(); .unwrap();
let src_dir = manifest_dir.join("ipxe").join("src"); let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap())
.canonicalize()
.unwrap();
let repo_dir = out_dir.join("repo");
let repo = checkout(&out_dir, &repo_dir);
apply_patches(&repo, &manifest_dir, &repo_dir);
println!("cargo::rerun-if-changed=patches");
build(&manifest_dir, &repo_dir);
println!("cargo::rerun-if-changed=src/embed.ipxe");
copy_files(&repo_dir, &out_dir);
}
fn checkout(out_dir: &Path, repo_dir: &Path) -> Repository {
// Stamp file is used instead of comparing against HEAD to guard against incomplete clones
let stamp = out_dir.join("checkout.stamp");
if std::fs::read_to_string(&stamp).is_ok_and(|stamp| stamp == REVISION)
&& let Ok(repo) = gix::open(repo_dir)
{
return repo;
}
// Cleanup any left-overs
std::fs::remove_file(&stamp).ok();
std::fs::remove_dir_all(repo_dir).ok();
std::fs::create_dir_all(repo_dir).unwrap();
let mut prepare = gix::prepare_clone(URL, repo_dir)
.unwrap()
.with_revision(Some(REVISION))
.unwrap();
let interrupt = AtomicBool::new(false);
let (mut checkout, _) = prepare.fetch_then_checkout(Discard, &interrupt).unwrap();
let (repo, _) = checkout.main_worktree(Discard, &interrupt).unwrap();
std::fs::write(&stamp, REVISION).unwrap();
repo
}
fn apply_patches(repo: &Repository, manifest_dir: &Path, repo_dir: &Path) {
let commit_id = gix::ObjectId::from_hex(REVISION.as_bytes()).unwrap();
let tree = repo.find_commit(commit_id).unwrap().tree().unwrap();
// Get all the patches and sort them based on name
let mut patch_files: Vec<PathBuf> = std::fs::read_dir(manifest_dir.join("patches"))
.unwrap()
.map(|e| e.unwrap().path())
.filter(|e| e.extension().is_some_and(|e| e == "patch"))
.collect();
patch_files.sort();
let mut patched = HashMap::new();
for path in patch_files {
let text = std::fs::read_to_string(&path).unwrap();
let patch_set = PatchSet::parse(&text, ParseOptions::gitdiff());
for patch in patch_set {
let patch = patch.unwrap();
match patch.operation().strip_prefix(1) {
FileOperation::Modify { original, modified } => {
assert_eq!(original, modified);
let path = original.to_string();
// Either get the current in-memory patched file, otherwise get the prisine file
let current = patched
.remove(&path)
.unwrap_or_else(|| pristine(&tree, &path));
let new = apply(&current, patch.patch().as_text().unwrap()).unwrap();
patched.insert(path, new);
}
_ => todo!(),
}
}
}
// If the file was previously patched, but now isn't restore the pristine file
for path in modified_files(repo) {
if !patched.contains_key(&path) {
std::fs::write(repo_dir.join(&path), pristine(&tree, &path)).unwrap();
}
}
// Write all the patched files
for (path, content) in &patched {
let path = repo_dir.join(path);
let content = content.as_bytes();
// But only if the file is actually different from the one on disk
// This prevents changing the mtime unnecessarily
if std::fs::read(&path).ok().as_deref() != Some(content) {
std::fs::write(path, content).unwrap();
}
}
}
fn pristine(tree: &gix::Tree<'_>, path: &str) -> String {
let entry = tree.lookup_entry_by_path(path).unwrap().unwrap();
String::from_utf8(entry.object().unwrap().data.clone()).unwrap()
}
fn modified_files(repo: &gix::Repository) -> Vec<String> {
repo.status(Discard)
.unwrap()
.untracked_files(gix::status::UntrackedFiles::None)
.into_index_worktree_iter(Vec::<gix::bstr::BString>::new())
.unwrap()
.map(|item| item.unwrap().rela_path().to_string())
.collect()
}
fn build(manifest_dir: &Path, repo_dir: &Path) {
let src_dir = repo_dir.join(SRC_DIR);
let mut command = Command::new("make"); let mut command = Command::new("make");
command command
.stdout(Stdio::piped())
.current_dir(&src_dir) .current_dir(&src_dir)
.env_remove("DEBUG") .env_remove("DEBUG")
.env_remove("PROFILE") .env_remove("PROFILE")
.args([
"-j",
&std::thread::available_parallelism()
.unwrap()
.get()
.to_string(),
])
.arg("DOWNLOAD_PROTO_HTTP=1")
.arg(format!( .arg(format!(
"EMBED={}", "EMBED={}",
manifest_dir manifest_dir
.join("src") .join("src")
.join("embed.ipxe") .join("embed.ipxe")
.to_string_lossy(), .to_string_lossy()
)); ))
.args(FILES);
let files = ["bin/ipxe.pxe", "bin-x86_64-efi/ipxe.efi"].map(PathBuf::from); if let Ok(flags) = std::env::var("CARGO_MAKEFLAGS") {
for file in &files { command.env("MAKEFLAGS", flags);
command.arg(file);
} }
let output = command.output().unwrap(); let output = command.output().unwrap();
if !output.status.success() { if !output.status.success() {
error!("{}", String::from_utf8_lossy(&output.stderr)); panic!("make failed:\n{}", String::from_utf8_lossy(&output.stderr));
} }
}
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()) fn copy_files(repo_dir: &Path, out_dir: &Path) {
.canonicalize() let src_dir = repo_dir.join(SRC_DIR);
.unwrap() let bin_dir = out_dir.join("bin");
.join("ipxe");
std::fs::remove_dir_all(&out_dir).ok(); std::fs::create_dir_all(&bin_dir).unwrap();
for file in &files { for file in FILES {
let file = Path::new(file);
let input = src_dir.join(file); let input = src_dir.join(file);
let output = out_dir.join(file.file_name().unwrap()); let output = bin_dir.join(file.file_name().unwrap());
std::fs::create_dir_all(output.parent().unwrap()).unwrap();
std::fs::copy(input, output).unwrap(); std::fs::copy(input, output).unwrap();
} }
} }
Submodule ipxe/ipxe deleted from 8892af68a2
View File
+19
View File
@@ -0,0 +1,19 @@
diff --git a/src/config/general.h b/src/config/general.h
index 43858dceb..54cb1b657 100644
--- a/src/config/general.h
+++ b/src/config/general.h
@@ -60,10 +60,10 @@ FILE_SECBOOT ( PERMITTED );
//#define HTTP_ENC_PEERDIST /* PeerDist content encoding */
/* Disable protocols not historically included in BIOS builds */
-#if defined ( PLATFORM_pcbios )
- #undef DOWNLOAD_PROTO_HTTPS
- #undef HTTP_AUTH_NTLM
-#endif
+// #if defined ( PLATFORM_pcbios )
+// #undef DOWNLOAD_PROTO_HTTPS
+// #undef HTTP_AUTH_NTLM
+// #endif
/*****************************************************************************
*
+1 -1
View File
@@ -1,3 +1,3 @@
use include_dir::{Dir, include_dir}; use include_dir::{Dir, include_dir};
pub static IPXE_DIR: Dir<'_> = include_dir!("$OUT_DIR/ipxe"); pub static IPXE_DIR: Dir<'_> = include_dir!("$OUT_DIR/bin");
-1
View File
@@ -1,4 +1,3 @@
{% set httpUrl = "http://192.168.1.1:8000" -%}
#!ipxe #!ipxe
echo Starting ${serial} echo Starting ${serial}
+7 -1
View File
@@ -1,3 +1,4 @@
use std::net::IpAddr;
use std::path::PathBuf; use std::path::PathBuf;
use clap::{Args, Parser, Subcommand}; use clap::{Args, Parser, Subcommand};
@@ -50,5 +51,10 @@ pub enum Commands {
/// Generate completions for your current shell /// Generate completions for your current shell
ShellCompletions, ShellCompletions,
/// Serve config files over tftp /// Serve config files over tftp
Serve, Serve {
#[arg(short, long, default_value = "3000")]
port: u16,
#[arg(long, default_value = "0.0.0.0")]
ip: IpAddr,
},
} }
+1 -1
View File
@@ -102,7 +102,7 @@ impl Cluster {
path.add_extension("yaml"); path.add_extension("yaml");
let content = tokio::fs::read_to_string(path).await.unwrap(); let content = tokio::fs::read_to_string(path).await.unwrap();
let mut cluster: OptionalClusterDeserialize = serde_yaml::from_str(&content).unwrap(); let mut cluster: OptionalClusterDeserialize = serde_saphyr::from_str(&content).unwrap();
// For some reason apply on the cluster does not properly apply to the default settings... // For some reason apply on the cluster does not properly apply to the default settings...
// So we manually apply it here first // So we manually apply it here first
+13 -7
View File
@@ -4,8 +4,8 @@ use std::path::{Path, PathBuf};
use async_walkdir::WalkDir; use async_walkdir::WalkDir;
use futures::{Stream, StreamExt}; use futures::{Stream, StreamExt};
use minijinja::value::ViaDeserialize; use minijinja::syntax::SyntaxConfig;
use minijinja::{AutoEscape, Environment, path_loader}; use minijinja::{AutoEscape, Environment, path_loader, value};
use crate::{get_configs_path, get_talos_path}; use crate::{get_configs_path, get_talos_path};
@@ -19,21 +19,27 @@ pub struct PathEnvironment<'a> {
} }
fn apply_netmask( fn apply_netmask(
ip: ViaDeserialize<Ipv4Addr>, ip: value::Serde<Ipv4Addr>,
netmask: ViaDeserialize<Ipv4Addr>, netmask: value::Serde<Ipv4Addr>,
) -> Result<String, minijinja::Error> { ) -> Result<String, minijinja::Error> {
Ok((*ip & *netmask).to_string()) Ok((*ip & *netmask).to_string())
} }
pub fn configure_environment(env: &mut Environment) { pub fn configure_environment(env: &mut Environment) {
// Configure jinja // Configure jinja
env.set_trim_blocks(true); // Configure jinja
env.set_lstrip_blocks(true); env.set_syntax(
SyntaxConfig::builder()
.trim_blocks(true)
.lstrip_blocks(true)
.build()
.expect("Syntax config should be valid"),
);
env.set_undefined_behavior(minijinja::UndefinedBehavior::Strict); env.set_undefined_behavior(minijinja::UndefinedBehavior::Strict);
env.set_auto_escape_callback(|_| AutoEscape::None); env.set_auto_escape_callback(|_| AutoEscape::None);
// Add filters // Add filters
env.add_filter("to_prefix", |netmask: ViaDeserialize<Ipv4Addr>| { env.add_filter("to_prefix", |netmask: value::Serde<Ipv4Addr>| {
let mask = netmask.to_bits(); let mask = netmask.to_bits();
let prefix = mask.leading_ones(); let prefix = mask.leading_ones();
+10
View File
@@ -0,0 +1,10 @@
use crete::tftp::ServerError;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("No clusters where found")]
NoClustersFound,
#[error("Server error: {0}")]
ServerError(#[from] ServerError),
}
+55
View File
@@ -0,0 +1,55 @@
use crete::cluster::Cluster;
use crete::environment::configure_environment;
use ipxe::IPXE_DIR;
use minijinja::{Environment, context, value};
use regex::regex;
pub async fn get_file(
filename: impl ToString,
http_url: impl ToString,
) -> Option<(Vec<u8>, &'static str)> {
let re = regex!(r"^talos/(?<cluster>.+)/(?<node>.+)\.ya?ml$");
let filename: String = filename.to_string();
let re = re.clone();
const IPXE_PATTERN: &str = "ipxe/";
if filename == format!("{IPXE_PATTERN}boot.ipxe") {
let mut env = Environment::new();
configure_environment(&mut env);
let template = env
.template_from_str(include_str!("./boot.ipxe"))
.expect("Template should be valid");
let clusters = Cluster::get_all().await;
let content = template
.render(context! {
clusters => value::Serde(clusters.values().collect::<Vec<_>>()),
httpUrl => http_url.to_string()
})
.unwrap();
Some((content.into(), "text/plain; charset=utf-8"))
} else if let Some(filename) = filename.strip_prefix(IPXE_PATTERN) {
Some((
IPXE_DIR.get_file(filename)?.contents().to_owned(),
"application/octet-stream",
))
} else if let Some(caps) = re.captures(&filename) {
let cluster = &caps["cluster"];
let node = &caps["node"];
let clusters = Cluster::get_all().await;
let cluster = clusters.get(cluster)?;
let node = cluster.node(node)?;
let config = node.config(cluster).await;
Some((config.into(), "application/yaml; charset=utf-8"))
} else {
None
}
}
+46
View File
@@ -0,0 +1,46 @@
use std::path::PathBuf;
use axum::debug_handler;
use axum::extract::Request;
use axum::http::Uri;
use axum::response::IntoResponse;
use reqwest::{StatusCode, header};
use crate::file::get_file;
#[debug_handler]
pub async fn handler(req: Request) -> impl IntoResponse {
let uri: Uri = req
.headers()
.iter()
.find(|(name, _)| header::HOST.eq(name))
.map(|(_, value)| value)
.expect("Host header should exist")
.to_str()
.expect("Header value should be valid ASCII")
.parse()
.expect("Host should be valid Uri");
let host = uri.host().expect("Uri should have host");
let port = uri.port().map(|port| port.as_u16()).unwrap_or(80);
let Some(path) = req.uri().path().strip_prefix("/") else {
return StatusCode::BAD_REQUEST.into_response();
};
let Some((file, content_type)) = get_file(path, format!("http://{host}:{port}")).await else {
return StatusCode::NOT_FOUND.into_response();
};
let path = PathBuf::from(path);
let filename = path.file_name().unwrap();
let filename = filename.to_str().unwrap();
let content_type = [(header::CONTENT_TYPE, content_type)];
let disposition = [(
header::CONTENT_DISPOSITION,
format!("inline; filename=\"{filename}\""),
)];
(content_type, disposition, file).into_response()
}
+22 -113
View File
@@ -1,36 +1,27 @@
mod cli; mod cli;
mod error;
mod file;
mod http;
use std::path::PathBuf; use std::net::SocketAddr;
use std::pin::pin; use std::pin::pin;
use axum::extract::Request; use axum::Router;
use axum::response::IntoResponse;
use axum::routing::get; use axum::routing::get;
use axum::{Router, debug_handler};
use clap::{CommandFactory, Parser}; use clap::{CommandFactory, Parser};
use clap_complete::{Shell, generate as generate_complete}; use clap_complete::{Shell, generate as generate_complete};
use crete::cluster::Cluster; use crete::cluster::Cluster;
use crete::environment::{PathEnvironment, configure_environment}; use crete::environment::PathEnvironment;
use crete::talosconfig::{get_current_context, set_context}; use crete::talosconfig::{get_current_context, set_context};
use crete::tftp::ServerError;
use crete::{get_configs_path, get_repo_path, set_repo_path, tftp}; use crete::{get_configs_path, get_repo_path, set_repo_path, tftp};
use futures::StreamExt; use futures::StreamExt;
use futures::future::join_all; use futures::future::join_all;
use ipxe::IPXE_DIR; use minijinja::{context, value};
use minijinja::{Environment, context};
use regex::Regex;
use reqwest::{StatusCode, header};
use thiserror::Error;
use crate::cli::{Cli, Commands, GlobalOpts}; use crate::cli::{Cli, Commands, GlobalOpts};
use crate::error::Error;
#[derive(Debug, Error)] use crate::file::get_file;
enum Error { use crate::http::handler;
#[error("No clusters where found")]
NoClustersFound,
#[error("Server error: {0}")]
ServerError(#[from] ServerError),
}
async fn generate(opts: &GlobalOpts) -> Result<(), Error> { async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
let clusters = Cluster::get_all().await; let clusters = Cluster::get_all().await;
@@ -53,8 +44,8 @@ async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
let content = template let content = template
.render(context! { .render(context! {
clusters => clusters.values().collect::<Vec<_>>(), clusters => value::Serde(clusters.values().collect::<Vec<_>>()),
root => opts.repo root => value::Serde(&opts.repo)
}) })
.unwrap(); .unwrap();
@@ -99,92 +90,6 @@ async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
Ok(()) Ok(())
} }
#[debug_handler]
async fn handler(req: Request) -> impl IntoResponse {
println!("{req:?}");
let host = req
.headers()
.iter()
.find(|(name, _)| header::HOST.eq(name))
.map(|(_, value)| value)
.unwrap()
.to_str()
.unwrap();
println!("{host}");
let Some(path) = req.uri().path().strip_prefix("/") else {
return StatusCode::BAD_REQUEST.into_response();
};
let Some((file, content_type)) = get_file(path).await else {
return StatusCode::NOT_FOUND.into_response();
};
let path = PathBuf::from(path);
let filename = path.file_name().unwrap();
let filename = filename.to_str().unwrap();
let content_type = [(header::CONTENT_TYPE, content_type)];
let disposition = [(
header::CONTENT_DISPOSITION,
format!("inline; filename=\"{filename}\""),
)];
(content_type, disposition, file).into_response()
}
// TODO: Make this a proper async function
fn get_file(filename: impl Into<String>) -> impl Future<Output = Option<(Vec<u8>, &'static str)>> {
// TODO: Make this a static that is initialized once
let re =
Regex::new(r"^talos/(?<cluster>.+)/(?<node>.+)\.ya?ml$").expect("regex should be valid");
let filename: String = filename.into();
let re = re.clone();
async move {
const IPXE_PATTERN: &str = "ipxe/";
if filename == format!("{IPXE_PATTERN}boot.ipxe") {
let mut env = Environment::new();
configure_environment(&mut env);
let template = env
.template_from_str(include_str!("./boot.ipxe"))
.expect("Template should be valid");
let clusters = Cluster::get_all().await;
let content = template
.render(context! {
clusters => clusters.values().collect::<Vec<_>>(),
})
.unwrap();
Some((content.into(), "text/plain; charset=utf-8"))
} else if let Some(filename) = filename.strip_prefix(IPXE_PATTERN) {
Some((
IPXE_DIR.get_file(filename)?.contents().to_owned(),
"application/octet-stream",
))
} else if let Some(caps) = re.captures(&filename) {
let cluster = &caps["cluster"];
let node = &caps["node"];
let clusters = Cluster::get_all().await;
let cluster = clusters.get(cluster)?;
let node = cluster.node(node)?;
let config = node.config(cluster).await;
Some((config.into(), "application/yaml; charset=utf-8"))
} else {
None
}
}
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Error> { async fn main() -> Result<(), Error> {
let cli = Cli::parse(); let cli = Cli::parse();
@@ -199,16 +104,20 @@ async fn main() -> Result<(), Error> {
"crete", "crete",
&mut std::io::stdout(), &mut std::io::stdout(),
), ),
Commands::Serve => { Commands::Serve { port, ip } => {
tokio::spawn(async { tokio::spawn(async move {
let app = Router::new().fallback_service(get(handler)); let app = Router::new().fallback_service(get(handler));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); let addr: SocketAddr = (ip, port).into();
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
println!("Http server started on {addr}");
axum::serve(listener, app).await.unwrap(); axum::serve(listener, app).await.unwrap();
}); });
tftp::serve(move |filename| { tftp::serve(ip, move |filename, ip| async move {
let filename = filename.to_owned(); get_file(filename, format!("http://{ip}:{port}"))
async move { get_file(filename).await.map(|(file, _)| file) } .await
.map(|(file, _)| file)
}) })
.await?; .await?;
} }
+2 -2
View File
@@ -102,7 +102,7 @@ pub struct Node {
ntp: String, ntp: String,
install: Install, install: Install,
kernel_args: Vec<String>, kernel_args: Vec<String>,
patches: Patches<serde_yaml::Value>, patches: Patches<serde_json::Value>,
sops: Secret, sops: Secret,
} }
@@ -124,7 +124,7 @@ impl Node {
path.add_extension("yaml"); path.add_extension("yaml");
let content = tokio::fs::read_to_string(path).await.unwrap(); let content = tokio::fs::read_to_string(path).await.unwrap();
let node: OptionalNodeDeserialize = serde_yaml::from_str(&content).unwrap(); let node: OptionalNodeDeserialize = serde_saphyr::from_str(&content).unwrap();
// We want all vectors to be empty vectors by default // We want all vectors to be empty vectors by default
// Sadly we have to this manually // Sadly we have to this manually
+6 -6
View File
@@ -1,4 +1,4 @@
use minijinja::{Environment, context}; use minijinja::{Environment, context, value};
use optional_struct::optional_struct; use optional_struct::optional_struct;
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -21,7 +21,7 @@ async fn render(
env: &Environment<'static>, env: &Environment<'static>,
cluster: &Cluster, cluster: &Cluster,
node: &Node, node: &Node,
) -> Vec<serde_yaml::Value> { ) -> Vec<serde_json::Value> {
let env = env.clone(); let env = env.clone();
let cluster = cluster.clone(); let cluster = cluster.clone();
let node = node.clone(); let node = node.clone();
@@ -35,12 +35,12 @@ async fn render(
.get_template(&name) .get_template(&name)
.unwrap() .unwrap()
.render(context! { .render(context! {
node, node => value::Serde(&node),
cluster cluster => value::Serde(&cluster)
}) })
.unwrap(); .unwrap();
serde_yaml::from_str(&content).unwrap() serde_saphyr::from_str(&content).unwrap()
}) })
.collect() .collect()
}) })
@@ -64,7 +64,7 @@ impl Patches<String> {
env: &Environment<'static>, env: &Environment<'static>,
cluster: &Cluster, cluster: &Cluster,
node: &Node, node: &Node,
) -> Patches<serde_yaml::Value> { ) -> Patches<serde_json::Value> {
Patches { Patches {
all: render(self.all.clone(), env, cluster, node).await, all: render(self.all.clone(), env, cluster, node).await,
control_plane: render(self.control_plane.clone(), env, cluster, node).await, control_plane: render(self.control_plane.clone(), env, cluster, node).await,
+1 -1
View File
@@ -20,7 +20,7 @@ pub async fn get_current_context() -> Option<String> {
tokio::fs::read(&TalosConfig::merged_path()) tokio::fs::read(&TalosConfig::merged_path())
.await .await
.ok() .ok()
.and_then(|config| serde_yaml::from_slice(&config).ok()) .and_then(|config| serde_saphyr::from_slice(&config).ok())
.map(|config: TalosConfig| config.context) .map(|config: TalosConfig| config.context)
} }
+9 -6
View File
@@ -3,7 +3,7 @@ use std::net::SocketAddr;
use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt};
use num_enum::{IntoPrimitive, TryFromPrimitive}; use num_enum::{IntoPrimitive, TryFromPrimitive};
use udp_sas_async::tokio::UdpSocketSas; use tokio::net::UdpSocket;
use crate::tftp::Error; use crate::tftp::Error;
@@ -309,15 +309,16 @@ impl Packet {
Ok(()) Ok(())
} }
pub async fn recv_from(socket: &UdpSocketSas) -> Result<(Self, SocketAddr), Error> { pub async fn recv_from(socket: &UdpSocket) -> Result<(Self, SocketAddr), Error> {
let mut buf = Vec::with_capacity(PACKET_SIZE); let mut buf = [0u8; PACKET_SIZE];
let (_, src) = socket.recv_from(&mut buf).await?; let (_, src) = socket.recv_from(&mut buf).await?;
let packet = buf.try_into()?; let packet = buf.try_into()?;
Ok((packet, src)) Ok((packet, src))
} }
pub async fn send_to(&self, socket: &UdpSocketSas, src: SocketAddr) -> Result<(), Error> { pub async fn send_to(&self, socket: &UdpSocket, src: &SocketAddr) -> Result<(), Error> {
let buf: Vec<_> = self.try_into()?; let buf: Vec<_> = self.try_into()?;
socket.send_to(&buf, src).await?; socket.send_to(&buf, src).await?;
@@ -341,10 +342,12 @@ impl TryFrom<&Packet> for Vec<u8> {
} }
} }
impl TryFrom<Vec<u8>> for Packet { impl TryFrom<[u8; PACKET_SIZE]> for Packet {
type Error = Error; type Error = Error;
fn try_from(buf: Vec<u8>) -> Result<Self, <Packet as TryFrom<Vec<u8>>>::Error> { fn try_from(
buf: [u8; PACKET_SIZE],
) -> Result<Self, <Packet as TryFrom<[u8; PACKET_SIZE]>>::Error> {
let mut reader = BufReader::new(Cursor::new(buf)); let mut reader = BufReader::new(Cursor::new(buf));
Packet::decode(&mut reader) Packet::decode(&mut reader)
+45 -28
View File
@@ -2,10 +2,13 @@ use std::io;
use std::net::{IpAddr, SocketAddr}; use std::net::{IpAddr, SocketAddr};
use std::time::Duration; use std::time::Duration;
use socket_pktinfo::{PktInfo, PktInfoUdpSocket};
use socket2::Domain;
use thiserror::Error; use thiserror::Error;
use udp_sas_async::tokio::UdpSocketSas; use tokio::io::unix::{AsyncFd, AsyncFdRegisterError};
use tokio::net::UdpSocket;
use crate::tftp::packet::{ErrorCode, Mode}; use crate::tftp::packet::{ErrorCode, Mode, PACKET_SIZE};
use crate::tftp::{self, Packet}; use crate::tftp::{self, Packet};
#[derive(Debug, Error)] #[derive(Debug, Error)]
@@ -26,28 +29,30 @@ pub enum Error {
UnexpectedPacket(Packet), UnexpectedPacket(Packet),
#[error("Timeout")] #[error("Timeout")]
Timeout, Timeout,
#[error("AsyncFd: {0}")]
AsyncFd(#[from] AsyncFdRegisterError<PktInfoUdpSocket>),
} }
pub async fn handle_connection<F, Fut>( pub async fn handle_connection<F, Fut>(
info: PktInfo,
packet: Packet, packet: Packet,
src: SocketAddr,
dest: IpAddr,
callback: F, callback: F,
) -> Result<(), Error> ) -> Result<(), Error>
where where
F: Fn(&str) -> Fut, F: Fn(String, IpAddr) -> Fut,
Fut: Future<Output = Option<Vec<u8>>>, Fut: Future<Output = Option<Vec<u8>>>,
{ {
let socket = UdpSocketSas::bind( let src = info.addr_src;
format!("{dest}:0") let socket = UdpSocket::bind((info.addr_dst, 0)).await?;
.parse() println!(
.expect("Address should be valid"), "{}: Connecting back from {}",
)?; src,
println!("{src}: Connecting from {}", socket.local_addr()); socket.local_addr().expect("Local addr should be set")
);
let Packet::Read(request) = packet else { let Packet::Read(request) = packet else {
Packet::error(ErrorCode::NotDefined, "Operation not supported") Packet::error(ErrorCode::NotDefined, "Operation not supported")
.send_to(&socket, src) .send_to(&socket, &info.addr_src)
.await?; .await?;
return Err(Error::UnsupportedOperation(packet)); return Err(Error::UnsupportedOperation(packet));
@@ -56,9 +61,9 @@ where
let filename = request.filename(); let filename = request.filename();
println!("{src}: {filename} [{:?}]", request.mode()); println!("{src}: {filename} [{:?}]", request.mode());
let Some(data) = callback(filename).await else { let Some(data) = callback(filename.to_string(), info.addr_dst).await else {
Packet::error(ErrorCode::FileNotFound, "Unknown file") Packet::error(ErrorCode::FileNotFound, "Unknown file")
.send_to(&socket, src) .send_to(&socket, &src)
.await?; .await?;
return Err(Error::FileNotFound(filename.into())); return Err(Error::FileNotFound(filename.into()));
@@ -74,7 +79,7 @@ where
ErrorCode::NotDefined, ErrorCode::NotDefined,
format!("Mode {mode:?} is not supported"), format!("Mode {mode:?} is not supported"),
) )
.send_to(&socket, src) .send_to(&socket, &src)
.await?; .await?;
return Err(Error::UnsupportedMode(mode)); return Err(Error::UnsupportedMode(mode));
@@ -90,9 +95,10 @@ where
// Send the first chunk // Send the first chunk
if let Some((index, chunk)) = chunk { if let Some((index, chunk)) = chunk {
Packet::data((index + 1) as u16, chunk) Packet::data((index + 1) as u16, chunk)
.send_to(&socket, src) .send_to(&socket, &src)
.await?; .await?;
} }
tokio::select! { tokio::select! {
recv = Packet::recv_from(&socket) => { recv = Packet::recv_from(&socket) => {
timeout = 0; timeout = 0;
@@ -102,7 +108,7 @@ where
// really know how to test it properly so we don't support it. // really know how to test it properly so we don't support it.
if new_src != src { if new_src != src {
Packet::error(ErrorCode::Illegal, "Source changed") Packet::error(ErrorCode::Illegal, "Source changed")
.send_to(&socket, src) .send_to(&socket, &src)
.await?; .await?;
return Err(Error::SourceChanged(new_src)); return Err(Error::SourceChanged(new_src));
@@ -123,7 +129,7 @@ where
} }
_ => { _ => {
Packet::error(ErrorCode::Illegal, "Unexpected packet") Packet::error(ErrorCode::Illegal, "Unexpected packet")
.send_to(&socket, src) .send_to(&socket, &src)
.await?; .await?;
@@ -146,26 +152,37 @@ where
Ok(()) Ok(())
} }
pub async fn serve<F, Fut>(callback: F) -> Result<!, Error> pub async fn serve<F, Fut>(ip: IpAddr, callback: F) -> Result<!, Error>
where where
F: Fn(&str) -> Fut + Clone + Send + 'static, F: Fn(String, IpAddr) -> Fut + Clone + Send + 'static,
Fut: Future<Output = Option<Vec<u8>>> + Send + 'static, Fut: Future<Output = Option<Vec<u8>>> + Send + 'static,
{ {
let socket = UdpSocketSas::bind("0.0.0.0:69".parse().expect("Address should be valid"))?; let socket = PktInfoUdpSocket::new(Domain::IPV4)?;
socket.set_nonblocking(true)?;
println!("Server started"); let addr: SocketAddr = (ip, 69).into();
socket.bind(&addr.into())?;
let async_fd = unsafe { AsyncFd::register(socket)? };
let mut buf = [0u8; PACKET_SIZE];
println!("Tftp server started on {addr}");
loop { loop {
let mut buf = Vec::with_capacity(0); let mut guard = async_fd.readable().await?;
let (bytes, src, dest) = socket.recv_sas(&mut buf).await?;
println!("{src} -> {dest}");
println!("{bytes}, {}", buf.len());
let (packet, src) = Packet::recv_from(&socket).await?; let (packet, info) = match guard.try_io(|inner| inner.get_ref().recv(&mut buf)) {
Ok(Ok((_, info))) => (buf.try_into()?, info),
Ok(Err(e)) => return Err(e)?,
// Socket was not ready
Err(_would_block) => continue,
};
let callback = callback.clone(); let callback = callback.clone();
let src = info.addr_src;
tokio::spawn({ tokio::spawn({
async move { async move {
if let Err(err) = handle_connection(packet, src, dest, callback).await { if let Err(err) = handle_connection(info, packet, callback).await {
println!("{src}: {err}") println!("{src}: {err}")
} }
} }
+2
View File
@@ -0,0 +1,2 @@
[default.extend-words]
rela = "rela"