Refactor email body handling for clarity and consistency; update variable names for improved readability

This commit is contained in:
Alexander Tebiev 2025-04-11 11:40:11 +02:00
parent defbd813c6
commit be3aa8fd11

49
v3.rsc
View file

@ -48,7 +48,7 @@
## Means that new update will be installed only if MAJOR and MINOR version numbers remained the same as currently installed RouterOS. ## Means that new update will be installed only if MAJOR and MINOR version numbers remained the same as currently installed RouterOS.
## Example: v6.43.6 => major.minor.PATCH ## Example: v6.43.6 => major.minor.PATCH
## Script will send information if new version is greater than just patch. ## Script will send information if new version is greater than just patch.
:local installOnlyPatchUpdates false :local installOnlyPatchUpdates true
## If true, device public IP address information will be included into the email message ## If true, device public IP address information will be included into the email message
:local detectPublicIpAddress true :local detectPublicIpAddress true
@ -452,7 +452,6 @@
## Email body template ## Email body template
:local mailSubjectPrefix "$SMP Device - `$deviceIdentityNameShort`" :local mailSubjectPrefix "$SMP Device - `$deviceIdentityNameShort`"
:local mailBodyMessage ""
:local mailBodyCopyright "Mikrotik RouterOS automatic backup & update (ver. $scriptVersion) \nhttps://github.com/beeyev/Mikrotik-RouterOS-automatic-backup-and-update" :local mailBodyCopyright "Mikrotik RouterOS automatic backup & update (ver. $scriptVersion) \nhttps://github.com/beeyev/Mikrotik-RouterOS-automatic-backup-and-update"
:local changelogUrl "Check RouterOS changelog: https://mikrotik.com/download/changelogs/" :local changelogUrl "Check RouterOS changelog: https://mikrotik.com/download/changelogs/"
@ -521,7 +520,7 @@
:local isEmailNeedsToBeSent false :local isEmailNeedsToBeSent false
:local mailSubjectPartAction "" :local mailSubjectPartAction ""
:local mailBodyPartAction "" :local mailPtBodyAction ""
:local mailPtSubjectBackup "" :local mailPtSubjectBackup ""
:local mailPtBodyBackup "" :local mailPtBodyBackup ""
@ -549,20 +548,20 @@
:set isEmailNeedsToBeSent true :set isEmailNeedsToBeSent true
:set mailSubjectPartAction "New RouterOS available" :set mailSubjectPartAction "New RouterOS available"
:set mailBodyPartAction "New RouterOS version is available, current version: `$runningOsVersion`, new version: `$routerOsVersionAvailable`. \n$changelogUrl" :set mailPtBodyAction "New RouterOS version is available, current version: v$runningOsVersion, new version: v$routerOsVersionAvailable. \n$changelogUrl"
} else={ } else={
:if ($packageUpdateStatus = "System is already up to date") do={ :if ($packageUpdateStatus = "System is already up to date") do={
:log info ("$SMP No new RouterOS version is available, the latest version is already installed: `v$runningOsVersion`") :log info ("$SMP No new RouterOS version is available, the latest version is already installed: `v$runningOsVersion`")
:set isUpdateCheckSucceeded true :set isUpdateCheckSucceeded true
:set mailSubjectPartAction "No os update available" :set mailSubjectPartAction "No os update available"
:set mailBodyPartAction "No new RouterOS version is available, the latest version is already installed: `v$runningOsVersion`" :set mailPtBodyAction "No new RouterOS version is available, the latest version is already installed: `v$runningOsVersion`"
} else={ } else={
:log error ("$SMP Failed to check for new RouterOS version. Package check status: `$packageUpdateStatus`") :log error ("$SMP Failed to check for new RouterOS version. Package check status: `$packageUpdateStatus`")
:set isEmailNeedsToBeSent true :set isEmailNeedsToBeSent true
:set mailSubjectPartAction "Error unable to check new os version" :set mailSubjectPartAction "Error unable to check new os version"
:set mailBodyPartAction "An error occurred while checking for a new RouterOS version.\nStatus returned: `$packageUpdateStatus`\n\nPlease review the logs on the device for more details and verify internet connectivity." :set mailPtBodyAction "An error occurred while checking for a new RouterOS version.\nStatus returned: `$packageUpdateStatus`\n\nPlease review the logs on the device for more details and verify internet connectivity."
} }
} }
} }
@ -571,11 +570,11 @@
:if ($scriptMode = "osupdate" and $isNewOsUpdateAvailable = true) do={ :if ($scriptMode = "osupdate" and $isNewOsUpdateAvailable = true) do={
:if ($installOnlyPatchUpdates = true) do={ :if ($installOnlyPatchUpdates = true) do={
:if ([$FuncIsPatchUpdateOnly $runningOsVersion $routerOsVersionAvailable] = true) do={ :if ([$FuncIsPatchUpdateOnly $runningOsVersion $routerOsVersionAvailable] = true) do={
:log info ("$SMP New RouterOS version is available, and it is a patch update. Current version: `$runningOsVersion`, new version: `$routerOsVersionAvailable`.") :log info "$SMP New RouterOS version is available, and it is a patch update. Current version: v$runningOsVersion, new version: v$routerOsVersionAvailable"
:set isOsNeedsToBeUpdated true :set isOsNeedsToBeUpdated true
} else={ } else={
:log info ("$SMP The script will not install this update, because it is not a patch update. Current version: `$runningOsVersion`, new version: `$routerOsVersionAvailable`.") :log info "$SMP The script will not install this update, because it is not a patch update. Current version: v$runningOsVersion, new version: v$routerOsVersionAvailable"
:set mailBodyPartAction ($mailBodyPartAction . "\nThis update will not be installed, because the script is set to install only patch updates.") :set mailPtBodyAction ($mailPtBodyAction . "\nThis update will not be installed, because the script is set to install only patch updates.")
} }
} else={ } else={
:set isOsNeedsToBeUpdated true :set isOsNeedsToBeUpdated true
@ -597,10 +596,10 @@
#Email body if the purpose of the script is to update the device #Email body if the purpose of the script is to update the device
:set mailSubjectPartAction "Update preparation" :set mailSubjectPartAction "Update preparation"
:set mailBodyPartAction ($mailBodyPartAction . "\nThe update process for device '$deviceIdentityName' is scheduled to upgrade RouterOS from version v.$runningOsVersion to version v.$routerOsVersionAvailable (Update channel: $updateChannel)") :set mailPtBodyAction ($mailPtBodyAction . "\nThe update process for device '$deviceIdentityName' is scheduled to upgrade RouterOS from version v.$runningOsVersion to version v.$routerOsVersionAvailable (Update channel: $updateChannel)")
:set mailBodyPartAction ($mailBodyPartAction . "\nPlease note: The update will proceed only after a successful backup.") :set mailPtBodyAction ($mailPtBodyAction . "\nPlease note: The update will proceed only after a successful backup.")
:set mailBodyPartAction ($mailBodyPartAction . "\nA final report with detailed information will be sent once the update process is completed.") :set mailPtBodyAction ($mailPtBodyAction . "\nA final report with detailed information will be sent once the update process is completed.")
:set mailBodyPartAction ($mailBodyPartAction . "\nIf you do not receive a second email within the next 10 minutes, there may be an issue. Please check your device logs for further information.") :set mailPtBodyAction ($mailPtBodyAction . "\nIf you do not receive a second email within the next 10 minutes, there may be an issue. Please check your device logs for further information.")
} }
:do { :do {
@ -622,27 +621,28 @@
:if ($isEmailNeedsToBeSent = true) do={ :if ($isEmailNeedsToBeSent = true) do={
:log info "$SMP Preparing to send email..." :log info "$SMP Preparing to send email..."
:local mailSubject $mailSubjectPrefix :local mailStep1Subject $mailSubjectPrefix
:local mailStep1Body ""
# Assemble email subject # Assemble email subject
:if ($mailSubjectPartAction != "") do={ :if ($mailSubjectPartAction != "") do={
:set mailSubject ($mailSubject . " - " . $mailSubjectPartAction) :set mailStep1Subject ($mailStep1Subject . " - " . $mailSubjectPartAction)
} }
:if ($mailPtSubjectBackup != "") do={ :if ($mailPtSubjectBackup != "") do={
:set mailSubject ($mailSubject . " - " . $mailPtSubjectBackup) :set mailStep1Subject ($mailStep1Subject . " - " . $mailPtSubjectBackup)
} }
# Assemble email body # Assemble email body
:if ($mailBodyPartAction != "") do={ :if ($mailPtBodyAction != "") do={
:set mailBodyMessage ($mailBodyMessage . $mailBodyPartAction . "\n\n") :set mailStep1Body ($mailStep1Body . $mailPtBodyAction . "\n\n")
} }
:if ($mailPtBodyBackup != "") do={ :if ($mailPtBodyBackup != "") do={
:set mailBodyMessage ($mailBodyMessage . $mailPtBodyBackup . "\n\n") :set mailStep1Body ($mailStep1Body . $mailPtBodyBackup . "\n\n")
} }
:set mailBodyMessage ($mailBodyMessage . $mailBodyDeviceInfo . "\n\n" . $mailBodyCopyright) :set mailStep1Body ($mailStep1Body . $mailBodyDeviceInfo . "\n\n" . $mailBodyCopyright)
# Send email with backup files attached # Send email with backup files attached
:do { :do {
$FuncSendEmailSafe $emailAddress $mailSubject $mailBodyMessage $mailAttachments $FuncSendEmailSafe $emailAddress $mailStep1Subject $mailStep1Body $mailAttachments
} on-error={ } on-error={
:set isOsNeedsToBeUpdated false :set isOsNeedsToBeUpdated false
:log error "$SMP The script will not proceed with the update process, because the email was not sent." :log error "$SMP The script will not proceed with the update process, because the email was not sent."
@ -663,7 +663,12 @@
} on-error={ } on-error={
:log error "$SMP Failed to install new RouterOS version. Please check device logs for more details." :log error "$SMP Failed to install new RouterOS version. Please check device logs for more details."
:set mailBodyMessage ($mailBodyMessage . "\n\nUpdate process failed. Please check device logs for more details.") :local mailUpdateErrorSubject ($mailSubjectPrefix . " - Update failed")
:local mailUpdateErrorBody "The script was unable to install new RouterOS version. Please check device logs for more details."
# Send email with error message
$FuncSendEmailSafe $emailAddress $mailUpdateErrorSubject $mailUpdateErrorBody ""
:error $exitErrorMessage :error $exitErrorMessage
} }
} }