-
Notifications
You must be signed in to change notification settings - Fork 3
/
dcs-update.ps1
200 lines (197 loc) · 7.54 KB
/
dcs-update.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
[CmdletBinding()]
param (
$DcsPath,
$Version = "latest",
[string[]] $Modules,
[int] $Timeout = 3600,
[int] $RetryAttempts = 10
)
$ErrorActionPreference = 'Stop'
function Get-AutoUpdaterJson() {
$autoupdateJson = Get-Content -Raw $autoupdatePath | ConvertFrom-Json
if ($null -eq $autoupdateJson) {
throw "Could not read $autoupdatePath json"
}
return $autoupdateJson
}
function EscapeTeamcity([string] $Message) {
$Message = $Message.Replace("|","||");
$Message = $Message.Replace("'","|'");
$Message = $Message.Replace("`n","|n");
$Message = $Message.Replace("`r","|r");
$Message = $Message.Replace("[","|[");
$Message = $Message.Replace("]","|]");
return $Message;
}
function Start-ThreadJobHere([scriptblock]$ScriptBlock, [object[]]$ArgumentList) {
Start-ThreadJob -Init ([ScriptBlock]::Create("Set-Location '$pwd'")) -Script $ScriptBlock -ArgumentList $ArgumentList -StreamingHost $Host
}
function Start-Updater([String[]] $ArgumentList) {
$ArgumentList += "--quiet"
if ($env:TEAMCITY_VERSION) {
$logPath = "$env:temp/DCS/autoupdate_templog.txt"
# Start a job to monitor the log file once it is created
$logMonitorJob = Start-ThreadJobHere -ArgumentList $logPath -ScriptBlock {
param([string] $File)
${function:EscapeTeamcity} = ${using:function:EscapeTeamcity}
Write-host "##teamcity[compilationStarted compiler='DCS Updater Log']"
try {
Write-Host "##teamcity[message text='$(EscapeTeamcity -Message "Waiting for $File to exist and be modified")']"
$startDate = Get-Date
while (-not (Test-Path -LiteralPath $File) -or ((Get-Item -LiteralPath $File).LastWriteTime -lt $startDate)) {
sleep 10
}
Write-Host "##teamcity[message text='$(EscapeTeamcity -Message "Tailing $File")']"
Get-Content $File -Wait | ForEach-Object { Write-Host "##teamcity[message text='$(EscapeTeamcity -Message $_)']" }
} catch {
Write-Host "Error during log monitoring:`n$_" -F Red
throw
} finally {
Write-Host "##teamcity[compilationFinished compiler='DCS Updater Log']"
}
}
}
# Run the update when it finishes force the log monitor to stop
try {
Write-Host "Running $updaterPath $($ArgumentList | Join-String -Separator " ")"
$process = Start-Process $updaterPath -ArgumentList $ArgumentList -PassThru
$timeouted = $null # reset any previously set timeout
$process | Wait-Process -Timeout $Timeout -ErrorAction SilentlyContinue -ErrorVariable timeouted
} finally {
if ($logMonitorJob) {
# Allow 3 seconds for file to buffer before stopping
sleep 3;
$logMonitorJob | Stop-Job
$logMonitorJob = $null
}
}
if ($timeouted) {
Write-Host "Timeout breached, killing updater"
# terminate the process
$process | Stop-Process
}
}
function Get-FormedVersion($json) {
return "$($json.version)@$($json.branch)"
}
function Get-ProcessRunning {
$process = Get-Process "DCS" -ErrorAction SilentlyContinue
$process | % {
$parent = Get-Item $DcsPath
$child = Get-Item $_.Path
# Make sure we are checking only the DCS process from this game directory
if ($child.FullName.Contains($parent.FullName)) {
return $_
}
}
}
if ([string]::IsNullOrWhiteSpace($Version)) {
$Version = "none"
}
if (-not (Test-Path $DcsPath)) {
throw "No DCS Path provided"
}
$autoupdatePath = Join-Path $DcsPath "autoupdate.cfg"
if (-not (Test-Path $autoupdatePath)) {
throw "Could not find $autoupdatePath"
}
$updaterPath = Join-Path $DcsPath "bin/DCS_Updater.exe"
if (-not (Test-Path $DcsPath)) {
throw "Could not find $DcsPath"
}
# Ensure DCS isn't running
$process = Get-ProcessRunning
if ($process) {
Write-Host "DCS Running, closing it"
$process | Stop-Process
sleep 5
}
$currentJson = Get-AutoUpdaterJson
Write-Host "Requested DCS version: $Version, Current: $(Get-FormedVersion $currentJson)"
# Remove temp files
Remove-Item -Path "$DcsPath\_downloads\" -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue
Remove-Item -Path "$DcsPath\_backup.*\" -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue
$attemptNumber = 0
$correctVersionFound = $true
if ($Version -eq "latest") {
if ($env:TEAMCITY_VERSION) {
Write-Host "##teamcity[progressMessage 'Updating to latest DCS version']"
}
Start-Updater "update"
$currentJson = Get-AutoUpdaterJson
} elseif ($Version -match '([0-9.]*)@([a-z_.]+)') {
if ($env:TEAMCITY_VERSION) {
Write-Host "##teamcity[progressMessage 'Updating to $Version']"
}
$requestedVersion = $Matches[1]
$requestedBranch = $Matches[2]
$correctVersionFound = $false
do {
try {
$attemptNumber = $attemptNumber + 1
Write-Host "Attempt $attemptNumber/$RetryAttempts to update to $Version"
if ($env:TEAMCITY_VERSION) {
Write-Host "##teamcity[progressMessage 'Updating to $Version, attempt ($attemptNumber/$RetryAttempts)']"
}
Start-Updater "update","$Version"
$currentJson = Get-AutoUpdaterJson
# Check version
if (-not ([string]::IsNullOrWhiteSpace($requestedVersion))) {
if ($currentJson.version -eq $requestedVersion) {
$correctVersionFound = $true
break
}
} else { # Check branch
if ($currentJson.branch -eq $requestedBranch) {
$correctVersionFound = $true
break
}
}
throw "Matching version not found"
} catch {
Write-Host "Attempt $attemptNumber/$RetryAttempts failed, waiting 60 seconds before retry, reason:`n`n$_`n" -F Red
# Wait a minute before retry
sleep 60
}
} while ($attemptNumber -lt $RetryAttempts -or $RetryAttempts -lt 0)
if ($correctVersionFound -eq $false) {
if ($env:TEAMCITY_VERSION) {
Write-Host "##teamcity[progressMessage 'Updating to version $Version failed after $attemptNumber attempts)']"
}
throw "Could not update to requested version"
}
} elseif ($Version -eq "none") {
$currentJson = Get-AutoUpdaterJson
# Ignore
} else {
throw "Invalid version requested"
}
function Get-MissingModules() {
$currentJson = Get-AutoUpdaterJson
$missingModules = @()
foreach ($module in $Modules) {
if (-not $currentJson.modules.Contains("$module")) {
$missingModules += $module
}
}
return $missingModules
}
if ($null -ne $Modules) {
$missingModules = Get-MissingModules
if ($missingModules.Length -gt 0) {
if ($env:TEAMCITY_VERSION) {
Write-Host "##teamcity[progressMessage 'Installing modules: $($missingModules -join ", ")']"
}
Write-Host "Following modules are not installed: $($missingModules -join ", ")"
Start-Updater "install",$($missingModules -join " ")
$missingModules = Get-MissingModules
if ($missingModules.Length -gt 0) {
Write-Host "Following modules failed to be installed: $($missingModules -join ", ")" -F Red
} else {
Write-Host "Modules successfully installed: $($missingModules -join ", ")" -F Green
}
} else {
Write-Host "All required modules were installed"
}
}
return $currentJson