Cross-platform updates to DSC code in PowerShell (#13399)

This commit is contained in:
Andrew 2021-02-02 14:47:53 -08:00 committed by GitHub
parent 00b4b83839
commit 8e3c3e04f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 2730 additions and 14 deletions

View file

@ -587,8 +587,10 @@ Fix steps:
-not ($Runtime -like 'fxdependent*')) {
$json = & $publishPath\pwsh -noprofile -command {
$expFeatures = [System.Collections.Generic.List[string]]::new()
Get-ExperimentalFeature | ForEach-Object { $expFeatures.Add($_.Name) }
# Special case for DSC code in PS;
# this experimental feature requires new DSC module that is not inbox,
# so we don't want default DSC use case be broken
$expFeatures = Get-ExperimentalFeature | Where-Object Name -NE PS7DscSupport | ForEach-Object -MemberName Name
# Make sure ExperimentalFeatures from modules in PSHome are added
# https://github.com/PowerShell/PowerShell/issues/10550
@ -598,7 +600,7 @@ Fix steps:
}
}
ConvertTo-Json $expFeatures.ToArray()
ConvertTo-Json $expFeatures
}
$config += @{ ExperimentalFeatures = ([string[]] ($json | ConvertFrom-Json)) }

View file

@ -41,9 +41,7 @@ namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal
using (System.Management.Automation.PowerShell powerShell = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace))
{
const string script = "param($targetType,$moduleName) & (Microsoft.PowerShell.Core\\Get-Module $moduleName) { New-Object $targetType } ";
powerShell.AddScript(script);
powerShell.AddScript("param($targetType,$moduleName) & (Microsoft.PowerShell.Core\\Get-Module $moduleName) { New-Object $targetType } ");
powerShell.AddArgument(targetType);
powerShell.AddArgument(moduleName);
@ -945,6 +943,20 @@ namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal
return null;
}
/// <summary>
/// Reads CIM MOF schema file and returns classes defined in it.
/// This is used MOF->PSClass conversion tool.
/// </summary>
/// <param name="mofPath">
/// Path to CIM MOF schema file for reading.
/// </param>
/// <returns>List of classes from MOF schema file.</returns>
public static List<CimClass> ReadCimSchemaMof(string mofPath)
{
var parser = new Microsoft.PowerShell.DesiredStateConfiguration.CimDSCParser(MyClassCallback);
return parser.ParseSchemaMof(mofPath);
}
/// <summary>
/// Import CIM classes from the given file.
/// </summary>

View file

@ -0,0 +1,68 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Management.Automation;
using System.Security;
namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal.CrossPlatform
{
/// <summary>
/// Class that does high level Cim schema parsing.
/// </summary>
internal class CimDSCParser
{
private readonly JsonDeserializer _jsonDeserializer;
internal CimDSCParser()
{
_jsonDeserializer = JsonDeserializer.Create();
}
internal IEnumerable<PSObject> ParseSchemaJson(string filePath, bool useNewRunspace = false)
{
try
{
string json = File.ReadAllText(filePath);
string fileNameDefiningClass = Path.GetFileNameWithoutExtension(filePath);
int dotIndex = fileNameDefiningClass.IndexOf(".schema", StringComparison.InvariantCultureIgnoreCase);
if (dotIndex != -1)
{
fileNameDefiningClass = fileNameDefiningClass.Substring(0, dotIndex);
}
IEnumerable<PSObject> result = _jsonDeserializer.DeserializeClasses(json, useNewRunspace);
foreach (dynamic classObject in result)
{
string superClassName = classObject.SuperClassName;
string className = classObject.ClassName;
if (string.Equals(superClassName, "OMI_BaseResource", StringComparison.OrdinalIgnoreCase))
{
// Get the name of the file without schema.mof/json extension
if (!className.Equals(fileNameDefiningClass, StringComparison.OrdinalIgnoreCase))
{
PSInvalidOperationException e = PSTraceSource.NewInvalidOperationException(
ParserStrings.ClassNameNotSameAsDefiningFile, className, fileNameDefiningClass);
throw e;
}
}
}
return result;
}
catch (Exception exception)
{
PSInvalidOperationException e = PSTraceSource.NewInvalidOperationException(
exception, ParserStrings.CimDeserializationError, filePath);
e.SetErrorId("CimDeserializationError");
throw e;
}
}
}
}

