Refactor win_chocolatey module

* Refactor code to be more robust. Run main logic inside a try {} catch {}
  block. If there is any error, bail out and log all the command output
  automatically.
* Rely on error code generated by chocolatey instead of scraping text
  output to determine success/failure.
* Add support for unattended installs: (`-y` flag is a requirement by
  chocolatey)
* Before (un)installing, check existence of files.
* Use functions to abstract logic
* The great rewrite of 0.9.9, the `choco` interface has changed, check
  if chocolatey is installed and an older version. If so upgrade to
  latest.
* Allow upgrading packages that are already installed
* Use verbose logging for chocolate actions
* Adding functionality to specify a source for a chocolatey repository.
  (@smadam813)
* Removing pre-determined sources and adding specified source url in
  it's place. (@smadam813)

Contains contributions from:
*  Adam Keech  <akeech@chathamfinancial.com> (@smadam813)
reviewable/pr18780/r1
Pepe Barbe 10 years ago committed by Greg DeKoenigsberg
parent e633d9946f
commit b5d22eb1ec

@ -16,25 +16,11 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
$ErrorActionPreference = "Stop"
# WANT_JSON # WANT_JSON
# POWERSHELL_COMMON # POWERSHELL_COMMON
function Write-Log
{
param
(
[parameter(mandatory=$false)]
[System.String]
$message
)
$date = get-date -format 'yyyy-MM-dd hh:mm:ss.zz'
Write-Host "$date | $message"
Out-File -InputObject "$date $message" -FilePath $global:LoggingFile -Append
}
$params = Parse-Args $args; $params = Parse-Args $args;
$result = New-Object PSObject; $result = New-Object PSObject;
Set-Attr $result "changed" $false; Set-Attr $result "changed" $false;
@ -48,21 +34,22 @@ Else
Fail-Json $result "missing required argument: name" Fail-Json $result "missing required argument: name"
} }
if(($params.logPath).length -gt 0) If ($params.force)
{ {
$global:LoggingFile = $params.logPath $force = $params.force | ConvertTo-Bool
} }
else Else
{ {
$global:LoggingFile = "c:\ansible-playbook.log" $force = $false
} }
If ($params.force)
If ($params.upgrade)
{ {
$force = $params.force | ConvertTo-Bool $upgrade = $params.upgrade | ConvertTo-Bool
} }
Else Else
{ {
$force = $false $upgrade = $false
} }
If ($params.version) If ($params.version)
@ -74,6 +61,15 @@ Else
$version = $null $version = $null
} }
If ($params.source)
{
$source = $params.source.ToString().ToLower()
}
Else
{
$source = $null
}
If ($params.showlog) If ($params.showlog)
{ {
$showlog = $params.showlog | ConvertTo-Bool $showlog = $params.showlog | ConvertTo-Bool
@ -96,157 +92,230 @@ Else
$state = "present" $state = "present"
} }
$ChocoAlreadyInstalled = get-command choco -ErrorAction 0 Function Chocolatey-Install-Upgrade
if ($ChocoAlreadyInstalled -eq $null)
{ {
[CmdletBinding()]
param()
$ChocoAlreadyInstalled = get-command choco -ErrorAction 0
if ($ChocoAlreadyInstalled -eq $null)
{
#We need to install chocolatey #We need to install chocolatey
$install_choco_result = iex ((new-object net.webclient).DownloadString("https://chocolatey.org/install.ps1")) iex ((new-object net.webclient).DownloadString("https://chocolatey.org/install.ps1"))
$result.changed = $true $result.changed = $true
$executable = "C:\ProgramData\chocolatey\bin\choco.exe" $script:executable = "C:\ProgramData\chocolatey\bin\choco.exe"
} }
Else else
{ {
$executable = "choco.exe" $script:executable = "choco.exe"
}
If ($params.source) if ((choco --version) -lt '0.9.9')
{
$source = $params.source.ToString().ToLower()
If (($source -ne "chocolatey") -and ($source -ne "webpi") -and ($source -ne "windowsfeatures") -and ($source -ne "ruby") -and (!$source.startsWith("http://", "CurrentCultureIgnoreCase")) -and (!$source.startsWith("https://", "CurrentCultureIgnoreCase")))
{ {
Fail-Json $result "source is $source - must be one of chocolatey, ruby, webpi, windowsfeatures or a custom source url." Choco-Upgrade chocolatey
}
} }
} }
Elseif (!$params.source)
Function Choco-IsInstalled
{ {
$source = "chocolatey" [CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=1)]
[string]$package
)
$cmd = "$executable list --local-only $package"
$results = invoke-expression $cmd
if ($LastExitCode -ne 0)
{
Set-Attr $result "choco_error_cmd" $cmd
Set-Attr $result "choco_error_log" "$results"
Throw "Error checking installation status for $package"
}
If ("$results" -match " $package .* (\d+) packages installed.")
{
return $matches[1] -gt 0
}
$false
} }
if ($source -eq "webpi") Function Choco-Upgrade
{ {
# check whether 'webpi' installation source is available; if it isn't, install it [CmdletBinding()]
$webpi_check_cmd = "$executable list webpicmd -localonly"
$webpi_check_result = invoke-expression $webpi_check_cmd param(
Set-Attr $result "chocolatey_bootstrap_webpi_check_cmd" $webpi_check_cmd [Parameter(Mandatory=$true, Position=1)]
Set-Attr $result "chocolatey_bootstrap_webpi_check_log" $webpi_check_result [string]$package,
if ( [Parameter(Mandatory=$false, Position=2)]
( [string]$version,
($webpi_check_result.GetType().Name -eq "String") -and [Parameter(Mandatory=$false, Position=3)]
($webpi_check_result -match "No packages found") [string]$source,
) -or [Parameter(Mandatory=$false, Position=4)]
($webpi_check_result -contains "No packages found.") [bool]$force
) )
if (-not (Choco-IsInstalled $package))
{ {
#lessmsi is a webpicmd dependency, but dependency resolution fails unless it's installed separately throw "$package is not installed, you cannot upgrade"
$lessmsi_install_cmd = "$executable install lessmsi" }
$lessmsi_install_result = invoke-expression $lessmsi_install_cmd
Set-Attr $result "chocolatey_bootstrap_lessmsi_install_cmd" $lessmsi_install_cmd
Set-Attr $result "chocolatey_bootstrap_lessmsi_install_log" $lessmsi_install_result
$webpi_install_cmd = "$executable install webpicmd" $cmd = "$executable upgrade -dv -y $package"
$webpi_install_result = invoke-expression $webpi_install_cmd
Set-Attr $result "chocolatey_bootstrap_webpi_install_cmd" $webpi_install_cmd
Set-Attr $result "chocolatey_bootstrap_webpi_install_log" $webpi_install_result
if (($webpi_install_result | select-string "already installed").length -gt 0) if ($version)
{ {
#no change $cmd += " -version $version"
} }
elseif (($webpi_install_result | select-string "webpicmd has finished successfully").length -gt 0)
if ($source)
{ {
$result.changed = $true $cmd += " -source $source"
} }
Else
if ($force)
{ {
Fail-Json $result "WebPI install error: $webpi_install_result" $cmd += " -force"
} }
$results = invoke-expression $cmd
if ($LastExitCode -ne 0)
{
Set-Attr $result "choco_error_cmd" $cmd
Set-Attr $result "choco_error_log" "$results"
Throw "Error installing $package"
} }
}
$expression = $executable if ("$results" -match ' upgraded (\d+)/\d+ package\(s\)\. ')
if ($state -eq "present")
{
$expression += " install $package"
}
Elseif ($state -eq "absent")
{
$expression += " uninstall $package"
}
if ($force)
{
if ($state -eq "present")
{ {
$expression += " -force" if ($matches[1] -gt 0)
{
$result.changed = $true
}
} }
}
if ($version)
{
$expression += " -version $version"
}
if ($source -eq "chocolatey")
{
$expression += " -source https://chocolatey.org/api/v2/"
}
elseif (($source -eq "windowsfeatures") -or ($source -eq "webpi") -or ($source -eq "ruby"))
{
$expression += " -source $source"
}
elseif(($source -ne $Null) -and ($source -ne ""))
{
$expression += " -source $source"
} }
Set-Attr $result "chocolatey command" $expression Function Choco-Install
$op_result = invoke-expression $expression
if ($state -eq "present")
{ {
if ( [CmdletBinding()]
(($op_result | select-string "already installed").length -gt 0) -or
# webpi has different text output, and that doesn't include the package name but instead the human-friendly name param(
(($op_result | select-string "No products to be installed").length -gt 0) [Parameter(Mandatory=$true, Position=1)]
[string]$package,
[Parameter(Mandatory=$false, Position=2)]
[string]$version,
[Parameter(Mandatory=$false, Position=3)]
[string]$source,
[Parameter(Mandatory=$false, Position=4)]
[bool]$force,
[Parameter(Mandatory=$false, Position=5)]
[bool]$upgrade
) )
if (Choco-IsInstalled $package)
{ {
#no change if ($upgrade)
{
Choco-Upgrade -package $package -version $version -source $source -force $force
} }
elseif (
(($op_result | select-string "has finished successfully").length -gt 0) -or return
# webpi has different text output, and that doesn't include the package name but instead the human-friendly name }
(($op_result | select-string "Install of Products: SUCCESS").length -gt 0) -or
(($op_result | select-string "gem installed").length -gt 0) -or $cmd = "$executable install -dv -y $package"
(($op_result | select-string "gems installed").length -gt 0)
) if ($version)
{ {
$result.changed = $true $cmd += " -version $version"
} }
Else
if ($source)
{ {
Fail-Json $result "Install error: $op_result" $cmd += " -source $source"
} }
if ($force)
{
$cmd += " -force"
}
$results = invoke-expression $cmd
if ($LastExitCode -ne 0)
{
Set-Attr $result "choco_error_cmd" $cmd
Set-Attr $result "choco_error_log" "$results"
Throw "Error installing $package"
}
$result.changed = $true
} }
Elseif ($state -eq "absent")
Function Choco-Uninstall
{ {
$op_result = invoke-expression "$executable uninstall $package" [CmdletBinding()]
# HACK: Misleading - 'Uninstalling from folder' appears in output even when package is not installed, hence order of checks this way
if ( param(
(($op_result | select-string "not installed").length -gt 0) -or [Parameter(Mandatory=$true, Position=1)]
(($op_result | select-string "Cannot find path").length -gt 0) [string]$package,
[Parameter(Mandatory=$false, Position=2)]
[string]$version,
[Parameter(Mandatory=$false, Position=3)]
[bool]$force
) )
if (-not (Choco-IsInstalled $package))
{ {
#no change return
} }
elseif (($op_result | select-string "Uninstalling from folder").length -gt 0)
$cmd = "$executable uninstall -dv -y $package"
if ($version)
{ {
$cmd += " -version $version"
}
if ($force)
{
$cmd += " -force"
}
$results = invoke-expression $cmd
if ($LastExitCode -ne 0)
{
Set-Attr $result "choco_error_cmd" $cmd
Set-Attr $result "choco_error_log" "$results"
Throw "Error uninstalling $package"
}
$result.changed = $true $result.changed = $true
}
Try
{
Chocolatey-Install-Upgrade
if ($state -eq "present")
{
Choco-Install -package $package -version $version -source $source `
-force $force -upgrade $upgrade
} }
else else
{ {
Fail-Json $result "Uninstall error: $op_result" Choco-Uninstall -package $package -version $version -force $force
} }
}
if ($showlog) Exit-Json $result;
}
Catch
{ {
Set-Attr $result "chocolatey_log" $op_result Fail-Json $result $_.Exception.Message
} }
Set-Attr $result "chocolatey_success" "true"
Exit-Json $result;

@ -53,42 +53,29 @@ options:
- no - no
default: no default: no
aliases: [] aliases: []
version: upgrade:
description: description:
- Specific version of the package to be installed - If package is already installed it, try to upgrade to the latest version or to the specified version
- Ignored when state == 'absent'
required: false
default: null
aliases: []
showlog:
description:
- Outputs the chocolatey log inside a chocolatey_log property.
required: false required: false
choices: choices:
- yes - yes
- no - no
default: no default: no
aliases: [] aliases: []
source: version:
description: description:
- Which source to install from - Specific version of the package to be installed
require: false - Ignored when state == 'absent'
choices: required: false
- chocolatey default: null
- ruby
- webpi
- windowsfeatures
default: chocolatey
aliases: [] aliases: []
logPath: source:
description: description:
- Where to log command output to - Specify source rather than using default chocolatey repository
require: false require: false
default: c:\\ansible-playbook.log default: null
aliases: [] aliases: []
author: author: Trond Hindenes, Peter Mounce, Pepe Barbe, Adam Keech
- '"Trond Hindenes (@trondhindenes)" <trond@hindenes.com>'
- '"Peter Mounce (@petemounce)" <public@neverrunwithscissors.com>'
''' '''
# TODO: # TODO:
@ -111,10 +98,8 @@ EXAMPLES = '''
name: git name: git
state: absent state: absent
# Install Application Request Routing v3 from webpi # Install git from specified repository
# Logically, this requires that you install IIS first (see win_feature)
# To find a list of packages available via webpi source, `choco list -source webpi`
win_chocolatey: win_chocolatey:
name: ARRv3 name: git
source: webpi source: https://someserver/api/v2/
''' '''

Loading…
Cancel
Save