Skip to content

Commit

Permalink
Fix profile collection on non-Windows, add PS 7 profiles (#1260)
Browse files Browse the repository at this point in the history
* Fix platform information collection on *nix

* Add profiles for PS 7-preview.1

* Use correct schema version

* Address @bergmeister's feedback

* Improve dict name
  • Loading branch information
rjmholt authored Aug 23, 2019
1 parent 45cdba0 commit a60ee23
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public CompatibilityProfileCollector Build(SMA.PowerShell pwsh)
}
}

private static readonly Version s_currentProfileSchemaVersion = new Version(1, 1);
// Increment the minor version if non-breaking additions have been made to the API
// Increment the major version if breaking changes have been made to the API
private static readonly Version s_currentProfileSchemaVersion = new Version(1, 2);

private readonly PowerShellDataCollector _pwshDataCollector;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Management.Infrastructure;
using Microsoft.PowerShell.CrossCompatibility.Data;
using Microsoft.PowerShell.CrossCompatibility.Utility;
Expand All @@ -29,6 +30,76 @@ public class PlatformInformationCollector : IDisposable
"/etc/os-release",
};

private static readonly IReadOnlyList<string> s_distributionIdKeys = new string[]
{
"ID",
"DISTRIB_ID"
};

private static readonly IReadOnlyList<string> s_distributionVersionKeys = new string[]
{
"VERSION_ID",
"DISTRIB_RELEASE"
};

private static readonly IReadOnlyList<string> s_distributionPrettyNameKeys = new string[]
{
"PRETTY_NAME",
"DISTRIB_DESCRIPTION"
};

private static readonly Regex s_macOSNameRegex = new Regex(
@"System Version: (.*?)(\(|$)",
RegexOptions.Multiline | RegexOptions.Compiled);


/// <summary>
/// Collect all release info files into a lookup table in memory.
/// Overrides pre-existing keys if there are duplicates.
/// </summary>
/// <returns>A dictionary with the keys and values of all the release info files on the machine.</returns>
public static IReadOnlyDictionary<string, string> GetLinuxReleaseInfo()
{
var releaseInfoKeyValueEntries = new Dictionary<string, string>();

foreach (string path in s_releaseInfoPaths)
{
try
{
using (FileStream fileStream = File.OpenRead(path))
using (var reader = new StreamReader(fileStream))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();

if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#"))
{
continue;
}

string[] elements = line.Split('=');
releaseInfoKeyValueEntries[elements[0]] = Dequote(elements[1]);
}
}
}
catch (IOException)
{
// Different Linux distributions have different /etc/*-release files.
// It's more efficient (and correct timing-wise) for us to try to read the file and catch the exception
// than to test for its existence and then open it.
//
// See:
// - https://www.freedesktop.org/software/systemd/man/os-release.html
// - https://gist.github.com/natefoo/814c5bf936922dad97ff

// Ignore that the file doesn't exist and just continue to the next one.
}
}

return releaseInfoKeyValueEntries;
}

private readonly Lazy<Hashtable> _lazyPSVersionTable;

private readonly Lazy<PowerShellVersion> _lazyPSVersion;
Expand Down Expand Up @@ -129,16 +200,17 @@ public OperatingSystemData GetOperatingSystemData()
{
var osData = new OperatingSystemData()
{
Description = GetOSDescription(),
Architecture = GetOSArchitecture(),
Family = GetOSFamily(),
Name = GetOSName(),
Platform = GetOSPlatform(),
Version = GetOSVersion(),
};

switch (osData.Family)
{
case OSFamily.Windows:
osData.Name = osData.Description;
if (!string.IsNullOrEmpty(Environment.OSVersion.ServicePack))
{
osData.ServicePack = Environment.OSVersion.ServicePack;
Expand All @@ -147,53 +219,83 @@ public OperatingSystemData GetOperatingSystemData()
break;

case OSFamily.Linux:
IReadOnlyDictionary<string, string> lsbInfo = GetLinuxReleaseInfo();
osData.DistributionId = lsbInfo["DistributionId"];
osData.DistributionVersion = lsbInfo["DistributionVersion"];
osData.DistributionPrettyName = lsbInfo["DistributionPrettyName"];
IReadOnlyDictionary<string, string> releaseInfo = GetLinuxReleaseInfo();

osData.DistributionId = GetEntryFromReleaseInfo(releaseInfo, s_distributionIdKeys);
osData.DistributionVersion = GetEntryFromReleaseInfo(releaseInfo, s_distributionVersionKeys);
osData.DistributionPrettyName = GetEntryFromReleaseInfo(releaseInfo, s_distributionPrettyNameKeys);
osData.Name = osData.DistributionPrettyName;
break;

case OSFamily.MacOS:
osData.Name = GetMacOSName();
break;
}

return osData;
}

/// <summary>
/// Collect all release info files into a lookup table in memory.
/// Overrides pre-existing keys if there are duplicates.
/// </summary>
/// <returns>A dictionary with the keys and values of all the release info files on the machine.</returns>
public IReadOnlyDictionary<string, string> GetLinuxReleaseInfo()
private string GetMacOSName()
{
var dict = new Dictionary<string, string>();

foreach (string path in s_releaseInfoPaths)
try
{
try
using (var spProcess = new Process())
{
using (FileStream fileStream = File.OpenRead(path))
using (var reader = new StreamReader(fileStream))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
spProcess.StartInfo.UseShellExecute = false;
spProcess.StartInfo.RedirectStandardOutput = true;
spProcess.StartInfo.CreateNoWindow = true;
spProcess.StartInfo.FileName = "/usr/sbin/system_profiler";
spProcess.StartInfo.Arguments = "SPSoftwareDataType";

if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#"))
{
continue;
}
spProcess.Start();
spProcess.WaitForExit();

string[] elements = line.Split('=');
dict[elements[0]] = Dequote(elements[1]);
}
}
}
catch (IOException)
{
// Do nothing - just continue
string output = spProcess.StandardOutput.ReadToEnd();
return s_macOSNameRegex.Match(output).Groups[1].Value;
}
}
catch
{
return null;
}
}