View file

@ -0,0 +1,72 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal.CrossPlatform
{
internal class JsonDeserializer
{
#region Constructors
/// <summary>
/// Instantiates a default deserializer.
/// </summary>
/// <returns>Default deserializer.</returns>
public static JsonDeserializer Create()
{
return new JsonDeserializer();
}
#endregion Constructors
#region Methods
/// <summary>
/// Returns schema of Cim classes from specified json file.
/// </summary>
/// <param name="json">Json text to deserialize.</param>
/// <param name="useNewRunspace">If a new runspace should be used.</param>
/// <returns>Deserialized PSObjects.</returns>
public IEnumerable<PSObject> DeserializeClasses(string json, bool useNewRunspace = false)
{
if (string.IsNullOrEmpty(json))
{
throw new ArgumentNullException(nameof(json));
}
System.Management.Automation.PowerShell powerShell = null;
if (useNewRunspace)
{
// currently using RunspaceMode.NewRunspace will reset PSModulePath env var for the entire process
// this is something we want to avoid in DSC GuestConfigAgent scenario, so we use following workaround
var s_iss = InitialSessionState.CreateDefault();
s_iss.EnvironmentVariables.Add(
new SessionStateVariableEntry(
"PSModulePath",
Environment.GetEnvironmentVariable("PSModulePath"),
description: null));
powerShell = System.Management.Automation.PowerShell.Create(s_iss);
}
else
{
powerShell = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace);
}
using (powerShell)
{
return powerShell.AddCommand("Microsoft.PowerShell.Utility\\ConvertFrom-Json")
.AddParameter("InputObject", json)
.AddParameter("Depth", 100) // maximum supported by cmdlet
.Invoke();
}
}
#endregion Methods
}
}

File diff suppressed because it is too large Load diff

View file

