Compare commits
6 Commits
e7de889403
...
7c06f30675
Author | SHA1 | Date | |
---|---|---|---|
7c06f30675 | |||
c7ead8559b | |||
a19d12d6d7 | |||
b5cd0cc2d5 | |||
0c41e9305c | |||
708350711b |
@ -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)))
|
||||
|
60
examples/acquire-wan-address.fnl
Normal file
60
examples/acquire-wan-address.fnl
Normal file
@ -0,0 +1,60 @@
|
||||
(local { : merge : split : file-exists? : system } (require :anoia))
|
||||
(local svc (require :anoia.svc))
|
||||
|
||||
;; structurally this is remarkably similar to
|
||||
;; acquire-lan-prefix.fnl. maybe they should be merged: if not then
|
||||
;; we could at least extract some common code
|
||||
|
||||
;; (alternatively we could move all the parsing code into the thing in
|
||||
;; the odhcp service that *writes* this stuff)
|
||||
|
||||
; (parse-address "2001:8b0:1111:1111:0:ffff:51bb:4cf2/128,3600,7200")
|
||||
|
||||
|
||||
(fn parse-address [str]
|
||||
(fn parse-extra [s]
|
||||
(let [out {}]
|
||||
(each [name val (string.gmatch s ",(.-)=([^,]+)")]
|
||||
(tset out name val))
|
||||
out))
|
||||
(let [(address len preferred valid extra)
|
||||
(string.match str "(.-)/(%d+),(%d+),(%d+)(.*)$")]
|
||||
(merge {: address : len : preferred : valid} (parse-extra extra))))
|
||||
|
||||
(local bound-states
|
||||
{ :bound true
|
||||
:rebound true
|
||||
:informed true
|
||||
:updated true
|
||||
:ra-updated true
|
||||
})
|
||||
|
||||
(fn changes [old-addresses new-addresses]
|
||||
(let [added {}
|
||||
deleted {}
|
||||
old-set (collect [_ v (ipairs old-addresses)] (values v true))
|
||||
new-set (collect [_ v (ipairs new-addresses)] (values v true))]
|
||||
(each [_ address (ipairs new-addresses)]
|
||||
(if (not (. old-set address))
|
||||
(table.insert added (parse-address address))))
|
||||
(each [_ address (ipairs old-addresses)]
|
||||
(if (not (. new-set address))
|
||||
(table.insert deleted (parse-address address))))
|
||||
(values added deleted)))
|
||||
|
||||
(let [[state-directory wan-device] arg
|
||||
dir (svc.open state-directory)]
|
||||
(var addresses [])
|
||||
(while true
|
||||
(while (not (dir:ready?)) (dir:wait))
|
||||
(if (. bound-states (dir:output "state"))
|
||||
(let [new-addresses (split " " (dir:output "/addresses"))
|
||||
(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)))
|
||||
(set addresses new-addresses)))
|
||||
(dir:wait)))
|
8
examples/acquire-wan-address.nix
Normal file
8
examples/acquire-wan-address.nix
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
writeFennelScript
|
||||
, linotify
|
||||
, anoia
|
||||
}:
|
||||
writeFennelScript "acquire-wan-address"
|
||||
[ linotify anoia ]
|
||||
./acquire-wan-address.fnl
|
@ -295,15 +295,6 @@ in rec {
|
||||
dependencies = [ services.wan ];
|
||||
};
|
||||
|
||||
# services.set-wan-address =
|
||||
# oneshot {
|
||||
# name = "set-wan-address";
|
||||
# # FIXME nasty bit of hardcoding - should get this from dhcp6c
|
||||
# up = "ip address add 2001:8b0:1111:1111:0:ffff:51bb:4cf2/128 dev ppp0";
|
||||
# down = "ip address del 2001:8b0:1111:1111:0:ffff:51bb:4cf2/128 dev ppp0";
|
||||
# dependencies = [ services.dhcp6 ];
|
||||
# };
|
||||
|
||||
services.acquire-lan-prefix =
|
||||
let script = pkgs.callPackage ./acquire-delegated-prefix.nix { };
|
||||
in longrun {
|
||||
@ -312,6 +303,14 @@ in rec {
|
||||
dependencies = [ services.dhcp6 ];
|
||||
};
|
||||
|
||||
services.acquire-wan-address =
|
||||
let script = pkgs.callPackage ./acquire-wan-address.nix { };
|
||||
in longrun {
|
||||
name = "acquire-wan-address";
|
||||
run = "${script} /run/service-state/dhcp6c.wan $(output ${services.wan} ifname)";
|
||||
dependencies = [ services.dhcp6 ];
|
||||
};
|
||||
|
||||
services.default = target {
|
||||
name = "default";
|
||||
contents = with config.services; [
|
||||
@ -331,7 +330,7 @@ in rec {
|
||||
config.services.hostname
|
||||
dhcp6
|
||||
acquire-lan-prefix
|
||||
# set-wan-address
|
||||
acquire-wan-address
|
||||
];
|
||||
};
|
||||
defaultProfile.packages = with pkgs; [
|
||||
|
@ -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 }
|
@ -16,7 +16,10 @@ let packages = [
|
||||
fennel
|
||||
];
|
||||
join = ps: builtins.concatStringsSep ";" ps;
|
||||
luapath = join (builtins.map (f: "${f}/share/lua/${lua.luaversion}/?.lua") packages);
|
||||
luapath = join (builtins.map (f:
|
||||
"${f}/share/lua/${lua.luaversion}/?.lua;" +
|
||||
"${f}/share/lua/${lua.luaversion}/?/init.lua"
|
||||
) packages);
|
||||
luacpath = join (builtins.map (f: "${f}/lib/lua/${lua.luaversion}/?.so") packages);
|
||||
|
||||
in writeScriptBin "fennelrepl" ''
|
||||
@ -29,6 +32,10 @@ in writeScriptBin "fennelrepl" ''
|
||||
if more_fennel then
|
||||
fennel.path = more_fennel .. ";" .. fennel.path
|
||||
end
|
||||
print("path", fennel.path)
|
||||
fennel.repl()
|
||||
if #arg > 0 then
|
||||
script = table.remove(arg, 1)
|
||||
fennel.dofile(script, {}, arg)
|
||||
else
|
||||
fennel.repl()
|
||||
end
|
||||
''
|
||||
|
@ -19,8 +19,8 @@ name : packages : source :
|
||||
buildPhase = ''
|
||||
(
|
||||
echo "#!${lua}/bin/lua"
|
||||
echo "package.path = ${lib.strings.escapeShellArg luapath} .. package.path"
|
||||
echo "package.cpath = ${lib.strings.escapeShellArg luacpath} .. package.cpath"
|
||||
echo "package.path = ${lib.strings.escapeShellArg (builtins.concatStringsSep "" luapath)} .. package.path"
|
||||
echo "package.cpath = ${lib.strings.escapeShellArg (builtins.concatStringsSep "" luacpath)} .. package.cpath"
|
||||
fennel --correlate --compile ${source}
|
||||
) > ${name}.lua
|
||||
'';
|
||||
|
Loading…
Reference in New Issue
Block a user