aboutsummaryrefslogtreecommitdiffstats
path: root/script-updates
blob: 98d2440251b20d1c50ee8ba0cfb9d41d96ec04bd (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
#!rsc
# RouterOS script: script-updates
# Copyright (c) 2013-2018 Christian Hesse <mail@eworm.de>
#
# update installed scripts from file or url

:global "script-updates-fetch";
:global "script-updates-baseurl";
:global "script-updates-urlsuffix";
:global "script-updates-ignore";

:foreach script in=[ / system script find ] do={
  :local ignore 0;
  :local scriptname [ / system script get $script name ];
  :local scriptpolicy [ / system script get $script policy ];
  :local scriptfile [ / file find where name=("script-updates/" . $scriptname) ];
  :local sourcenew;
  :if ([ :len $scriptfile ] > 0) do={
    :set sourcenew [ / file get $scriptfile content ];
    / file remove $scriptfile;
  }

  :foreach scheduler in=[ / system scheduler find where on-event=$scriptname ] do={
    :local schedulername [ / system scheduler get $scheduler name ];
    :local schedulerpolicy [ / system scheduler get $scheduler policy ];
    :if ($scriptpolicy != schedulerpolicy) do={
      :log warning ("Policies differ for script " . $scriptname . \
        " and its scheduler " . $schedulername . "!");
    }
  }

  :if ([ :len $sourcenew ] = 0 && $"script-updates-fetch" = true) do={
    :foreach "ignore-loop" in=$"script-updates-ignore" do={
      :if ($"ignore-loop" = $scriptname) do={ :set ignore 1; }
    }

    :if ($ignore = 0) do={
      :log debug ("Fetching script from url: " . $scriptname);
      :do {
        :local result [ / tool fetch check-certificate=yes-without-crl \
            ($"script-updates-baseurl" . $scriptname . $"script-updates-urlsuffix") \
            output=user as-value ];
        :if ($result->"status" = "finished") do={
          :set sourcenew ($result->"data");
        }
      } on-error={
        :log info ("Failed fetching " . $scriptname);
      }
    }
  }

  :if ([ :len $sourcenew ] > 0) do={
    :if ([ :pick $sourcenew 0 5 ] = "#!rsc") do={
      :local sourcecurrent [ / system script get $script source ];
      :if ($sourcenew != $sourcecurrent) do={
        :local "dont-require-permissions" \
            ($sourcenew~"\n# requires: dont-require-permissions=yes\n");
        :log info ("Updating script: " . $scriptname);
        / system script set owner=$scriptname source=$sourcenew \
            dont-require-permissions=$"dont-require-permissions" $script;
      } else={
        :log debug ("Script " .  $scriptname . " did not change");
      }
    } else={
      :log warning ("Looks like new script " . $scriptname . " is not valid. Ignoring!");
    }
  } else={
    :log debug ("No update for script " . $scriptname);
  }
}