odhcp-script: delete stale dirs from previous runs
This requires adding LFS as a dependency because native Lua has no way to iterate a directory, but it seems to be Not Huge and hopefully we'll have other uses for it
This commit is contained in:
parent
09fe21260e
commit
12e25722fa
3 changed files with 28 additions and 2 deletions
|
@ -14,6 +14,7 @@ let packages = [
|
||||||
linotify
|
linotify
|
||||||
anoia
|
anoia
|
||||||
fennel
|
fennel
|
||||||
|
lua.pkgs.luafilesystem
|
||||||
];
|
];
|
||||||
join = ps: builtins.concatStringsSep ";" ps;
|
join = ps: builtins.concatStringsSep ";" ps;
|
||||||
luapath = join (builtins.map (f:
|
luapath = join (builtins.map (f:
|
||||||
|
|
|
@ -1,8 +1,25 @@
|
||||||
(local { : split : merge : mkdir } (require :anoia))
|
(local { : split : merge : mkdir } (require :anoia))
|
||||||
(local { : view } (require :fennel))
|
(local { : view } (require :fennel))
|
||||||
|
(local lfs (require :lfs))
|
||||||
|
|
||||||
|
(fn rmtree [pathname]
|
||||||
|
(case (lfs.symlinkattributes pathname)
|
||||||
|
nil true
|
||||||
|
{:mode "directory"}
|
||||||
|
(do
|
||||||
|
(each [f (lfs.dir pathname)]
|
||||||
|
(when (not (or (= f ".") (= f "..")))
|
||||||
|
(rmtree ( .. pathname "/" f)))
|
||||||
|
(lfs.rmdir pathname)))
|
||||||
|
{:mode "file"}
|
||||||
|
(os.remove pathname)
|
||||||
|
{:mode "link"}
|
||||||
|
(os.remove pathname)
|
||||||
|
unknown
|
||||||
|
(error (.. "can't remove " pathname " of kind \"" unknown.mode "\""))))
|
||||||
|
|
||||||
|
|
||||||
(local state-directory (assert (os.getenv "SERVICE_STATE")))
|
(local state-directory (assert (os.getenv "SERVICE_STATE")))
|
||||||
|
|
||||||
(mkdir state-directory)
|
(mkdir state-directory)
|
||||||
|
|
||||||
(fn write-value [name value]
|
(fn write-value [name value]
|
||||||
|
@ -13,7 +30,6 @@
|
||||||
(fn write-value-from-env [name]
|
(fn write-value-from-env [name]
|
||||||
(write-value name (os.getenv (string.upper name))))
|
(write-value name (os.getenv (string.upper name))))
|
||||||
|
|
||||||
|
|
||||||
(fn parse-address [str]
|
(fn parse-address [str]
|
||||||
(fn parse-extra [s]
|
(fn parse-extra [s]
|
||||||
(let [out {}]
|
(let [out {}]
|
||||||
|
@ -39,6 +55,10 @@
|
||||||
;; a half-updated snapshot
|
;; a half-updated snapshot
|
||||||
(os.remove (.. state-directory "/state"))
|
(os.remove (.. state-directory "/state"))
|
||||||
|
|
||||||
|
;; remove parsed addresses/prefixes from any previous run
|
||||||
|
(rmtree (.. state-directory "/prefix"))
|
||||||
|
(rmtree (.. state-directory "/address"))
|
||||||
|
|
||||||
(let [wanted
|
(let [wanted
|
||||||
[
|
[
|
||||||
:addresses
|
:addresses
|
||||||
|
|
|
@ -9,6 +9,11 @@ cleanup(){
|
||||||
trap 'exit 1' INT HUP QUIT TERM ALRM USR1
|
trap 'exit 1' INT HUP QUIT TERM ALRM USR1
|
||||||
trap 'cleanup' EXIT
|
trap 'cleanup' EXIT
|
||||||
|
|
||||||
|
# call the script twice with different state, to test that it cleans
|
||||||
|
# out old values when it runs again
|
||||||
|
|
||||||
|
(set -a; . ./test.env ; ADDRESSES=2001:80:111:111:0:fff:123:567/128,3600,7200 SERVICE_STATE=$statedir fennelrepl odhcp6-script.fnl ppp0 bound) 10>&1
|
||||||
|
|
||||||
(set -a; . ./test.env ; SERVICE_STATE=$statedir fennelrepl odhcp6-script.fnl ppp0 bound) 10>&1
|
(set -a; . ./test.env ; SERVICE_STATE=$statedir fennelrepl odhcp6-script.fnl ppp0 bound) 10>&1
|
||||||
|
|
||||||
(cd $statedir && find . -type f | xargs grep '' | sort) > actual
|
(cd $statedir && find . -type f | xargs grep '' | sort) > actual
|
||||||
|
|
Loading…
Reference in a new issue