Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/Isolation/DockerRunnerHost.cs @ 17013

Last change on this file since 17013 was 17013, checked in by dpiringe, 5 years ago

#2924:

  • some changes in CLIApplication.cs to reduce unnecessary allocation of string objects
  • renamed AppTest to ConsoleOptimizer and fixed race condition
  • replaced enum RunnerJob with class RunnerMessage for more control of saved data
  • changed usage of BinaryFormatter with HEAL.Attic, following types are now Storable:
    • ConsoleOptimizer
    • InspectApplication
    • ApplicationBase
    • ApplicationRunner
    • AssemblyInfo
    • Runner
    • UniPath
    • RunnerMessage
  • switched QuietMode from ApplicationRunner to IRunner
  • DockerRunnerHost can now automatically build docker images for linux and windows containers (also identifies which container type is active) -> removes the condition to have the image preinstalled
    • to achieve this, there are two new folders DockerLinuxBuild and DockerWindowsBuild included in build output, which include Dockerfiles to build images for linux and windows container
  • added NuGet package System.CodeDom to project HeuristicLab.Scripting-3.3
  • added method Send(RunnerMessage) to IRunnerHost and transferred methods Pause and Resume to IRunner
  • added internal reference of RunnerHost in Runner
  • added abstract method SendMessage(RunnerMessage) in RunnerHost which gets called from method Send(RunnerMessage)
  • because of a Google.Protobuf "bug", RunnerMessages cannot get serialized/deserialized directly on stream -> workaround with a byte array, which gets written and read
    • additionally, the length of the array gets sent first (length as integer -> 4 bytes)
    • static method in RunnerMessage to read a message from a stream
    • the method SendMessage(RunnerMessage) in RunnerHost implements this functionality
File size: 2.4 KB
Line 
1using System.Diagnostics;
2using System.IO;
3using System.Threading.Tasks;
4using HeuristicLab.PluginInfrastructure.Exceptions;
5
6namespace HeuristicLab.PluginInfrastructure {
7  /// <summary>
8  /// IRunner for isolation with docker.
9  /// </summary>
10  public class DockerRunnerHost : RunnerHost {
11    #region Constants
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!";
20    #endregion
21
22    #region Constructors
23    public DockerRunnerHost()
24      : base(Docker, ContainerStartup + GetTargetOSMounting() + Image, null, null, null) {
25    }
26    #endregion
27
28    #region Helper
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) {
39      var process = new Process {
40        StartInfo = new ProcessStartInfo {
41          FileName = Docker,
42          Arguments = "image build -t " + Image +
43            " -f " + Path.GetFullPath(pathToDockerfile) +
44            " .",
45          UseShellExecute = false,
46          RedirectStandardOutput = true,
47          RedirectStandardInput = false,
48          RedirectStandardError = true,
49          CreateNoWindow = false
50        },
51        EnableRaisingEvents = false
52      };
53      process.Start();
54      process.BeginOutputReadLine();
55      process.BeginErrorReadLine();
56      process.WaitForExit();
57      return process.ExitCode == 0;
58    }
59    #endregion
60  }
61}
Note: See TracBrowser for help on using the repository browser.