lease-script: move code into function

This commit is contained in:
Christian Hesse 2024-03-04 13:48:01 +01:00
parent d6ea66ccd3
commit 49650d8b14

View file

@ -8,45 +8,54 @@
# run scripts on DHCP lease # run scripts on DHCP lease
# https://git.eworm.de/cgit/routeros-scripts/about/doc/lease-script.md # https://git.eworm.de/cgit/routeros-scripts/about/doc/lease-script.md
:local 0 [ :jobname ];
:global GlobalFunctionsReady; :global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; } :while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
:global Grep; :local Main do={
:global IfThenElse; :local ScriptName [ :tostr $1 ];
:global LogPrintExit2; :local leaseActIP [ :tostr $2 ];
:global ParseKeyValueStore; :local leaseActMAC [ :tostr $2 ];
:global ScriptLock; :local leaseServerName [ :tostr $2 ];
:local leaseBound [ :tostr $2 ];
:if ([ :typeof $leaseActIP ] = "nothing" || \ :global Grep;
[ :typeof $leaseActMAC ] = "nothing" || \ :global IfThenElse;
[ :typeof $leaseServerName ] = "nothing" || \ :global LogPrintExit2;
[ :typeof $leaseBound ] = "nothing") do={ :global ParseKeyValueStore;
$LogPrintExit2 error $0 ("This script is supposed to run from ip dhcp-server.") true; :global ScriptLock;
}
$LogPrintExit2 debug $0 ("DHCP Server " . $leaseServerName . " " . [ $IfThenElse ($leaseBound = 0) \ :if ([ :typeof $leaseActIP ] = "nothing" || \
"de" "" ] . "assigned lease " . $leaseActIP . " to " . $leaseActMAC) false; [ :typeof $leaseActMAC ] = "nothing" || \
[ :typeof $leaseServerName ] = "nothing" || \
[ :typeof $leaseBound ] = "nothing") do={
$LogPrintExit2 error $ScriptName ("This script is supposed to run from ip dhcp-server.") true;
}
$ScriptLock $0 false 10; $LogPrintExit2 debug $ScriptName ("DHCP Server " . $leaseServerName . " " . [ $IfThenElse ($leaseBound = 0) \
"de" "" ] . "assigned lease " . $leaseActIP . " to " . $leaseActMAC) false;
:if ([ :len [ /system/script/job/find where script=$0 ] ] > 1) do={ $ScriptLock $ScriptName false 10;
$LogPrintExit2 debug $0 ("More invocations are waiting, exiting early.") true;
}
:local RunOrder ({}); :if ([ :len [ /system/script/job/find where script=$ScriptName ] ] > 1) do={
:foreach Script in=[ /system/script/find where source~("\n# provides: lease-script\\b") ] do={ $LogPrintExit2 debug $ScriptName ("More invocations are waiting, exiting early.") true;
:local ScriptVal [ /system/script/get $Script ]; }
:local Store [ $ParseKeyValueStore [ $Grep ($ScriptVal->"source") ("\23 provides: lease-script, ") ] ];
:set ($RunOrder->($Store->"order" . "-" . $ScriptVal->"name")) ($ScriptVal->"name"); :local RunOrder ({});
} :foreach Script in=[ /system/script/find where source~("\n# provides: lease-script\\b") ] do={
:local ScriptVal [ /system/script/get $Script ];
:local Store [ $ParseKeyValueStore [ $Grep ($ScriptVal->"source") ("\23 provides: lease-script, ") ] ];
:foreach Order,Script in=$RunOrder do={ :set ($RunOrder->($Store->"order" . "-" . $ScriptVal->"name")) ($ScriptVal->"name");
:do { }
$LogPrintExit2 debug $0 ("Running script with order " . $Order . ": " . $Script) false;
/system/script/run $Script; :foreach Order,Script in=$RunOrder do={
} on-error={ :do {
$LogPrintExit2 warning $0 ("Running script '" . $Script . "' failed!") false; $LogPrintExit2 debug $ScriptName ("Running script with order " . $Order . ": " . $Script) false;
/system/script/run $Script;
} on-error={
$LogPrintExit2 warning $ScriptName ("Running script '" . $Script . "' failed!") false;
}
} }
} }
$Main [ :jobname ] $leaseActIP $leaseActMAC $leaseServerName $leaseBound;