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

Adapt Update-Configuration and Update-File to recent changes.

No checks are done in Update-Configuration any more, as the control
logic has been changed to do those beforehand now. Therefore also
Update-File is not comparing the write-times any more nor terminating
the script if something fails (now returning $False instead).

Refers to #18, #28
parent 4c2b7d2a
No related branches found
No related tags found
No related merge requests found
......@@ -200,14 +200,10 @@ function Create-Backup {
function Update-File {
# Check the given $SrcFile if a file with the same name is existing in
# $DstPath. If $SrcFile is newer, stop the service, create a backup of the
# file in $DstPath and finally copy the file from $SrcFile to $DstPath.
# Use the given $SrcFile to update the file with the same name in $DstPath,
# creating a backup of the original file before replacing it.
#
# Return $True if the file was updated, $False otherwise.
#
# WARNING: the function TERMINATES the script on any error!
#
Param (
[Parameter(Mandatory=$True)]
[ValidateScript({[IO.Path]::IsPathRooted($_)})]
......@@ -219,49 +215,44 @@ function Update-File {
)
$DstFile = "$($DstPath)\$(Split-Path -Leaf $SrcFile)"
if (-Not (Test-Path "$DstFile")) {
Log-Info "File not existing in destination, NOT UPDATING: [$($DstFile)]"
Return $False
}
if (File-IsUpToDate -ExistingFile $DstFile -UpdateCandidate $SrcFile) {
Return $False
}
Stop-MyService "Found newer file at $($SrcFile), updating..."
Write-Verbose "Trying to update [$($DstFile)] with [$($SrcFile)]..."
if (-Not (Create-Backup -FileName $DstFile)) {
Return $False
}
try {
Copy-Item -Path $SrcFile -Destination $DstPath -ErrorAction Stop
Copy-Item -Path $SrcFile -Destination $DstPath
Log-Info "Updated config file [$($DstFile)]."
}
catch {
Exit
Log-Error "Copying [$($SrcFile)] FAILED:`n> $($_.Exception.Message)"
Return $False
}
Return $True
}
function Update-Configuration {
$RetOr = $False
# common config files first:
ForEach ($NewConfig in Get-ChildItem $UpdPathConfigCommon) {
$ret = Update-File $NewConfig.FullName $ConfigPath
$RetOr = $RetOr -Or $ret
}
# then host specific config files:
ForEach ($NewConfig in Get-ChildItem $UpdPathConfig) {
$ret = Update-File $NewConfig.FullName $ConfigPath
$RetOr = $RetOr -Or $ret
}
if (-Not ($RetOr)) {
Log-Debug "No (new) configuration file(s) found."
# Update the common and host-specific configuration files with their new
# versions, stopping the service if necessary.
# The function DOES NOT do any checks, it simply runs the necessary update
# commands - meaning everything else (do the files exist, is an update
# required) has to be checked beforehand!!
#
# Return $True if all files were updated successfully.
$NewComm = Join-Path $UpdPathConfig "config.common.xml"
$NewHost = Join-Path $UpdPathConfig "$($env:COMPUTERNAME).xml"
Write-Verbose "Updating configuration files:`n> $($NewComm)`n> $($NewHost)"
Stop-MyService "Updating configuration using files at [$($UpdPathConfig)]."
$Ret = Update-File $NewComm $ConfigPath
# only continue if the first update worked:
if ($Ret) {
$Ret = Update-File $NewHost $ConfigPath
}
Return $RetOr
Return $Ret
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment