Debian Codegraph @

test/support/windows-integration/plugins/modules/async_status.ps1

raw
1 #!powershell
2
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 #Requires -Module Ansible.ModuleUtils.Legacy
6
7 $results = @{changed=$false}
8
9 $parsed_args = Parse-Args $args
10 $jid = Get-AnsibleParam $parsed_args "jid" -failifempty $true -resultobj $results
11 $mode = Get-AnsibleParam $parsed_args "mode" -Default "status" -ValidateSet "status","cleanup"
12
13 # parsed in from the async_status action plugin
14 $async_dir = Get-AnsibleParam $parsed_args "_async_dir" -type "path" -failifempty $true
15
16 Function Convert-JsonObject {
17 param(
18 [Parameter(Mandatory, ValueFromPipeline)]
19 [AllowNull()]
20 [object]
21 $InputObject
22 )
23
24 process {
25 # Using the full type name is important as PSCustomObject is an
26 # alias for PSObject which all piped objects are.
27 if ($InputObject -is [System.Management.Automation.PSCustomObject]) {
28 $value = @{}
29 foreach ($prop in $InputObject.PSObject.Properties) {
30 $value[$prop.Name] = Convert-JsonObject -InputObject $prop.Value
31 }
32 $value
33 }
34 elseif ($InputObject -is [Array]) {
35 , @($InputObject | Convert-JsonObject)
36 }
37 else {
38 $InputObject
39 }
40 }
41 }
42
43 $log_path = [System.IO.Path]::Combine($async_dir, $jid)
44
45 If(-not $(Test-Path $log_path))
46 {
47 Fail-Json @{ansible_job_id=$jid; started=$true; finished=$true} "could not find job at '$async_dir'"
48 }
49
50 If($mode -eq "cleanup") {
51 Remove-Item $log_path -Recurse
52 Exit-Json @{ansible_job_id=$jid; erased=$log_path}
53 }
54
55 # NOT in cleanup mode, assume regular status mode
56 # no remote kill mode currently exists, but probably should
57 # consider log_path + ".pid" file and also unlink that above
58
59 $data = $null
60 Try {
61 $data_raw = Get-Content $log_path
62
63 # TODO: move this into module_utils/powershell.ps1?
64 $data = $data_raw | ConvertFrom-Json | Convert-JsonObject
65 }
66 Catch {
67 If(-not $data_raw) {
68 # file not written yet? That means it is running
69 Exit-Json @{results_file=$log_path; ansible_job_id=$jid; started=1; finished=0}
70 }
71 Else {
72 Fail-Json @{ansible_job_id=$jid; results_file=$log_path; started=1; finished=1} "Could not parse job output: $data"
73 }
74 }
75
76 If (-not $data.ContainsKey("started")) {
77 $data['finished'] = $true
78 $data['ansible_job_id'] = $jid
79 }
80 ElseIf (-not $data.ContainsKey("finished")) {
81 $data['finished'] = $false
82 }
83
84 Exit-Json $data