-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(GH-61) Fix choco install package result #103
base: development
Are you sure you want to change the base?
Changes from 11 commits
13dc62b
c2e80c6
e3d87fb
b5e589e
f8ad82e
cfb4fad
db65157
edd29ac
2285eed
60b5c52
119a7e3
1b84d8c
5ce77a1
36cbc0f
a31199b
f918292
a765b4a
c62705e
6f99ed1
ff013ba
8d1c67f
d6c73a3
7270d0a
93afb73
7b4d50c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# Copyright (c) 2017 Chocolatey Software, Inc. | ||
# Copyright (c) 2013 - 2017 Lawrence Gripper & original authors/contributors from https://github.com/chocolatey/cChoco | ||
# 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. | ||
|
@@ -91,26 +90,26 @@ function Set-TargetResource | |
$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 | ||
UninstallPackage -pName $Name -arguments $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 | ||
UninstallPackage -pName $Name -arguments $Params | ||
Write-Verbose -Message "Re-Installing $Name with correct version $version" | ||
InstallPackage -pName $Name -pParams $Params -pVersion $Version -pSource $Source -cParams $chocoParams | ||
InstallPackage -pName $Name -arguments $Params -pVersion $Version -pSource $Source -cParams $chocoParams | ||
} elseif ($AutoUpgrade) { | ||
Write-Verbose -Message "Upgrading $Name due to version mis-match" | ||
Upgrade-Package -pName $Name -pParams $Params -pSource $Source | ||
Upgrade-Package -pName $Name -arguments $Params -pSource $Source | ||
} | ||
} | ||
} | ||
} else { | ||
$whatIfShouldProcess = $pscmdlet.ShouldProcess("$Name", 'Install package from Chocolatey') | ||
if ($whatIfShouldProcess) { | ||
InstallPackage -pName $Name -pParams $Params -pVersion $Version -pSource $Source -cParams $chocoParams | ||
InstallPackage -pName $Name -arguments $Params -pVersion $Version -pSource $Source -cParams $chocoParams | ||
} | ||
} | ||
} | ||
|
@@ -240,13 +239,9 @@ function InstallPackage | |
$chocoinstallparams += " $cParams" | ||
} | ||
Write-Verbose -Message "Install command: 'choco install $pName $chocoinstallparams'" | ||
|
||
$packageInstallOuput = Invoke-Expression -Command "choco install $pName $chocoinstallparams" | ||
$packageInstallOuput = Invoke-Chocolatey "install $pName $chocoinstallparams" | ||
Write-Verbose -Message "Package output $packageInstallOuput " | ||
|
||
# Clear Package Cache | ||
Get-ChocoInstalledPackage 'Purge' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason this has been removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the purge command can cause errors and hangs code in DSC configurations. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are no issues submitted around the purge command. If this isn't the fix for the issue itself can you raise an issue we can look at and remove it from this PR? |
||
|
||
#refresh path varaible in powershell, as choco doesn"t, to pull in git | ||
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') | ||
} | ||
|
@@ -266,19 +261,16 @@ function UninstallPackage | |
if (-not ($pParams)) | ||
{ | ||
Write-Verbose -Message 'Uninstalling Package Standard' | ||
$packageUninstallOuput = choco uninstall $pName -y | ||
$packageUninstallOuput = Invoke-Chocolatey "uninstall $pName -y" | ||
} | ||
elseif ($pParams) | ||
{ | ||
Write-Verbose -Message "Uninstalling Package with params $pParams" | ||
$packageUninstallOuput = choco uninstall $pName --params="$pParams" -y | ||
$packageUninstallOuput = Invoke-Chocolatey "uninstall $pName --params=`"$pParams`" -y" | ||
} | ||
|
||
Write-Verbose -Message "Package uninstall output $packageUninstallOuput " | ||
|
||
# Clear Package Cache | ||
Get-ChocoInstalledPackage 'Purge' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. he purge command can cause errors and hangs code in DSC configurations. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See previous comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the purge command again. |
||
|
||
#refresh path varaible in powershell, as choco doesn"t, to pull in git | ||
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') | ||
} | ||
|
@@ -332,7 +324,7 @@ Function Test-LatestVersionInstalled { | |
|
||
Write-Verbose -Message "Testing if $pName can be upgraded: 'choco upgrade $pName $chocoupgradeparams'" | ||
|
||
$packageUpgradeOuput = Invoke-Expression -Command "choco upgrade $pName $chocoupgradeparams" | ||
$packageUpgradeOuput = Invoke-Chocolatey "choco upgrade $pName $chocoupgradeparams" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
$packageUpgradeOuput | ForEach-Object {Write-Verbose -Message $_} | ||
|
||
if ($packageUpgradeOuput -match "$pName.*is the latest version available based on your source") { | ||
|
@@ -345,6 +337,7 @@ Function Test-LatestVersionInstalled { | |
##attempting to work around the issues with Chocolatey calling Write-host in its scripts. | ||
function global:Write-Host | ||
{ | ||
[Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidGlobalFunctions','')] | ||
Param( | ||
[Parameter(Mandatory, Position = 0)] | ||
[Object] | ||
|
@@ -389,41 +382,81 @@ Function Upgrade-Package { | |
if ($cParams) { | ||
$chocoupgradeparams += " $cParams" | ||
} | ||
$cmd = "choco upgrade $pName $chocoupgradeparams" | ||
$cmd = "upgrade $pName $chocoupgradeparams" | ||
Write-Verbose -Message "Upgrade command: '$cmd'" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to keep that Verbose message consistent from what it was previously - prepend it with 'choco'? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see this change in the latest commit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed the choco |
||
|
||
if (-not (IsPackageInstalled -pName $pName)) | ||
{ | ||
throw "$pName is not installed, you cannot upgrade" | ||
} | ||
|
||
$packageUpgradeOuput = Invoke-Expression -Command $cmd | ||
$packageUpgradeOuput = Invoke-Chocolatey $cmd | ||
$packageUpgradeOuput | ForEach-Object { Write-Verbose -Message $_ } | ||
} | ||
|
||
# Clear Package Cache | ||
Get-ChocoInstalledPackage 'Purge' | ||
function Get-ChocoInstalledPackage { | ||
(choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's your thinking behind running this every time rather than caching it for 60 seconds? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In some instances of DSC configuratioin 60 seconds is to long if a small package is installed, so the test will fail I will remove the Return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See previous comment around this. |
||
|
||
function Get-ChocoInstalledPackage ($action) { | ||
$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 ($action -eq 'Purge') { | ||
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 { | ||
choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|" -OutVariable res | Export-Clixml -Path $ChocoInstallList | ||
} | ||
} | ||
<# | ||
.Synopsis | ||
Run chocolatey executable and throws error on failure | ||
.DESCRIPTION | ||
Run chocolatey executable and throws error on failure | ||
.EXAMPLE | ||
Invoke-Chocolatey "list -lo" | ||
.EXAMPLE | ||
Invoke-Chocolatey -arguments "list -lo" | ||
#> | ||
function Invoke-Chocolatey | ||
{ | ||
[CmdletBinding()] | ||
Param | ||
( | ||
# chocolatey arguments." | ||
[Parameter(Position=0)] | ||
[string]$arguments | ||
) | ||
|
||
Return $res | ||
[int[]]$validExitCodes = $( | ||
0, #most widely used success exit code | ||
1605, #(MSI uninstall) - the product is not found, could have already been uninstalled | ||
1614, #(MSI uninstall) - the product is uninstalled | ||
1641, #(MSI) - restart initiated | ||
3010 #(MSI, InnoSetup can be passed to provide this) - restart required | ||
) | ||
Write-Verbose -Message "command: 'choco $arguments'" | ||
|
||
$pinfo = New-Object System.Diagnostics.ProcessStartInfo | ||
$pinfo.FileName = "choco" | ||
$pinfo.RedirectStandardError = $true | ||
$pinfo.RedirectStandardOutput = $true | ||
$pinfo.UseShellExecute = $false | ||
$pinfo.Arguments = "$arguments" | ||
|
||
$p = New-Object System.Diagnostics.Process | ||
$p.StartInfo = $pinfo | ||
$p.Start() | Out-Null | ||
|
||
$output = $p.StandardOutput.ReadToEnd() | ||
$p.WaitForExit() | ||
pauby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$exitcode = $p.ExitCode | ||
$p.Dispose() | ||
|
||
#Set $LASTEXITCODE variable. | ||
powershell.exe -NoLogo -NoProfile -Noninteractive "exit $exitcode" | ||
|
||
if($exitcode -in $validExitCodes ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if a non-MSI exits with one of the MSI exit codes? That could indicate an error but it's ignored? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its ignore/ non error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If an EXE exits with an error code that matches one of those we need that to come through. Ignoring it would indicate it installed it okay when in fact it didn't. |
||
{ | ||
$output.Split("`n") | ||
$outputdata | ||
} | ||
else | ||
{ | ||
#when error, throw output as error, contains errormessage | ||
throw "Error: chocolatey command failed with exit code $exitcode.`n$output" | ||
} | ||
} | ||
|
||
Export-ModuleMember -Function *-TargetResource |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove this change from the commit?