routeros-scripts/script-updates

74 lines
2.6 KiB
Text
Raw Normal View History

#!rsc
2018-07-05 13:29:26 +00:00
# RouterOS script: script-updates
2019-01-01 20:19:19 +00:00
# Copyright (c) 2013-2019 Christian Hesse <mail@eworm.de>
2018-07-05 13:29:26 +00:00
#
2018-07-09 14:05:04 +00:00
# update installed scripts from file or url
:global "script-updates-fetch";
:global "script-updates-baseurl";
:global "script-updates-urlsuffix";
2018-07-09 14:05:04 +00:00
:global "script-updates-ignore";
2018-07-05 13:29:26 +00:00
:foreach script in=[ / system script find ] do={
2018-07-09 14:05:04 +00:00
:local ignore 0;
2018-07-05 13:29:26 +00:00
:local scriptname [ / system script get $script name ];
:local scriptpolicy [ / system script get $script policy ];
:local scriptfile [ / file find where name=("script-updates/" . $scriptname) ];
2018-08-24 14:14:38 +00:00
:local sourcenew;
:if ([ :len $scriptfile ] > 0) do={
:set sourcenew [ / file get $scriptfile content ];
/ file remove $scriptfile;
}
2018-07-09 14:05:04 +00:00
: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 . "!");
}
}
2018-08-24 14:14:38 +00:00
: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; }
}
2018-07-09 14:05:04 +00:00
: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);
2018-07-09 14:05:04 +00:00
}
}
}
2018-07-09 14:05:04 +00:00
2018-08-24 14:14:38 +00:00
: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;
:if ($scriptname = "global-functions") do={
/ system script run global-functions;
}
} else={
:log debug ("Script " . $scriptname . " did not change");
}
2018-07-09 14:05:04 +00:00
} else={
:log warning ("Looks like new script " . $scriptname . " is not valid. Ignoring!");
2018-07-09 14:05:04 +00:00
}
} else={
:log debug ("No update for script " . $scriptname);
2018-07-05 13:29:26 +00:00
}
}