Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
* development:
  [#135] A fix for passing custom source name in cChocoPackageInstaller Test-TargetResource
  (maint) Fix description and issue label
  (maint) Fix headings
  (GH-126) Document WinRM envelope size issue
  Corrected reversed logic in `if (-not $Purge) Using `$Purge.IsPresent` to be more explicit.
  Revered if Purge.IsPresent checks back to origional order
  Converted simple functions to "Advanced Functions" to match the rest of code
  (maint) Add build instructions
  (maint) Formatting
  Updated [Version] to Fully Qualified [System.Version]
  Choco appends '-beta' to beta version numbers Added parsing to split on '-' and only use the first section.
  Implemented caching choco version Ensured explicitly casting '0.10.4' to Version
  Powershell cannot compair 0.10.4 to a version number
  Fix Pester Test run Failure
  Choco Versions >10.4 overwhelm log with download Progress data
  • Loading branch information
pauby committed Feb 26, 2020
2 parents 7113f4c + e963174 commit 03e6836
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 47 deletions.
125 changes: 91 additions & 34 deletions DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,13 @@ function Test-TargetResource
Write-Verbose -Message "Checking if $Name is installed"

if ($AutoUpgrade -and $isInstalled) {
$testParams = @{
pName = $Name
}
if ($Source){
[string]$pSource = "-pSource `"$Source`""
$testParams.pSource = $Source
}
$result = Test-LatestVersionInstalled -pName $Name $pSource
$result = Test-LatestVersionInstalled @testParams
} else {
$result = $isInstalled
}
Expand Down Expand Up @@ -227,33 +230,39 @@ function InstallPackage

$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')

[string]$chocoinstallparams = '-y'
[string]$chocoParams = '-y'
if ($pParams) {
$chocoinstallparams += " --params=`"$pParams`""
$chocoParams += " --params=`"$pParams`""
}
if ($pVersion) {
$chocoinstallparams += " --version=`"$pVersion`""
$chocoParams += " --version=`"$pVersion`""
}
if ($pSource) {
$chocoinstallparams += " --source=`"$pSource`""
$chocoParams += " --source=`"$pSource`""
}
if ($cParams) {
$chocoinstallparams += " $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"
}
Write-Verbose -Message "Install command: 'choco install $pName $chocoinstallparams'"

$packageInstallOuput = Invoke-Expression -Command "choco install $pName $chocoinstallparams"
Write-Verbose -Message "Package output $packageInstallOuput "
$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'
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,
Expand All @@ -263,22 +272,26 @@ function UninstallPackage

$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')

