commit 0c41e9305c227126874c44ff96c0d57091a21dff
parent 708350711b9ada1a0eeaf1455feb3cc3fe568e87
Author: Daniel Barlow <dan@telent.net>
Date: Sat, 8 Jul 2023 22:15:56 +0100
extract service output watcher to fennel module
Diffstat:
3 files changed, 40 insertions(+), 33 deletions(-)
diff --git a/examples/acquire-delegated-prefix.fnl b/examples/acquire-delegated-prefix.fnl
@@ -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)))
diff --git a/pkgs/anoia/default.nix b/pkgs/anoia/default.nix
@@ -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}"
diff --git a/pkgs/anoia/svc.fnl b/pkgs/anoia/svc.fnl
@@ -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 }