Serve command #27

Merged
Dreaded_X merged 20 commits from feature/tftp_side into main 2026-10-21 05:15:47 +00:00
6 changed files with 107 additions and 43 deletions
Showing only changes of commit 8149ffbc37 - Show all commits
+1 -2
View File
@@ -1,5 +1,4 @@
#!ipxe
dhcp
echo "Hello, World!"
chain boot.ipxe
+19
View File
@@ -0,0 +1,19 @@
#!ipxe
echo Starting ${serial}
:start
goto node_${serial} || exit
# Default behavior (non install mode) is to exit iPXE script
{% for cluster in clusters%}
{% for _, node in cluster.nodes|items %}
{%- if node.install.serial -%}
# {{ cluster.name }}/{{ node.hostname }}
:node_{{ node.install.serial }}
imgfree
kernel https://pxe.factory.talos.dev/image/{{ node.schematic }}/v{{ cluster.version.talos }}/kernel-{{ node.arch }} {{ node.kernelArgs|join(" ") }} {% if node.install.auto %}talos.config={{httpUrl}}/talos/{{cluster.name}}/{{node.hostname}}.yaml{% endif +%}
initrd https://pxe.factory.talos.dev/image/{{ node.schematic }}/v{{ cluster.version.talos }}/initramfs-{{ node.arch }}.xz
boot
{% endif %}
{% endfor %}
{% endfor %}
+8 -4
View File
@@ -25,10 +25,7 @@ fn apply_netmask(
Ok((*ip & *netmask).to_string())
}
impl<'a> PathEnvironment<'a> {
pub fn new(path: &Path) -> Self {
let mut env = Environment::new();
pub fn configure_environment(env: &mut Environment) {
// Configure jinja
env.set_trim_blocks(true);
env.set_lstrip_blocks(true);
@@ -65,6 +62,13 @@ impl<'a> PathEnvironment<'a> {
})
.collect::<Vec<_>>()
});
}
impl<'a> PathEnvironment<'a> {
pub fn new(path: &Path) -> Self {
let mut env = Environment::new();
configure_environment(&mut env);
// Add path loader
env.set_loader(path_loader(&path));
+25 -2
View File
@@ -1,9 +1,14 @@
use crete::cluster::Cluster;
use crete::environment::configure_environment;
use ipxe::IPXE_DIR;
use minijinja::{Environment, context};
use regex::Regex;
// TODO: Make this a proper async function
pub fn get_file(filename: impl ToString) -> impl Future<Output = Option<(Vec<u8>, &'static str)>> {
pub fn get_file(
filename: impl ToString,
http_url: impl ToString,
) -> 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");
@@ -13,7 +18,25 @@ pub fn get_file(filename: impl ToString) -> impl Future<Output = Option<(Vec<u8>
async move {
const IPXE_PATTERN: &str = "ipxe/";
if let Some(filename) = filename.strip_prefix(IPXE_PATTERN) {
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<_>>(),
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",
+16 -1
View File
@@ -2,6 +2,7 @@ use std::path::PathBuf;
use axum::debug_handler;
use axum::extract::Request;
use axum::http::Uri;
use axum::response::IntoResponse;
use reqwest::{StatusCode, header};
@@ -9,11 +10,25 @@ 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).await else {
let Some((file, content_type)) = get_file(path, format!("http://{host}:{port}")).await else {
return StatusCode::NOT_FOUND.into_response();
};
+6 -2
View File
@@ -114,9 +114,13 @@ async fn main() -> Result<(), Error> {
axum::serve(listener, app).await.unwrap();
});
tftp::serve(ip, move |filename, _| {
tftp::serve(ip, move |filename, ip| {
let filename = filename.to_owned();
async move { get_file(filename).await.map(|(file, _)| file) }
async move {
get_file(filename, format!("http://{ip}:{port}"))
.await
.map(|(file, _)| file)
}
})
.await?;
}