liminix/examples/acquire-wan-address.fnl
Daniel Barlow 0a737c62cd convert acquire-wan-address to writeFennel
this means we can get rid of the inelegant environent variable
check at the bottom of the file
2023-09-12 17:51:00 +01:00

44 lines
1.3 KiB
Fennel

(local { : system } (require :anoia))
(local svc (require :anoia.svc))
(local bound-states
{ :bound true
:rebound true
:informed true
:updated true
:ra-updated true
})
(fn changes [old-addresses new-addresses]
(let [added {}
deleted {}]
(each [n address (pairs new-addresses)]
(if (not (. old-addresses n))
(table.insert added address)))
(each [n address (pairs old-addresses)]
(if (not (. new-addresses n))
(table.insert deleted address)))
(values added deleted)))
(fn update-addresses [wan-device addresses new-addresses]
(let [(added deleted) (changes addresses new-addresses)]
(each [_ p (ipairs added)]
(system
(.. "ip address add " p.address "/" p.len " dev " wan-device)))
(each [_ p (ipairs deleted)]
(system
(.. "ip address del " p.address "/" p.len " dev " wan-device)))
new-addresses))
(fn run []
(let [[state-directory wan-device] arg
dir (svc.open state-directory)]
(var addresses [])
(while true
(while (not (dir:ready?)) (dir:wait))
(when (. bound-states (dir:output "state"))
(set addresses
(update-addresses wan-device addresses (dir:output "address"))))
(dir:wait))))
{ : update-addresses : changes : run }