Files
eworm-de-routeros-scripts/script-updates
T

108 lines
3.7 KiB
Plaintext
Raw Permalink Normal View History

2018-09-27 00:18:43 +02:00
#!rsc
2018-07-05 15:29:26 +02:00
# RouterOS script: script-updates
2019-01-01 21:19:19 +01:00
# Copyright (c) 2013-2019 Christian Hesse <[email protected]>
2018-07-05 15:29:26 +02:00
#
2018-07-09 16:05:04 +02:00
# update installed scripts from file or url
:global GlobalConfigVersion;
:global ExpectedConfigVersion;
2019-01-03 17:45:43 +01:00
:global Identity;
:global ScriptUpdatesFetch;
:global ScriptUpdatesBaseUrl;
:global ScriptUpdatesUrlSuffix;
:global ScriptUpdatesIgnore;
2018-07-05 15:29:26 +02:00
:global SendNotification;
2019-01-03 17:45:43 +01:00
: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;
2018-08-24 16:14:38 +02:00
}
2018-07-09 16:05:04 +02:00
2019-01-03 17:45:43 +01: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 . "!");
}
}
2019-01-03 17:45:43 +01:00
:if ([ :len $SourceNew ] = 0 && $ScriptUpdatesFetch = true) do={
:foreach IgnoreLoop in=$ScriptUpdatesIgnore do={
:if ($IgnoreLoop = $ScriptName) do={ :set Ignore 1; }
}
2018-07-09 16:05:04 +02:00
2019-01-03 17:45:43 +01:00
:if ($Ignore = 0) do={
:log debug ("Fetching script from url: " . $ScriptName);
2018-09-14 11:10:03 +02:00
:do {
2019-01-03 17:45:43 +01:00
:local Result [ / tool fetch check-certificate=yes-without-crl \
($ScriptUpdatesBaseUrl . $ScriptName . $ScriptUpdatesUrlSuffix) \
2018-09-14 11:10:03 +02:00
output=user as-value ];
2019-01-03 17:45:43 +01:00
:if ($Result->"status" = "finished") do={
:set SourceNew ($Result->"data");
2018-09-14 11:10:03 +02:00
}
} on-error={
2019-01-03 17:45:43 +01:00
:log info ("Failed fetching " . $ScriptName);
2018-07-09 16:05:04 +02:00
}
}
}
2018-07-09 16:05:04 +02:00
2019-01-03 17:45:43 +01: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 DontRequirePermissions \
($SourceNew~"\n# requires: dont-require-permissions=yes\n");
:log info ("Updating script: " . $ScriptName);
/ system script set owner=$ScriptName source=$SourceNew \
dont-require-permissions=$DontRequirePermissions $Script;
:if ($ScriptName = "global-functions") do={
/ system script run global-functions;
}
} else={
2019-01-03 17:45:43 +01:00
:log debug ("Script " . $ScriptName . " did not change.");
}
2018-07-09 16:05:04 +02:00
} else={
2019-01-03 17:45:43 +01:00
:log warning ("Looks like new script " . $ScriptName . " is not valid. Ignoring!");
2018-07-09 16:05:04 +02:00
}
} else={
2019-01-03 17:45:43 +01:00
:log debug ("No update for script " . $ScriptName . ".");
2018-07-05 15:29:26 +02:00
}
}
:if ($GlobalConfigVersion < $ExpectedConfigVersion) do={
:global GlobalConfigChanges;
:local ChangeLogCode;
:local Changes;
:log debug ("Fetching changelog.");
:do {
:local Result [ / tool fetch check-certificate=yes-without-crl \
($ScriptUpdatesBaseUrl . "global-config.changes" . $ScriptUpdatesUrlSuffix) \
output=user as-value ];
:if ($Result->"status" = "finished") do={
:set ChangeLogCode ($Result->"data");
}
} on-error={
:log info ("Failed fetching changes!");
}
[ :parse $ChangeLogCode ];
:for I from=($GlobalConfigVersion + 1) to=$ExpectedConfigVersion do={
:set Changes ( $Changes . "\n * " . $GlobalConfigChanges->[ :tostr $I ] );
}
$SendNotification "Configuration warning!" \
2019-01-03 17:45:43 +01:00
("Current configuration on " . $Identity . " is out of date. " . \
"Please update global-config, then increase variable " . \
"GlobalConfigVersion (currently " . $GlobalConfigVersion . \
") to " . $ExpectedConfigVersion . " and re-run global-config.\n\n" . \
"Changes:" . $Changes);
}