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 $log_path = [System.IO.Path]::Combine($async_dir, $jid)
17 
18 If(-not $(Test-Path -LiteralPath $log_path))
19 {
20     Fail-Json @{ansible_job_id=$jid; started=1; finished=1} "could not find job at '$async_dir'"
21 }
22 
23 If($mode -eq "cleanup") {
24     Remove-Item -LiteralPath $log_path -Recurse
25     Exit-Json @{ansible_job_id=$jid; erased=$log_path}
26 }
27 
28 # NOT in cleanup mode, assume regular status mode
29 # no remote kill mode currently exists, but probably should
30 # consider log_path + ".pid" file and also unlink that above
31 
32 $data = $null
33 Try {
34     $data_raw = Get-Content -LiteralPath $log_path
35 
36     # TODO: move this into module_utils/powershell.ps1?
37     $jss = New-Object System.Web.Script.Serialization.JavaScriptSerializer
38     $data = $jss.DeserializeObject($data_raw)
39 }
40 Catch {
41     If(-not $data_raw) {
42         # file not written yet?  That means it is running
43         Exit-Json @{results_file=$log_path; ansible_job_id=$jid; started=1; finished=0}
44     }
45     Else {
46         Fail-Json @{ansible_job_id=$jid; results_file=$log_path; started=1; finished=1} "Could not parse job output: $data"
47     }
48 }
49 
50 If (-not $data.ContainsKey("started")) {
51     $data['finished'] = 1
52     $data['ansible_job_id'] = $jid
53 }
54 ElseIf (-not $data.ContainsKey("finished")) {
55     $data['finished'] = 0
56 }
57 
58 Exit-Json $data
59