extract service output watcher to fennel module
This commit is contained in:
parent
708350711b
commit
0c41e9305c
3 changed files with 40 additions and 33 deletions
|
@ -1,5 +1,5 @@
|
|||
(local inotify (require :inotify))
|
||||
(local { : merge : split : file-exists? : system } (require :anoia))
|
||||
(local svc (require :anoia.svc))
|
||||
|
||||
(fn parse-prefix [str]
|
||||
(fn parse-extra [s]
|
||||
|
@ -17,32 +17,6 @@
|
|||
;;(parse-prefix "2001:8b0:de3a:40dc::/64,7198,7198")
|
||||
;;(parse-prefix "2001:8b0:de3a:1001::/64,7198,7188,excluded=1/2,thi=10")
|
||||
|
||||
(fn read-line [name]
|
||||
(with-open [f (assert (io.open name :r) (.. "can't open file " name))]
|
||||
(f:read "*l")))
|
||||
|
||||
(fn watch-fsevents [directory-name]
|
||||
(let [handle (inotify.init)]
|
||||
(handle:addwatch directory-name
|
||||
inotify.IN_CREATE
|
||||
inotify.IN_MOVE
|
||||
inotify.IN_DELETE
|
||||
inotify.IN_DELETE_SELF
|
||||
inotify.IN_MOVED_FROM
|
||||
inotify.IN_MOVED_TO
|
||||
inotify.IN_CLOSE_WRITE)
|
||||
handle))
|
||||
|
||||
(fn watch-directory [pathname]
|
||||
(let [watcher (watch-fsevents pathname)]
|
||||
{
|
||||
:has-file? (fn [_ filename] (file-exists? (.. pathname "/" filename)))
|
||||
:wait-events (fn [] (watcher:read))
|
||||
:ready? (fn [self]
|
||||
(and (self:has-file? "state") (not (self:has-file? ".lock"))))
|
||||
:read-line (fn [_ filename] (read-line (.. pathname "/" filename)))
|
||||
:close #(watcher:close)
|
||||
}))
|
||||
|
||||
(local bound-states
|
||||
{ :bound true
|
||||
|
@ -68,12 +42,12 @@
|
|||
(values added deleted)))
|
||||
|
||||
(let [[state-directory lan-device] arg
|
||||
dir (watch-directory state-directory)]
|
||||
dir (svc.open state-directory)]
|
||||
(var prefixes [])
|
||||
(while true
|
||||
(while (not (dir:ready?)) (dir:wait-events))
|
||||
(if (. bound-states (dir:read-line "state"))
|
||||
(let [new-prefixes (split " " (dir:read-line "/prefixes"))
|
||||
(while (not (dir:ready?)) (dir:wait))
|
||||
(if (. bound-states (dir:output "state"))
|
||||
(let [new-prefixes (split " " (dir:output "/prefixes"))
|
||||
(added deleted) (changes prefixes new-prefixes)]
|
||||
(each [_ p (ipairs added)]
|
||||
(system
|
||||
|
@ -82,4 +56,4 @@
|
|||
(system
|
||||
(.. "ip address del " p.prefix "::1/" p.len " dev " lan-device)))
|
||||
(set prefixes new-prefixes)))
|
||||
(dir:wait-events)))
|
||||
(dir:wait)))
|
||||
|
|
|
@ -10,7 +10,9 @@ in stdenv.mkDerivation {
|
|||
src = ./.;
|
||||
nativeBuildInputs = [ fennel ];
|
||||
buildPhase = ''
|
||||
fennel --compile init.fnl > init.lua
|
||||
for f in *.fnl ; do
|
||||
fennel --compile $f > `basename $f .fnl`.lua
|
||||
done
|
||||
'';
|
||||
installPhase = ''
|
||||
mkdir -p "$out/share/lua/${lua.luaversion}/${pname}"
|
||||
|
|
31
pkgs/anoia/svc.fnl
Normal file
31
pkgs/anoia/svc.fnl
Normal file
|
@ -0,0 +1,31 @@
|
|||
(local inotify (require :inotify))
|
||||
(local { : file-exists? } (require :anoia))
|
||||
|
||||
(fn read-line [name]
|
||||
(with-open [f (assert (io.open name :r) (.. "can't open file " name))]
|
||||
(f:read "*l")))
|
||||
|
||||
(fn watch-fsevents [directory-name]
|
||||
(let [handle (inotify.init)]
|
||||
(handle:addwatch directory-name
|
||||
inotify.IN_CREATE
|
||||
inotify.IN_MOVE
|
||||
inotify.IN_DELETE
|
||||
inotify.IN_DELETE_SELF
|
||||
inotify.IN_MOVED_FROM
|
||||
inotify.IN_MOVED_TO
|
||||
inotify.IN_CLOSE_WRITE)
|
||||
handle))
|
||||
|
||||
(fn open [directory]
|
||||
(let [watcher (watch-fsevents directory)
|
||||
has-file? (fn [filename] (file-exists? (.. directory "/" filename)))]
|
||||
{
|
||||
:wait (fn [] (watcher:read))
|
||||
:ready? (fn [self]
|
||||
(and (has-file? "state") (not (has-file? ".lock"))))
|
||||
:output (fn [_ filename] (read-line (.. directory "/" filename)))
|
||||
:close #(watcher:close)
|
||||
}))
|
||||
|
||||
{ : open }
|
Loading…
Reference in a new issue