update acquire-delegated-prefix to use svc.events
This commit is contained in:
parent
fa040a194c
commit
92e107d77c
3 changed files with 34 additions and 58 deletions
|
@ -1,59 +1,31 @@
|
||||||
(local { : merge : split : file-exists? : system } (require :anoia))
|
(local { : system } (require :anoia))
|
||||||
(local svc (require :anoia.svc))
|
(local svc (require :anoia.svc))
|
||||||
|
|
||||||
(fn parse-prefix [str]
|
(fn changes [old-addresses new-addresses]
|
||||||
(fn parse-extra [s]
|
|
||||||
(let [out {}]
|
|
||||||
(each [name val (string.gmatch s ",(.-)=([^,]+)")]
|
|
||||||
(tset out name val))
|
|
||||||
out))
|
|
||||||
(let [(prefix len preferred valid extra)
|
|
||||||
(string.match str "(.-)::/(%d+),(%d+),(%d+)(.*)$")]
|
|
||||||
(merge {: prefix : len : preferred : valid} (parse-extra extra))))
|
|
||||||
|
|
||||||
|
|
||||||
;; Format: <prefix>/<length>,preferred,valid[,excluded=<excluded-prefix>/<length>][,class=<prefix class #>]
|
|
||||||
|
|
||||||
;;(parse-prefix "2001:8b0:de3a:40dc::/64,7198,7198")
|
|
||||||
;;(parse-prefix "2001:8b0:de3a:1001::/64,7198,7188,excluded=1/2,thi=10")
|
|
||||||
|
|
||||||
|
|
||||||
(local bound-states
|
|
||||||
{ :bound true
|
|
||||||
:rebound true
|
|
||||||
:informed true
|
|
||||||
:updated true
|
|
||||||
:ra-updated true
|
|
||||||
})
|
|
||||||
|
|
||||||
; (local { : view } (require :fennel))
|
|
||||||
|
|
||||||
(fn changes [old-prefixes new-prefixes]
|
|
||||||
(let [added {}
|
(let [added {}
|
||||||
deleted {}
|
deleted {}]
|
||||||
old-set (collect [_ v (ipairs old-prefixes)] (values v true))
|
(each [n address (pairs new-addresses)]
|
||||||
new-set (collect [_ v (ipairs new-prefixes)] (values v true))]
|
(if (not (. old-addresses n))
|
||||||
(each [_ prefix (ipairs new-prefixes)]
|
(table.insert added address)))
|
||||||
(if (not (. old-set prefix))
|
(each [n address (pairs old-addresses)]
|
||||||
(table.insert added (parse-prefix prefix))))
|
(if (not (. new-addresses n))
|
||||||
(each [_ prefix (ipairs old-prefixes)]
|
(table.insert deleted address)))
|
||||||
(if (not (. new-set prefix))
|
|
||||||
(table.insert deleted (parse-prefix prefix))))
|
|
||||||
(values added deleted)))
|
(values added deleted)))
|
||||||
|
|
||||||
(let [[state-directory lan-device] arg
|
(fn update-prefixes [device prefixes new-prefixes]
|
||||||
dir (svc.open state-directory)]
|
(let [(added deleted) (changes prefixes new-prefixes)]
|
||||||
(var prefixes [])
|
(each [_ p (ipairs added)]
|
||||||
(while true
|
(system
|
||||||
(while (not (dir:ready?)) (dir:wait))
|
(.. "ip address add " p.address "1/" p.len " dev " device)))
|
||||||
(if (. bound-states (dir:output "state"))
|
(each [_ p (ipairs deleted)]
|
||||||
(let [new-prefixes (split " " (dir:output "/prefixes"))
|
(system
|
||||||
(added deleted) (changes prefixes new-prefixes)]
|
(.. "ip address del " p.address "1/" p.len " dev " device)))))
|
||||||
(each [_ p (ipairs added)]
|
|
||||||
(system
|
(fn run []
|
||||||
(.. "ip address add " p.prefix "::1/" p.len " dev " lan-device)))
|
(let [[state-directory lan-device] arg
|
||||||
(each [_ p (ipairs deleted)]
|
dir (svc.open state-directory)]
|
||||||
(system
|
(accumulate [addresses []
|
||||||
(.. "ip address del " p.prefix "::1/" p.len " dev " lan-device)))
|
v (dir:events)]
|
||||||
(set prefixes new-prefixes)))
|
(update-prefixes lan-device addresses (v:output "prefix")))))
|
||||||
(dir:wait)))
|
|
||||||
|
{ : changes : run }
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
{
|
{
|
||||||
writeFennelScript
|
writeFennel
|
||||||
, linotify
|
, linotify
|
||||||
, anoia
|
, anoia
|
||||||
, lua
|
, lua
|
||||||
}:
|
}:
|
||||||
writeFennelScript "acquire-delegated-prefix"
|
writeFennel "acquire-delegated-prefix" {
|
||||||
[ linotify anoia lua.pkgs.luafilesystem ]
|
packages = [ linotify anoia lua.pkgs.luafilesystem ];
|
||||||
./acquire-delegated-prefix.fnl
|
mainFunction = "run";
|
||||||
|
} ./acquire-delegated-prefix.fnl
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
(local { : system } (require :anoia))
|
(local { : system } (require :anoia))
|
||||||
(local svc (require :anoia.svc))
|
(local svc (require :anoia.svc))
|
||||||
|
|
||||||
|
;; acquire-delegated-prefix has very similar code: we'd like to move
|
||||||
|
;; this to anoia.svc when we see what the general form would look like
|
||||||
|
|
||||||
(fn changes [old-addresses new-addresses]
|
(fn changes [old-addresses new-addresses]
|
||||||
(let [added {}
|
(let [added {}
|
||||||
deleted {}]
|
deleted {}]
|
||||||
|
|
Loading…
Reference in a new issue