2023-03-07 09:57:42 +00:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: packages-update
|
2024-01-01 14:25:25 +00:00
|
|
|
# Copyright (c) 2019-2024 Christian Hesse <mail@eworm.de>
|
2023-03-07 09:57:42 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
|
|
|
#
|
2023-11-15 20:31:19 +00:00
|
|
|
# requires RouterOS, version=7.12
|
|
|
|
#
|
2023-03-07 09:57:42 +00:00
|
|
|
# download packages and reboot for installation
|
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/packages-update.md
|
|
|
|
|
|
|
|
:global GlobalFunctionsReady;
|
|
|
|
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
|
|
|
|
2024-03-06 14:28:55 +00:00
|
|
|
:do {
|
|
|
|
:local ScriptName [ :jobname ];
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2024-03-04 12:48:01 +00:00
|
|
|
:global DownloadPackage;
|
|
|
|
:global Grep;
|
2024-01-31 16:46:31 +00:00
|
|
|
:global LogPrintExit2;
|
2024-03-04 12:48:01 +00:00
|
|
|
:global ParseKeyValueStore;
|
|
|
|
:global ScriptFromTerminal;
|
|
|
|
:global ScriptLock;
|
|
|
|
:global VersionToNum;
|
2024-01-31 16:46:31 +00:00
|
|
|
|
2024-03-04 12:48:01 +00:00
|
|
|
:global PackagesUpdateDeferReboot;
|
2024-03-08 08:19:43 +00:00
|
|
|
:global PackagesUpdateBackupFailure;
|
2024-01-31 16:46:31 +00:00
|
|
|
|
2024-03-04 12:48:01 +00:00
|
|
|
:local Schedule do={
|
|
|
|
:local ScriptName [ :tostr $1 ];
|
2024-01-26 07:54:08 +00:00
|
|
|
|
2024-03-04 12:48:01 +00:00
|
|
|
:global GetRandomNumber;
|
|
|
|
:global LogPrintExit2;
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2024-03-04 12:48:01 +00:00
|
|
|
:global RebootForUpdate do={
|
|
|
|
/system/reboot;
|
|
|
|
}
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2024-03-04 12:48:01 +00:00
|
|
|
:local StartTime [ :tostr [ :totime (10800 + [ $GetRandomNumber 7200 ]) ] ];
|
|
|
|
/system/scheduler/add name="_RebootForUpdate" start-time=$StartTime interval=1d \
|
|
|
|
on-event=("/system/scheduler/remove \"_RebootForUpdate\"; " . \
|
|
|
|
":global RebootForUpdate; \$RebootForUpdate;");
|
|
|
|
$LogPrintExit2 info $ScriptName ("Scheduled reboot for update at " . $StartTime . \
|
2024-03-04 15:18:29 +00:00
|
|
|
" local time (" . [ /system/clock/get time-zone-name ] . ").") false;
|
|
|
|
:return true;
|
2024-03-04 12:48:01 +00:00
|
|
|
}
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2024-03-05 15:12:36 +00:00
|
|
|
:if ([ $ScriptLock $ScriptName ] = false) do={
|
2024-03-06 14:28:55 +00:00
|
|
|
:error false;
|
2024-03-05 15:12:36 +00:00
|
|
|
}
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2024-03-04 12:48:01 +00:00
|
|
|
:local Update [ /system/package/update/get ];
|
2023-11-21 12:06:44 +00:00
|
|
|
|
2024-03-04 12:48:01 +00:00
|
|
|
:if ([ :typeof ($Update->"latest-version") ] = "nothing") do={
|
|
|
|
$LogPrintExit2 warning $ScriptName ("Latest version is not known.") true;
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 12:48:01 +00:00
|
|
|
:if ($Update->"installed-version" = $Update->"latest-version") do={
|
2024-03-04 15:18:29 +00:00
|
|
|
$LogPrintExit2 info $ScriptName ("Version " . $Update->"latest-version" . " is already installed.") false;
|
2024-03-06 14:28:55 +00:00
|
|
|
:error true;
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 12:48:01 +00:00
|
|
|
:local NumInstalled [ $VersionToNum ($Update->"installed-version") ];
|
|
|
|
:local NumLatest [ $VersionToNum ($Update->"latest-version") ];
|
2023-09-12 23:01:34 +00:00
|
|
|
|
2024-03-04 12:48:01 +00:00
|
|
|
:if ($NumInstalled < 0x070d0000 && $NumLatest > 0x070d0000) do={
|
|
|
|
$LogPrintExit2 error $ScriptName ("Migration to wireless/wifi package in RouterOS " . \
|
|
|
|
($Update->"latest-version") . " is pending. Please update manually!") true;
|
|
|
|
}
|
2023-09-12 23:01:34 +00:00
|
|
|
|
2024-03-04 12:48:01 +00:00
|
|
|
:local DoDowngrade false;
|
|
|
|
:if ($NumInstalled > $NumLatest) do={
|
|
|
|
:if ([ $ScriptFromTerminal $ScriptName ] = true) do={
|
|
|
|
:put "Latest version is older than installed one. Want to downgrade? [y/N]";
|
2023-03-07 09:57:42 +00:00
|
|
|
:if (([ /terminal/inkey timeout=60 ] % 32) = 25) do={
|
2024-03-04 12:48:01 +00:00
|
|
|
:set DoDowngrade true;
|
2023-03-07 09:57:42 +00:00
|
|
|
} else={
|
2024-03-04 12:48:01 +00:00
|
|
|
:put "Canceled...";
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
|
|
|
} else={
|
2024-03-04 12:48:01 +00:00
|
|
|
$LogPrintExit2 warning $ScriptName ("Not installing downgrade automatically.") true;
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 12:48:01 +00:00
|
|
|
:foreach Package in=[ /system/package/find where !bundle ] do={
|
|
|
|
:local PkgName [ /system/package/get $Package name ];
|
|
|
|
:if ([ $DownloadPackage $PkgName ($Update->"latest-version") ] = false) do={
|
|
|
|
$LogPrintExit2 error $ScriptName ("Download for package " . $PkgName . " failed, update aborted.") true;
|
|
|
|
}
|
|
|
|
}
|
2023-03-07 09:57:42 +00:00
|
|
|
|
2024-03-04 12:48:01 +00:00
|
|
|
:local RunOrder ({});
|
|
|
|
:foreach Script in=[ /system/script/find where source~("\n# provides: backup-script\\b") ] do={
|
|
|
|
:local ScriptVal [ /system/script/get $Script ];
|
|
|
|
:local Store [ $ParseKeyValueStore [ $Grep ($ScriptVal->"source") ("\23 provides: backup-script, ") ] ];
|
|
|
|
|
|
|
|
:set ($RunOrder->($Store->"order" . "-" . $ScriptVal->"name")) ($ScriptVal->"name");
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
2024-03-04 12:48:01 +00:00
|
|
|
|
|
|
|
:foreach Order,Script in=$RunOrder do={
|
2024-03-08 08:19:43 +00:00
|
|
|
:set PackagesUpdateBackupFailure false;
|
2024-03-04 12:48:01 +00:00
|
|
|
:do {
|
|
|
|
$LogPrintExit2 info $ScriptName ("Running backup script " . $Script . " before update.") false;
|
|
|
|
/system/script/run $Script;
|
|
|
|
} on-error={
|
2024-03-08 08:19:43 +00:00
|
|
|
:set PackagesUpdateBackupFailure true;
|
|
|
|
}
|
|
|
|
|
|
|
|
:if ($PackagesUpdateBackupFailure = true) do={
|
2024-03-04 12:48:01 +00:00
|
|
|
$LogPrintExit2 warning $ScriptName ("Running backup script " . $Script . " before update failed!") false;
|
|
|
|
:if ([ $ScriptFromTerminal $ScriptName ] = true) do={
|
|
|
|
:put "Do you want to continue anyway? [y/N]";
|
|
|
|
:if (([ /terminal/inkey timeout=60 ] % 32) = 25) do={
|
|
|
|
$LogPrintExit2 info $ScriptName ("User requested to continue anyway.") false;
|
|
|
|
} else={
|
|
|
|
$LogPrintExit2 info $ScriptName ("Canceled update...") true;
|
|
|
|
}
|
|
|
|
} else={
|
2024-03-08 09:14:26 +00:00
|
|
|
$LogPrintExit2 warning $ScriptName ("Canceled non-interactive update.") true;
|
2024-03-04 12:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-26 08:11:17 +00:00
|
|
|
}
|
2024-03-04 12:48:01 +00:00
|
|
|
|
|
|
|
:if ($DoDowngrade = true) do={
|
|
|
|
$LogPrintExit2 info $ScriptName ("Rebooting for downgrade.") false;
|
|
|
|
:delay 1s;
|
|
|
|
/system/package/downgrade;
|
|
|
|
}
|
|
|
|
|
|
|
|
:if ([ $ScriptFromTerminal $ScriptName ] = true) do={
|
|
|
|
:put "Do you want to (s)chedule reboot or (r)eboot now? [s/R]";
|
|
|
|
:if (([ /terminal/inkey timeout=60 ] % 32) = 19) do={
|
|
|
|
$Schedule $ScriptName;
|
|
|
|
}
|
|
|
|
} else={
|
|
|
|
:if ($PackagesUpdateDeferReboot = true) do={
|
|
|
|
$Schedule $ScriptName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$LogPrintExit2 info $ScriptName ("Rebooting for update.") false;
|
|
|
|
:delay 1s;
|
|
|
|
/system/reboot;
|
2024-03-06 14:28:55 +00:00
|
|
|
} on-error={ }
|