Community discussions

MikroTik App
 
PackElend
Member Candidate
Member Candidate
Topic Author
Posts: 269
Joined: Tue Sep 29, 2020 6:05 pm

fetch (and deploy) only if source is newer

Wed May 04, 2022 1:16 pm

Hello MTs,
is there any possibility to fetch & deploy only if the script is updated at the source?
I could not find anything.

The only thing I can think of is to use version numbers in a master script, which contains the path to the scripts.
Only if the version number is higher than mine, fetch and convert to script (and maybe deploy it).

Maybe REST API could be used to check if the script at the source is newer than the local one but it looks like I cannot restrict REST API to read-only, https://help.mikrotik.com/docs/display/ ... Properties leaves me clueless (I reckon I have to create a custom skin)
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: fetch (and deploy) only if source is newer

Wed May 04, 2022 6:25 pm

That is something I have been struggling with as well... Did not find a suitable solution so far.
I thought about generating a kind of manifest containing script names and checksums - but there is nothing within RouterOS to generate something like a checksum.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: fetch (and deploy) only if source is newer

Wed May 04, 2022 6:36 pm

With /tool fetch you can not make "HTTP DATE" request, only get, put, post, etc.

You can put at the start of the remote script something like "# 2022-05-04 17:30" and read from RouterBOARD just the first 18 Bytes (with my fetch method),
(and check later if the downloaded file is complete, reading correctly, at the end, someting like "# SCRIPTEND #")
 
PackElend
Member Candidate
Member Candidate
Topic Author
Posts: 269
Joined: Tue Sep 29, 2020 6:05 pm

Re: fetch (and deploy) only if source is newer

Wed May 04, 2022 10:50 pm

or have a simple instructor or dictionary script
  1. instructor
    a little script, which does the actual pull, keep this up to date, what is fetch maybe every minute (script can alter this as well) but how to deal with if, if you have multiple clients?
  2. dictionary
    a list of scripts to pull including their latest date and/or version (would prevent pull only due to fact that destination and source have always a time difference due to the pooling frequency) .
    Compare these values with values in the local database, if they differ pull it.
    It could even contain flags like "update only", "execute" etc,
or a mix of both
 
KeithTrundle
just joined
Posts: 2
Joined: Thu Nov 21, 2013 9:18 pm

Re: fetch (and deploy) only if source is newer

Fri May 13, 2022 10:08 pm

Since the firmware version comes in as text I guess. ie: 6.42.3 if it's an exact match you can do a Boolean if then true/false.
This isn't very helpful when a value can be less than greater than or equal to.
If you import the value as a string:
:local currentFirmware [:tostr [/system routerboard get current-firmware]]
You can do a simple if statement to compare the $currentFirmware to the scripted value.

{
:local expectedFirmwareVersion "6.47.4"
:local currentFirmware [:tostr [/system routerboard get current-firmware]]
:if ($currentFirmware < $expectedFirmwareVersion) do= {
:put "Firmware Upgrade Necessary"}
}

And of course manipulate the argument and values as necessary to fir your needs...
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: fetch (and deploy) only if source is newer

Sat May 14, 2022 12:08 am

On RouterOS you cannot compare if a string is >, <, <= or => against another string.
You can just do = and !=
For a bug => does not give an error, but acts exactly like =
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: fetch (and deploy) only if source is newer

Sat May 14, 2022 12:54 am

Not sure why the topic switched to comparing software and firmware version (strings)... It it kind of unrelated to the initial topic.

Anyway... I wrote a function that converts the version string to a number: VersionToNum
With that you can compare versions.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: fetch (and deploy) only if source is newer

Sat May 14, 2022 1:12 am

I'm really curious to understand how internally string variables are managed...

until there is not letters (beta, rc, etc.) and symbols:

terminal code

[rex@tended] > :if ("7.1.2" < "6.50.5") do={ :put "!"}
[rex@tended] > :if ("7.1.2" <= "6.50.5") do={ :put "!"}
[rex@tended] > :if ("7.1.2" = "6.50.5") do={ :put "!"}
[rex@tended] > :if ("7.1.2" => "6.50.5") do={ :put "!"}
[rex@tended] > :if ("7.1.2" > "6.50.5") do={ :put "!"}
!
[rex@tended] > :if ("7.1.2" != "6.50.5") do={ :put "!"}
!
[rex@tended] > 
results <, <= and > are wrong (and => is not congruent if > is right), because when comparing two strings of different length, the shorter is always the minor...
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: fetch (and deploy) only if source is newer  [SOLVED]

Sat May 14, 2022 1:22 am

Not sure why the topic switched to comparing software and firmware version (strings)... It it kind of unrelated to the initial topic.
With /tool fetch you can not make "HTTP DATE" request.
With REST API you can call a GET and read the date of the file, but the source must be a RouterOS with v7.1.x
:local scriptdate (([/tool fetch user=rex password=tended as-value output=user http-method=get \
    url="https://192.168.88.1/rest/file?name=disk1/scriptfolder/script.rsc&.proplist=creation-time"])->"data")
After comparing the last version with downloadable version, download if the remote script is newer and run it.

Obviously if on remote server is implemented a .php server or similar, is possible to create .php page where fetch can get any info from remote server.

This is the solution of OP.

And because that, we try to find a solution for RouterOS v6 sources, like put version number inside the script (or on separate file companion) where read the first x bytes,
or the file companion content, and compare the value internally with latest version applied. If different, read new file and apply it.
Or not?
 
PackElend
Member Candidate
Member Candidate
Topic Author
Posts: 269
Joined: Tue Sep 29, 2020 6:05 pm

Re: fetch (and deploy) only if source is newer

Sun May 15, 2022 10:06 pm

:local scriptdate (([/tool fetch user=rex password=tended as-value output=user http-method=get \
    url="https://192.168.88.1/rest/file?name=disk1/scriptfolder/script.rsc&.proplist=creation-time"])->"data")
hey, thanks a lot.
That reduces load but as scripts fatching isn't a real load, I would rather say, it reduces the noise on the network :)
That is the best we can get, as long as ROS does not react to changes on a script.

The only thing to keep in mind, even if the filels(script source) at the source and at the destination are the same, the won't have the same timestamp, as it there is only local creation date.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: fetch (and deploy) only if source is newer

Mon May 16, 2022 2:30 am

Is why the date/time must be checked only on the remote source and value stored on local variables.
Reading local file creation date/time, is always different.

Who is online

Users browsing this forum: marcelofares and 26 guests