routeros-scripts/packages-update

90 lines
3.1 KiB
Text
Raw Normal View History

#!rsc by RouterOS
2019-02-19 15:41:27 +00:00
# RouterOS script: packages-update
2020-01-01 16:00:39 +00:00
# Copyright (c) 2019-2020 Christian Hesse <mail@eworm.de>
# 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
:global DownloadPackage;
2020-02-28 14:26:26 +00:00
:global LogPrintExit;
:global ScriptFromTerminal;
:global ScriptLock;
:global VersionToNum;
$ScriptLock "packages-update";
2019-02-19 15:41:27 +00:00
2019-07-24 08:13:39 +00:00
:local Update [ / system package update get ];
2019-02-19 15:41:27 +00:00
:if ([ :typeof ($Update->"latest-version") ] = "nothing") do={
$LogPrintExit warning "Latest version is not known." true;
}
2019-07-24 08:13:39 +00:00
:if ($Update->"installed-version" = $Update->"latest-version") do={
$LogPrintExit info ("Version " . $Update->"latest-version" . " is already installed.") true;
2019-02-19 15:41:27 +00:00
}
:local NumInstalled [ $VersionToNum ($Update->"installed-version") ];
:local NumLatest [ $VersionToNum ($Update->"latest-version") ];
:local DoDowngrade false;
:if ($NumInstalled > $NumLatest) do={
:if ([ $ScriptFromTerminal "packages-update" ] = true) do={
:put "Latest version is older than installed one. Want to downgrade? [y/N]";
:if (([ :terminal inkey timeout=60 ] % 32) = 25) do={
:set DoDowngrade true;
} else={
:put "Canceled...";
}
} else={
$LogPrintExit warning ("Not installing downgrade automatically.") true;
}
}
2019-02-19 15:41:27 +00:00
:foreach Package in=[ / system package find where !bundle ] do={
:local PkgName [ / system package get $Package name ];
2019-07-24 08:13:39 +00:00
if ([ $DownloadPackage $PkgName ($Update->"latest-version") ] = false) do={
$LogPrintExit error ("Download for package " . $PkgName . " failed.") true;
2019-02-19 15:41:27 +00:00
}
}
:foreach Script in=[ / system script find where name~"^(cloud|email|upload)-backup\$" ] do={
2020-10-07 22:02:02 +00:00
:local ScriptName [ / system script get $Script name ];
:do {
2020-10-07 22:02:02 +00:00
$LogPrintExit info ("Running backup script " . $ScriptName . " before update.") false;
/ system script run $Script;
} on-error={
2020-10-07 22:02:02 +00:00
$LogPrintExit warning ("Running backup script " . $ScriptName . " before update failed!") false;
:if ([ $ScriptFromTerminal "packages-update" ] = true) do={
:put "Do you want to continue anyway? [y/N]";
:if (([ :terminal inkey timeout=60 ] % 32) = 25) do={
$LogPrintExit info ("User requested to continue anyway.") false;
} else={
$LogPrintExit info ("Canceled update...") true;
}
} else={
$LogPrintExit info ("Canceled non-interactive update.") true;
}
}
2019-02-19 15:41:27 +00:00
}
:if ($DoDowngrade = true) do={
$LogPrintExit info ("Rebooting for downgrade.") false;
:delay 1s;
/ system package downgrade;
}
:if ([ $ScriptFromTerminal "packages-update" ] = true) do={
2019-12-20 13:38:42 +00:00
:put "Do you want to (s)chedule reboot or (r)eboot now? [s/R]";
:if (([ :terminal inkey timeout=60 ] % 32) = 19) do={
/ system scheduler add name="reboot-for-update" start-time=03:00:00 interval=1d \
on-event=(":global RandomDelay; \$RandomDelay 3600; " . \
"/ system scheduler remove reboot-for-update; / system reboot;");
$LogPrintExit info ("Scheduled reboot for update between 03:00 and 04:00.") true;
}
}
2020-04-03 15:22:03 +00:00
$LogPrintExit info ("Rebooting for update.") false;
2019-02-19 15:41:27 +00:00
:delay 1s;
/ system reboot;