-
Notifications
You must be signed in to change notification settings - Fork 100
/
cChocoPackageInstall.psm1
559 lines (495 loc) · 18.6 KB
/
cChocoPackageInstall.psm1
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# Copyright (c) 2017 Chocolatey Software, Inc.
# Copyright (c) 2013 - 2017 Lawrence Gripper & original authors/contributors from https://github.com/chocolatey/cChoco
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
function Get-TargetResource
{
[OutputType([hashtable])]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "MinimumVersion")]
param
(
[parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Name,
[ValidateNotNullOrEmpty()]
[string]
$Params,
[ValidateNotNullOrEmpty()]
[string]
$Version,
[ValidateNotNull()]
[string]
$MinimumVersion,
[ValidateNotNullOrEmpty()]
[string]
$Source
)
Write-Verbose -Message 'Start Get-TargetResource'
if (-Not (Test-ChocoInstalled)) {
throw "cChocoPackageInstall requires Chocolatey to be installed, consider using cChocoInstaller with 'dependson' in dsc config"
}
#Needs to return a hashtable that returns the current
#status of the configuration component
$Configuration = @{
Name = $Name
Params = $Params
Version = $Version
Source = $Source
}
return $Configuration
}
function Set-TargetResource
{
[CmdletBinding(SupportsShouldProcess)]
param
(
[parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Name,
[ValidateSet('Present','Absent')]
[string]
$Ensure='Present',
[ValidateNotNullOrEmpty()]
[string]
$Params,
[ValidateNotNullOrEmpty()]
[string]
$Version,
[ValidateNotNull()]
[string]
$MinimumVersion,
[string]
$Source,
[String]
$chocoParams,
[bool]
$AutoUpgrade = $false
)
Write-Verbose -Message 'Start Set-TargetResource'
$isVersionPresent = $PSBoundParameters.ContainsKey('Version')
$isMinimumVersionPresent = $PSBoundParameters.ContainsKey('MinimumVersion')
if ($isVersionPresent -and $isMinimumVersionPresent ) {
throw "Cannot specify 'Version' and 'MinimumVersion' in the same configuration"
}
if (-Not (Test-ChocoInstalled)) {
throw "cChocoPackageInstall requires Chocolatey to be installed, consider using cChocoInstaller with 'dependson' in dsc config"
}
$isInstalled = IsPackageInstalled -pName $Name
#Determine the correct package version to use get to desired state
if ($isVersionPresent -or $isMinimumVersionPresent) {
if ($isVersionPresent) {
$versionToInstall = $PSBoundParameters['Version']
}
else {
$versionToInstall = $PSBoundParameters['MinimumVersion']
}
}
#Uninstall if Ensure is set to absent and the package is installed
if ($isInstalled) {
if ($Ensure -eq 'Absent') {
$whatIfShouldProcess = $pscmdlet.ShouldProcess("$Name", 'Remove Chocolatey package')
if ($whatIfShouldProcess) {
Write-Verbose -Message "Removing $Name as ensure is set to absent"
UninstallPackage -pName $Name -pParams $Params
}
} else {
$whatIfShouldProcess = $pscmdlet.ShouldProcess("$Name", 'Installing / upgrading package from Chocolatey')
if ($whatIfShouldProcess) {
if ($Version) {
Write-Verbose -Message "Uninstalling $Name due to version mis-match"
UninstallPackage -pName $Name -pParams $Params
Write-Verbose -Message "Re-Installing $Name with correct version $versionToInstall"
InstallPackage -pName $Name -pParams $Params -pVersion $versionToInstall -pSource $Source -cParams $chocoParams
}
elseif ($MinimumVersion) {
Write-Verbose -Message "Upgrading $Name because installed version is lower that the specified minimum"
$chocoParams += " --version='$versionToInstall'"
Upgrade-Package -pName $Name -pParams $Params -pSource $Source -cParams $chocoParams
}
elseif ($AutoUpgrade) {
Write-Verbose -Message "Upgrading $Name due to version mis-match"
Upgrade-Package -pName $Name -pParams $Params -pSource $Source -cParams $chocoParams
}
}
}
} else {
$whatIfShouldProcess = $pscmdlet.ShouldProcess("$Name", 'Install package from Chocolatey')
if ($whatIfShouldProcess) {
InstallPackage -pName $Name -pParams $Params -pVersion $versionToInstall -pSource $Source -cParams $chocoParams
}
}
}
function Test-TargetResource
{
[CmdletBinding(SupportsShouldProcess)]
[OutputType([bool])]
param
(
[parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Name,
[ValidateSet('Present','Absent')]
[string]
$Ensure='Present',
[ValidateNotNullOrEmpty()]
[string]
$Params,
[ValidateNotNullOrEmpty()]
[string]
$Version,
[ValidateNotNull()]
[string]
$MinimumVersion,
[string]
$Source,
[ValidateNotNullOrEmpty()]
[String]
$chocoParams,
[bool]
$AutoUpgrade = $false
)
Write-Verbose -Message 'Start Test-TargetResource'
$isVersionPresent = $PSBoundParameters.ContainsKey('Version')
$isMinimumVersionPresent = $PSBoundParameters.ContainsKey('MinimumVersion')
if ($isVersionPresent -and $isMinimumVersionPresent ) {
throw "Cannot specify 'Version' and 'MinimumVersion' in the same configuration"
}
if (-Not (Test-ChocoInstalled)) {
return $false
}
$isInstalled = IsPackageInstalled -pName $Name
if ($ensure -eq 'Absent') {
if ($isInstalled -eq $false) {
return $true
} else {
return $false
}
}
if ($version) {
Write-Verbose -Message "Checking if $Name is installed and if version matches $version"
$result = IsPackageInstalled -pName $Name -pVersion $Version
}
elseif ($MinimumVersion) {
Write-Verbose -Message "Checking if $Name is installed and version is $MinimumVersion or higher"
$result = IsPackageInstalled -pName $Name -pMinimumVersion $MinimumVersion
}
else {
Write-Verbose -Message "Checking if $Name is installed"
if ($AutoUpgrade -and $isInstalled) {
$testParams = @{
pName = $Name
}
if ($Source){
$testParams.pSource = $Source
}
$result = Test-LatestVersionInstalled @testParams
} else {
$result = $isInstalled
}
}
Return $result
}
function Test-ChocoInstalled
{
Write-Verbose -Message 'Test-ChocoInstalled'
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
Write-Verbose -Message "Env:Path contains: $env:Path"
if (Test-Command -command choco)
{
Write-Verbose -Message 'YES - Choco is Installed'
return $true
}
Write-Verbose -Message 'NO - Choco is not Installed'
return $false
}
Function Test-Command
{
[CmdletBinding()]
[OutputType([bool])]
Param (
[string]$command = 'choco'
)
Write-Verbose -Message "Test-Command $command"
if (Get-Command -Name $command -ErrorAction SilentlyContinue) {
Write-Verbose -Message "$command exists"
return $true
} else {
Write-Verbose -Message "$command does NOT exist"
return $false
}
}
function InstallPackage
{
[Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')]
param(
[Parameter(Position=0,Mandatory)]
[string]$pName,
[Parameter(Position=1)]
[string]$pParams,
[Parameter(Position=2)]
[string]$pVersion,
[Parameter(Position=3)]
[string]$pSource,
[Parameter(Position=4)]
[string]$cParams
)
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
[string]$chocoParams = '-y'
if ($pParams) {
$chocoParams += " --params=`"$pParams`""
}
if ($pVersion) {
$chocoParams += " --version=`"$pVersion`""
}
if ($pSource) {
$chocoParams += " --source=`"$pSource`""
}
if ($cParams) {
$chocoParams += " $cParams"
}
# Check if Chocolatey version is Greater than 0.10.4, and add --no-progress
if ((Get-ChocoVersion) -ge [System.Version]('0.10.4')){
$chocoParams += " --no-progress"
}
$cmd = "choco install $pName $chocoParams"
Write-Verbose -Message "Install command: '$cmd'"
$packageInstallOuput = Invoke-Expression -Command $cmd
Write-Verbose -Message "Package output $packageInstallOuput"
# Clear Package Cache
Get-ChocoInstalledPackage -Purge
#refresh path varaible in powershell, as choco doesn"t, to pull in git
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
}
function UninstallPackage
{
[Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')]
param(
[Parameter(Position=0,Mandatory)]
[string]$pName,
[Parameter(Position=1)]
[string]$pParams
)
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
[string]$chocoParams = "-y"
if ($pParams) {
$chocoParams += " --params=`"$pParams`""
}
if ($pVersion) {
$chocoParams += " --version=`"$pVersion`""
}
# Check if Chocolatey version is Greater than 0.10.4, and add --no-progress
if ((Get-ChocoVersion) -ge [System.Version]('0.10.4')){
$chocoParams += " --no-progress"
}
$cmd = "choco uninstall $pName $chocoParams"
Write-Verbose -Message "Uninstalling $pName with: '$cmd'"
$packageUninstallOuput = Invoke-Expression -Command $cmd
Write-Verbose -Message "Package uninstall output $packageUninstallOuput "
# Clear Package Cache
Get-ChocoInstalledPackage -Purge
#refresh path varaible in powershell, as choco doesn"t, to pull in git
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
}
function IsPackageInstalled
{
[CmdletBinding(DefaultParameterSetName = 'RequiredVersion')]
[OutputType([Boolean])]
param(
[Parameter(Position=0, Mandatory)]
[string]$pName,
[Parameter(ParameterSetName = 'RequiredVersion')]
[string]$pVersion,
[Parameter(ParameterSetName = 'MinimumVersion')]
[string]$pMinimumVersion
)
Write-Verbose -Message "Start IsPackageInstalled $pName"
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
Write-Verbose -Message "Path variables: $($env:Path)"
$installedPackages = Get-ChocoInstalledPackage
if ($pVersion) {
Write-Verbose 'Comparing required version'
$installedPackages = $installedPackages | Where-Object { $_.Name -eq $pName -and $_.Version -eq $pVersion}
}
elseif ($pMinimumVersion) {
Write-Verbose 'Comparing minimum version'
# version comparison can be done with [System.Version] but this lacks the ability to compare pre-release versions
# because of this limitation MinimumVersion cannot be used in conjuction with pre-release packages
$pre = ($pMinimumVersion -split "-")[1]
if ($pre) {
throw "MinimumVersion does not support comparing pre-releases, please use Version parameter instead"
}
$comparablePackages = $installedPackages | Where-Object { $_.Name -eq $pName} | ForEach-Object {
# as mentioned above we cant convert prerelease versions to [Sytem.Version] so we ignore anything after "-"
# leaving just the . seperated numeric version. this is loosely equivalent to "rounding down"
$parseableVersion = ($_.Version -split "-")[0]
$v = [System.Version]($parseableVersion)
$_ | Add-Member -MemberType NoteProperty -Name ComparableVersion -Value $v -PassThru
}
$installedPackages = $comparablePackages | Where-Object {$_.ComparableVersion -ge $pMinimumVersion}
}
else {
Write-Verbose "Finding packages -eq $pName"
$installedPackages = $installedPackages | Where-Object { $_.Name -eq $pName}
}
$count = @($installedPackages).Count
Write-Verbose "Found $Count matching packages"
if ($Count -gt 0)
{
$installedPackages | ForEach-Object {Write-Verbose -Message "Found: $($_.Name) with version $($_.Version)"}
return $true
}
return $false
}
Function Test-LatestVersionInstalled {
[Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')]
param(
[Parameter(Mandatory)]
[string]$pName,
[string]$pSource
)
Write-Verbose -Message "Testing if $pName can be upgraded"
[string]$chocoParams = '--noop'
if ($pSource) {
$chocoParams += " --source=`"$pSource`""
}
$cmd = "choco upgrade $pName $chocoParams"
Write-Verbose -Message "Testing if $pName can be upgraded: '$cmd'"
$packageUpgradeOuput = Invoke-Expression -Command $cmd
$packageUpgradeOuput | ForEach-Object {Write-Verbose -Message $_}
if ($packageUpgradeOuput -match "$pName.*is the latest version available based on your source") {
return $true
}
return $false
}
##region - chocolately installer work arounds. Main issue is use of write-host
##attempting to work around the issues with Chocolatey calling Write-host in its scripts.
function global:Write-Host
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalFunctions", "")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "NoNewLine")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "ForegroundColor")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "BackgroundColor")]
param(
[Parameter(Mandatory, Position = 0)]
[Object]
$Object,
[Switch]
$NoNewLine,
[ConsoleColor]
$ForegroundColor,
[ConsoleColor]
$BackgroundColor
)
#Override default Write-Host...
Write-Verbose -Message $Object
}
Function Upgrade-Package {
[Diagnostics.CodeAnalysis.SuppressMessage('PSUseApprovedVerbs','')]
[Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')]
param(
[Parameter(Position=0,Mandatory)]
[string]$pName,
[Parameter(Position=1)]
[string]$pParams,
[Parameter(Position=2)]
[string]$pSource,
[Parameter(Position=3)]
[string]$cParams
)
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
Write-Verbose -Message "Path variables: $($env:Path)"
[string]$chocoParams = '-dv -y'
if ($pParams) {
$chocoParams += " --params=`"$pParams`""
}
if ($pSource) {
$chocoParams += " --source=`"$pSource`""
}
if ($cParams) {
$chocoParams += " $cParams"
}
# Check if Chocolatey version is Greater than 0.10.4, and add --no-progress
if ((Get-ChocoVersion) -ge [System.Version]('0.10.4')){
$chocoParams += " --no-progress"
}
$cmd = "choco upgrade $pName $chocoParams"
Write-Verbose -Message "Upgrade command: '$cmd'"
if (-not (IsPackageInstalled -pName $pName))
{
throw "$pName is not installed, you cannot upgrade"
}
$packageUpgradeOuput = Invoke-Expression -Command $cmd
$packageUpgradeOuput | ForEach-Object { Write-Verbose -Message $_ }
# Clear Package Cache
Get-ChocoInstalledPackage -Purge
}
function Get-ChocoInstalledPackage {
[CmdletBinding()]
param (
[switch]$Purge,
[switch]$NoCache
)
$ChocoInstallLP = Join-Path -Path $env:ChocolateyInstall -ChildPath 'cache'
if ( -not (Test-Path $ChocoInstallLP)){
New-Item -Name 'cache' -Path $env:ChocolateyInstall -ItemType Directory | Out-Null
}
$ChocoInstallList = Join-Path -Path $ChocoInstallLP -ChildPath 'ChocoInstalled.xml'
if ($Purge.IsPresent) {
Remove-Item $ChocoInstallList -Force
$res = $true
} else {
$PackageCacheSec = (Get-Date).AddSeconds('-60')
if ( $PackageCacheSec -lt (Get-Item $ChocoInstallList -ErrorAction SilentlyContinue).LastWriteTime ) {
$res = Import-Clixml $ChocoInstallList
} else {
$res = choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|"
if ( -not $NoCache){
$res | Export-Clixml -Path $ChocoInstallList
}
}
}
Return $res
}
function Get-ChocoVersion {
[OutputType([System.Version])]
[CmdletBinding()]
param (
[switch]$Purge,
[switch]$NoCache
)
$chocoInstallCache = Join-Path -Path $env:ChocolateyInstall -ChildPath 'cache'
if ( -not (Test-Path $chocoInstallCache)){
New-Item -Name 'cache' -Path $env:ChocolateyInstall -ItemType Directory | Out-Null
}
$chocoVersion = Join-Path -Path $chocoInstallCache -ChildPath 'ChocoVersion.xml'
if ($Purge.IsPresent) {
Remove-Item $chocoVersion -Force
$res = $true
} else {
$cacheSec = (Get-Date).AddSeconds('-60')
if ( $cacheSec -lt (Get-Item $chocoVersion -ErrorAction SilentlyContinue).LastWriteTime ) {
$res = Import-Clixml $chocoVersion
} else {
$cmd = choco -v
$res = [System.Version]($cmd.Split('-')[0])
if (-not $NoCache) {
$res | Export-Clixml -Path $chocoVersion
}
}
}
return $res
}
Export-ModuleMember -Function *-TargetResource