2024-03-05 00:01:19 +01:00
|
|
|
(local nl (require :anoia.nl))
|
2024-03-07 00:18:28 +01:00
|
|
|
(local { : assoc : system } (require :anoia))
|
2023-03-01 18:55:52 +01:00
|
|
|
|
|
|
|
; (local { : view} (require :fennel))
|
|
|
|
|
|
|
|
(fn parse-args [args]
|
|
|
|
(match args
|
|
|
|
["-v" & rest] (assoc (parse-args rest) :verbose true)
|
|
|
|
["-t" timeout & rest] (assoc (parse-args rest) :timeout (tonumber timeout))
|
2024-03-06 01:32:38 +01:00
|
|
|
["-s" service & rest] (assoc (parse-args rest) :service service)
|
2023-03-01 18:55:52 +01:00
|
|
|
[linkname "up"] {:link linkname :expecting "up"}
|
|
|
|
[linkname "running"] {:link linkname :expecting "running"}
|
|
|
|
[linkname "present"] {:link linkname :expecting "present"}
|
|
|
|
[linkname nil] {:link linkname :expecting "present"}
|
|
|
|
_ nil))
|
|
|
|
|
2024-03-07 00:17:24 +01:00
|
|
|
(fn event-matches? [params v]
|
2024-03-05 00:01:19 +01:00
|
|
|
(let [got
|
|
|
|
(match v
|
|
|
|
;; - up: Reflects the administrative state of the interface (IFF_UP)
|
|
|
|
;; - running: Reflects the operational state (IFF_RUNNING).
|
2024-03-05 00:11:04 +01:00
|
|
|
{:event "newlink" :name params.link :up :yes :running :yes}
|
2024-03-05 00:01:19 +01:00
|
|
|
{:present true :up true :running true}
|
2023-03-01 18:55:52 +01:00
|
|
|
|
2024-03-05 00:11:04 +01:00
|
|
|
{:event "newlink" :name params.link :up :yes}
|
2024-03-05 00:01:19 +01:00
|
|
|
{:present :true :up true}
|
2023-03-01 18:55:52 +01:00
|
|
|
|
2024-03-05 00:11:04 +01:00
|
|
|
{:event "newlink" :name params.link}
|
2024-03-05 00:01:19 +01:00
|
|
|
{:present true }
|
2023-03-01 18:55:52 +01:00
|
|
|
|
2024-03-05 00:01:19 +01:00
|
|
|
_
|
|
|
|
{})]
|
2024-03-07 00:18:28 +01:00
|
|
|
(not (not (. got params.expecting)))))
|
|
|
|
|
|
|
|
(var up :unknown)
|
|
|
|
(fn toggle-service [service wanted?]
|
|
|
|
(when (not (= up wanted?))
|
|
|
|
(set up
|
|
|
|
(if wanted?
|
2024-03-17 00:09:41 +01:00
|
|
|
(pcall system (.. "s6-rc -b -u change " service))
|
|
|
|
(not (pcall system (.. "s6-rc -b -d change " service)))))
|
2024-03-07 00:18:28 +01:00
|
|
|
))
|
2023-03-01 18:55:52 +01:00
|
|
|
|
2024-03-06 01:10:59 +01:00
|
|
|
(fn run [args event-fn]
|
2024-03-07 00:18:28 +01:00
|
|
|
(set up :unknown)
|
2024-03-05 18:56:59 +01:00
|
|
|
(let [parameters
|
2024-03-06 01:10:59 +01:00
|
|
|
(assert (parse-args args)
|
|
|
|
(.. "Usage: ifwait [-v] ifname [present|up|running]"))]
|
2024-03-05 18:56:59 +01:00
|
|
|
(when parameters.verbose
|
2024-03-06 01:10:59 +01:00
|
|
|
(print (.. "ifwait: waiting for "
|
2024-03-05 18:56:59 +01:00
|
|
|
parameters.link " to be " parameters.expecting)))
|
|
|
|
|
2024-03-07 00:18:28 +01:00
|
|
|
(if parameters.service
|
|
|
|
(each [e (event-fn)]
|
|
|
|
(if (= e.name parameters.link)
|
|
|
|
(toggle-service parameters.service (event-matches? parameters e))))
|
|
|
|
(each [e (event-fn)
|
|
|
|
&until (event-matches? parameters e)]
|
|
|
|
true))))
|
2024-03-05 18:56:59 +01:00
|
|
|
|
2024-03-06 01:10:59 +01:00
|
|
|
(when (not (= (. arg 0) "test"))
|
|
|
|
(run arg #(nl.events {:link true})))
|
2024-03-05 18:56:59 +01:00
|
|
|
|
|
|
|
{ : run }
|