Files
edubox/agent/installer/setup-wizard/PrerequisiteChecker.cs
T

143 lines
4.0 KiB
C#

using System.Diagnostics;
using System.Management;
using System.Runtime.InteropServices;
namespace StudioE5.SetupWizard;
public record PrerequisiteResult(
bool WindowsCompatible,
ulong RamMB,
ulong FreeDiskMB,
bool VirtualEnvironmentInstalled,
bool PodmanInstalled,
bool PodmanMachineReady)
{
public bool AllReady => WindowsCompatible && RamMB >= 4096 && FreeDiskMB >= 5120 && VirtualEnvironmentInstalled && PodmanInstalled && PodmanMachineReady;
}
public static class PrerequisiteChecker
{
public static PrerequisiteResult Check()
{
return new PrerequisiteResult(
WindowsCompatible: IsWindowsCompatible(),
RamMB: GetTotalPhysicalMemoryMB(),
FreeDiskMB: GetFreeDiskSpaceMB("C:\\"),
VirtualEnvironmentInstalled: IsWSLInstalled(),
PodmanInstalled: IsPodmanInstalled(),
PodmanMachineReady: IsPodmanMachineReady()
);
}
private static bool IsWindowsCompatible()
{
var os = Environment.OSVersion;
if (os.Platform != PlatformID.Win32NT)
return false;
// Windows 10 version 2004 (build 19041) or Windows 11.
return Environment.OSVersion.Version.Build >= 19041;
}
private static ulong GetTotalPhysicalMemoryMB()
{
try
{
using var searcher = new ManagementObjectSearcher("SELECT TotalVisibleMemorySize FROM Win32_OperatingSystem");
foreach (ManagementObject obj in searcher.Get())
{
var kb = Convert.ToUInt64(obj["TotalVisibleMemorySize"]);
return kb / 1024;
}
}
catch
{
// ignored
}
return 0;
}
private static ulong GetFreeDiskSpaceMB(string path)
{
try
{
var drive = new DriveInfo(Path.GetPathRoot(path) ?? path);
return (ulong)(drive.AvailableFreeSpace / (1024 * 1024));
}
catch
{
return 0;
}
}
private static bool IsWSLInstalled()
{
try
{
var psi = new ProcessStartInfo("wsl.exe", "--version")
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using var process = Process.Start(psi);
if (process == null) return false;
process.WaitForExit();
return process.ExitCode == 0;
}
catch
{
return false;
}
}
private static bool IsPodmanInstalled()
{
try
{
var psi = new ProcessStartInfo("podman.exe", "--version")
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using var process = Process.Start(psi);
if (process == null) return false;
process.WaitForExit();
return process.ExitCode == 0;
}
catch
{
return false;
}
}
private static bool IsPodmanMachineReady()
{
try
{
var psi = new ProcessStartInfo("podman.exe", "machine list --format json")
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using var process = Process.Start(psi);
if (process == null) return false;
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0) return false;
// Very permissive check: if podman machine list returns any JSON, we consider it ready.
return output.TrimStart().StartsWith("[") || output.TrimStart().StartsWith("{");
}
catch
{
return false;
}
}
}