@ -1721,7 +1721,10 @@ namespace System.Management.Automation
foreach (var keyword in matchedResults)
{
string usageString = Microsoft.PowerShell.DesiredStateConfiguration.Internal.DscClassCache.GetDSCResourceUsageString(keyword);
string usageString = Microsoft.PowerShell.DesiredStateConfiguration.Internal.CrossPlatform.DscClassCache.NewApiIsUsed
? Microsoft.PowerShell.DesiredStateConfiguration.Internal.CrossPlatform.DscClassCache.GetDSCResourceUsageString(keyword)
: Microsoft.PowerShell.DesiredStateConfiguration.Internal.DscClassCache.GetDSCResourceUsageString(keyword);
if (results == null)
{
results = new List<CompletionResult>();

View file

@ -123,6 +123,9 @@ namespace System.Management.Automation
new ExperimentalFeature(
name: "PSNotApplyErrorActionToStderr",
description: "Don't have $ErrorActionPreference affect stderr output"),
new ExperimentalFeature(
name: "PS7DscSupport",
description: "Support the cross-platform class-based DSC"),
new ExperimentalFeature(
name: "PSSubsystemPluginModel",
description: "A plugin model for registering and un-registering PowerShell subsystems"),

View file

@ -1595,8 +1595,8 @@ namespace System.Management.Automation
{ "BooleanArray", "bool[]" },
{ "UInt8Array", "byte[]" },
{ "SInt8Array", "Sbyte[]" },
{ "UInt16Array", "uint16[]" },
{ "SInt16Array", "int64[]" },
{ "UInt16Array", "UInt16[]" },
{ "SInt16Array", "Int16[]" },
{ "UInt32Array", "UInt32[]" },
{ "SInt32Array", "Int32[]" },
{ "UInt64Array", "UInt64[]" },

View file

@ -1014,7 +1014,7 @@ namespace System.Management.Automation
/// It's known as "Program Files" module path in windows powershell.
/// </summary>
/// <returns></returns>
private static string GetSharedModulePath()
internal static string GetSharedModulePath()
{
#if UNIX
return Platform.SelectProductNameForDirectory(Platform.XDG_Type.SHARED_MODULES);

View file

@ -12,6 +12,7 @@ using System.Management.Automation.Runspaces;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Dsc = Microsoft.PowerShell.DesiredStateConfiguration.Internal;
namespace System.Management.Automation.Language
{
@ -2932,6 +2933,7 @@ namespace System.Management.Automation.Language
//
Runspaces.Runspace localRunspace = null;
bool topLevel = false;
bool useCrossPlatformSchema = false;
try
{
// At this point, we'll need a runspace to use to hold the metadata for the parse. If there is no
@ -2967,7 +2969,6 @@ namespace System.Management.Automation.Language
ExpressionAst configurationBodyScriptBlock = null;
// Automatically import the PSDesiredStateConfiguration module at this point.
PowerShell p = null;
// Save the parser we're using so we can resume the current parse when we're done.
@ -2995,7 +2996,43 @@ namespace System.Management.Automation.Language
{
// Load the default CIM keywords
Collection<Exception> CIMKeywordErrors = new Collection<Exception>();
Microsoft.PowerShell.DesiredStateConfiguration.Internal.DscClassCache.LoadDefaultCimKeywords(CIMKeywordErrors);
if (ExperimentalFeature.IsEnabled(Dsc.CrossPlatform.DscClassCache.DscExperimentalFeatureName))
{
// In addition to checking if experimental feature is enabled
// also check if PSDesiredStateConfiguration is already loaded
// if pre-v3 is already loaded then use old mof-based APIs
// otherwise use json-based APIs
p.AddCommand(new CmdletInfo("Get-Module", typeof(Microsoft.PowerShell.Commands.GetModuleCommand)));
p.AddParameter("Name", "PSDesiredStateConfiguration");
bool prev3IsLoaded = false;
foreach (PSModuleInfo moduleInfo in p.Invoke<PSModuleInfo>())
{
if (moduleInfo.Version.Major < 3)
{
prev3IsLoaded = true;
break;
}
}
p.Commands.Clear();
useCrossPlatformSchema = !prev3IsLoaded;
if (useCrossPlatformSchema)
{
Dsc.CrossPlatform.DscClassCache.LoadDefaultCimKeywords(CIMKeywordErrors);
}
else
{
Dsc.DscClassCache.LoadDefaultCimKeywords(CIMKeywordErrors);
}
}
else
{
Dsc.DscClassCache.LoadDefaultCimKeywords(CIMKeywordErrors);
}
// Report any errors encountered while loading CIM dynamic keywords.
if (CIMKeywordErrors.Count > 0)
@ -3237,7 +3274,15 @@ namespace System.Management.Automation.Language
// Clear out all of the cached classes and keywords.
// They will need to be reloaded when the generated function is actually run.
//
Microsoft.PowerShell.DesiredStateConfiguration.Internal.DscClassCache.ClearCache();
if (useCrossPlatformSchema)
{
Dsc.CrossPlatform.DscClassCache.ClearCache();
}
else
{
Dsc.DscClassCache.ClearCache();
}
System.Management.Automation.Language.DynamicKeyword.Reset();
}

View file

@ -1452,6 +1452,12 @@ ModuleVersion : Version of module to import. If used, ModuleName must represent
<data name="PsDscRunAsCredentialMergeErrorForCompositeResources" xml:space="preserve">
<value>Conflict in using PsDscRunAsCredential for Resource {0} because it already specifies PsDscRunAsCredential value. We can only use one PsDscRunAsCredential for the composite resource. </value>
</data>
<data name="PsDscMissingSchemaStore" xml:space="preserve">
<value>Unable to find DSC schema store at "{0}". Please ensure PSDesiredStateConfiguration v3 module is installed.</value>
</data>
<data name="PS7DscSupportDisabled" xml:space="preserve">
<value>PS7DscSupport experimental feature is disabled; use Enable-ExperimentalFeature or update powershell.config.json to enable this feature.</value>
</data>
<data name="ParserError" xml:space="preserve">
<value>{0}</value>
</data>

View file

@ -174,7 +174,14 @@ Describe "Default enablement of Experimental Features" -Tags CI {
(Join-Path -Path $PSHOME -ChildPath 'powershell.config.json') | Should -Exist
foreach ($expFeature in Get-ExperimentalFeature) {
$expFeature.Enabled | Should -BeEnabled -Name $expFeature.Name
if ($expFeature.Name -ne "PS7DscSupport")
{
$expFeature.Enabled | Should -BeEnabled -Name $expFeature.Name
}
else
{
$expFeature.Enabled | Should -Not -BeEnabled -Name $expFeature.Name
}
}
}
}