global-functions: $ScriptInstallUpdate: add a version check for scripts

New RouterOS releases bring new features, and thus new syntax.
Installing a new script on old RouterOS results in syntax errors. We
want to give clear warning messages, so by adding an extra comment into
the script...

    # requires RouterOS, version=7.8beta3

... installing on unsupported RouterOS version is mitigated.
This commit is contained in:
Christian Hesse 2023-01-19 22:43:03 +01:00
parent 9887ab0849
commit de9dde1b14

View file

@ -700,9 +700,12 @@
:global ScriptUpdatesUrlSuffix; :global ScriptUpdatesUrlSuffix;
:global CertificateAvailable; :global CertificateAvailable;
:global EitherOr;
:global Grep;
:global IfThenElse; :global IfThenElse;
:global LogPrintExit2; :global LogPrintExit2;
:global ParseKeyValueStore; :global ParseKeyValueStore;
:global RequiredRouterOS;
:global SendNotification2; :global SendNotification2;
:global SymbolForNotification; :global SymbolForNotification;
:global ValidateSyntax; :global ValidateSyntax;
@ -773,18 +776,24 @@
:if ([ :len $SourceNew ] > 0) do={ :if ([ :len $SourceNew ] > 0) do={
:if ($SourceNew != $ScriptVal->"source") do={ :if ($SourceNew != $ScriptVal->"source") do={
:if ([ :pick $SourceNew 0 18 ] = "#!rsc by RouterOS\n") do={ :if ([ :pick $SourceNew 0 18 ] = "#!rsc by RouterOS\n") do={
:if ([ $ValidateSyntax $SourceNew ] = true) do={ :local Required ([ $ParseKeyValueStore [ $Grep $SourceNew "# requires RouterOS, " ] ]->"version");
$LogPrintExit2 info $0 ("Updating script: " . $ScriptVal->"name") false; :if ([ $RequiredRouterOS $0 [ $EitherOr $Required "0.0" ] false ] = true) do={
/system/script/set owner=($ScriptVal->"name") source=$SourceNew $Script; :if ([ $ValidateSyntax $SourceNew ] = true) do={
:if ($ScriptVal->"name" = "global-config") do={ $LogPrintExit2 info $0 ("Updating script: " . $ScriptVal->"name") false;
:set ReloadGlobalConfig true; /system/script/set owner=($ScriptVal->"name") source=$SourceNew $Script;
} :if ($ScriptVal->"name" = "global-config") do={
:if ($ScriptVal->"name" = "global-functions" || $ScriptVal->"name" ~ ("^mod/.")) do={ :set ReloadGlobalConfig true;
:set ReloadGlobalFunctions true; }
:if ($ScriptVal->"name" = "global-functions" || $ScriptVal->"name" ~ ("^mod/.")) do={
:set ReloadGlobalFunctions true;
}
} else={
$LogPrintExit2 warning $0 ("Syntax validation for script '" . $ScriptVal->"name" . \
"' failed! Ignoring!") false;
} }
} else={ } else={
$LogPrintExit2 warning $0 ("Syntax validation for script '" . $ScriptVal->"name" . \ $LogPrintExit2 warning $0 ("The script '" . $ScriptVal->"name" . "' requires RouterOS " . \
"' failed! Ignoring!") false; $Required . ", which is not met by your installation. Ignoring!") false;
} }
} else={ } else={
$LogPrintExit2 warning $0 ("Looks like new script '" . $ScriptVal->"name" . \ $LogPrintExit2 warning $0 ("Looks like new script '" . $ScriptVal->"name" . \