#Todo: Refactor
if (-not ($pParams))
{
Write-Verbose -Message 'Uninstalling Package Standard'
$packageUninstallOuput = choco uninstall $pName -y
[string]$chocoParams = "-y"
if ($pParams) {
$chocoParams += " --params=`"$pParams`""
}
elseif ($pParams)
{
Write-Verbose -Message "Uninstalling Package with params $pParams"
$packageUninstallOuput = choco uninstall $pName --params="$pParams" -y
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'
Get-ChocoInstalledPackage -Purge

#refresh path varaible in powershell, as choco doesn"t, to pull in git
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
Expand Down Expand Up @@ -325,14 +338,15 @@ Function Test-LatestVersionInstalled {
)
Write-Verbose -Message "Testing if $pName can be upgraded"

[string]$chocoupgradeparams = '--noop'
[string]$chocoParams = '--noop'
if ($pSource) {
$chocoupgradeparams += " --source=`"$pSource`""
$chocoParams += " --source=`"$pSource`""
}

Write-Verbose -Message "Testing if $pName can be upgraded: 'choco upgrade $pName $chocoupgradeparams'"
$cmd = "choco upgrade $pName $chocoParams"
Write-Verbose -Message "Testing if $pName can be upgraded: '$cmd'"

$packageUpgradeOuput = Invoke-Expression -Command "choco upgrade $pName $chocoupgradeparams"
$packageUpgradeOuput = Invoke-Expression -Command $cmd
$packageUpgradeOuput | ForEach-Object {Write-Verbose -Message $_}

if ($packageUpgradeOuput -match "$pName.*is the latest version available based on your source") {
Expand Down Expand Up @@ -379,17 +393,22 @@ Function Upgrade-Package {
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
Write-Verbose -Message "Path variables: $env:Path"

[string]$chocoupgradeparams = '-dv -y'
[string]$chocoParams = '-dv -y'
if ($pParams) {
$chocoupgradeparams += " --params=`"$pParams`""
$chocoParams += " --params=`"$pParams`""
}
if ($pSource) {
$chocoupgradeparams += " --source=`"$pSource`""
$chocoParams += " --source=`"$pSource`""
}
if ($cParams) {
$chocoupgradeparams += " $cParams"
$chocoParams += " $cParams"
}
$cmd = "choco upgrade $pName $chocoupgradeparams"
# 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))
Expand All @@ -401,29 +420,67 @@ Function Upgrade-Package {
$packageUpgradeOuput | ForEach-Object { Write-Verbose -Message $_ }

# Clear Package Cache
Get-ChocoInstalledPackage 'Purge'
Get-ChocoInstalledPackage -Purge
}

function Get-ChocoInstalledPackage ($action) {
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 ($action -eq 'Purge') {
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 {
choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|" -OutVariable res | Export-Clixml -Path $ChocoInstallList
$res = choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|"
if ( -not $NoCache){
$res | Export-Clixml -Path $ChocoInstallList
}
}
}

Return $res
}

function Get-ChocoVersion {
[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])
$res | Export-Clixml -Path $chocoVersion
}
}
Return $res
}

Export-ModuleMember -Function *-TargetResource
36 changes: 23 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@
| master | [![Build status](https://ci.appveyor.com/api/projects/status/qma3jnh23w5vjt46/branch/master?svg=true&passingText=master%20-%20OK&pendingText=master%20-%20PENDING&failingText=master%20-%20FAILED)](https://ci.appveyor.com/project/LawrenceGripper/cchoco/branch/master) |
| development | [![Build status](https://ci.appveyor.com/api/projects/status/qma3jnh23w5vjt46/branch/development?svg=true&passingText=development%20-%20OK&pendingText=development%20-%20PENDING&failingText=development%20-%20FAILED)](https://ci.appveyor.com/project/LawrenceGripper/cchoco/branch/development) |

Community Chocolatey DSC Resource
=============================
# Community Chocolatey DSC Resource

[![Join the chat at https://gitter.im/chocolatey/cChoco](https://badges.gitter.im/chocolatey/cChoco.svg)](https://gitter.im/chocolatey/cChoco?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

This resource is aimed at getting and installing packages from the choco gallery.
This resource is aimed at getting and installing packages using Chocolatey.

The resource takes the name of the package and will then install that package.

See [ExampleConfig.ps1](ExampleConfig.ps1) for example usage.

See list of packages here: https://chocolatey.org/packages

Contributing
=============================
## Contributing

Happy to accept new features and fixes. Outstanding issues which can be worked on tagged HelpedWanted under issues.
Happy to accept new features and fixes. Outstanding issues which can be worked on tagged `Up For Grabs` under issues.

Submitting a PR
=============================
### Submitting a PR

Here's the general process of fixing an issue in the DSC Resource Kit:
1. Fork the repository.
Expand All @@ -35,14 +32,27 @@ Here's the general process of fixing an issue in the DSC Resource Kit:
9. Make sure your code does not contain merge conflicts.
10. Address comments (if any).

Build and Publishing
============================
### Build and Publishing

AppVeyor is used to package up the resource and publish to the Powershell Gallery (on successful build from a newly pushed tag only).
AppVeyor is used to package up the resource and publish to the PowerShell Gallery (on successful build from a newly pushed tag only).

The AppVeyor scripts do the following:
- Test the resources using 'xDSCResourceDesigner'
- Verify best practises using 'PSScriptAnalyzer'
- Update the version in the manifest file
- Publish the module to the powershell gallery
- Checkin updated manifest file to github
- Publish the module to the PowerShell gallery
- Check in updated manifest file to GitHub

To build:

1. Update `ModuleVersion` in `cChoco.psd1` - use `major.minor.patch.0`;
2. Update `version` in `appveyor.yml` - use `major.minor.patch.{build}`;
3. Merge development branch to master - `git checkout master`, `git merge development`;
4. Tag master with new version - `git tag v<major.minor.patch>`;
5. Push changes with tag `git push v<major.minor.patch>`

## Known Issues / Troubleshooting

### WS-Management - Exceeds the maximum envelope size allowed

The maximum envelope size for WinRM is not sufficient for installing large packages. To increase the envelope size use `winrm set winrm/config @{MaxEnvelopeSizekb=”153600″}` - this exampe will increase it to 150MB.

0 comments on commit 03e6836

Please sign in to comment.