/system script add comment="=SYS-ROS-M9E=" dont-require-permissions=no name=sys-ros-m9e owner=admin policy=ftp,reboot,read,write,policy,test,sniff source="# System Maintenance [ ver. 2.3.5 ]\ \n# 1. Upgrade ROS and Bootloader;\ \n# Require: ROS 6.42 or higher\ \n# Links:\ \n# https://wiki.mikrotik.com/wiki/Manual:Scripting\ \n# https://wiki.mikrotik.com/wiki/Manual:Scripting_Tips_and_Tricks\ \n# Shevchenko Dritry - 29.08.2019 GMT+4 2019\ \n\ \n# 1. Global Variables\ \n# 1.1 Allow ROS update \ \n:global m9eUpdateROS;\ \n:if ( [ :len \$m9eUpdateROS ] = 0 ) do={ :set m9eUpdateROS false }\ \n# 1.2 List of functions\ \n# 1.2.1 Loading functions\ \n:do { /system script run ros-functions } on-error={ \ \n\t:log error ( \"sys-ros-m9e: can't load necessery functions\" );\ \n\t:error \"[ERR]: can't load necessery functions - inconsistent list of scrips\" ;\ \n}\ \n# 1.2.2 Fuctions declaration\ \n:global funcIsFlash;\ \n:global funcCmd2Script;\ \n:global funcROSVersionCompare;\ \n# 2. Local Variables\ \n# 2.1 ROS firmware versions\ \n:local roscurrentfmw [ /system resource get version ];\ \n:if ( \$roscurrentfmw ~\"^[0-9]+\\\\.[0-9]+\\\\.[0-9]+.*\\\\(.*\\\\)\\\$\" ) do={\ \n\t:set roscurrentfmw [ :pick \$roscurrentfmw 0 [ :find \$roscurrentfmw \" \" ] ];\ \n}\ \n:local rosupgradefmw;\ \n:local roscurmajver [ :pick \$roscurrentfmw 0 [ :find \$roscurrentfmw \".\" ] ];\ \n# 2.2 ROS upgrade needed\ \n:local rosupgrade false;\ \n# 2.3 Restart needed\ \n:local rbtFile \"ros-upgrade-once.txt\";\ \n:if ( [ :len [ file find where type=\"disk\" and name=\"flash\" ] ] != 0 ) do={ :set rbtFile \"flash/ros-upgrade-once.txt\" }\ \n# 2.4 Verbose output\ \n:local VERBOSE false;\ \n\ \n# 3. Main\ \n# 3.0 Version check\ \n:if ( [ \$funcROSVersionCompare versionA=\$roscurrentfmw versionB=\"6.42\" ] < 0 ) do={\ \n\t:log error (\"sys-ros-m9e: ros version is less than minimal required\");\ \n\t:if ( \$VERBOSE ) do={ :error \"[ERR]: ROS version is less than minimal required.\" }\ \n}\ \n# 3.1 Bootloader firmware auto-upgrade\ \n:local rbdcurrentupd [ /system routerboard settings get auto-upgrade ];\ \n\ \n# 3.2 Upgrade proccess\ \n# 3.2.1 Check if new firmware availabale\ \n/system package update check-for-updates once;\ \n:delay 10s; \ \n:set rosupgradefmw [ /system package update get latest-version ];\ \n\ \n:if ( \$roscurrentfmw != \$rosupgradefmw ) do={\ \n\t:if ( \$m9eUpdateROS ) do= { \ \n\t\t:if ( \$VERBOSE ) do={ :put \"WRN: Upgrading RouterOS firmware\" }\ \n\t\t:set rosupgrade true;\ \n\t\t:set m9eUpdateROS false;\ \n\t} else={\ \n\t\t:if ( \$VERBOSE ) do={ :put \"WRN: RouterOS upgrades avaliable but disabled\" }\ \n\t\tlog info ( \"sys-ros-m9e: new ros firmware avaliable but upgrade disabled\" );\ \n\t}\ \n} else={\ \n\t:if ( \$VERBOSE ) do={ :put \"[MSG]: RouterOS firmware is up to date\" }\ \n\tlog info ( \"sys-ros-m9e: ros firmware is up to date\" );\ \n}\ \n# 3.2.2 Upgrade process\ \n:if ( \$rosupgrade ) do={\ \n\t:do {\ \n\t\tlog warning ( \"sys-ros-m9e: upgrading ros firmware from \$roscurrentfmw to \$rosupgradefmw\" );\ \n\t\tlog warning ( \"sys-ros-m9e: system will be rebooted\" );\ \n\t\t:if ( \$rbdcurrentupd ) do={\ \n\t\t\t:local command ( \"/file print file=\" . \$rbtFile );\ \n\t\t\t\$funcCmd2Script cmd=\$command\ \n\t\t} else={\ \n\t\t\t:if ( \$VERBOSE ) do={ :put \"WRN: You should enable routerboard firmware auto-upgrade\" }\ \n\t\t\tlog warn ( \"sys-ros-m9e: you should enable rbd firmware auto-upgrade\" )\ \n\t\t}\ \n\t\t/system package update install;\ \n\t\tdelay 240s;\ \n\t\t:if ( \$VERBOSE ) do={ :put \"[MSG]: RouterOS firmware upgraded\" }\ \n\t} on-error={\ \n\t\t:if ( \$VERBOSE ) do={ :put \"[ERR]: RouterOS firmware upgrade failed!\" }\ \n\t\tlog error ( \"sys-ros-m9e: ros firmware upgrade failed, reboot system\" )\ \n\t\t/system reboot;\ \n\t}\ \n};\ \n# 3.3 Environment cleanup\ \n:do { /system script environment remove [ find name~\"func\" ] } on-error={\ \n\t:log warning ( \"sys-ros-m9e: script environment cleanup is failed\" );\ \n};\ \n# 4. End" /system scheduler add comment="System mainteinance" interval=1d name=sys-m9e on-event=sys-ros-m9e policy=ftp,reboot,read,write,policy,test,sniff start-date=sep/10/2019 start-time=00:30:00