Skip to content
Snippets Groups Projects
Commit b6b1d5cd authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard:
Browse files

Wrap main logic in a try/catch block.

Closes #28
parent cb2fdc91
No related branches found
No related tags found
No related merge requests found
...@@ -569,90 +569,98 @@ Exit-IfDirMissing $UploadPathLogs "log file target" ...@@ -569,90 +569,98 @@ Exit-IfDirMissing $UploadPathLogs "log file target"
# terminates the entire script: # terminates the entire script:
Upload-LogFiles Upload-LogFiles
$UpdItems = @() try {
$ConfigShouldBeUpdated = NewConfig-Available $ConfigPath $UpdItems = @()
$ServiceShouldBeUpdated = ServiceUpdate-Requested $ConfigShouldBeUpdated = NewConfig-Available $ConfigPath
if (-Not ($ConfigShouldBeUpdated -Or $ServiceShouldBeUpdated)) { $ServiceShouldBeUpdated = ServiceUpdate-Requested
Log-Debug "No update action found to be necessary." if (-Not ($ConfigShouldBeUpdated -Or $ServiceShouldBeUpdated)) {
Exit Log-Debug "No update action found to be necessary."
} Exit
}
# define where the configuration is located that should be tested: # define where the configuration is located that should be tested:
$ConfigToTest = $ConfigPath $ConfigToTest = $ConfigPath
if ($ConfigShouldBeUpdated) { if ($ConfigShouldBeUpdated) {
$ConfigToTest = $UpdPathConfig $ConfigToTest = $UpdPathConfig
$UpdItems += "configuration files" $UpdItems += "configuration files"
} }
# define which configuration checker executable to use for testing: # define which configuration checker executable to use for testing:
$ConftestExe = "$($InstallationPath)\AutoTxConfigTest.exe" $ConftestExe = "$($InstallationPath)\AutoTxConfigTest.exe"
if ($ServiceShouldBeUpdated) { if ($ServiceShouldBeUpdated) {
$UpdPackage = Find-InstallationPackage $UpdPackage = Find-InstallationPackage
$ConftestExe = "$($UpdPackage)\$($ServiceName)\AutoTxConfigTest.exe" $ConftestExe = "$($UpdPackage)\$($ServiceName)\AutoTxConfigTest.exe"
$UpdItems += "service binaries" $UpdItems += "service binaries"
} }
# now we're all set and can run the config test: # now we're all set and can run the config test:
$ConfigValid, $ConfigSummary = Config-IsValid $ConftestExe $ConfigToTest $ConfigValid, $ConfigSummary = Config-IsValid $ConftestExe $ConfigToTest
# if we don't have a valid configuration we complain and terminate: # if we don't have a valid configuration we complain and terminate:
if (-Not ($ConfigValid)) { if (-Not ($ConfigValid)) {
Log-Error "Configuration not valid for service, $($Me) terminating!" Log-Error "Configuration not valid for service, $($Me) terminating!"
Send-MailReport -Subject "Update failed, configuration invalid!" ` Send-MailReport -Subject "Update failed, configuration invalid!" `
-Body $("An update action was found to be necessary, however the" -Body $("An update action was found to be necessary, however the"
"configuration didn't`npass the validator.`n`nThe following" "configuration didn't`npass the validator.`n`nThe following"
"summary was generated by the configuration checker:" "summary was generated by the configuration checker:"
"`n`n$($ConfigSummary)") "`n`n$($ConfigSummary)")
Exit Exit
} }
# reaching this point means # reaching this point means
# (1) something needs to be updated (config, service or both) # (1) something needs to be updated (config, service or both)
# AND # AND
# (2) the config validates with the corresponding service version # (2) the config validates with the corresponding service version
Write-Verbose "Required update items:`n> - $($UpdItems -join "`n> - ")`n" Write-Verbose "Required update items:`n> - $($UpdItems -join "`n> - ")`n"
if ($ConfigShouldBeUpdated) { if ($ConfigShouldBeUpdated) {
$ConfigUpdated = Update-Configuration $ConfigUpdated = Update-Configuration
if (-Not $ConfigUpdated) { if (-Not $ConfigUpdated) {
$msg = "Updating the configuration failed, $($Me) terminating!" $msg = "Updating the configuration failed, $($Me) terminating!"
Log-Error $msg Log-Error $msg
Send-MailReport -Subject "updated failed!" -Body $msg Send-MailReport -Subject "updated failed!" -Body $msg
Exit Exit
}
} }
}
if ($ServiceShouldBeUpdated) { if ($ServiceShouldBeUpdated) {
$ServiceUpdated = Update-ServiceBinaries $ServiceUpdated = Update-ServiceBinaries
if (-Not $ServiceUpdated) { if (-Not $ServiceUpdated) {
$msg = "Updating the service binaries failed, $($Me) terminating!" $msg = "Updating the service binaries failed, $($Me) terminating!"
Log-Error $msg Log-Error $msg
Send-MailReport -Subject "updated failed!" -Body $msg Send-MailReport -Subject "updated failed!" -Body $msg
Exit Exit
}
} }
}
$UpdSummary = "Updated $($UpdItems -join " and ")." $UpdSummary = "Updated $($UpdItems -join " and ")."
if ($ServiceRunningBefore) { if ($ServiceRunningBefore) {
Log-Debug "$($UpdSummary) Trying to start the service again..." Log-Debug "$($UpdSummary) Trying to start the service again..."
Start-MyService Start-MyService
} else { } else {
Log-Debug "$($UpdSummary) Leaving the service stopped, as before." Log-Debug "$($UpdSummary) Leaving the service stopped, as before."
} }
$UpdDetails = $("An $($Me) run completed successfully. Updated items:" $UpdDetails = $("An $($Me) run completed successfully. Updated items:"
"`n> - $($UpdItems -join "`n> - ")") "`n> - $($UpdItems -join "`n> - ")")
if ($ConfigUpdated) { if ($ConfigUpdated) {
$UpdDetails += "`n`nConfig validation summary:`n$($ConfigSummary)" $UpdDetails += "`n`nConfig validation summary:`n$($ConfigSummary)"
}
}
catch {
$UpdDetails = $("Unexpected problem, check logs! $($Me) terminating."
"`n`n$($_.Exception.Message)")
$UpdSummary = "ERROR, unhandled problem occurered!"
Log-Error $UpdDetails
} }
Send-MailReport -Subject "$UpdSummary" -Body "$UpdDetails"
Send-MailReport -Subject "$UpdSummary" -Body "$UpdDetails"
Upload-LogFiles Upload-LogFiles
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment