1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using HeuristicLab.CommandLineInterface;
|
---|
4 | using HeuristicLab.PluginInfrastructure;
|
---|
5 |
|
---|
6 | namespace HeuristicLab {
|
---|
7 |
|
---|
8 | public enum IsolationType {
|
---|
9 | Docker,
|
---|
10 | Native
|
---|
11 | }
|
---|
12 |
|
---|
13 | [CommandLineInterface.Application(
|
---|
14 | "HL", "1.0.0.0",
|
---|
15 | SubCommands = new Type[] {
|
---|
16 | typeof(OptimizeCommand),
|
---|
17 | typeof(InspectCommand)
|
---|
18 | }
|
---|
19 | )]
|
---|
20 | class ApplicationCommand : ICommand {
|
---|
21 |
|
---|
22 | [Option(Description = "Sets the path for the binaries.")]
|
---|
23 | public string BasePath { get; set; }
|
---|
24 |
|
---|
25 | [Option(Shortcut = "i", Description = "Sets the isolation type. Possible types are: NATIVE or DOCKER.")]
|
---|
26 | public IsolationType Isolation { get; set; }
|
---|
27 |
|
---|
28 | [Option]
|
---|
29 | private bool StartAsRunnerHost { get; set; }
|
---|
30 |
|
---|
31 | public void Execute() {
|
---|
32 | if (StartAsRunnerHost) {
|
---|
33 | StartupRunnerHost();
|
---|
34 | } else {
|
---|
35 | SetupIsolation();
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | private void StartupRunnerHost() {
|
---|
40 | Stream stdin = Console.OpenStandardInput();
|
---|
41 | var message = (TransportRunnerMessage)RunnerMessage.ReadMessageFromStream(stdin);
|
---|
42 | IRunner runner = message.Runner;
|
---|
43 |
|
---|
44 | if (runner != null)
|
---|
45 | runner.Run();
|
---|
46 | else
|
---|
47 | Console.Error.WriteLine("Cannot deserialize data from stdin to a type of RunnerConfig!");
|
---|
48 | }
|
---|
49 |
|
---|
50 | private void SetupIsolation() {
|
---|
51 | switch (Isolation) {
|
---|
52 | case IsolationType.Native: Settings.Host = new NativeRunnerHost(); break;
|
---|
53 | case IsolationType.Docker: Settings.Host = new DockerRunnerHost(); break;
|
---|
54 | default: throw new ArgumentException("Invalid value for isolation.", "Isolation");
|
---|
55 | }
|
---|
56 |
|
---|
57 | if (BasePath != null && !Directory.Exists(BasePath))
|
---|
58 | throw new DirectoryNotFoundException("Path for binaries is not a directory or does not exists.");
|
---|
59 | else BasePath = Directory.GetCurrentDirectory();
|
---|
60 |
|
---|
61 | IPluginLoader validator = PluginLoaderFactory.Create();
|
---|
62 | Settings.assemblyInfos = validator.Validate(BasePath);
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|