win_chocolatey: fixes for proxy support (#28809)

This commit is contained in:
Jordan Borean 2017-08-29 23:26:13 -04:00 committed by GitHub
parent a41da28f3f
commit 113635e44c
2 changed files with 93 additions and 57 deletions

View file

@ -9,7 +9,6 @@
$ErrorActionPreference = 'Stop' $ErrorActionPreference = 'Stop'
# As of chocolatey 0.9.10, non-zero success exit codes can be returned # As of chocolatey 0.9.10, non-zero success exit codes can be returned
# See https://github.com/chocolatey/choco/issues/512#issuecomment-214284461 # See https://github.com/chocolatey/choco/issues/512#issuecomment-214284461
$successexitcodes = (0, 1605, 1614, 1641, 3010) $successexitcodes = (0, 1605, 1614, 1641, 3010)
@ -34,7 +33,7 @@ $ignoredependencies = Get-AnsibleParam -obj $params -name "ignore_dependencies"
$skipscripts = Get-AnsibleParam -obj $params -name "skip_scripts" -type "bool" -default $false $skipscripts = Get-AnsibleParam -obj $params -name "skip_scripts" -type "bool" -default $false
$proxy_url = Get-AnsibleParam -obj $params -name "proxy_url" -type "str" $proxy_url = Get-AnsibleParam -obj $params -name "proxy_url" -type "str"
$proxy_username = Get-AnsibleParam -obj $params -name "proxy_username" -type "str" $proxy_username = Get-AnsibleParam -obj $params -name "proxy_username" -type "str"
$proxy_password = Get-AnsibleParam -obj $params -name "proxy_password" -type "str" $proxy_password = Get-AnsibleParam -obj $params -name "proxy_password" -type "str" -failifempty ($proxy_username -ne $null)
$result = @{ $result = @{
changed = $false changed = $false
@ -53,7 +52,6 @@ if ($upgrade)
} }
} }
Function Chocolatey-Install-Upgrade Function Chocolatey-Install-Upgrade
{ {
[CmdletBinding()] [CmdletBinding()]
@ -124,9 +122,8 @@ Function Chocolatey-Install-Upgrade
{ {
Add-Warning -obj $result -message "Chocolatey was older than v0.10.5, so it was upgraded during this task run." Add-Warning -obj $result -message "Chocolatey was older than v0.10.5, so it was upgraded during this task run."
$script:options = @( "-dv" ) $script:options = @( "-dv" )
Choco-Upgrade -package chocolatey Choco-Upgrade -package chocolatey -proxy_url $proxy_url -proxy_username $proxy_username -proxy_password $proxy_password
} }
} }
# set the default verbosity options # set the default verbosity options
@ -183,7 +180,6 @@ Function Choco-IsInstalled
return $false return $false
} }
Function Choco-Upgrade Function Choco-Upgrade
{ {
[CmdletBinding()] [CmdletBinding()]
@ -201,7 +197,10 @@ Function Choco-Upgrade
[bool] $allowemptychecksums, [bool] $allowemptychecksums,
[bool] $ignorechecksums, [bool] $ignorechecksums,
[bool] $ignoredependencies, [bool] $ignoredependencies,
[bool] $allowdowngrade [bool] $allowdowngrade,
[string] $proxy_url,
[string] $proxy_username,
[string] $proxy_password
) )
if (-not (Choco-IsInstalled $package)) if (-not (Choco-IsInstalled $package))
@ -266,6 +265,21 @@ Function Choco-Upgrade
$options += "--allow-downgrade" $options += "--allow-downgrade"
} }
if ($proxy_url)
{
$options += "--proxy=`"'$proxy_url'`""
}
if ($proxy_username)
{
$options += "--proxy-user=`"'$proxy_username'`""
}
if ($proxy_password)
{
$options += "--proxy-password=`"'$proxy_password'`""
}
# NOTE: Chocolatey does not use stderr except for help output # NOTE: Chocolatey does not use stderr except for help output
Try { Try {
$output = & $script:executable upgrade $script:options $options $output = & $script:executable upgrade $script:options $options
@ -298,7 +312,6 @@ Function Choco-Upgrade
$result.failed = $false $result.failed = $false
} }
Function Choco-Install Function Choco-Install
{ {
[CmdletBinding()] [CmdletBinding()]
@ -316,7 +329,10 @@ Function Choco-Install
[bool] $allowemptychecksums, [bool] $allowemptychecksums,
[bool] $ignorechecksums, [bool] $ignorechecksums,
[bool] $ignoredependencies, [bool] $ignoredependencies,
[bool] $allowdowngrade [bool] $allowdowngrade,
[string] $proxy_url,
[string] $proxy_username,
[string] $proxy_password
) )
if (Choco-IsInstalled $package) if (Choco-IsInstalled $package)
@ -327,7 +343,8 @@ Function Choco-Install
-skipscripts $skipscripts -source $source -installargs $installargs ` -skipscripts $skipscripts -source $source -installargs $installargs `
-packageparams $packageparams -allowemptychecksums $allowemptychecksums ` -packageparams $packageparams -allowemptychecksums $allowemptychecksums `
-ignorechecksums $ignorechecksums -ignoredependencies $ignoredependencies ` -ignorechecksums $ignorechecksums -ignoredependencies $ignoredependencies `
-allowdowngrade $allowdowngrade -allowdowngrade $allowdowngrade -proxy_url $proxy_url `
-proxy_username $proxy_username -proxy_password $proxy_password
return return
} }
elseif (-not $force) elseif (-not $force)
@ -388,6 +405,21 @@ Function Choco-Install
$options += "--skip-scripts" $options += "--skip-scripts"
} }
if ($proxy_url)
{
$options += "--proxy=`"'$proxy_url'`""
}
if ($proxy_username)
{
$options += "--proxy-user=`"'$proxy_username'`""
}
if ($proxy_password)
{
$options += "--proxy-password=`"'$proxy_password'`""
}
# NOTE: Chocolatey does not use stderr except for help output # NOTE: Chocolatey does not use stderr except for help output
Try { Try {
$output = & $script:executable install $script:options $options $output = & $script:executable install $script:options $options
@ -485,50 +517,8 @@ Function Choco-Uninstall
$result.failed = $false $result.failed = $false
} }
Function Choco-ConfigureProxy
{
[CmdletBinding()]
param(
[string] $proxy_url,
[string] $proxy_username,
[string] $proxy_password
)
$hash = @{
proxy = $proxy_url
proxyUser = $proxy_username
proxyPassword = $proxy_password
}
foreach ($h in $hash.GetEnumerator()) {
if ($($h.Value))
{
$cmd = "$executable config set $($h.Name) $($h.Value)"
}
else
{
$cmd = "$executable config unset $($h.Name)"
}
$results = Invoke-Expression $cmd
if ($LastExitCode -ne 0)
{
$result.choco_error_cmd = $cmd
$result.choco_error_log = $results
Throw "Error setting $($h.Name) with $($h.Value)"
}
If ("$results" -notmatch "Nothing to change. Config already set.")
{
$result.changed = $true
}
}
}
Chocolatey-Install-Upgrade Chocolatey-Install-Upgrade
Choco-ConfigureProxy
if ($state -in ("absent", "reinstalled")) { if ($state -in ("absent", "reinstalled")) {
Choco-Uninstall -package $package -version $version -force $force -timeout $timeout ` Choco-Uninstall -package $package -version $version -force $force -timeout $timeout `
@ -542,8 +532,8 @@ if ($state -in ("downgrade", "latest", "present", "reinstalled")) {
-skipscripts $skipscripts -source $source -installargs $installargs ` -skipscripts $skipscripts -source $source -installargs $installargs `
-packageparams $packageparams -allowemptychecksums $allowemptychecksums ` -packageparams $packageparams -allowemptychecksums $allowemptychecksums `
-ignorechecksums $ignorechecksums -ignoredependencies $ignoredependencies ` -ignorechecksums $ignorechecksums -ignoredependencies $ignoredependencies `
-allowdowngrade ($state -eq "downgrade") -allowdowngrade ($state -eq "downgrade") -proxy_url $proxy_url `
-proxy_username $proxy_username -proxy_password $proxy_password
} }
Exit-Json -obj $result Exit-Json -obj $result

