Skip to content

Commit

Permalink
Added resource tracing info
Browse files Browse the repository at this point in the history
  • Loading branch information
anmenaga committed Dec 1, 2023
1 parent 52329a5 commit 9eddd7f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
7 changes: 7 additions & 0 deletions dsc/tests/dsc_args.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,11 @@ resources:
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'DEBUG'
$LASTEXITCODE | Should -Be 0
}

It 'resource tracing shows up' {
# Assumption here is that DSC/PowerShellGroup provider is visible
dsc -l trace resource list 2> $TestDrive/tracing.txt
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'PSModulePath'
$LASTEXITCODE | Should -Be 0
}
}
2 changes: 2 additions & 0 deletions dsc_lib/src/discovery/command_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::discovery::discovery_trait::ResourceDiscovery;
use crate::dscresources::dscresource::{DscResource, ImplementedAs};
use crate::dscresources::resource_manifest::{ResourceManifest, import_manifest};
use crate::dscresources::command_resource::invoke_command;
use crate::dscresources::command_resource::print_resource_traces;
use crate::dscerror::DscError;
use std::collections::BTreeMap;
use std::env;
Expand Down Expand Up @@ -122,6 +123,7 @@ impl CommandDiscovery {
continue;
},
};
print_resource_traces(&stderr);

if exit_code != 0 {
/* In case of "resource list" operation - print failure from provider as warning
Expand Down
29 changes: 28 additions & 1 deletion dsc_lib/src/dscresources/command_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,30 @@ use serde_json::Value;
use std::{collections::HashMap, process::Command, io::{Write, Read}, process::Stdio};
use crate::dscerror::DscError;
use super::{dscresource::get_diff,resource_manifest::{ResourceManifest, InputKind, ReturnKind, SchemaKind}, invoke_result::{GetResult, SetResult, TestResult, ValidateResult, ExportResult}};
use tracing::{debug, info};
use tracing::{debug, info, trace};

pub const EXIT_PROCESS_TERMINATED: i32 = 0x102;


pub fn print_resource_traces(stderr: &String)
{
if !stderr.is_empty()
{
for trace_line in stderr.lines() {
match serde_json::from_str::<Value>(trace_line){
Result::Ok(json_obj) => {
let trace = json_obj.get("Trace");
if trace.is_some()
{
trace!("{}", trace.unwrap().as_str().unwrap_or_default());
}
},
Result::Err(_) => {}
};
}
}
}

/// Invoke the get operation on a resource
///
/// # Arguments
Expand Down Expand Up @@ -49,6 +69,7 @@ pub fn invoke_get(resource: &ResourceManifest, cwd: &str, filter: &str) -> Resul

info!("Invoking get {} using {}", &resource.resource_type, &resource.get.executable);
let (exit_code, stdout, stderr) = invoke_command(&resource.get.executable, get_args, input_filter, Some(cwd), env)?;
print_resource_traces(&stderr);
if exit_code != 0 {
return Err(DscError::Command(resource.resource_type.clone(), exit_code, stderr));
}
Expand Down Expand Up @@ -131,6 +152,7 @@ pub fn invoke_set(resource: &ResourceManifest, cwd: &str, desired: &str, skip_te

info!("Getting current state for set by invoking get {} using {}", &resource.resource_type, &resource.get.executable);
let (exit_code, stdout, stderr) = invoke_command(&resource.get.executable, get_args, get_input, Some(cwd), get_env)?;
print_resource_traces(&stderr);
if exit_code != 0 {
return Err(DscError::Command(resource.resource_type.clone(), exit_code, stderr));
}
Expand All @@ -144,6 +166,7 @@ pub fn invoke_set(resource: &ResourceManifest, cwd: &str, desired: &str, skip_te

info!("Invoking set {} using {}", &resource.resource_type, &set.executable);
let (exit_code, stdout, stderr) = invoke_command(&set.executable, set.args.clone(), input_desired, Some(cwd), env)?;
print_resource_traces(&stderr);
if exit_code != 0 {
return Err(DscError::Command(resource.resource_type.clone(), exit_code, stderr));
}
Expand Down Expand Up @@ -231,6 +254,7 @@ pub fn invoke_test(resource: &ResourceManifest, cwd: &str, expected: &str) -> Re

info!("Invoking test {} using {}", &resource.resource_type, &test.executable);
let (exit_code, stdout, stderr) = invoke_command(&test.executable, args, input_expected, Some(cwd), env)?;
print_resource_traces(&stderr);
if exit_code != 0 {
return Err(DscError::Command(resource.resource_type.clone(), exit_code, stderr));
}
Expand Down Expand Up @@ -306,6 +330,7 @@ pub fn invoke_validate(resource: &ResourceManifest, cwd: &str, config: &str) ->
};

let (exit_code, stdout, stderr) = invoke_command(&validate.executable, validate.args.clone(), Some(config), Some(cwd), None)?;
print_resource_traces(&stderr);
if exit_code != 0 {
return Err(DscError::Command(resource.resource_type.clone(), exit_code, stderr));
}
Expand All @@ -331,6 +356,7 @@ pub fn get_schema(resource: &ResourceManifest, cwd: &str) -> Result<String, DscE
match schema_kind {
SchemaKind::Command(ref command) => {
let (exit_code, stdout, stderr) = invoke_command(&command.executable, command.args.clone(), None, Some(cwd), None)?;
print_resource_traces(&stderr);
if exit_code != 0 {
return Err(DscError::Command(resource.resource_type.clone(), exit_code, stderr));
}
Expand Down Expand Up @@ -375,6 +401,7 @@ pub fn invoke_export(resource: &ResourceManifest, cwd: &str) -> Result<ExportRes
};

let (exit_code, stdout, stderr) = invoke_command(&export.executable, export.args.clone(), None, Some(cwd), None)?;
print_resource_traces(&stderr);
if exit_code != 0 {
return Err(DscError::Command(resource.resource_type.clone(), exit_code, stderr));
}
Expand Down
18 changes: 14 additions & 4 deletions powershellgroup/powershellgroup.resource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,23 @@ if ($null -eq $DscModule)

Import-Module $DscModule -DisableNameChecking

# Adding some debug info to STDERR
$m = gmo PSDesiredStateConfiguration
$trace = @{"Trace"="PSVersion="+$PSVersionTable.PSVersion.ToString()} | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)
$trace = @{"Trace"="PSPath="+$PSHome} | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)
$trace = @{"Trace"="ModuleVersion="+$m.Version.ToString()} | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)
$trace = @{"Trace"="ModulePath="+$m.Path} | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)
$trace = @{"Trace"="PSModulePath="+$env:PSModulePath} | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)

if ($Operation -eq 'List')
{
$DscResources= Get-DscResource
#TODO: following should be added to debug stream of every operation
#$m = gmo PSDesiredStateConfiguration
#$r += @{"DebugInfo"=@{"ModuleVersion"=$m.Version.ToString();"ModulePath"=$m.Path;"PSVersion"=$PSVersionTable.PSVersion.ToString();"PSPath"=$PSHome}}
#$r[0] | ConvertTo-Json -Compress -Depth 3

foreach ($r in $DscResources)
{
if ($r.ImplementedAs -eq "Binary")
Expand Down

0 comments on commit 9eddd7f

Please sign in to comment.