Skip to content

Commit 12b423a

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
Add better tracing and fix propagation of inDesiredState
1 parent a1eab51 commit 12b423a

File tree

5 files changed

+63
-21
lines changed

5 files changed

+63
-21
lines changed

dsc_lib/src/dscresources/command_resource.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -870,15 +870,15 @@ pub fn log_stderr_line<'a>(process_id: &u32, trace_line: &'a str) -> &'a str
870870
}
871871
}
872872
else if let Ok(json_obj) = serde_json::from_str::<Value>(trace_line) {
873-
if let Some(msg) = json_obj.get("Error") {
873+
if let Some(msg) = json_obj.get("error") {
874874
error!("PID {process_id}: {}", msg.as_str().unwrap_or_default());
875-
} else if let Some(msg) = json_obj.get("Warning") {
875+
} else if let Some(msg) = json_obj.get("warn") {
876876
warn!("PID {process_id}: {}", msg.as_str().unwrap_or_default());
877-
} else if let Some(msg) = json_obj.get("Info") {
877+
} else if let Some(msg) = json_obj.get("info") {
878878
info!("PID {process_id}: {}", msg.as_str().unwrap_or_default());
879-
} else if let Some(msg) = json_obj.get("Debug") {
879+
} else if let Some(msg) = json_obj.get("debug") {
880880
debug!("PID {process_id}: {}", msg.as_str().unwrap_or_default());
881-
} else if let Some(msg) = json_obj.get("Trace") {
881+
} else if let Some(msg) = json_obj.get("trace") {
882882
trace!("PID {process_id}: {}", msg.as_str().unwrap_or_default());
883883
} else {
884884
// the line is a valid json, but not one of standard trace lines - return it as filtered stderr_line

powershell-adapter/Tests/win_powershellgroup.tests.ps1

+37
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,41 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
8787
"$TestDrive/tracing.txt" | Should -Not -FileContentMatchExactly 'Constructing Get-DscResource cache'
8888
}
8989
}
90+
91+
It '_inDesiredState is returned correction: <Context>' -Skip:(!$IsWindows) -TestCases @(
92+
@{ Context = 'Both running'; FirstState = 'Running'; SecondState = 'Running' }
93+
@{ Context = 'Both stopped'; FirstState = 'Stopped'; SecondState = 'Stopped' }
94+
@{ Context = 'First Stopped'; FirstState = 'Stopped'; SecondState = 'Running' }
95+
@{ Context = 'First Running'; FirstState = 'Running'; SecondState = 'Stopped' }
96+
) {
97+
param($Context, $FirstState, $SecondState)
98+
$yaml = @"
99+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
100+
resources:
101+
- name: Use Windows PowerShell resources
102+
type: Microsoft.Windows/WindowsPowerShell
103+
properties:
104+
resources:
105+
- name: Check Spooler service 1
106+
type: PsDesiredStateConfiguration/Service
107+
properties:
108+
Name: Spooler
109+
State: $FirstState
110+
- name: Check Spooler service 2
111+
type: PsDesiredStateConfiguration/Service
112+
properties:
113+
Name: Spooler
114+
State: $SecondState
115+
"@
116+
117+
$inDesiredState = if ($FirstState -eq $SecondState) {
118+
$FirstState -eq (Get-Service Spooler).Status
119+
} else {
120+
$false
121+
}
122+
123+
$out = dsc config test -i $yaml | ConvertFrom-Json
124+
$LASTEXITCODE | Should -Be 0
125+
$out.results[0].result.inDesiredState | Should -Be $inDesiredState
126+
}
90127
}

powershell-adapter/psDscAdapter/powershell.resource.ps1

+15-12
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ if ($Operation -eq 'ClearCache') {
4646
}
4747

