feat(homelab): start containerizing homelab binary
This commit is contained in:
@@ -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<T: AsRef<Path>>(path: T) -> anyhow::Result<Config> {
|
||||
Ok(toml::from_slice::<Config>(&bytes)?)
|
||||
}
|
||||
|
||||
fn env_or<S: Into<String>>(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<Vec<Lease>> {
|
||||
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()
|
||||
|
||||
@@ -9,7 +9,7 @@ pub struct SSHTransport {
|
||||
}
|
||||
|
||||
impl SSHTransport {
|
||||
pub fn new(host: &str) -> Result<Self> {
|
||||
pub fn new(host: &str, user: &str, key_path: &Path) -> Result<Self> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user