Compare commits

..

2 commits

Author SHA1 Message Date
Armaël Guéneau
53c83b8a3b Add support for "external" port reservation and add ones for forgejo 2024-10-25 22:20:09 +02:00
Armaël Guéneau
734d97e191 add Display impl for printing PortRegistration 2024-10-25 22:08:41 +02:00

View file

@ -1,5 +1,5 @@
use serde::Deserialize;
use std::{env,fs::File};
use std::{env,fmt,fs::File};
use std::io::BufReader;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
@ -9,9 +9,6 @@ use anyhow::{anyhow, Result};
mod parsing;
use parsing::*;
// TODO: add support for (builtin?) "external" port reservations
// -> bespin, 80+443 pour forgejo
// parses a diplonat port parameter: ({tcp,udp}_port XX YY...)
// matches `DiplonatParameter` in diplonat (`src/consul_actor.rs`)
#[derive(Deserialize, Debug)]
@ -40,12 +37,31 @@ struct Ctx {
enum Port { Udp(u16), Tcp(u16) }
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct PortRegistration {
file: PathBuf,
job: String,
group: String,
task: String,
service: Option<String>,
enum PortRegistration {
Config {
file: PathBuf,
job: String,
group: String,
task: String,
service: Option<String>
},
Builtin(String),
}
impl fmt::Display for PortRegistration {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
PortRegistration::Config { file, job, group, task, service } =>
write!(f, "in {}, job \"{}\", group \"{}\", task \"{}\", service \"{}\"",
file.display(), job, group, task,
match &service {
None => "<noname>",
Some(s) => s
}),
PortRegistration::Builtin(reason) =>
write!(f, "{}", reason),
}
}
}
impl Ctx {
@ -72,7 +88,7 @@ impl Ctx {
self.add_diplonat_tag(
conf,
&job.datacenters,
PortRegistration {
PortRegistration::Config {
file: PathBuf::from(file),
job: jobname.clone(),
group: groupname.clone(),
@ -128,6 +144,13 @@ fn main() -> Result<ExitCode> {
}
let mut ctx = Ctx::new();
// port registrations managed externally and not described in .hcl files
ctx.ports.insert((String::from("bespin"), Port::Tcp(80)),
vec![PortRegistration::Builtin(String::from("forjego"))]);
ctx.ports.insert((String::from("bespin"), Port::Tcp(443)),
vec![PortRegistration::Builtin(String::from("forjego"))]);
for file in &args[1..] {
ctx.add_file(Path::new(file))?
}
@ -138,12 +161,7 @@ fn main() -> Result<ExitCode> {
have_conflicts = true;
println!("Conflict in site {}, port {:?}:", &datacenter, port);
for reg in &regs {
println!("- in {}, job \"{}\", group \"{}\", task \"{}\", service \"{}\"",
reg.file.display(), reg.job, reg.group, reg.task,
match &reg.service {
None => "<noname>",
Some(s) => s
})
println!("- {}", reg)
}
println!()
}