diff --git a/nix/homelab/config.toml b/nix/homelab/config.toml index 35af01f..c7b979b 100644 --- a/nix/homelab/config.toml +++ b/nix/homelab/config.toml @@ -92,7 +92,7 @@ routes = [ service = "prometheus-stack-grafana", port = 80, private = true - }, + } ] [pihole] @@ -109,4 +109,6 @@ extra_hosts = [ [router] host = "192.168.15.1:22" +user = "luca" +key_path = "/home/luca/.ssh/id_ed25519" lease_file = "/var/dhcpd/var/db/dhcpd.leases" diff --git a/nix/homelab/kustomize/metallb/pool.yaml b/nix/homelab/kustomize/metallb/pool.yaml index 40280b6..7616895 100644 --- a/nix/homelab/kustomize/metallb/pool.yaml +++ b/nix/homelab/kustomize/metallb/pool.yaml @@ -5,7 +5,7 @@ metadata: namespace: metallb-system spec: addresses: - - 192.168.27.12-192.168.27.30 + - 192.168.27.12-192.168.27.40 --- apiVersion: metallb.io/v1beta1 kind: L2Advertisement diff --git a/nix/homelab/src/main.rs b/nix/homelab/src/main.rs index 96afb12..7e08ed8 100644 --- a/nix/homelab/src/main.rs +++ b/nix/homelab/src/main.rs @@ -5,8 +5,8 @@ mod lease_parser; mod pihole; mod transport; -use std::collections::HashSet; -use std::path::Path; +use std::path::{Path, PathBuf}; +use std::{collections::HashSet, env}; use anyhow::Context; use clap::{CommandFactory, Parser}; @@ -58,6 +58,8 @@ pub struct PiHoleConfig { #[derive(Serialize, Deserialize)] pub struct RouterConfig { host: String, + user: String, + key_path: PathBuf, lease_file: String, } @@ -69,6 +71,10 @@ pub fn parse_config>(path: T) -> anyhow::Result { Ok(toml::from_slice::(&bytes)?) } +fn env_or>(key: &str, fallback: S) -> String { + env::var(key).unwrap_or_else(|_| fallback.into()) +} + #[tokio::main(flavor = "current_thread")] async fn main() -> anyhow::Result<()> { let cli = Cli::parse(); @@ -79,7 +85,8 @@ async fn main() -> anyhow::Result<()> { generate_routes(&config)?; } Some(Commands::SyncDNS {}) => { - let config = parse_config("./config.toml")?; + let config_path = env_or("CONFIG_PATH", "./config.toml"); + let config = parse_config(config_path)?; let pihole_config = config .pihole .context("pihole configuration is necessary for syncing dns")?; @@ -96,7 +103,11 @@ async fn main() -> anyhow::Result<()> { .to_string(); let leases = tokio::task::spawn_blocking(move || -> anyhow::Result> { - let r = Router::new(SSHTransport::new(&router_config.host)?); + let r = Router::new(SSHTransport::new( + &router_config.host, + &router_config.user, + &router_config.key_path, + )?); let leases = r .dhcp_leases(&router_config.lease_file)? .into_iter() diff --git a/nix/homelab/src/transport/ssh.rs b/nix/homelab/src/transport/ssh.rs index 5a6f9e4..962fb83 100644 --- a/nix/homelab/src/transport/ssh.rs +++ b/nix/homelab/src/transport/ssh.rs @@ -9,7 +9,7 @@ pub struct SSHTransport { } impl SSHTransport { - pub fn new(host: &str) -> Result { + pub fn new(host: &str, user: &str, key_path: &Path) -> Result { let stream = TcpStream::connect(host)?; let mut s = Self { @@ -17,12 +17,7 @@ impl SSHTransport { }; s.session.set_tcp_stream(stream); s.session.handshake()?; - s.session.userauth_pubkey_file( - "luca", - None, - Path::new("/home/luca/.ssh/id_ed25519"), - None, - )?; + s.session.userauth_pubkey_file(user, None, key_path, None)?; Ok(s) } }