Merge pull request #398 from elventear/win_choco

Refactor `win_chocolatey` module
reviewable/pr18780/r1
Greg DeKoenigsberg 9 years ago
commit 78ffe4317d

@ -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"
} }
Function Chocolatey-Install-Upgrade
{
[CmdletBinding()]
param()
$ChocoAlreadyInstalled = get-command choco -ErrorAction 0 $ChocoAlreadyInstalled = get-command choco -ErrorAction 0
if ($ChocoAlreadyInstalled -eq $null) 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() Choco-Upgrade chocolatey
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."
} }
} }
Elseif (!$params.source)
{
$source = "chocolatey"
} }
if ($source -eq "webpi")
Function Choco-IsInstalled
{ {
# 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 (
(
($webpi_check_result.GetType().Name -eq "String") -and
($webpi_check_result -match "No packages found")
) -or
($webpi_check_result -contains "No packages found.")
) )
$cmd = "$executable list --local-only $package"
$results = invoke-expression $cmd
if ($LastExitCode -ne 0)
{ {
#lessmsi is a webpicmd dependency, but dependency resolution fails unless it's installed separately Set-Attr $result "choco_error_cmd" $cmd
$lessmsi_install_cmd = "$executable install lessmsi" Set-Attr $result "choco_error_log" "$results"
$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" Throw "Error checking installation status for $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 ("$results" -match " $package .* (\d+) packages installed.")
{ {
#no change return $matches[1] -gt 0
}
$false
} }
elseif (($webpi_install_result | select-string "webpicmd has finished successfully").length -gt 0)
Function Choco-Upgrade
{ {
$result.changed = $true [CmdletBinding()]
param(
[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
)
if (-not (Choco-IsInstalled $package))
{
throw "$package is not installed, you cannot upgrade"
} }
Else
$cmd = "$executable upgrade -dv -y $package"
if ($version)
{ {
Fail-Json $result "WebPI install error: $webpi_install_result" $cmd += " -version $version"
} }
if ($source)
{
$cmd += " -source $source"
} }
if ($force)
{
$cmd += " -force"
} }
$expression = $executable
if ($state -eq "present") $results = invoke-expression $cmd
if ($LastExitCode -ne 0)
{ {
$expression += " install $package" Set-Attr $result "choco_error_cmd" $cmd
Set-Attr $result "choco_error_log" "$results"
Throw "Error installing $package"
} }
Elseif ($state -eq "absent")
if ("$results" -match ' upgraded (\d+)/\d+ package\(s\)\. ')
{
if ($matches[1] -gt 0)
{ {
$expression += " uninstall $package" $result.changed = $true
} }
if ($force) }
}
Function Choco-Install
{ {
if ($state -eq "present") [CmdletBinding()]
param(
[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)
{ {
$expression += " -force" if ($upgrade)
{
Choco-Upgrade -package $package -version $version -source $source -force $force
} }
return
} }
$cmd = "$executable install -dv -y $package"
if ($version) if ($version)
{ {
$expression += " -version $version" $cmd += " -version $version"
} }
if ($source -eq "chocolatey")
if ($source)
{ {
$expression += " -source https://chocolatey.org/api/v2/" $cmd += " -source $source"
} }
elseif (($source -eq "windowsfeatures") -or ($source -eq "webpi") -or ($source -eq "ruby"))
if ($force)
{ {
$expression += " -source $source" $cmd += " -force"
} }
elseif(($source -ne $Null) -and ($source -ne ""))
$results = invoke-expression $cmd
if ($LastExitCode -ne 0)
{ {
$expression += " -source $source" Set-Attr $result "choco_error_cmd" $cmd
Set-Attr $result "choco_error_log" "$results"
Throw "Error installing $package"
} }
Set-Attr $result "chocolatey command" $expression $result.changed = $true
$op_result = invoke-expression $expression }
if ($state -eq "present")
Function Choco-Uninstall
{ {
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)]
[bool]$force
) )
if (-not (Choco-IsInstalled $package))
{ {
#no change return
} }
elseif (
(($op_result | select-string "has finished successfully").length -gt 0) -or $cmd = "$executable uninstall -dv -y $package"
# 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 if ($version)
(($op_result | select-string "gem installed").length -gt 0) -or
(($op_result | select-string "gems installed").length -gt 0)
)
{ {
$result.changed = $true $cmd += " -version $version"
} }
Else
if ($force)
{ {
Fail-Json $result "Install error: $op_result" $cmd += " -force"
} }
}
Elseif ($state -eq "absent") $results = invoke-expression $cmd
{
$op_result = invoke-expression "$executable uninstall $package" if ($LastExitCode -ne 0)
# HACK: Misleading - 'Uninstalling from folder' appears in output even when package is not installed, hence order of checks this way
if (
(($op_result | select-string "not installed").length -gt 0) -or
(($op_result | select-string "Cannot find path").length -gt 0)
)
{ {
#no change Set-Attr $result "choco_error_cmd" $cmd
Set-Attr $result "choco_error_log" "$results"
Throw "Error uninstalling $package"
} }
elseif (($op_result | select-string "Uninstalling from folder").length -gt 0)
{
$result.changed = $true $result.changed = $true
} }
else Try
{ {
Fail-Json $result "Uninstall error: $op_result" Chocolatey-Install-Upgrade
if ($state -eq "present")
{
Choco-Install -package $package -version $version -source $source `
-force $force -upgrade $upgrade
} }
else
{
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