From 737a0d601565b5291434afd842c8c17547a64f19 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Tue, 3 Oct 2017 07:34:00 +1100 Subject: [PATCH] win_dotnet_ngen: fix after broken in 2.4 (#31076) * win_dotnet_ngen: fix after broken in 2.4 * added description to return values (cherry picked from commit 12a4dca447120867965d8a970d260152a77d0be5) --- .../modules/windows/win_dotnet_ngen.ps1 | 101 ++++++++---------- .../modules/windows/win_dotnet_ngen.py | 53 ++++++++- .../targets/win_dotnet_ngen/aliases | 1 + .../targets/win_dotnet_ngen/tasks/main.yml | 20 ++++ 4 files changed, 119 insertions(+), 56 deletions(-) create mode 100644 test/integration/targets/win_dotnet_ngen/aliases create mode 100644 test/integration/targets/win_dotnet_ngen/tasks/main.yml diff --git a/lib/ansible/modules/windows/win_dotnet_ngen.ps1 b/lib/ansible/modules/windows/win_dotnet_ngen.ps1 index 947d32cde7c..e7ec31939f4 100644 --- a/lib/ansible/modules/windows/win_dotnet_ngen.ps1 +++ b/lib/ansible/modules/windows/win_dotnet_ngen.ps1 @@ -1,71 +1,62 @@ #!powershell # This file is part of Ansible -# + # Copyright 2015, Peter Mounce -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright (c) 2017 Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -$ErrorActionPreference = "Stop" +#Requires -Module Ansible.ModuleUtils.Legacy.psm1 +#Requires -Module Ansible.ModuleUtils.CommandUtil.psm1 -# WANT_JSON -# POWERSHELL_COMMON +$ErrorActionPreference = 'Stop' -$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 $result = @{ changed = $false } -function Invoke-NGen -{ - [CmdletBinding()] - - param - ( - [Parameter(Mandatory=$false, Position=0)] [string] $arity = "" - ) - - if ($arity -eq $null) - { - $arity = "" - } - $cmd = "$($env:windir)\microsoft.net\framework$($arity)\v4.0.30319\ngen.exe" - if (test-path $cmd) - { - $update = Invoke-Expression "$cmd update /force"; - $(result.dotnet_ngen$($arity)_update_exit_code) = $lastexitcode - $(result.dotnet_ngen$($arity)_update_output) = $update - $eqi = Invoke-Expression "$cmd executequeueditems"; - $(result.dotnet_ngen$($arity)_eqi_exit_code) = $lastexitcode - $(result.dotnet_ngen$($arity)_eqi_output) = $eqi - +Function Invoke-Ngen($architecture="") { + $cmd = "$($env:windir)\Microsoft.NET\Framework$($architecture)\v4.0.30319\ngen.exe" + + if (Test-Path -Path $cmd) { + $arguments = "update /force" + if ($check_mode) { + $ngen_result = @{ + rc = 0 + stdout = "check mode output for $cmd $arguments" + } + } else { + try { + $ngen_result = Run-Command -command "$cmd $arguments" + } catch { + Fail-Json -obj $result -message "failed to execute '$cmd $arguments': $($_.Exception.Message)" + } + } + $result."dotnet_ngen$($architecture)_update_exit_code" = $ngen_result.rc + $result."dotnet_ngen$($architecture)_update_output" = $ngen_result.stdout + + $arguments = "executeQueuedItems" + if ($check_mode) { + $executed_queued_items = @{ + rc = 0 + stdout = "check mode output for $cmd $arguments" + } + } else { + try { + $executed_queued_items = Run-Command -command "$cmd $arguments" + } catch { + Fail-Json -obj $result -message "failed to execute '$cmd $arguments': $($_.Exception.Message)" + } + } + $result."dotnet_ngen$($architecture)_eqi_exit_code" = $executed_queued_items.rc + $result."dotnet_ngen$($architecture)_eqi_output" = $executed_queued_items.stdout $result.changed = $true } - else - { - Write-Host "Not found: $cmd" - } } -Try -{ - Invoke-NGen - Invoke-NGen -arity "64" +Invoke-Ngen +Invoke-Ngen -architecture "64" - Exit-Json $result; -} -Catch -{ - Fail-Json $result $_.Exception.Message -} +Exit-Json -obj $result diff --git a/lib/ansible/modules/windows/win_dotnet_ngen.py b/lib/ansible/modules/windows/win_dotnet_ngen.py index 394621e03ec..4cc061a3b8a 100644 --- a/lib/ansible/modules/windows/win_dotnet_ngen.py +++ b/lib/ansible/modules/windows/win_dotnet_ngen.py @@ -45,6 +45,57 @@ options: {} ''' EXAMPLES = r''' - # Run ngen tasks +- name: run ngen tasks win_dotnet_ngen: ''' + +RETURN = r''' +dotnet_ngen_update_exit_code: + description: The exit code after running the 32-bit ngen.exe update /force + command. + returned: 32-bit ngen executable exists + type: int + sample: 0 +dotnet_ngen_update_output: + description: The stdout after running the 32-bit ngen.exe update /force + command. + returned: 32-bit ngen executable exists + type: str + sample: sample output +dotnet_ngen_eqi_exit_code: + description: The exit code after running the 32-bit ngen.exe + executeQueuedItems command. + returned: 32-bit ngen executable exists + type: int + sample: 0 +dotnet_ngen_eqi_output: + description: The stdout after running the 32-bit ngen.exe executeQueuedItems + command. + returned: 32-bit ngen executable exists + type: str + sample: sample output +dotnet_ngen64_update_exit_code: + description: The exit code after running the 64-bit ngen.exe update /force + command. + returned: 64-bit ngen executable exists + type: int + sample: 0 +dotnet_ngen64_update_output: + description: The stdout after running the 64-bit ngen.exe update /force + command. + returned: 64-bit ngen executable exists + type: str + sample: sample output +dotnet_ngen64_eqi_exit_code: + description: The exit code after running the 64-bit ngen.exe + executeQueuedItems command. + returned: 64-bit ngen executable exists + type: int + sample: 0 +dotnet_ngen64_eqi_output: + description: The stdout after running the 64-bit ngen.exe executeQueuedItems + command. + returned: 64-bit ngen executable exists + type: str + sample: sample output +''' diff --git a/test/integration/targets/win_dotnet_ngen/aliases b/test/integration/targets/win_dotnet_ngen/aliases new file mode 100644 index 00000000000..10e03fc2bf7 --- /dev/null +++ b/test/integration/targets/win_dotnet_ngen/aliases @@ -0,0 +1 @@ +windows/ci/group1 diff --git a/test/integration/targets/win_dotnet_ngen/tasks/main.yml b/test/integration/targets/win_dotnet_ngen/tasks/main.yml new file mode 100644 index 00000000000..8fd2594483f --- /dev/null +++ b/test/integration/targets/win_dotnet_ngen/tasks/main.yml @@ -0,0 +1,20 @@ +# this only tests check mode as the full run can take several minutes to +# complete, this way we at least verify the script is parsable +--- +- name: run in check mode + win_dotnet_ngen: + register: result_check + check_mode: yes + +- name: assert run in check mode + assert: + that: + - result_check|changed + - result_check.dotnet_ngen_update_exit_code == 0 + - result_check.dotnet_ngen_update_output == "check mode output for C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\ngen.exe update /force" + - result_check.dotnet_ngen_eqi_exit_code == 0 + - result_check.dotnet_ngen_eqi_output == "check mode output for C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\ngen.exe executeQueuedItems" + - result_check.dotnet_ngen64_update_exit_code == 0 + - result_check.dotnet_ngen64_update_output == "check mode output for C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\ngen.exe update /force" + - result_check.dotnet_ngen64_eqi_exit_code == 0 + - result_check.dotnet_ngen64_eqi_output == "check mode output for C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\ngen.exe executeQueuedItems"