Files
2025-08-28 16:30:29 +02:00

78 lines
2.3 KiB
C#

using System.Diagnostics;
using static IronPython.Modules._ast;
namespace Hawkeye.VisionBuilder.Workflow.DataTransfer;
public class DockerScriptRunner
{
private Process process;
private readonly string _workingDirectory;
public DockerScriptRunner(string workingDirectory)
{
_workingDirectory = workingDirectory;
}
public static string ConvertToWslPath(string windowsPath)
{
if (string.IsNullOrWhiteSpace(windowsPath))
throw new ArgumentException("Path cannot be null or empty.", nameof(windowsPath));
// Replace backslashes with forward slashes
string wslPath = windowsPath.Replace('\\', '/');
// Extract the drive letter and convert it to WSL format
if (wslPath.Length > 1 && wslPath[1] == ':')
{
char driveLetter = char.ToLower(wslPath[0]);
wslPath = $"/mnt/{driveLetter}{wslPath.Substring(2)}";
}
return wslPath;
}
public void Start()
{
var killContainerArg = @"-d ubuntu -u root -e sh -c ""docker rm -f cv || true""";
var killProcess = new Process
{
StartInfo = new ProcessStartInfo("wsl", killContainerArg)
{
UseShellExecute = false
}
};
killProcess.Start();
killProcess.WaitForExit();
killProcess.Dispose();
//startInfo.CreateNoWindow = true;
// create and start the process
process = new Process();
var convertedWd = ConvertToWslPath(_workingDirectory);
var arg =
$@"-d ubuntu -u root -e sh -c ""cd \""{convertedWd}\"" && docker run --rm --name cv --gpus all -it -p 8000:8000 -v \""$(pwd)\"":/app -w /app cv uvicorn --host 0.0.0.0 PythonModelAPI:app """;
Console.WriteLine("args:"+arg);
process.StartInfo = new ProcessStartInfo("wsl", arg)
{
UseShellExecute = false,
CreateNoWindow = true
};
process.Start();
// on windows only
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
ChildProcessTracker.AddProcess(process);
}
public void Stop()
{
if (process != null && !process.HasExited)
{
process.Kill();
process.Dispose();
process = null;
}
}
}