Changeset 17013 for branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/Isolation/DockerRunnerHost.cs
- Timestamp:
- 06/17/19 10:40:41 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/Isolation/DockerRunnerHost.cs
r16993 r17013 1 1 using System.Diagnostics; 2 using System.IO; 3 using System.Threading.Tasks; 2 4 using HeuristicLab.PluginInfrastructure.Exceptions; 3 5 … … 8 10 public class DockerRunnerHost : RunnerHost { 9 11 #region Constants 10 private const string Docker = "docker"; 11 private const string ContainerStartup = "container run -i --rm "; 12 private const string Mounting = @"--mount type=bind,source=/c/Users/,target=/Users "; 13 private const string Image = "heuristiclab33:latest"; 14 private const string DockerExceptionMessage = "Docker is not running or image '" + Image + "' does not exists!"; 12 private readonly static string Docker = "docker"; 13 private readonly static string ContainerStartup = "container run -i --rm "; 14 private readonly static string MountingLinux = @"--mount type=bind,source=/c/Users/,target=/Users "; 15 private readonly static string MountingWindows = @"--mount type=bind,source=C:\Users\,target=C:\Users\ "; 16 private readonly static string LinuxImage = "." + Path.DirectorySeparatorChar + "DockerLinuxBuild" + Path.DirectorySeparatorChar + "Dockerfile"; 17 private readonly static string WindowsImage = "." + Path.DirectorySeparatorChar + "DockerWindowsBuild" + Path.DirectorySeparatorChar + "Dockerfile"; 18 private readonly static string Image = "heuristiclab33:latest"; 19 private readonly static string DockerExceptionMessage = "Docker is not running!"; 15 20 #endregion 16 21 17 22 #region Constructors 18 public DockerRunnerHost(bool doDockerAvailableCheck = true) 19 : base(Docker, ContainerStartup + Mounting + Image, null, null, null) { 20 if (doDockerAvailableCheck && !IsDockerAvailable()) 21 throw new DockerException(DockerExceptionMessage); 23 public DockerRunnerHost() 24 : base(Docker, ContainerStartup + GetTargetOSMounting() + Image, null, null, null) { 22 25 } 23 26 #endregion 24 27 25 28 #region Helper 26 private bool IsDockerAvailable() { 29 30 private static string GetTargetOSMounting() { 31 Task<bool> win = Task.Run(() => BuildImage(WindowsImage)); 32 Task<bool> lin = Task.Run(() => BuildImage(LinuxImage)); 33 if (win.Result) return MountingWindows; 34 else if (lin.Result) return MountingLinux; 35 else throw new DockerException(DockerExceptionMessage); 36 } 37 38 private static bool BuildImage(string pathToDockerfile) { 27 39 var process = new Process { 28 40 StartInfo = new ProcessStartInfo { 29 41 FileName = Docker, 30 Arguments = "image inspect " + Image, 42 Arguments = "image build -t " + Image + 43 " -f " + Path.GetFullPath(pathToDockerfile) + 44 " .", 31 45 UseShellExecute = false, 32 46 RedirectStandardOutput = true, … … 37 51 EnableRaisingEvents = false 38 52 }; 39 40 53 process.Start(); 41 54 process.BeginOutputReadLine();
Note: See TracChangeset
for help on using the changeset viewer.