View file

@ -111,15 +111,19 @@ options:
version_added: '2.4' version_added: '2.4'
proxy_url: proxy_url:
description: description:
- Proxy url used to install chocolatey and the package - Proxy url used to install chocolatey and the package.
version_added: '2.4' version_added: '2.4'
proxy_username: proxy_username:
description: description:
- Proxy username used to install chocolatey and the package - Proxy username used to install chocolatey and the package.
- When dealing with a username with double quote characters C("), they
need to be escaped with C(\) beforehand. See examples for more details.
version_added: '2.4' version_added: '2.4'
proxy_password: proxy_password:
description: description:
- Proxy password used to install chocolatey and the package - Proxy password used to install chocolatey and the package.
- See notes in C(proxy_username) when dealing with double quotes in a
password.
version_added: '2.4' version_added: '2.4'
notes: notes:
- Provide the C(version) parameter value as a string (e.g. C('6.1')), otherwise it - Provide the C(version) parameter value as a string (e.g. C('6.1')), otherwise it
@ -191,4 +195,46 @@ EXAMPLES = r'''
proxy_url: http://proxy-server:8080/ proxy_url: http://proxy-server:8080/
proxy_username: joe proxy_username: joe
proxy_password: p@ssw0rd proxy_password: p@ssw0rd
- name: Install curl with proxy credentials that contain quotes
win_chocolatey:
name: curl
proxy_url: http://proxy-server:8080/
proxy_username: user with \"escaped\" double quotes
proxy_password: pass with \"escaped\" double quotes
'''
RETURN = r'''
choco_bootstrap_output:
description: DEPRECATED, will be removed in 2.6, use stdout instead.
returned: changed, choco task returned a failure
type: str
sample: Chocolatey upgraded 1/1 packages.
choco_error_cmd:
description: DEPRECATED, will be removed in 2.6, use command instead.
returned: changed, choco task returned a failure
type: str
sample: choco.exe install -r --no-progress -y sysinternals --timeout 2700 --failonunfound
choco_error_log:
description: DEPRECATED, will be removed in 2.6, use stdout instead.
returned: changed, choco task returned a failure
type: str
sample: sysinternals not installed. The package was not found with the source(s) listed
command:
description: The full command used in the chocolatey task.
returned: changed
type: str
sample: choco.exe install -r --no-progress -y sysinternals --timeout 2700 --failonunfound
rc:
description: The return code from the chocolatey task.
returned: changed
type: int
sample: 0
stdout:
description: The stdout from the chocolatey task. The verbosity level of the
messages are affected by Ansible verbosity setting, see notes for more
details.
returned: changed
type: str
sample: Chocolatey upgraded 1/1 packages.
''' '''