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

First version of an automatic update script for service and config.

Refers to #13
parent a5039946
No related branches found
No related tags found
No related merge requests found
# helper script to be run by the task scheduler for automatic updates of the
# AutoTx service binaries and configuration file
# ------------------------------------------------------------------------------
$InstallationPath = "C:\Tools\AutoTx"
$ServiceName = "AutoTx"
$UpdateSourcePath = "C:\ATX\TARGET\d-vamp-dw\_AUTOTX_"
# ------------------------------------------------------------------------------
function Check-ServiceState([string]$ServiceName) {
$Continue = $True
try {
$Service = Get-Service $ServiceName -ErrorAction Stop
if ($Service.Status -ne "Running") {
Write-Host "Service $($ServiceName) is not running."
$Continue = $False
}
}
catch {
Write-Host $_.Exception.Message
$Continue = $False
}
if ($Continue) {
Return
}
Write-Host "ERROR: Service $($ServiceName) must be installed and running."
Exit
}
function Exit-IfDirMissing([string]$DirName, [string]$Desc) {
if (Test-Path -PathType Container $DirName) {
Return
}
Write-Host "ERROR: can't find / access $($Desc) path: $($DirName)"
Exit
}
function Stop-MyService {
Write-Host -NoNewLine "Stopping service $($ServiceName): "
try {
Stop-Service $ServiceName -ErrorAction Stop
Write-Host "[OK]" -Fore Green
}
catch {
Write-Host "[FAILED]" -Fore Red
Exit
}
}
function Start-MyService {
if ((Get-Service $ServiceName).Status -eq "Running") {
Return
}
Write-Host -NoNewLine "Starting service $($ServiceName): "
try {
Start-Service $ServiceName -ErrorAction Stop
Write-Host "[OK]" -Fore Green
}
catch {
$ex = $_.Exception
Write-Host "[FAILED]" -Fore Red
Write-Host $ex.Message
}
}
function Update-FileIfNewer([string]$SourcePath, [string]$Destination) {
# SourcePath is expected to be a FILE (full path)
# Destination is expected to be a DIRECTORY (full path)
$SrcDir = Split-Path $SourcePath -Parent
$SrcFile = Split-Path $SourcePath -Leaf
$SrcFileNoSuffix = [io.path]::GetFileNameWithoutExtension($SrcFile)
$SrcFileSuffix = [io.path]::GetExtension($SrcFile)
$DstPath = "$($Destination)\$($SrcFile)"
if (-Not (Test-Path "$DstPath")) {
Write-Host "File not existing in destination, NOT UPDATING: $DstPath"
Return
}
$SrcWriteTime = (Get-Item "$SourcePath").LastWriteTime
$TgtWriteTime = (Get-Item "$DstPath").LastWriteTime
if (-Not ($SrcWriteTime -gt $TgtWriteTime)) {
Return
}
Write-Host "Found newer file at $($SourcePath), updating..."
Stop-MyService
$BakTimeStamp = Get-Date -Format s | foreach {$_ -replace ":", "."}
$BakName = "$($SrcFileNoSuffix)_pre-$BakTimeStamp$SrcFileSuffix"
Write-Host "Creating backup: $($BakName)"
Rename-Item "$DstPath" "$Destination\$BakName"
try {
Copy-Item -Path $SourcePath -Destination $Destination -ErrorAction Stop
}
catch {
$ex = $_.Exception
Write-Host "Copying $($SourcePath) FAILED!" -Fore Red
Write-Host $ex.Message
Exit
}
}
function Update-ConfigFile {
$NewConfig = "$($UpdateConfigPath)\configuration.xml"
if (Test-Path -PathType Leaf $NewConfig) {
Update-FileIfNewer $NewConfig $InstallationPath
}
}
function Copy-ServiceFiles {
Write-Host -NoNewLine "Updating service binaries: "
try {
Copy-Item -Recurse -Force -ErrorAction Stop `
-Path "$UpdateBinariesPath" `
-Destination "$InstallationPath"
# Copy-FileIfNew "configuration.xml" $ServiceDir
# Copy-FileIfNew "status.xml" $ServiceDir
# Copy-FileIfNew "service.log" $ServiceDir
# Clear-Content "$($ServiceDir)\service.log"
Write-Host "[OK]" -Fore Green
}
catch {
$ex = $_.Exception
Write-Host "[FAILED]" -Fore Red
Write-Host $ex.Message
Exit
}
}
function Update-ServiceBinaries {
$MarkerFile = "$($UpdateMarkerPath)\$($env:COMPUTERNAME)"
if (Test-Path "$MarkerFile" -Type Leaf) {
Return
}
Write-Host "No marker file found, trying to update service binaries..."
Stop-MyService
Copy-ServiceFiles
New-Item -Type File "$MarkerFile" | Out-Null
}
# first check if the service is installed and running at all
Check-ServiceState $ServiceName
$UpdateConfigPath = "$($UpdateSourcePath)\Configs\$($env:COMPUTERNAME)"
$UpdateMarkerPath = "$($UpdateSourcePath)\Service\UpdateMarkers"
$UpdateBinariesPath = "$($UpdateSourcePath)\Service\LatestBinaries"
Exit-IfDirMissing $InstallationPath "installation"
Exit-IfDirMissing $UpdateSourcePath "update source"
Exit-IfDirMissing $UpdateConfigPath "configuration update"
Exit-IfDirMissing $UpdateMarkerPath "update marker"
Exit-IfDirMissing $UpdateBinariesPath "service binaries update"
Update-ConfigFile
Update-ServiceBinaries
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