Compare commits
10
Commits
main
..
71706588d3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71706588d3
|
||
|
|
721ce459de
|
||
|
|
bd36d58911
|
||
|
|
44402acfca
|
||
|
|
a531f48a5d
|
||
|
|
ca53e50053
|
||
|
|
1f268116de
|
||
|
|
f03c06fdf2
|
||
|
|
27f6557197
|
||
|
|
344f345854
|
@@ -14,6 +14,6 @@ jobs:
|
||||
- uses: actions/checkout@v6
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- uses: crate-ci/committed@v1.1.11
|
||||
- uses: crate-ci/committed@master
|
||||
with:
|
||||
commits: "origin/${{ gitea.base_ref }}..HEAD"
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
[submodule "ipxe/ipxe"]
|
||||
path = ipxe/ipxe
|
||||
url = git@github.com:rkubes/ipxe
|
||||
|
||||
@@ -76,6 +76,6 @@ repos:
|
||||
glob: .gitea/workflows/*.yaml
|
||||
|
||||
- repo: https://github.com/crate-ci/committed
|
||||
rev: v1.1.11
|
||||
rev: v1.1.8
|
||||
hooks:
|
||||
- id: committed
|
||||
|
||||
@@ -1,14 +1,5 @@
|
||||
[workspace]
|
||||
pr_name = "chore(release): Release v{{ version }}"
|
||||
changelog_path = "./CHANGELOG.md"
|
||||
|
||||
[[package]]
|
||||
name = "crete"
|
||||
changelog_include = ["ipxe"]
|
||||
|
||||
[[package]]
|
||||
name = "ipxe"
|
||||
release = false
|
||||
|
||||
[[package]]
|
||||
name = "xtask"
|
||||
|
||||
Generated
+252
-1828
File diff suppressed because it is too large
Load Diff
+12
-16
@@ -10,36 +10,32 @@ license = "GPL-3.0-or-later"
|
||||
members = ["ipxe", "xtask"]
|
||||
|
||||
[workspace.dependencies]
|
||||
schemars = { version = "1.2.3", features = ["semver1"] }
|
||||
serde_json = "1.0.154"
|
||||
schemars = { version = "1.2.1", features = ["semver1"] }
|
||||
serde_json = "1.0.149"
|
||||
|
||||
[dependencies]
|
||||
async-walkdir = "2.2.0"
|
||||
async-walkdir = "2.1.0"
|
||||
byteorder = "1.5.0"
|
||||
clap = { version = "4.6.7", features = ["derive"] }
|
||||
clap_complete = "4.6.11"
|
||||
clap = { version = "4.6.0", features = ["derive"] }
|
||||
clap_complete = "4.6.0"
|
||||
futures = "0.3.34"
|
||||
gix-discover = { version = "0.57.0", features = ["sha1"] }
|
||||
minijinja = { version = "3.0.1", features = ["json", "serde"] }
|
||||
gix-discover = { version = "0.55.0", features = ["sha1"] }
|
||||
minijinja = { version = "2.18.0", features = ["json", "loader"] }
|
||||
netascii = "0.1.0"
|
||||
num_enum = "0.7.6"
|
||||
optional_struct = "0.5.2"
|
||||
regex = "1.13.1"
|
||||
reqwest = { version = "0.13.5", default-features = false, features = [
|
||||
reqwest = { version = "0.13.2", default-features = false, features = [
|
||||
"native-tls",
|
||||
] }
|
||||
schemars = { workspace = true }
|
||||
semver = { version = "1.0.28", features = ["serde"] }
|
||||
serde = { version = "1.0.229", features = ["derive"] }
|
||||
semver = { version = "1.0.27", features = ["serde"] }
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = { workspace = true }
|
||||
serde_yaml = "0.9.34"
|
||||
thiserror = "2.0.21"
|
||||
tokio = { version = "1.54.0", features = ["full"] }
|
||||
thiserror = "2.0.18"
|
||||
tokio = { version = "1.52.1", features = ["full"] }
|
||||
ipxe = { path = "./ipxe" }
|
||||
axum = { version = "0.8.9", features = ["macros"] }
|
||||
socket2 = "0.6.5"
|
||||
socket-pktinfo = "0.4.1"
|
||||
serde-saphyr = "2.0.0"
|
||||
|
||||
[lib]
|
||||
test = false
|
||||
|
||||
+1
-7
@@ -4,13 +4,7 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[build-dependencies]
|
||||
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",
|
||||
] }
|
||||
build-print = "1.0.1"
|
||||
|
||||
[dependencies]
|
||||
include_dir = "0.7.4"
|
||||
|
||||
+28
-151
@@ -1,180 +1,57 @@
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
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";
|
||||
use build_print::error;
|
||||
|
||||
fn main() {
|
||||
let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
|
||||
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
|
||||
.canonicalize()
|
||||
.unwrap();
|
||||
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(¤t, 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 src_dir = manifest_dir.join("ipxe").join("src");
|
||||
|
||||
let mut command = Command::new("make");
|
||||
command
|
||||
.stdout(Stdio::piped())
|
||||
.current_dir(&src_dir)
|
||||
.env_remove("DEBUG")
|
||||
.env_remove("PROFILE")
|
||||
.args([
|
||||
"-j",
|
||||
&std::thread::available_parallelism()
|
||||
.unwrap()
|
||||
.get()
|
||||
.to_string(),
|
||||
])
|
||||
.arg(format!(
|
||||
"EMBED={}",
|
||||
manifest_dir
|
||||
.join("src")
|
||||
.join("embed.ipxe")
|
||||
.to_string_lossy()
|
||||
))
|
||||
.args(FILES);
|
||||
.to_string_lossy(),
|
||||
));
|
||||
|
||||
if let Ok(flags) = std::env::var("CARGO_MAKEFLAGS") {
|
||||
command.env("MAKEFLAGS", flags);
|
||||
let files = ["bin/ipxe.pxe", "bin-x86_64-efi/ipxe.efi"].map(PathBuf::from);
|
||||
for file in &files {
|
||||
command.arg(file);
|
||||
}
|
||||
|
||||
let output = command.output().unwrap();
|
||||
|
||||
if !output.status.success() {
|
||||
panic!("make failed:\n{}", String::from_utf8_lossy(&output.stderr));
|
||||
}
|
||||
error!("{}", String::from_utf8_lossy(&output.stderr));
|
||||
}
|
||||
|
||||
fn copy_files(repo_dir: &Path, out_dir: &Path) {
|
||||
let src_dir = repo_dir.join(SRC_DIR);
|
||||
let bin_dir = out_dir.join("bin");
|
||||
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap())
|
||||
.canonicalize()
|
||||
.unwrap()
|
||||
.join("ipxe");
|
||||
|
||||
std::fs::create_dir_all(&bin_dir).unwrap();
|
||||
for file in FILES {
|
||||
let file = Path::new(file);
|
||||
std::fs::remove_dir_all(&out_dir).ok();
|
||||
for file in &files {
|
||||
let input = src_dir.join(file);
|
||||
let output = bin_dir.join(file.file_name().unwrap());
|
||||
let output = out_dir.join(file.file_name().unwrap());
|
||||
|
||||
std::fs::create_dir_all(output.parent().unwrap()).unwrap();
|
||||
std::fs::copy(input, output).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Submodule
+1
Submodule ipxe/ipxe added at 8892af68a2
@@ -1,19 +0,0 @@
|
||||
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
|
||||
|
||||
/*****************************************************************************
|
||||
*
|
||||
+2
-1
@@ -1,4 +1,5 @@
|
||||
#!ipxe
|
||||
|
||||
dhcp
|
||||
chain boot.ipxe
|
||||
|
||||
echo "Hello, World!"
|
||||
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
use include_dir::{Dir, include_dir};
|
||||
|
||||
pub static IPXE_DIR: Dir<'_> = include_dir!("$OUT_DIR/bin");
|
||||
pub static IPXE_DIR: Dir<'_> = include_dir!("$OUT_DIR/ipxe");
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
[toolchain]
|
||||
channel = "nightly-2026-10-01"
|
||||
channel = "nightly-2026-03-12"
|
||||
profile = "default"
|
||||
components = ["rust-analyzer"]
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
#!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 %}
|
||||
+1
-7
@@ -1,4 +1,3 @@
|
||||
use std::net::IpAddr;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::{Args, Parser, Subcommand};
|
||||
@@ -51,10 +50,5 @@ pub enum Commands {
|
||||
/// Generate completions for your current shell
|
||||
ShellCompletions,
|
||||
/// Serve config files over tftp
|
||||
Serve {
|
||||
#[arg(short, long, default_value = "3000")]
|
||||
port: u16,
|
||||
#[arg(long, default_value = "0.0.0.0")]
|
||||
ip: IpAddr,
|
||||
},
|
||||
Serve,
|
||||
}
|
||||
|
||||
+1
-1
@@ -102,7 +102,7 @@ impl Cluster {
|
||||
path.add_extension("yaml");
|
||||
let content = tokio::fs::read_to_string(path).await.unwrap();
|
||||
|
||||
let mut cluster: OptionalClusterDeserialize = serde_saphyr::from_str(&content).unwrap();
|
||||
let mut cluster: OptionalClusterDeserialize = serde_yaml::from_str(&content).unwrap();
|
||||
|
||||
// For some reason apply on the cluster does not properly apply to the default settings...
|
||||
// So we manually apply it here first
|
||||
|
||||
+11
-21
@@ -4,8 +4,8 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use async_walkdir::WalkDir;
|
||||
use futures::{Stream, StreamExt};
|
||||
use minijinja::syntax::SyntaxConfig;
|
||||
use minijinja::{AutoEscape, Environment, path_loader, value};
|
||||
use minijinja::value::ViaDeserialize;
|
||||
use minijinja::{AutoEscape, Environment, path_loader};
|
||||
|
||||
use crate::{get_configs_path, get_talos_path};
|
||||
|
||||
@@ -19,27 +19,24 @@ pub struct PathEnvironment<'a> {
|
||||
}
|
||||
|
||||
fn apply_netmask(
|
||||
ip: value::Serde<Ipv4Addr>,
|
||||
netmask: value::Serde<Ipv4Addr>,
|
||||
ip: ViaDeserialize<Ipv4Addr>,
|
||||
netmask: ViaDeserialize<Ipv4Addr>,
|
||||
) -> Result<String, minijinja::Error> {
|
||||
Ok((*ip & *netmask).to_string())
|
||||
}
|
||||
|
||||
pub fn configure_environment(env: &mut Environment) {
|
||||
impl<'a> PathEnvironment<'a> {
|
||||
pub fn new(path: &Path) -> Self {
|
||||
let mut env = Environment::new();
|
||||
|
||||
// Configure jinja
|
||||
// Configure jinja
|
||||
env.set_syntax(
|
||||
SyntaxConfig::builder()
|
||||
.trim_blocks(true)
|
||||
.lstrip_blocks(true)
|
||||
.build()
|
||||
.expect("Syntax config should be valid"),
|
||||
);
|
||||
env.set_trim_blocks(true);
|
||||
env.set_lstrip_blocks(true);
|
||||
env.set_undefined_behavior(minijinja::UndefinedBehavior::Strict);
|
||||
env.set_auto_escape_callback(|_| AutoEscape::None);
|
||||
|
||||
// Add filters
|
||||
env.add_filter("to_prefix", |netmask: value::Serde<Ipv4Addr>| {
|
||||
env.add_filter("to_prefix", |netmask: ViaDeserialize<Ipv4Addr>| {
|
||||
let mask = netmask.to_bits();
|
||||
let prefix = mask.leading_ones();
|
||||
|
||||
@@ -68,13 +65,6 @@ pub fn configure_environment(env: &mut Environment) {
|
||||
})
|
||||
.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));
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
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
@@ -1,55 +0,0 @@
|
||||
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
@@ -1,46 +0,0 @@
|
||||
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()
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
#![feature(path_absolute_method)]
|
||||
#![feature(path_absolute_method, never_type)]
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::OnceLock;
|
||||
|
||||
|
||||
+45
-25
@@ -1,27 +1,30 @@
|
||||
mod cli;
|
||||
mod error;
|
||||
mod file;
|
||||
mod http;
|
||||
|
||||
use std::net::SocketAddr;
|
||||
use std::pin::pin;
|
||||
|
||||
use axum::Router;
|
||||
use axum::routing::get;
|
||||
use clap::{CommandFactory, Parser};
|
||||
use clap_complete::{Shell, generate as generate_complete};
|
||||
use crete::cluster::Cluster;
|
||||
use crete::environment::PathEnvironment;
|
||||
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 futures::StreamExt;
|
||||
use futures::future::join_all;
|
||||
use minijinja::{context, value};
|
||||
use ipxe::IPXE_DIR;
|
||||
use minijinja::context;
|
||||
use regex::Regex;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::cli::{Cli, Commands, GlobalOpts};
|
||||
use crate::error::Error;
|
||||
use crate::file::get_file;
|
||||
use crate::http::handler;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
enum Error {
|
||||
#[error("No clusters where found")]
|
||||
NoClustersFound,
|
||||
#[error("Server error: {0}")]
|
||||
ServerError(#[from] ServerError),
|
||||
}
|
||||
|
||||
async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
|
||||
let clusters = Cluster::get_all().await;
|
||||
@@ -44,8 +47,8 @@ async fn generate(opts: &GlobalOpts) -> Result<(), Error> {
|
||||
|
||||
let content = template
|
||||
.render(context! {
|
||||
clusters => value::Serde(clusters.values().collect::<Vec<_>>()),
|
||||
root => value::Serde(&opts.repo)
|
||||
clusters => clusters.values().collect::<Vec<_>>(),
|
||||
root => opts.repo
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
@@ -104,22 +107,39 @@ async fn main() -> Result<(), Error> {
|
||||
"crete",
|
||||
&mut std::io::stdout(),
|
||||
),
|
||||
Commands::Serve { port, ip } => {
|
||||
tokio::spawn(async move {
|
||||
let app = Router::new().fallback_service(get(handler));
|
||||
let addr: SocketAddr = (ip, port).into();
|
||||
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
||||
Commands::Serve => {
|
||||
let re = Regex::new(r"talos/(?<cluster>.+)/(?<node>.+)\.ya?ml")
|
||||
.expect("regex should be valid");
|
||||
|
||||
println!("Http server started on {addr}");
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
});
|
||||
tftp::serve(move |filename| {
|
||||
let filename = filename.to_owned();
|
||||
let re = re.clone();
|
||||
|
||||
tftp::serve(ip, move |filename, ip| async move {
|
||||
get_file(filename, format!("http://{ip}:{port}"))
|
||||
.await
|
||||
.map(|(file, _)| file)
|
||||
println!("{filename}");
|
||||
|
||||
async move {
|
||||
const IPXE_PATTERN: &str = "ipxe/";
|
||||
if let Some(filename) = filename.strip_prefix(IPXE_PATTERN) {
|
||||
println!("Looking for iPXE binary: {filename}");
|
||||
Some(IPXE_DIR.get_file(filename)?.contents().to_owned())
|
||||
} 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())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
})
|
||||
.await?;
|
||||
.await?
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
+2
-2
@@ -102,7 +102,7 @@ pub struct Node {
|
||||
ntp: String,
|
||||
install: Install,
|
||||
kernel_args: Vec<String>,
|
||||
patches: Patches<serde_json::Value>,
|
||||
patches: Patches<serde_yaml::Value>,
|
||||
sops: Secret,
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ impl Node {
|
||||
path.add_extension("yaml");
|
||||
let content = tokio::fs::read_to_string(path).await.unwrap();
|
||||
|
||||
let node: OptionalNodeDeserialize = serde_saphyr::from_str(&content).unwrap();
|
||||
let node: OptionalNodeDeserialize = serde_yaml::from_str(&content).unwrap();
|
||||
|
||||
// We want all vectors to be empty vectors by default
|
||||
// Sadly we have to this manually
|
||||
|
||||
+6
-6
@@ -1,4 +1,4 @@
|
||||
use minijinja::{Environment, context, value};
|
||||
use minijinja::{Environment, context};
|
||||
use optional_struct::optional_struct;
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -21,7 +21,7 @@ async fn render(
|
||||
env: &Environment<'static>,
|
||||
cluster: &Cluster,
|
||||
node: &Node,
|
||||
) -> Vec<serde_json::Value> {
|
||||
) -> Vec<serde_yaml::Value> {
|
||||
let env = env.clone();
|
||||
let cluster = cluster.clone();
|
||||
let node = node.clone();
|
||||
@@ -35,12 +35,12 @@ async fn render(
|
||||
.get_template(&name)
|
||||
.unwrap()
|
||||
.render(context! {
|
||||
node => value::Serde(&node),
|
||||
cluster => value::Serde(&cluster)
|
||||
node,
|
||||
cluster
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
serde_saphyr::from_str(&content).unwrap()
|
||||
serde_yaml::from_str(&content).unwrap()
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
@@ -64,7 +64,7 @@ impl Patches<String> {
|
||||
env: &Environment<'static>,
|
||||
cluster: &Cluster,
|
||||
node: &Node,
|
||||
) -> Patches<serde_json::Value> {
|
||||
) -> Patches<serde_yaml::Value> {
|
||||
Patches {
|
||||
all: render(self.all.clone(), env, cluster, node).await,
|
||||
control_plane: render(self.control_plane.clone(), env, cluster, node).await,
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ pub async fn get_current_context() -> Option<String> {
|
||||
tokio::fs::read(&TalosConfig::merged_path())
|
||||
.await
|
||||
.ok()
|
||||
.and_then(|config| serde_saphyr::from_slice(&config).ok())
|
||||
.and_then(|config| serde_yaml::from_slice(&config).ok())
|
||||
.map(|config: TalosConfig| config.context)
|
||||
}
|
||||
|
||||
|
||||
+4
-6
@@ -310,8 +310,8 @@ impl Packet {
|
||||
}
|
||||
|
||||
pub async fn recv_from(socket: &UdpSocket) -> Result<(Self, SocketAddr), Error> {
|
||||
let mut buf = [0u8; PACKET_SIZE];
|
||||
let (_, src) = socket.recv_from(&mut buf).await?;
|
||||
let mut buf = Vec::with_capacity(PACKET_SIZE);
|
||||
let (_, src) = socket.recv_buf_from(&mut buf).await?;
|
||||
|
||||
let packet = buf.try_into()?;
|
||||
|
||||
@@ -342,12 +342,10 @@ impl TryFrom<&Packet> for Vec<u8> {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<[u8; PACKET_SIZE]> for Packet {
|
||||
impl TryFrom<Vec<u8>> for Packet {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(
|
||||
buf: [u8; PACKET_SIZE],
|
||||
) -> Result<Self, <Packet as TryFrom<[u8; PACKET_SIZE]>>::Error> {
|
||||
fn try_from(buf: Vec<u8>) -> Result<Self, <Packet as TryFrom<Vec<u8>>>::Error> {
|
||||
let mut reader = BufReader::new(Cursor::new(buf));
|
||||
|
||||
Packet::decode(&mut reader)
|
||||
|
||||
+16
-39
@@ -1,14 +1,11 @@
|
||||
use std::io;
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
use std::net::SocketAddr;
|
||||
use std::time::Duration;
|
||||
|
||||
use socket_pktinfo::{PktInfo, PktInfoUdpSocket};
|
||||
use socket2::Domain;
|
||||
use thiserror::Error;
|
||||
use tokio::io::unix::{AsyncFd, AsyncFdRegisterError};
|
||||
use tokio::net::UdpSocket;
|
||||
|
||||
use crate::tftp::packet::{ErrorCode, Mode, PACKET_SIZE};
|
||||
use crate::tftp::packet::{ErrorCode, Mode};
|
||||
use crate::tftp::{self, Packet};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
@@ -29,30 +26,26 @@ pub enum Error {
|
||||
UnexpectedPacket(Packet),
|
||||
#[error("Timeout")]
|
||||
Timeout,
|
||||
#[error("AsyncFd: {0}")]
|
||||
AsyncFd(#[from] AsyncFdRegisterError<PktInfoUdpSocket>),
|
||||
}
|
||||
|
||||
pub async fn handle_connection<F, Fut>(
|
||||
info: PktInfo,
|
||||
src: SocketAddr,
|
||||
packet: Packet,
|
||||
callback: F,
|
||||
) -> Result<(), Error>
|
||||
where
|
||||
F: Fn(String, IpAddr) -> Fut,
|
||||
F: Fn(&str) -> Fut,
|
||||
Fut: Future<Output = Option<Vec<u8>>>,
|
||||
{
|
||||
let src = info.addr_src;
|
||||
let socket = UdpSocket::bind((info.addr_dst, 0)).await?;
|
||||
let socket = UdpSocket::bind("127.0.0.1:0").await?;
|
||||
println!(
|
||||
"{}: Connecting back from {}",
|
||||
src,
|
||||
socket.local_addr().expect("Local addr should be set")
|
||||
"{src}: Connecting from {}",
|
||||
socket.local_addr().expect("Socket is bound")
|
||||
);
|
||||
|
||||
let Packet::Read(request) = packet else {
|
||||
Packet::error(ErrorCode::NotDefined, "Operation not supported")
|
||||
.send_to(&socket, &info.addr_src)
|
||||
.send_to(&socket, &src)
|
||||
.await?;
|
||||
|
||||
return Err(Error::UnsupportedOperation(packet));
|
||||
@@ -61,7 +54,7 @@ where
|
||||
let filename = request.filename();
|
||||
println!("{src}: {filename} [{:?}]", request.mode());
|
||||
|
||||
let Some(data) = callback(filename.to_string(), info.addr_dst).await else {
|
||||
let Some(data) = callback(filename).await else {
|
||||
Packet::error(ErrorCode::FileNotFound, "Unknown file")
|
||||
.send_to(&socket, &src)
|
||||
.await?;
|
||||
@@ -92,7 +85,7 @@ where
|
||||
let mut timeout = 0;
|
||||
|
||||
loop {
|
||||
// Send the first chunk
|
||||
// Send the first byte
|
||||
if let Some((index, chunk)) = chunk {
|
||||
Packet::data((index + 1) as u16, chunk)
|
||||
.send_to(&socket, &src)
|
||||
@@ -152,37 +145,21 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn serve<F, Fut>(ip: IpAddr, callback: F) -> Result<!, Error>
|
||||
pub async fn serve<F, Fut>(callback: F) -> Result<!, Error>
|
||||
where
|
||||
F: Fn(String, IpAddr) -> Fut + Clone + Send + 'static,
|
||||
F: Fn(&str) -> Fut + Clone + Send + 'static,
|
||||
Fut: Future<Output = Option<Vec<u8>>> + Send + 'static,
|
||||
{
|
||||
let socket = PktInfoUdpSocket::new(Domain::IPV4)?;
|
||||
socket.set_nonblocking(true)?;
|
||||
let socket = UdpSocket::bind("127.0.0.1:69").await?;
|
||||
|
||||
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}");
|
||||
println!("Server started");
|
||||
loop {
|
||||
let mut guard = async_fd.readable().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 (packet, src) = Packet::recv_from(&socket).await?;
|
||||
|
||||
let callback = callback.clone();
|
||||
let src = info.addr_src;
|
||||
tokio::spawn({
|
||||
async move {
|
||||
if let Err(err) = handle_connection(info, packet, callback).await {
|
||||
if let Err(err) = handle_connection(src, packet, callback).await {
|
||||
println!("{src}: {err}")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
[default.extend-words]
|
||||
rela = "rela"
|
||||
+1
-1
@@ -4,7 +4,7 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
repo_path = "1.4.1"
|
||||
repo_path = "1.2.4"
|
||||
schemars = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
crete = { path = ".." }
|
||||
|
||||
Reference in New Issue
Block a user