global-functions: $ScriptInstallUpdate: resolve nested conditions...

... and check one after another in a do-block. This uses `:error` as
poor man's continue. 🤪
This commit is contained in:
Christian Hesse 2025-01-29 21:58:47 +01:00
parent 3c8ec5169f
commit 8b19e74736

View file

@ -1109,12 +1109,31 @@
} }
} }
:if ([ :len $SourceNew ] > 0) do={ :do {
:if ([ :len $SourceNew ] = 0) do={
$LogPrint debug $0 ("No update for script '" . $ScriptVal->"name" . "'.");
:error false;
}
:local SourceCRLF [ :tocrlf $SourceNew ]; :local SourceCRLF [ :tocrlf $SourceNew ];
:if ($SourceNew != $ScriptVal->"source" && $SourceCRLF != $ScriptVal->"source") do={ :if ($SourceNew = $ScriptVal->"source" || $SourceCRLF = $ScriptVal->"source") do={
:if ([ :pick $SourceNew 0 18 ] = "#!rsc by RouterOS\n") do={ $LogPrint debug $0 ("Script '" . $ScriptVal->"name" . "' did not change.");
:local Required ([ $ParseKeyValueStore [ $Grep $SourceNew ("\23 requires RouterOS, ") ] ]->"version"); :error false;
:if ([ $RequiredRouterOS $0 [ $EitherOr $Required "0.0" ] false ] = true) do={ }
:if ([ :pick $SourceNew 0 18 ] != "#!rsc by RouterOS\n") do={
$LogPrint warning $0 ("Looks like new script '" . $ScriptVal->"name" . \
"' is not valid (missing shebang). Ignoring!");
:error false;
}
:local RequiredROS ([ $ParseKeyValueStore [ $Grep $SourceNew ("\23 requires RouterOS, ") ] ]->"version");
:if ([ $RequiredRouterOS $0 [ $EitherOr $RequiredROS "0.0" ] false ] = false) do={
$LogPrintOnce warning $0 ("The script '" . $ScriptVal->"name" . "' requires RouterOS " . \
$RequiredROS . ", which is not met by your installation. Ignoring!");
:error false;
}
:local RequiredDM [ $ParseKeyValueStore [ $Grep $SourceNew ("\23 requires device-mode, ") ] ]; :local RequiredDM [ $ParseKeyValueStore [ $Grep $SourceNew ("\23 requires device-mode, ") ] ];
:local MissingDM ({}); :local MissingDM ({});
:foreach Feature,Value in=$RequiredDM do={ :foreach Feature,Value in=$RequiredDM do={
@ -1122,8 +1141,17 @@
:set MissingDM ($MissingDM, $Feature); :set MissingDM ($MissingDM, $Feature);
} }
} }
:if ([ :len $MissingDM ] = 0) do={ :if ([ :len $MissingDM ] > 0) do={
:if ([ $ValidateSyntax $SourceNew ] = true) do={ $LogPrintOnce warning $0 ("The script '" . $ScriptVal->"name" . "' requires disabled " . \
"device-mode features (" . [ :tostr $MissingDM ] . "). Ignoring!");
:error false;
}
:if ([ $ValidateSyntax $SourceNew ] = false) do={
$LogPrint warning $0 ("Syntax validation for script '" . $ScriptVal->"name" . "' failed! Ignoring!");
:error false;
}
$LogPrint info $0 ("Updating script: " . $ScriptVal->"name"); $LogPrint info $0 ("Updating script: " . $ScriptVal->"name");
/system/script/set owner=($ScriptVal->"name") \ /system/script/set owner=($ScriptVal->"name") \
source=[ $IfThenElse ($ScriptUpdatesCRLF = true) $SourceCRLF $SourceNew ] $Script; source=[ $IfThenElse ($ScriptUpdatesCRLF = true) $SourceCRLF $SourceNew ] $Script;
@ -1133,28 +1161,7 @@
:if ($ScriptVal->"name" = "global-functions" || $ScriptVal->"name" ~ ("^mod/.")) do={ :if ($ScriptVal->"name" = "global-functions" || $ScriptVal->"name" ~ ("^mod/.")) do={
:set ReloadGlobalFunctions true; :set ReloadGlobalFunctions true;
} }
} else={ } on-error={ }
$LogPrint warning $0 ("Syntax validation for script '" . $ScriptVal->"name" . \
"' failed! Ignoring!");
}
} else={
$LogPrintOnce warning $0 ("The script '" . $ScriptVal->"name" . "' requires disabled " . \
"device-mode features (" . [ :tostr $MissingDM ] . "). Ignoring!");
}
} else={
$LogPrintOnce warning $0 ("The script '" . $ScriptVal->"name" . "' requires RouterOS " . \
$Required . ", which is not met by your installation. Ignoring!");
}
} else={
$LogPrint warning $0 ("Looks like new script '" . $ScriptVal->"name" . \
"' is not valid (missing shebang). Ignoring!");
}
} else={
$LogPrint debug $0 ("Script '" . $ScriptVal->"name" . "' did not change.");
}
} else={
$LogPrint debug $0 ("No update for script '" . $ScriptVal->"name" . "'.");
}
} }
:if ($ReloadGlobalFunctions = true) do={ :if ($ReloadGlobalFunctions = true) do={