forked from jenkinsci/docker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjenkins-support.psm1
More file actions
223 lines (197 loc) · 8.93 KB
/
jenkins-support.psm1
File metadata and controls
223 lines (197 loc) · 8.93 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# compare if version1 < version2
function Compare-VersionLessThan([string] $version1 = '', [string] $version2 = '') {
# Quick check for equality
if($version1 -eq $version2) {
return $false
}
# Convert '-' to '.' to ease comparison
$normalizedVersion1 = $version1 -replace '-', '.'
$normalizedVersion2 = $version2 -replace '-', '.'
$version1Parts = $normalizedVersion1.Split('.')
$version2Parts = $normalizedVersion2.Split('.')
# Compare major versions
if ($version1Parts[0] -lt $version2parts[0]) {
return $true
}
if ($version1Parts[0] -gt $version2parts[0]) {
return $false
}
$maxLength = [Math]::Max($version1Parts.Length, $version2parts.Length)
# First parts are equal, compare subsequent parts
for ($i = 1; $i -lt $maxLength; $i++) {
$version1part = if ($i -lt $version1Parts.Length) { $version1Parts[$i] } else { '0' }
$version2part = if ($i -lt $version2Parts.Length) { $version2Parts[$i] } else { '0' }
if ($version1part -eq $version2part) {
continue
}
# Security fix backport special case
# Ex: 3894.vd0f0248b_a_fc4 < 3894.3896.vca_2c931e7935
# -> normal incrementals version includes a "v" as first char of second part
# -> security fix backport adds the backport source first part as second part
# https://github.com/jenkinsci/workflow-cps-plugin/releases/tag/3894.vd0f0248b_a_fc4
# https://github.com/jenkinsci/workflow-cps-plugin/releases/tag/3894.3896.vca_2c931e7935
# If only the nth part of $version1 starts with a "v", then $version1 is older
if ($version1part.Substring(0,1) -eq 'v' -and $version2part.Substring(0,1) -ne 'v') {
return $true
}
# Try numeric comparison first, fall back to string comparison
$num1 = 0
$num2 = 0
$isNum1 = [int]::TryParse($version1part, [ref]$num1)
$isNum2 = [int]::TryParse($version2part, [ref]$num2)
if ($isNum1 -and $isNum2) {
# Both are numeric, compare as integers
return ($num1 -lt $num2)
} else {
# At least one is not numeric, use string comparison
return ($version1part -lt $version2part)
}
}
}
function Get-EnvOrDefault($name, $def) {
$entry = Get-ChildItem env: | Where-Object { $_.Name -eq $name } | Select-Object -First 1
if(($null -ne $entry) -and ![System.String]::IsNullOrWhiteSpace($entry.Value)) {
return $entry.Value
}
return $def
}
function Expand-Zip($archive, $file) {
# load ZIP methods
Add-Type -AssemblyName System.IO.Compression.FileSystem
Write-Verbose "Unzipping $file from $archive"
$contents = ""
if(Test-Path $archive) {
# open ZIP archive for reading
$zip = [System.IO.Compression.ZipFile]::OpenRead($archive)
if($null -ne $zip) {
$entry = $zip.GetEntry($file)
if($null -ne $entry) {
$reader = New-Object -TypeName System.IO.StreamReader -ArgumentList $entry.Open()
$contents = $reader.ReadToEnd()
$reader.Dispose()
}
# close ZIP file
$zip.Dispose()
}
}
return $contents
}
# returns a plugin version from a plugin archive
function Get-PluginVersion($archive) {
$archive = $archive.Trim()
Write-Verbose "Getting plugin version for $archive"
if(-not (Test-Path $archive)) {
return ""
}
$version = Expand-Zip $archive "META-INF/MANIFEST.MF" | ForEach-Object {$_ -split "`n"} | Select-String -Pattern "^Plugin-Version:\s+" | ForEach-Object {$_ -replace "^Plugin-Version:\s+(.*)", '$1'} | Select-Object -First 1 | Out-String
return $version.Trim()
}
# Copy files from C:/ProgramData/Jenkins/Reference/ into $JENKINS_HOME
# So the initial JENKINS-HOME is set with expected content.
# Don't override, as this is just a reference setup, and use from UI
# can then change this, upgrade plugins, etc.
function Copy-ReferenceFile($file) {
$action = ""
$reason = ""
$log = $false
$refDir = Get-EnvOrDefault 'REF' 'C:/ProgramData/Jenkins/Reference'
if(-not (Test-Path $refDir)) {
return
}
Push-Location $refDir
$rel = Resolve-Path -Relative -Path $file
Pop-Location
$dir = Split-Path -Parent $rel
if($file -match "plugins[\\/].*\.jpi") {
$fileName = Split-Path -Leaf $file
$versionMarker = (Join-Path $env:JENKINS_HOME (Join-Path "plugins" "${fileName}.version_from_image"))
$containerVersion = Get-PluginVersion (Join-Path $env:JENKINS_HOME $rel)
$imageVersion = Get-PluginVersion $file
if(Test-Path $versionMarker) {
$markerVersion = (Get-Content -Raw $versionMarker).Trim()
if(Compare-VersionLessThan $markerVersion $containerVersion) {
if((Compare-VersionLessThan $containerVersion $imageVersion) -and ![System.String]::IsNullOrWhiteSpace($env:PLUGINS_FORCE_UPGRADE)) {
$action = "UPGRADED"
$reason="Manually upgraded version ($containerVersion) is older than image version $imageVersion"
$log=$true
} else {
$action="SKIPPED"
$reason="Installed version ($containerVersion) has been manually upgraded from initial version ($markerVersion)"
$log=$true
}
} else {
if($imageVersion -eq $containerVersion) {
$action = "SKIPPED"
$reason = "Version from image is the same as the installed version $imageVersion"
} else {
if(Compare-VersionLessThan $imageVersion $containerVersion) {
$action = "SKIPPED"
$log = $true
$reason = "Image version ($imageVersion) is older than installed version ($containerVersion)"
} else {
$action="UPGRADED"
$log=$true
$reason="Image version ($imageVersion) is newer than installed version ($containerVersion)"
}
}
}
} else {
if(![System.String]::IsNullOrWhiteSpace($env:TRY_UPGRADE_IF_NO_MARKER)) {
if($imageVersion -eq $containerVersion) {
$action = "SKIPPED"
$reason = "Version from image is the same as the installed version $imageVersion (no marker found)"
# Add marker for next time
Add-Content -Path $versionMarker -Value $imageVersion
} else {
if(Compare-VersionLessThan $imageVersion $containerVersion) {
$action = "SKIPPED"
$log = $true
$reason = "Image version ($imageVersion) is older than installed version ($containerVersion) (no marker found)"
} else {
$action = "UPGRADED"
$log = $true
$reason = "Image version ($imageVersion) is newer than installed version ($containerVersion) (no marker found)"
}
}
}
}
if((-not (Test-Path (Join-Path $env:JENKINS_HOME $rel))) -or ($action -eq "UPGRADED") -or ($file -match "\.override")) {
if([System.String]::IsNullOrWhiteSpace($action)) {
$action = "INSTALLED"
}
$log=$true
if(-not (Test-Path (Join-Path $env:JENKINS_HOME $dir))) {
New-Item -ItemType Directory -Path (Join-Path $env:JENKINS_HOME $dir)
}
Copy-Item $file (Join-Path $env:JENKINS_HOME $rel)
# pin plugins on initial copy
Write-Output $null >> (Join-Path $env:JENKINS_HOME "${rel}.pinned")
Add-Content -Path $versionMarker -Value $imageVersion
if([System.String]::IsNullOrWhiteSpace($reason)) {
$reason = $imageVersion
}
} else {
if([System.String]::IsNullOrWhiteSpace($action)) {
$action = "SKIPPED"
}
}
} else {
if((-not (Test-Path (Join-Path $env:JENKINS_HOME $rel))) -or ($file -match "\.override")) {
$action = "INSTALLED"
$log = $true
if(-not (Test-Path (Join-Path $env:JENKINS_HOME (Split-Path -Parent $rel)))) {
New-Item -ItemType Directory (Join-Path $env:JENKINS_HOME (Split-Path -Parent $rel))
}
Copy-Item $file (Join-Path $env:JENKINS_HOME $rel)
} else {
$action="SKIPPED"
}
}
if(![System.String]::IsNullOrWhiteSpace($env:VERBOSE) -or $log) {
if([System.String]::IsNullOrWhiteSpace($reason)) {
Add-Content -Path $COPY_REFERENCE_FILE_LOG -Value "$action $rel"
} else {
Add-Content -Path $COPY_REFERENCE_FILE_LOG -Value "$action $rel : $reason"
}
}
}