win_chocolatey: Add check-mode support (#22501)

This patch implements:
- check-mode support
- add state "reinstalled"
- cleanup of timeout parameter
pull/22634/head
Dag Wieers 8 years ago committed by Matt Davis
parent 6ce338d29c
commit 589c483cfc

@ -24,7 +24,8 @@ $result = @{
changed = $false changed = $false
} }
$params = Parse-Args $args $params = Parse-Args $args -supports_check_mode $true
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
$package = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true $package = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
$force = Get-AnsibleParam -obj $params -name "force" -type "bool" -default $false $force = Get-AnsibleParam -obj $params -name "force" -type "bool" -default $false
@ -32,8 +33,8 @@ $upgrade = Get-AnsibleParam -obj $params -name "upgrade" -type "bool" -default $
$version = Get-AnsibleParam -obj $params -name "version" -type "str" $version = Get-AnsibleParam -obj $params -name "version" -type "str"
$source = Get-AnsibleParam -obj $params -name "source" -type "str" $source = Get-AnsibleParam -obj $params -name "source" -type "str"
$showlog = Get-AnsibleParam -obj $params -name "showlog" -type "bool" -default $false $showlog = Get-AnsibleParam -obj $params -name "showlog" -type "bool" -default $false
$executiontimeout = Get-Attr -obj $params -name execution_timeout -default $null $timeout = Get-AnsibleParam -obj $params -name "timeout" -type "int" -default 2700 -aliases "execution_timeout"
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present","absent","latest" $state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present","absent","latest","reinstalled"
$installargs = Get-AnsibleParam -obj $params -name "install_args" -type "str" $installargs = Get-AnsibleParam -obj $params -name "install_args" -type "str"
$packageparams = Get-AnsibleParam -obj $params -name "params" -type "str" $packageparams = Get-AnsibleParam -obj $params -name "params" -type "str"
$allowemptychecksums = Get-AnsibleParam -obj $params -name "allow_empty_checksums" -type "bool" -default $false $allowemptychecksums = Get-AnsibleParam -obj $params -name "allow_empty_checksums" -type "bool" -default $false
@ -143,7 +144,7 @@ Function Choco-Upgrade
[Parameter(Mandatory=$false, Position=9)] [Parameter(Mandatory=$false, Position=9)]
[bool]$ignoredependencies, [bool]$ignoredependencies,
[Parameter(Mandatory=$false, Position=10)] [Parameter(Mandatory=$false, Position=10)]
[string]$executiontimeout [int]$timeout
) )
if (-not (Choco-IsInstalled $package)) if (-not (Choco-IsInstalled $package))
@ -151,7 +152,12 @@ Function Choco-Upgrade
throw "$package is not installed, you cannot upgrade" throw "$package is not installed, you cannot upgrade"
} }
$cmd = "$executable upgrade -dv -y $package" $cmd = "$executable upgrade -dv -y $package -timeout $timeout"
if ($check_mode)
{
$cmd += " -whatif"
}
if ($version) if ($version)
{ {
@ -193,11 +199,6 @@ Function Choco-Upgrade
$cmd += " -ignoredependencies" $cmd += " -ignoredependencies"
} }
if ($executiontimeout)
{
$cmd += " --execution-timeout=$executiontimeout"
}
$output = invoke-expression $cmd $output = invoke-expression $cmd
$result.rc = $LastExitCode $result.rc = $LastExitCode
@ -243,7 +244,7 @@ Function Choco-Install
[Parameter(Mandatory=$false, Position=10)] [Parameter(Mandatory=$false, Position=10)]
[bool]$ignoredependencies, [bool]$ignoredependencies,
[Parameter(Mandatory=$false, Position=11)] [Parameter(Mandatory=$false, Position=11)]
[string]$executiontimeout [int]$timeout
) )
if ((Choco-IsInstalled $package) -and -not $force) if ((Choco-IsInstalled $package) -and -not $force)
@ -253,7 +254,7 @@ Function Choco-Install
Choco-Upgrade -package $package -version $version -source $source -force $force ` Choco-Upgrade -package $package -version $version -source $source -force $force `
-installargs $installargs -packageparams $packageparams ` -installargs $installargs -packageparams $packageparams `
-allowemptychecksums $allowemptychecksums -ignorechecksums $ignorechecksums ` -allowemptychecksums $allowemptychecksums -ignorechecksums $ignorechecksums `
-ignoredependencies $ignoredependencies -executiontimeout $executiontimeout -ignoredependencies $ignoredependencies -timeout $timeout
return return
} }
@ -264,7 +265,12 @@ Function Choco-Install
} }
} }
$cmd = "$executable install -dv -y $package" $cmd = "$executable install -dv -y $package -timeout $timeout"
if ($check_mode)
{
$cmd += " -whatif"
}
if ($version) if ($version)
{ {
@ -306,11 +312,6 @@ Function Choco-Install
$cmd += " -ignoredependencies" $cmd += " -ignoredependencies"
} }
if ($executiontimeout)
{
$cmd += " --execution-timeout=$executiontimeout"
}
$results = invoke-expression $cmd $results = invoke-expression $cmd
$result.rc = $LastExitCode $result.rc = $LastExitCode
@ -336,7 +337,7 @@ Function Choco-Uninstall
[Parameter(Mandatory=$false, Position=3)] [Parameter(Mandatory=$false, Position=3)]
[bool]$force, [bool]$force,
[Parameter(Mandatory=$false, Position=4)] [Parameter(Mandatory=$false, Position=4)]
[string]$executiontimeout [int]$timeout
) )
@ -345,7 +346,12 @@ Function Choco-Uninstall
return return
} }
$cmd = "$executable uninstall -dv -y $package" $cmd = "$executable uninstall -dv -y $package -timeout $timeout"
if ($check_mode)
{
$cmd += " -whatif"
}
if ($version) if ($version)
{ {
@ -362,11 +368,6 @@ Function Choco-Uninstall
$cmd += " -params '$packageparams'" $cmd += " -params '$packageparams'"
} }
if ($executiontimeout)
{
$cmd += " --execution-timeout=$executiontimeout"
}
$results = invoke-expression $cmd $results = invoke-expression $cmd
$result.rc = $LastExitCode $result.rc = $LastExitCode
@ -389,19 +390,27 @@ Try
Choco-Install -package $package -version $version -source $source -force $force ` Choco-Install -package $package -version $version -source $source -force $force `
-installargs $installargs -packageparams $packageparams ` -installargs $installargs -packageparams $packageparams `
-allowemptychecksums $allowemptychecksums -ignorechecksums $ignorechecksums ` -allowemptychecksums $allowemptychecksums -ignorechecksums $ignorechecksums `
-ignoredependencies $ignoredependencies -executiontimeout $executiontimeout -ignoredependencies $ignoredependencies -timeout $timeout
} }
elseif ($state -eq "latest") elseif ($state -eq "latest")
{ {
Choco-Upgrade -package $package -version $version -source $source -force $force ` Choco-Upgrade -package $package -version $version -source $source -force $force `
-installargs $installargs -packageparams $packageparams ` -installargs $installargs -packageparams $packageparams `
-allowemptychecksums $allowemptychecksums -ignorechecksums $ignorechecksums ` -allowemptychecksums $allowemptychecksums -ignorechecksums $ignorechecksums `
-ignoredependencies $ignoredependencies -ignoredependencies $ignoredependencies -timeout $timeout
} }
elseif ($state -eq "absent") elseif ($state -eq "absent")
{ {
Choco-Uninstall -package $package -version $version -force $force ` Choco-Uninstall -package $package -version $version -force $force -timeout $timeout
-executiontimeout $executiontimeout }
elseif ($state -eq "reinstalled")
{
Choco-Uninstall -package $package -version $version -force $force -timeout $timeout
Choco-Install -package $package -version $version -source $source -force $force `
-installargs $installargs -packageparams $packageparams `
-allowemptychecksums $allowemptychecksums -ignorechecksums $ignorechecksums `
-ignoredependencies $ignoredependencies -timeout $timeout
} }
Exit-Json $result Exit-Json $result

