Skip to content
Snippets Groups Projects
Commit 4dedb522 authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard: Committed by Niko Ehrenfeuchter (adm)
Browse files

Split Update-FileIfNewer() into smaller functions, validate parameters.

Refers to #13
parent f9de68d9
No related branches found
No related tags found
No related merge requests found
...@@ -83,56 +83,119 @@ function Start-MyService { ...@@ -83,56 +83,119 @@ function Start-MyService {
} }
function Update-FileIfNewer([string]$SourcePath, [string]$Destination) { function Get-WriteTime([string]$FileName) {
# SourcePath is expected to be a FILE (full path) try {
# Destination is expected to be a DIRECTORY (full path) $TimeStamp = (Get-Item "$FileName").LastWriteTime
$SrcDir = Split-Path $SourcePath -Parent }
$SrcFile = Split-Path $SourcePath -Leaf catch {
$SrcFileNoSuffix = [io.path]::GetFileNameWithoutExtension($SrcFile) $ex = $_.Exception.Message
$SrcFileSuffix = [io.path]::GetExtension($SrcFile) Log-Error "Error determining file age of '$($FileName)'!`n$($ex)"
$DstPath = "$($Destination)\$($SrcFile)" Exit
if (-Not (Test-Path "$DstPath")) {
Log-Info "File not existing in destination, NOT UPDATING: $($DstPath)"
Return
} }
Return $TimeStamp
}
$SrcWriteTime = (Get-Item "$SourcePath").LastWriteTime
$TgtWriteTime = (Get-Item "$DstPath").LastWriteTime function File-IsUpToDate([string]$ExistingFile, [string]$UpdateCandidate) {
if (-Not ($SrcWriteTime -gt $TgtWriteTime)) { # Compare write-timestamps, return $False if $UpdateCandidate is newer.
Return Write-Verbose "Comparing $($UpdateCandidate) vs. $($ExistingFile)..."
$CandidateTime = Get-WriteTime -FileName $UpdateCandidate
$ExistingTime = Get-WriteTime -FileName $ExistingFile
if ($CandidateTime -le $ExistingTime) {
Write-Verbose "File $($ExistingFile) is up-to-date."
Return $True
} }
Log-Info -Message "Found newer file at $($SourcePath), updating..." Return $False
Stop-MyService }
function Create-Backup {
Param (
[Parameter(Mandatory=$True)]
[ValidateScript({Test-Path -PathType Leaf $_})]
[String]$FileName
)
$FileWithoutSuffix = [io.path]::GetFileNameWithoutExtension($FileName)
$FileSuffix = [io.path]::GetExtension($FileName)
$BaseDir = Split-Path -Parent $FileName
# assemble a timestamp string like "2017-12-04T16.41.35" # assemble a timestamp string like "2017-12-04T16.41.35"
$BakTimeStamp = Get-Date -Format s | foreach {$_ -replace ":", "."} $BakTimeStamp = Get-Date -Format s | foreach {$_ -replace ":", "."}
$BakName = "$($SrcFileNoSuffix)_pre-$BakTimeStamp$SrcFileSuffix" $BakName = "$($FileWithoutSuffix)_pre-$($BakTimeStamp)$($FileSuffix)"
Log-Info "Creating backup of '$($DstPath)' to '$($BakName)'." Log-Info "Creating backup of '$($FileName)' as '$($BaseDir)\$($BakName)'."
try { try {
Rename-Item "$DstPath" "$Destination\$BakName" -ErrorAction Stop Rename-Item "$FileName" "$BaseDir\$BakName" -ErrorAction Stop
} }
catch { catch {
$ex = $_.Exception.Message $ex = $_.Exception.Message
Log-Error "Backing up '$($DstPath)' as '$($BakName) FAILED!`n$($ex)" Log-Error "Backing up '$($FileName)' as '$($BakName) FAILED!`n$($ex)"
Exit Exit
} }
}
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.
#
# 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($_)})]
[String]$SrcFile,
[Parameter(Mandatory=$True)]
[ValidateScript({(Get-Item $_).PSIsContainer})]
[String]$DstPath
)
$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
}
Log-Info "Found newer file at $($SrcFile), updating..."
Stop-MyService
try { try {
Copy-Item -Path $SourcePath -Destination $Destination -ErrorAction Stop Create-Backup -FileName $DstFile
Log-Info "Updated config file '$($DstPath)'."
} }
catch { catch {
$ex = $_.Exception.Message Log-Error "Backing up $($DstFile) FAILED!`n$($_.Exception.Message)"
Log-Error "Copying $($SourcePath) FAILED!`n$($ex)" Exit
}
try {
Copy-Item -Path $SrcFile -Destination $DstPath -ErrorAction Stop
Log-Info "Updated config file '$($DstFile)'."
}
catch {
Log-Error "Copying $($SrcFile) FAILED!`n$($_.Exception.Message)"
Exit Exit
} }
Return $True
} }
function Update-Configuration { function Update-Configuration {
$NewConfig = "$($UpdateConfigPath)\configuration.xml" $NewConfig = "$($UpdateConfigPath)\configuration.xml"
if (Test-Path -PathType Leaf $NewConfig) { if (Test-Path -PathType Leaf $NewConfig) {
Update-FileIfNewer $NewConfig $InstallationPath $ret = Update-File $NewConfig $InstallationPath
} else {
$ret = $False
Write-Verbose "No configuration file found at '$($NewConfig)'."
} }
Return $ret
} }
...@@ -216,7 +279,7 @@ function Log-Message([string]$Type, [string]$Message, [int]$Id){ ...@@ -216,7 +279,7 @@ function Log-Message([string]$Type, [string]$Message, [int]$Id){
$msg += "--- Log Message ---`n$($Message)`n--- Log Message ---`n" $msg += "--- Log Message ---`n$($Message)`n--- Log Message ---`n"
$msg += "--- Exception ---`n$($ex)`n--- Exception ---" $msg += "--- Exception ---`n$($ex)`n--- Exception ---"
} }
Write-Host $msg Write-Verbose $msg
} }
...@@ -246,7 +309,7 @@ Exit-IfDirMissing $UpdateConfigPath "configuration update" ...@@ -246,7 +309,7 @@ Exit-IfDirMissing $UpdateConfigPath "configuration update"
Exit-IfDirMissing $UpdateMarkerPath "update marker" Exit-IfDirMissing $UpdateMarkerPath "update marker"
Exit-IfDirMissing $UpdateBinariesPath "service binaries update" Exit-IfDirMissing $UpdateBinariesPath "service binaries update"
Update-Configuration $ConfigUpdated = Update-Configuration
Update-ServiceBinaries # Update-ServiceBinaries
Start-MyService Start-MyService
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