feat: bootstrap awg2 harness rust workspace and docs
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
use std::{thread, time::Duration};
|
||||
|
||||
use crate::{config::Config, firewall, reboot, traffic};
|
||||
|
||||
pub fn run(cfg: Config) -> anyhow::Result<()> {
|
||||
println!("starting daemon");
|
||||
|
||||
let _ = traffic::reconcile_traffic(&cfg);
|
||||
let _ = firewall::apply_firewall(&cfg);
|
||||
let _ = reboot::maybe_schedule_reboot(&cfg);
|
||||
|
||||
let events_cfg = cfg.clone();
|
||||
thread::spawn(move || {
|
||||
let _ = docker_events_loop(events_cfg);
|
||||
});
|
||||
|
||||
loop {
|
||||
if let Err(e) = traffic::reconcile_traffic(&cfg) {
|
||||
eprintln!("traffic reconcile error: {e}");
|
||||
}
|
||||
if let Err(e) = firewall::apply_firewall(&cfg) {
|
||||
eprintln!("firewall reconcile error: {e}");
|
||||
}
|
||||
thread::sleep(Duration::from_secs(cfg.traffic.reconcile_interval_secs.max(5)));
|
||||
}
|
||||
}
|
||||
|
||||
fn docker_events_loop(cfg: Config) -> anyhow::Result<()> {
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
let mut child = Command::new("docker")
|
||||
.args(["events", "--filter", "event=start", "--format", "{{.Actor.Attributes.name}}"])
|
||||
.stdout(Stdio::piped())
|
||||
.spawn()?;
|
||||
|
||||
let stdout = child
|
||||
.stdout
|
||||
.take()
|
||||
.ok_or_else(|| anyhow::anyhow!("failed to capture docker events stdout"))?;
|
||||
let reader = BufReader::new(stdout);
|
||||
|
||||
for line in reader.lines() {
|
||||
let name = line?.trim().to_string();
|
||||
if cfg.amnezia.container_names.iter().any(|n| n == &name) {
|
||||
let _ = traffic::reconcile_traffic(&cfg);
|
||||
let _ = firewall::apply_firewall(&cfg);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user