@ -38,20 +38,21 @@ description:
options: options:
name: name:
description: description:
- Name of the package to be installed - Name of the package to be installed.
required: true required: true
state: state:
description: description:
- State of the package on the system - State of the package on the system.
choices: choices:
- present - present
- absent - absent
- latest - latest
- reinstalled
default: present default: present
force: force:
description: description:
- Forces install of the package (even if it already exists). - Forces install of the package (even if it already exists).
- Using C(force) will cause ansible to always report that a change was made - Using C(force) will cause ansible to always report that a change was made.
choices: choices:
- yes - yes
- no - no
@ -70,10 +71,10 @@ options:
- Ignored when C(state) is set to "absent". - Ignored when C(state) is set to "absent".
source: source:
description: description:
- Specify source rather than using default chocolatey repository - Specify source rather than using default chocolatey repository.
install_args: install_args:
description: description:
- Arguments to pass to the native installer - Arguments to pass to the native installer.
version_added: '2.1' version_added: '2.1'
params: params:
description: description:
@ -81,24 +82,25 @@ options:
version_added: '2.1' version_added: '2.1'
allow_empty_checksums: allow_empty_checksums:
description: description:
- Allow empty Checksums to be used - Allow empty checksums to be used.
default: false default: false
version_added: '2.2' version_added: '2.2'
ignore_checksums: ignore_checksums:
description: description:
- Ignore Checksums - Ignore checksums altogether.
default: false default: false
version_added: '2.2' version_added: '2.2'
ignore_dependencies: ignore_dependencies:
description: description:
- Ignore dependencies, only install/upgrade the package itself - Ignore dependencies, only install/upgrade the package itself.
default: false default: false
version_added: '2.1' version_added: '2.1'
execution_timeout: timeout:
description: description:
- Timeout to pass to the native installer - The time to allow chocolatey to finish before timing out.
required: false default: 2700
version_added: '2.3' version_added: '2.3'
aliases: [ execution_timeout ]
author: "Trond Hindenes (@trondhindenes), Peter Mounce (@petemounce), Pepe Barbe (@elventear), Adam Keech (@smadam813)" author: "Trond Hindenes (@trondhindenes), Peter Mounce (@petemounce), Pepe Barbe (@elventear), Adam Keech (@smadam813)"
''' '''

Loading…
Cancel
Save