private string GetOSDescription()
{
#if CoreCLR
// This key was introduced in PowerShell 6
return (string)PSVersionTable["OS"];
#else
if (_lazyWin32OperatingSystemInfo.IsValueCreated)
{
return _lazyWin32OperatingSystemInfo.Value.OSName;
}

return dict;
return RegistryCurrentVersionInfo.ProductName;
#endif
}

private string GetOSVersion()
{
#if CoreCLR
// On Linux, we want to record the kernel branch, since this can differentiate Azure
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return File.ReadAllText("/proc/sys/kernel/osrelease");
}
#endif
return Environment.OSVersion.Version.ToString();
}

private string GetOSPlatform()
{
#if CoreCLR
if (PSVersion.Major >= 6)
{
return (string)PSVersionTable["Platform"];
}
#endif
return "Win32NT";
}

private OSFamily GetOSFamily()
Expand Down Expand Up @@ -310,46 +412,6 @@ private uint GetWinSkuId()
return (uint)WindowsSku.Undefined;
}

private string GetOSName()
{
#if CoreCLR
// This key was introduced in PowerShell 6
if (PSVersion.Major >= 6)
{
return (string)PSVersionTable["OS"];
}
#endif
if (_lazyWin32OperatingSystemInfo.IsValueCreated)
{
return _lazyWin32OperatingSystemInfo.Value.OSName;
}

return RegistryCurrentVersionInfo.ProductName;
}

private string GetOSVersion()
{
#if CoreCLR
// On Linux, we want to record the kernel branch, since this can differentiate Azure
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return File.ReadAllText("/proc/sys/kernel/osrelease");
}
#endif
return Environment.OSVersion.Version.ToString();
}

private string GetOSPlatform()
{
#if CoreCLR
if (PSVersion.Major >= 6)
{
return (string)PSVersionTable["Platform"];
}
#endif
return "Win32NT";
}

private static CurrentVersionInfo ReadCurrentVersionFromRegistry()
{
using (RegistryKey currentVersion = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"))
Expand All @@ -360,13 +422,18 @@ private static CurrentVersionInfo ReadCurrentVersionFromRegistry()
}
}

[DllImport("kernel32.dll")]
private static extern bool GetProductInfo(
int dwOSMajorVersion,
int dwOSMinorVersion,
int dwSpMajorVersion,
int dwSpMinorVersion,
out uint pdwReturnedProductType);
private static string GetEntryFromReleaseInfo(IReadOnlyDictionary<string, string> releaseInfo, IEnumerable<string> possibleKeys)
{
foreach (string key in possibleKeys)
{
if (releaseInfo.TryGetValue(key, out string entry))
{
return entry;
}
}

return null;
}

private static Win32OSCimInfo GetWin32OperatingSystemInfo()
{
Expand Down Expand Up @@ -443,6 +510,15 @@ private static string Dequote(string s)
return sb.ToString();
}

[DllImport("kernel32.dll")]
private static extern bool GetProductInfo(
int dwOSMajorVersion,
int dwOSMinorVersion,
int dwSpMajorVersion,
int dwSpMinorVersion,
out uint pdwReturnedProductType);


#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ namespace Microsoft.PowerShell.CrossCompatibility.Data
public class OperatingSystemData : ICloneable
{
/// <summary>
/// The name of the operating system as
/// reported by $PSVersionTable.
/// The name of the operating system.
/// </summary>
[DataMember]
public string Name { get; set; }

/// <summary>
/// The description of the operating system as
/// reported by $PSVersionTable.
/// </summary>
[DataMember]
public string Description { get; set; }

/// <summary>
/// The platform as reported by
/// $PSVersionTable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ public OperatingSystemData(OperatingSystemDataMut operatingSystemData)
}

/// <summary>
/// The name of the operating system as reported by $PSVersionTable.OS.
/// The name of the operating system.
/// </summary>
public string Name => _operatingSystemData.Name;

/// <summary>
/// The description of the operating system as reported by $PSVersionTable.OS.
/// </summary>
public string Description => _operatingSystemData.Description;

/// <summary>
/// The name of the platform as reported by $PSVersionTable.Platform.
/// </summary>
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

0 comments on commit a60ee23

Please sign in to comment.