4848
if ('Validate' -ne $Operation) {
49-
# write $jsonInput to STDERR for debugging
50-
$trace = @{'debug' = 'jsonInput=' + $jsonInput } | ConvertTo-Json -Compress
51-
$host.ui.WriteErrorLine($trace)
49+
Write-DscTrace -Operation Debug -Message "jsonInput=$jsonInput"
5250

5351
# load private functions of psDscAdapter stub module
5452
if ($PSVersionTable.PSVersion.Major -le 5) {
@@ -135,16 +133,14 @@ switch ($Operation) {
135133
{ @('Get','Set','Test','Export') -contains $_ } {
136134
$desiredState = $psDscAdapter.invoke( { param($jsonInput) Get-DscResourceObject -jsonInput $jsonInput }, $jsonInput )
137135
if ($null -eq $desiredState) {
138-
$trace = @{'debug' = 'ERROR: Failed to create configuration object from provided input JSON.' } | ConvertTo-Json -Compress
139-
$host.ui.WriteErrorLine($trace)
136+
Write-DscTrace -Operation Error -message 'Failed to create configuration object from provided input JSON.'
140137
exit 1
141138
}
142139

143140
# only need to cache the resources that are used
144141
$dscResourceModules = $desiredState | ForEach-Object { $_.Type.Split('/')[0] }
145142
if ($null -eq $dscResourceModules) {
146-
$trace = @{'debug' = 'ERROR: Could not get list of DSC resource types from provided JSON.' } | ConvertTo-Json -Compress
147-
$host.ui.WriteErrorLine($trace)
143+
Write-DscTrace -Operation Error -Message 'Could not get list of DSC resource types from provided JSON.'
148144
exit 1
149145
}
150146

@@ -162,21 +158,28 @@ switch ($Operation) {
162158
}
163159
}
164160

161+
$inDesiredState = $true
165162
foreach ($ds in $desiredState) {
166163
# process the INPUT (desiredState) for each resource as dscresourceInfo and return the OUTPUT as actualState
167164
$actualState = $psDscAdapter.invoke( { param($op, $ds, $dscResourceCache) Invoke-DscOperation -Operation $op -DesiredState $ds -dscResourceCache $dscResourceCache }, $Operation, $ds, $dscResourceCache)
168165
if ($null -eq $actualState) {
169-
$trace = @{'debug' = 'ERROR: Incomplete GET for resource ' + $ds.Name } | ConvertTo-Json -Compress
170-
$host.ui.WriteErrorLine($trace)
166+
Write-DscTrace -Operation Error -Message 'Incomplete GET for resource ' + $ds.Name
171167
exit 1
172168
}
169+
if ($null -ne $actualState.Properties -and $actualState.Properties.InDesiredState -eq $false) {
170+
$inDesiredState = $false
171+
}
173172
$result += $actualState
174173
}
175174

176175
# OUTPUT json to stderr for debug, and to stdout
177-
$result = @{ result = $result } | ConvertTo-Json -Depth 10 -Compress
178-
$trace = @{'debug' = 'jsonOutput=' + $result } | ConvertTo-Json -Compress
179-
$host.ui.WriteErrorLine($trace)
176+
if ($Operation -eq 'Test') {
177+
$result = @{ result = $result; _inDesiredState = $inDesiredState } | ConvertTo-Json -Depth 10 -Compress
178+
}
179+
else {
180+
$result = @{ result = $result } | ConvertTo-Json -Depth 10 -Compress
181+
}
182+
Write-DscTrace -Operation Debug -Message "jsonOutput=$result"
180183
return $result
181184
}
182185
'Validate' {

powershell-adapter/psDscAdapter/psDscAdapter.psm1

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Write-DscTrace {
1313
[string]$Message
1414
)
1515

16-
$trace = @{$Operation = $Message } | ConvertTo-Json -Compress
16+
$trace = @{$Operation.ToLower() = $Message } | ConvertTo-Json -Compress
1717
$host.ui.WriteErrorLine($trace)
1818
}
1919

@@ -438,7 +438,7 @@ function Invoke-DscOperation {
438438
'PowerShell version: ' + $psVersion | Write-DscTrace
439439

440440
# get details from cache about the DSC resource, if it exists
441-
$cachedDscResourceInfo = $dscResourceCache | Where-Object Type -EQ $DesiredState.type | ForEach-Object DscResourceInfo
441+
$cachedDscResourceInfo = $dscResourceCache | Where-Object Type -EQ $DesiredState.type | ForEach-Object DscResourceInfo | Select-Object -First 1
442442

443443
# if the resource is found in the cache, get the actual state
444444
if ($cachedDscResourceInfo) {

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Write-DscTrace {
1313
[string]$Message
1414
)
1515

16-
$trace = @{$Operation = $Message } | ConvertTo-Json -Compress
16+
$trace = @{$Operation.ToLower() = $Message } | ConvertTo-Json -Compress
1717
$host.ui.WriteErrorLine($trace)
1818
}
1919

@@ -324,7 +324,7 @@ function Invoke-DscOperation {
324324
'PSDesiredStateConfiguration module version: ' + $moduleVersion | Write-DscTrace
325325

326326
# get details from cache about the DSC resource, if it exists
327-
$cachedDscResourceInfo = $dscResourceCache | Where-Object Type -EQ $DesiredState.type | ForEach-Object DscResourceInfo
327+
$cachedDscResourceInfo = $dscResourceCache | Where-Object Type -EQ $DesiredState.type | ForEach-Object DscResourceInfo | Select-Object -First 1
328328

329329
# if the resource is found in the cache, get the actual state
330330
if ($cachedDscResourceInfo) {
@@ -367,6 +367,7 @@ function Invoke-DscOperation {
367367

368368
# using the cmdlet the appropriate dsc module, and handle errors
369369
try {
370+
Write-DscTrace -Operation Debug -Message "Module: $($cachedDscResourceInfo.ModuleName), Name: $($cachedDscResourceInfo.Name), Property: $($property)"
370371
$invokeResult = Invoke-DscResource -Method $Operation -ModuleName $cachedDscResourceInfo.ModuleName -Name $cachedDscResourceInfo.Name -Property $property
371372

372373
if ($invokeResult.GetType().Name -eq 'Hashtable') {
@@ -381,6 +382,7 @@ function Invoke-DscOperation {
381382
$addToActualState.properties = $ResultProperties
382383
}
383384
catch {
385+
$_.Exception | Format-List * -Force | Out-String | Write-DscTrace -Operation Debug
384386
'Exception: ' + $_.Exception.Message | Write-DscTrace -Operation Error
385387
exit 1
386388
}

0 commit comments

Comments
 (0)