2020-09-18 09:00:27 +00:00
|
|
|
#!rsc by RouterOS
|
2019-02-19 15:41:27 +00:00
|
|
|
# RouterOS script: packages-update
|
2023-01-02 22:33:49 +00:00
|
|
|
# Copyright (c) 2019-2023 Christian Hesse <mail@eworm.de>
|
2020-06-19 20:17:42 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
2019-02-19 15:41:27 +00:00
|
|
|
#
|
|
|
|
# download packages and reboot for installation
|
2020-03-27 20:49:17 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/packages-update.md
|
2019-02-19 15:41:27 +00:00
|
|
|
|
2021-02-22 14:14:10 +00:00
|
|
|
:local 0 "packages-update";
|
2021-02-18 13:52:47 +00:00
|
|
|
:global GlobalFunctionsReady;
|
|
|
|
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
|
|
|
|
2019-02-19 15:41:27 +00:00
|
|
|
:global DownloadPackage;
|
2021-02-22 14:14:10 +00:00
|
|
|
:global LogPrintExit2;
|
2019-12-04 08:21:07 +00:00
|
|
|
:global ScriptFromTerminal;
|
2019-12-05 08:55:37 +00:00
|
|
|
:global ScriptLock;
|
2020-07-07 07:28:16 +00:00
|
|
|
:global VersionToNum;
|
2019-12-05 08:55:37 +00:00
|
|
|
|
2021-02-22 18:52:20 +00:00
|
|
|
$ScriptLock $0;
|
2019-02-19 15:41:27 +00:00
|
|
|
|
2022-05-10 13:13:05 +00:00
|
|
|
:local Update [ /system/package/update/get ];
|
2019-02-19 15:41:27 +00:00
|
|
|
|
2019-07-24 08:22:37 +00:00
|
|
|
:if ([ :typeof ($Update->"latest-version") ] = "nothing") do={
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 warning $0 ("Latest version is not known.") true;
|
2019-07-24 08:22:37 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 08:13:39 +00:00
|
|
|
:if ($Update->"installed-version" = $Update->"latest-version") do={
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 info $0 ("Version " . $Update->"latest-version" . " is already installed.") true;
|
2019-02-19 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 07:28:16 +00:00
|
|
|
:local NumInstalled [ $VersionToNum ($Update->"installed-version") ];
|
|
|
|
:local NumLatest [ $VersionToNum ($Update->"latest-version") ];
|
|
|
|
|
|
|
|
:local DoDowngrade false;
|
|
|
|
:if ($NumInstalled > $NumLatest) do={
|
2021-02-22 19:01:43 +00:00
|
|
|
:if ([ $ScriptFromTerminal $0 ] = true) do={
|
2020-07-07 07:28:16 +00:00
|
|
|
:put "Latest version is older than installed one. Want to downgrade? [y/N]";
|
2022-05-10 13:13:05 +00:00
|
|
|
:if (([ /terminal/inkey timeout=60 ] % 32) = 25) do={
|
2020-07-07 07:28:16 +00:00
|
|
|
:set DoDowngrade true;
|
|
|
|
} else={
|
|
|
|
:put "Canceled...";
|
|
|
|
}
|
|
|
|
} else={
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 warning $0 ("Not installing downgrade automatically.") true;
|
2020-07-07 07:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 13:13:05 +00:00
|
|
|
:foreach Package in=[ /system/package/find where !bundle ] do={
|
|
|
|
:local PkgName [ /system/package/get $Package name ];
|
2021-04-29 12:52:38 +00:00
|
|
|
:if ([ $DownloadPackage $PkgName ($Update->"latest-version") ] = false) do={
|
2021-04-29 12:55:13 +00:00
|
|
|
$LogPrintExit2 error $0 ("Download for package " . $PkgName . " failed, update aborted.") true;
|
2019-02-19 15:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 13:13:05 +00:00
|
|
|
:foreach Script in=[ /system/script/find where source~"\n# provides: backup-script\n" ] do={
|
|
|
|
:local ScriptName [ /system/script/get $Script name ];
|
2020-07-07 10:18:37 +00:00
|
|
|
:do {
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 info $0 ("Running backup script " . $ScriptName . " before update.") false;
|
2022-05-10 13:13:05 +00:00
|
|
|
/system/script/run $Script;
|
2020-07-07 10:18:37 +00:00
|
|
|
} on-error={
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 warning $0 ("Running backup script " . $ScriptName . " before update failed!") false;
|
2021-02-22 19:01:43 +00:00
|
|
|
:if ([ $ScriptFromTerminal $0 ] = true) do={
|
2020-10-07 14:04:41 +00:00
|
|
|
:put "Do you want to continue anyway? [y/N]";
|
2022-05-10 13:13:05 +00:00
|
|
|
:if (([ /terminal/inkey timeout=60 ] % 32) = 25) do={
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 info $0 ("User requested to continue anyway.") false;
|
2020-10-07 14:04:41 +00:00
|
|
|
} else={
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 info $0 ("Canceled update...") true;
|
2020-10-07 14:04:41 +00:00
|
|
|
}
|
|
|
|
} else={
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 info $0 ("Canceled non-interactive update.") true;
|
2020-10-07 14:04:41 +00:00
|
|
|
}
|
2020-07-07 10:18:37 +00:00
|
|
|
}
|
2019-02-19 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 07:28:16 +00:00
|
|
|
:if ($DoDowngrade = true) do={
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 info $0 ("Rebooting for downgrade.") false;
|
2020-07-07 07:28:16 +00:00
|
|
|
:delay 1s;
|
2022-05-10 13:13:05 +00:00
|
|
|
/system/package/downgrade;
|
2020-07-07 07:28:16 +00:00
|
|
|
}
|
2019-07-24 08:49:51 +00:00
|
|
|
|
2021-02-22 19:01:43 +00:00
|
|
|
:if ([ $ScriptFromTerminal $0 ] = true) do={
|
2019-12-20 13:38:42 +00:00
|
|
|
:put "Do you want to (s)chedule reboot or (r)eboot now? [s/R]";
|
2022-05-10 13:13:05 +00:00
|
|
|
:if (([ /terminal/inkey timeout=60 ] % 32) = 19) do={
|
|
|
|
/system/scheduler/add name="reboot-for-update" start-time=03:00:00 interval=1d \
|
2019-12-04 08:21:07 +00:00
|
|
|
on-event=(":global RandomDelay; \$RandomDelay 3600; " . \
|
2022-05-10 13:13:05 +00:00
|
|
|
"/system/scheduler/remove reboot-for-update; /system/reboot;");
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 info $0 ("Scheduled reboot for update between 03:00 and 04:00.") true;
|
2019-12-04 08:21:07 +00:00
|
|
|
}
|
2019-09-02 07:39:19 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 info $0 ("Rebooting for update.") false;
|
2019-02-19 15:41:27 +00:00
|
|
|
:delay 1s;
|
2022-05-10 13:13:05 +00:00
|
|
|
/system/reboot;
|