script-updates: support fetch from url

This commit is contained in:
Christian Hesse 2018-07-09 16:05:04 +02:00
parent 9d498a60bd
commit 56e641a980
2 changed files with 46 additions and 9 deletions

View file

@ -37,6 +37,12 @@
# ntp settings. A pool can rotate servers. # ntp settings. A pool can rotate servers.
:global "ntp-pool" "pool.ntp.org"; :global "ntp-pool" "pool.ntp.org";
# Enable this to fetch scripts from given url.
:global "script-updates-fetch" false;
:global "script-updates-baseurl" "https://git.eworm.de/cgit.cgi/routeros-scripts/plain/";
:global "script-updates-ignore" { "global-config"; "GeneratePSK" }
:global "script-updates-fetch-ignore" { "" };
# Do *NOT* change these! # Do *NOT* change these!
:global "sent-routeros-update-notification" false; :global "sent-routeros-update-notification" false;
:global "identity" [ / system identity get name ]; :global "identity" [ / system identity get name ];

View file

@ -1,17 +1,48 @@
# RouterOS script: script-updates # RouterOS script: script-updates
# Copyright (c) 2013-2018 Christian Hesse <mail@eworm.de> # Copyright (c) 2013-2018 Christian Hesse <mail@eworm.de>
# #
# update installed scripts from file # update installed scripts from file or url
:global "script-updates-fetch";
:global "script-updates-baseurl";
:global "script-updates-ignore";
:global "script-updates-fetch-ignore";
:foreach script in=[ / system script find ] do={ :foreach script in=[ / system script find ] do={
:local ignore 0;
:local scriptname [ / system script get $script name ]; :local scriptname [ / system script get $script name ];
:foreach "ignore-loop" in=$"script-updates-ignore" do={
:if ($"ignore-loop" = $scriptname) do={ :set ignore 1; }
}
:if ($ignore = 0) do={
:local scriptfile [ / file find where name=("script-updates/" . $scriptname) ]; :local scriptfile [ / file find where name=("script-updates/" . $scriptname) ];
:if ([ :len $scriptfile ] = 0 && $"script-updates-fetch" = true) do={
:foreach "ignore-loop" in=$"script-updates-fetch-ignore" do={
:if ($"ignore-loop" = $scriptname) do={ :set ignore 1; }
}
:if ($ignore = 0) do={
:log debug ("Fetching script from url: " . $scriptname);
/ tool fetch dst-path=("script-updates/" . $scriptname) ($"script-updates-baseurl" . $scriptname);
:set scriptfile [ / file find where name=("script-updates/" . $scriptname) ];
}
}
:if ([ :len $scriptfile ] > 0) do={ :if ([ :len $scriptfile ] > 0) do={
:local filecontent [ / file get $scriptfile content ];
:local scriptsource [ / system script get $script source ];
:if ($filecontent = $scriptsource) do={
:log debug ("Script " . $scriptname . " did not change");
} else={
:log info ("Updating script: " . $scriptname); :log info ("Updating script: " . $scriptname);
:local scriptcontent [ / file get $scriptfile content ]; / system script set owner=$scriptname source=$filecontent $script;
/ system script set owner=$scriptname source=$scriptcontent $script; }
/ file remove $scriptfile; / file remove $scriptfile;
} else={ } else={
:log debug ("No update for script " . $scriptname); :log debug ("No update for script " . $scriptname);
} }
}
} }