aboutsummaryrefslogtreecommitdiffstats
path: root/packages-update
blob: 7c88eacf7f5331efdaa3162286c7d5b7c738cc16 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!rsc by RouterOS
# RouterOS script: packages-update
# Copyright (c) 2019-2020 Christian Hesse <mail@eworm.de>
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
#
# download packages and reboot for installation
# https://git.eworm.de/cgit/routeros-scripts/about/doc/packages-update.md

:global DownloadPackage;
:global LogPrintExit;
:global ScriptFromTerminal;
:global ScriptLock;
:global VersionToNum;

$ScriptLock "packages-update";

:local Update [ / system package update get ];

:if ([ :typeof ($Update->"latest-version") ] = "nothing") do={
  $LogPrintExit warning "Latest version is not known." true;
}

:if ($Update->"installed-version" = $Update->"latest-version") do={
  $LogPrintExit info ("Version " . $Update->"latest-version" . " is already installed.") true;
}

: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;
  }
}

:foreach Package in=[ / system package find where !bundle ] do={
  :local PkgName [ / system package get $Package name ];
  if ([ $DownloadPackage $PkgName ($Update->"latest-version") ] = false) do={
    $LogPrintExit error ("Download for package " . $PkgName . " failed.") true;
  }
}

:foreach Script in=[ / system script find where name~"^(cloud|email|upload)-backup\$" ] do={
  :local ScriptName [ / system script get $Script name ];
  :do {
    $LogPrintExit info ("Running backup script " . $ScriptName . " before update.") false;
    / system script run $Script;
  } on-error={
    $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;
    }
  }
}

:if ($DoDowngrade = true) do={
  $LogPrintExit info ("Rebooting for downgrade.") false;
  :delay 1s;
  / system package downgrade;
}

:if ([ $ScriptFromTerminal "packages-update" ] = true) do={
  :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;
  }
}

$LogPrintExit info ("Rebooting for update.") false;
:delay 1s;
/ system reboot;