Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
auto-tx
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
vamp
auto-tx
Commits
4dedb522
Commit
4dedb522
authored
7 years ago
by
Niko Ehrenfeuchter
Committed by
Niko Ehrenfeuchter (adm)
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Split Update-FileIfNewer() into smaller functions, validate parameters.
Refers to
#13
parent
f9de68d9
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
AutoTx/Resources/Update-AutoTxService.ps1
+92
-29
92 additions, 29 deletions
AutoTx/Resources/Update-AutoTxService.ps1
with
92 additions
and
29 deletions
AutoTx/Resources/Update-AutoTxService.ps1
+
92
−
29
View file @
4dedb522
...
@@ -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
=
"
$(
$
Src
File
No
Suffix
)
_pre-
$BakTimeStamp
$Src
FileSuffix
"
$BakName
=
"
$(
$File
Without
Suffix
)
_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-
H
os
t
$msg
Write-
Verb
os
e
$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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment