use hotplug hooks, cant wait to use ts.

This commit is contained in:
lda
2026-04-26 22:17:28 +07:00 Verified
parent dc35e6e5d1
commit 35bed390f7
9 changed files with 258 additions and 1 deletions
+38
View File
@@ -28,6 +28,8 @@ pub enum Command {
InitConfig(InitConfigArgs),
/// Reload a running agent daemon by sending SIGHUP.
Reload(ReloadArgs),
/// Pass local hotplug observations through to the wakey CLI.
Observe(ObserveArgs),
}
#[derive(Args)]
@@ -101,3 +103,39 @@ pub struct ReloadArgs {
#[arg(long, default_value = DEFAULT_PID_FILE)]
pub pid_file: PathBuf,
}
#[derive(Args)]
pub struct ObserveArgs {
#[command(subcommand)]
pub command: ObserveCommand,
}
#[derive(Subcommand)]
pub enum ObserveCommand {
/// Observe a dnsmasq DHCP lease event.
Dhcp(ObserveDhcpArgs),
/// Observe a neighbor-table event.
Neigh(ObserveNeighArgs),
}
#[derive(Args)]
pub struct ObserveDhcpArgs {
#[arg(long)]
pub action: String,
#[arg(long)]
pub mac: String,
#[arg(long)]
pub ip: Option<String>,
#[arg(long)]
pub hostname: Option<String>,
}
#[derive(Args)]
pub struct ObserveNeighArgs {
#[arg(long)]
pub action: String,
#[arg(long)]
pub mac: Option<String>,
#[arg(long)]
pub ip: Option<String>,
}
+56 -1
View File
@@ -9,7 +9,7 @@ mod tracing;
use anyhow::Result;
use clap::Parser;
use cli::{Cli, Command, InitConfigArgs};
use cli::{Cli, Command, InitConfigArgs, ObserveCommand};
#[tokio::main]
async fn main() -> Result<()> {
@@ -86,11 +86,66 @@ async fn main() -> Result<()> {
::tracing::info!(pid_file = %args.pid_file.display(), "wakey-agent command: reload");
serve::reload_daemon(&args.pid_file)?
}
Command::Observe(args) => observe(args)?,
}
Ok(())
}
fn observe(args: cli::ObserveArgs) -> Result<()> {
let mut cmd = std::process::Command::new(resolve_wakey_binary());
cmd.arg("observe");
match args.command {
ObserveCommand::Dhcp(args) => {
cmd.arg("dhcp")
.arg("--action")
.arg(args.action)
.arg("--mac")
.arg(args.mac);
if let Some(ip) = args.ip {
cmd.arg("--ip").arg(ip);
}
if let Some(hostname) = args.hostname {
cmd.arg("--hostname").arg(hostname);
}
}
ObserveCommand::Neigh(args) => {
cmd.arg("neigh").arg("--action").arg(args.action);
if let Some(mac) = args.mac {
cmd.arg("--mac").arg(mac);
}
if let Some(ip) = args.ip {
cmd.arg("--ip").arg(ip);
}
}
}
let status = cmd.status()?;
if !status.success() {
anyhow::bail!("wakey observe exited with {status}");
}
Ok(())
}
fn resolve_wakey_binary() -> std::path::PathBuf {
if let Ok(current) = std::env::current_exe()
&& let Some(dir) = current.parent()
{
let sibling = dir.join("wakey");
if sibling.exists() {
return sibling;
}
}
let root_bin = std::path::PathBuf::from("/root/.bin/wakey");
if root_bin.exists() {
return root_bin;
}
"wakey".into()
}
fn init_config(args: InitConfigArgs) -> Result<()> {
if args.stdout && args.config.is_some() {
anyhow::bail!("--stdout cannot be used with --config");