Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some Dotnet refactor and cleanup plus the addition of a solution file #133

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
10 changes: 3 additions & 7 deletions dotnet/Metaparticle.Package/DockerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@
namespace Metaparticle.Package {
public class DockerBuilder : ImageBuilder
{
public DockerBuilder()
{
}

public bool Build(string configFile, string imageName, TextWriter o, TextWriter e)
{
System.Console.WriteLine(configFile);
var info = Directory.GetParent(configFile);
System.Console.WriteLine(string.Format("build -t {0} {1}", imageName, info.FullName));
System.Console.WriteLine($"build -t {imageName} {info.FullName}");
var err = new StringWriter();
var proc = Exec("docker", string.Format("build -t {0} {1}", imageName, info.FullName), stdout: o, stderr: err);
var proc = Exec("docker", $"build -t {imageName} {info.FullName}", o, err);
System.Console.WriteLine(err.ToString());
return proc.ExitCode == 0;
}

public bool Push(string imageName, TextWriter stdout, TextWriter stderr)
{
var proc = Exec("docker", string.Format("push {0}", imageName), stdout: stdout, stderr: stderr);
var proc = Exec("docker", $"push {imageName}", stdout, stderr);
return proc.ExitCode == 0;
}
}
Expand Down
53 changes: 53 additions & 0 deletions dotnet/Metaparticle.Package/DockerfileWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using dockerfile;

namespace Metaparticle.Package
{
public static class DockerfileWriter
{
/// <summary>
/// Creates the dockerfle and returns the name of the file
/// </summary>
/// <param name="dir">Directory to create the file</param>
/// <param name="exe">Executable used to run command/s</param>
/// <param name="args">args for the executable</param>
/// <param name="config"></param>
/// <returns></returns>
public static string Write(string dir, string exe, string[] args, Config config)
{
var dockerfilename = dir + "/Dockerfile";
if (!string.IsNullOrEmpty(config.Dockerfile)) {
File.Copy(config.Dockerfile, dockerfilename);
return dockerfilename;
}
var instructions = new List<Instruction>();
instructions.Add(new Instruction("FROM", "debian:9"));
instructions.Add(new Instruction("RUN", " apt-get update && apt-get -qq -y install libunwind8 libicu57 libssl1.0 liblttng-ust0 libcurl3 libuuid1 libkrb5-3 zlib1g"));
// TODO: lots of things here are constant, figure out how to cache for perf?
instructions.Add(new Instruction("COPY", "* /exe/"));
instructions.Add(new Instruction("CMD", $"/exe/{exe} {GetArgs(args)}"));

var df = new Dockerfile(instructions.ToArray(), new Comment[0]);
File.WriteAllText(dockerfilename, df.Contents());

return dockerfilename;
}

private static string GetArgs(string[] args)
{
if (args == null || args.Length == 0)
{
return "";
}
var b = new StringBuilder();
foreach (var arg in args)
{
b.Append(arg);
b.Append(" ");
}
return b.ToString().Trim();
}
}
}
3 changes: 3 additions & 0 deletions dotnet/Metaparticle.Package/Metaparticle.Tests/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace Metaparticle.Tests
{
public class Config : Attribute
{
/// <summary>
/// Directories containing the test projects. This may be absolute or relative to the current project.
/// </summary>
public string[] Names { get;set; }
}
}
61 changes: 11 additions & 50 deletions dotnet/Metaparticle.Package/Metaparticle.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using dockerfile;
using Metaparticle.Tests;
using static Metaparticle.Package.Util;
using RuntimeConfig = Metaparticle.Runtime.Config;
using TestConfig = Metaparticle.Tests.Config;

namespace Metaparticle.Package
{
public class Driver
public class Driver
{
private Config config;
private RuntimeConfig runtimeConfig;
private TestConfig testConfig;
private readonly Config config;
private readonly RuntimeConfig runtimeConfig;
private readonly TestConfig testConfig;

public Driver(Config config, RuntimeConfig runtimeConfig, TestConfig testConfig)
public Driver(Config config, RuntimeConfig runtimeConfig, TestConfig testConfig)
{
this.config = config;
this.runtimeConfig = runtimeConfig;
this.testConfig = testConfig;
}

private ImageBuilder getBuilder()
private ImageBuilder getBuilder()
{
switch (config.Builder.ToLowerInvariant())
{
Expand Down Expand Up @@ -55,27 +52,12 @@ private ContainerExecutor getExecutor()
}
}

private static string getArgs(string[] args)
{
if (args == null || args.Length == 0)
{
return "";
}
var b = new StringBuilder();
foreach (var arg in args)
{
b.Append(arg);
b.Append(" ");
}
return b.ToString().Trim();
}

public void Build(string[] args)
{
var proc = Process.GetCurrentProcess();
var procName = proc.ProcessName;
string exe = null;
string dir = null;
string dir;
TextWriter o = config.Verbose ? Console.Out : null;
TextWriter e = config.Quiet ? Console.Error : null;
if (procName == "dotnet")
Expand All @@ -84,7 +66,6 @@ public void Build(string[] args)

dir = "bin/release/netcoreapp2.0/debian.8-x64/publish";
Exec("dotnet", "publish -r debian.8-x64 -c release", stdout: o, stderr: e);
//var dirInfo = new UnixDirectoryInfo(dir);
var files = Directory.GetFiles(dir);
foreach (var filePath in files)
{
Expand All @@ -101,7 +82,7 @@ public void Build(string[] args)
var prog = proc.MainModule.FileName;
dir = Directory.GetParent(prog).FullName;
}
var dockerfilename = writeDockerfile(dir, exe, args, config);
var dockerfilename = DockerfileWriter.Write(dir, exe, args, config);
var builder = getBuilder();

string imgName = (string.IsNullOrEmpty(config.Repository) ? exe : config.Repository);
Expand Down Expand Up @@ -151,31 +132,11 @@ private void RunTests()
{
throw new Exception("Tests Failed.");
}
}

private string writeDockerfile(string dir, string exe, string[] args, Config config)
{
var dockerfilename = dir + "/Dockerfile";
if (!string.IsNullOrEmpty(config.Dockerfile)) {
File.Copy(config.Dockerfile, dockerfilename);
return dockerfilename;
}
var instructions = new List<Instruction>();
instructions.Add(new Instruction("FROM", "debian:9"));
instructions.Add(new Instruction("RUN", " apt-get update && apt-get -qq -y install libunwind8 libicu57 libssl1.0 liblttng-ust0 libcurl3 libuuid1 libkrb5-3 zlib1g"));
// TODO: lots of things here are constant, figure out how to cache for perf?
instructions.Add(new Instruction("COPY", string.Format("* /exe/", dir)));
instructions.Add(new Instruction("CMD", string.Format("/exe/{0} {1}", exe, getArgs(args))));

var df = new Dockerfile(instructions.ToArray(), new Comment[0]);
File.WriteAllText(dockerfilename, df.Contents());

return dockerfilename;
}

public static bool InDockerContainer()
public static bool InDockerContainer()
{
switch (System.Environment.GetEnvironmentVariable("METAPARTICLE_IN_CONTAINER"))
switch (Environment.GetEnvironmentVariable("METAPARTICLE_IN_CONTAINER"))
{
case "true":
case "1":
Expand All @@ -189,7 +150,7 @@ public static bool InDockerContainer()
if (File.Exists(cgroupPath)) {
var info = File.ReadAllText(cgroupPath);
// This is a little approximate...
return info.IndexOf("docker") != -1 || info.IndexOf("kubepods") != -1;
return info.IndexOf("docker", StringComparison.Ordinal) != -1 || info.IndexOf("kubepods", StringComparison.Ordinal) != -1;
}
return false;
}
Expand Down
31 changes: 17 additions & 14 deletions dotnet/Metaparticle.Runtime/Config.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
namespace Metaparticle.Runtime {
public class Config : System.Attribute {
public int Replicas { get; set; }
namespace Metaparticle.Runtime
{
public class Config : System.Attribute
{
public int Replicas { get; set; }

public int Shards { get; set; }
public int Shards { get; set; }

public int JobCount { get; set; }
public int JobCount { get; set; }

public string ShardExpression { get; set; }
public string ShardExpression { get; set; }

public string Executor { get; set; }
public string Executor { get; set; }

public int[] Ports { get; set; }
public int[] Ports { get; set; }

public bool Public { get; set; }
public bool Public { get; set; }

public Config() {
Executor = "docker";
Replicas = 1;
}
}
public Config()
{
Executor = "docker";
Replicas = 1;
}
}
}
62 changes: 62 additions & 0 deletions dotnet/dotnet.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Metaparticle.Package", "Metaparticle.Package\Metaparticle.Package.csproj", "{37E6F49B-77B6-49D6-86C1-2EBC17080D8C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Metaparticle.Runtime", "Metaparticle.Runtime\Metaparticle.Runtime.csproj", "{4167A6E1-C7C2-4186-8AA1-88DAB52D753B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Metaparticle.Package.Tests", "Metaparticle.Package.Tests\Metaparticle.Package.Tests.csproj", "{108E0A37-6F7E-49A1-AA81-35A540E91588}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{37E6F49B-77B6-49D6-86C1-2EBC17080D8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{37E6F49B-77B6-49D6-86C1-2EBC17080D8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{37E6F49B-77B6-49D6-86C1-2EBC17080D8C}.Debug|x64.ActiveCfg = Debug|Any CPU
{37E6F49B-77B6-49D6-86C1-2EBC17080D8C}.Debug|x64.Build.0 = Debug|Any CPU
{37E6F49B-77B6-49D6-86C1-2EBC17080D8C}.Debug|x86.ActiveCfg = Debug|Any CPU
{37E6F49B-77B6-49D6-86C1-2EBC17080D8C}.Debug|x86.Build.0 = Debug|Any CPU
{37E6F49B-77B6-49D6-86C1-2EBC17080D8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{37E6F49B-77B6-49D6-86C1-2EBC17080D8C}.Release|Any CPU.Build.0 = Release|Any CPU
{37E6F49B-77B6-49D6-86C1-2EBC17080D8C}.Release|x64.ActiveCfg = Release|Any CPU
{37E6F49B-77B6-49D6-86C1-2EBC17080D8C}.Release|x64.Build.0 = Release|Any CPU
{37E6F49B-77B6-49D6-86C1-2EBC17080D8C}.Release|x86.ActiveCfg = Release|Any CPU
{37E6F49B-77B6-49D6-86C1-2EBC17080D8C}.Release|x86.Build.0 = Release|Any CPU
{4167A6E1-C7C2-4186-8AA1-88DAB52D753B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4167A6E1-C7C2-4186-8AA1-88DAB52D753B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4167A6E1-C7C2-4186-8AA1-88DAB52D753B}.Debug|x64.ActiveCfg = Debug|Any CPU
{4167A6E1-C7C2-4186-8AA1-88DAB52D753B}.Debug|x64.Build.0 = Debug|Any CPU
{4167A6E1-C7C2-4186-8AA1-88DAB52D753B}.Debug|x86.ActiveCfg = Debug|Any CPU
{4167A6E1-C7C2-4186-8AA1-88DAB52D753B}.Debug|x86.Build.0 = Debug|Any CPU
{4167A6E1-C7C2-4186-8AA1-88DAB52D753B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4167A6E1-C7C2-4186-8AA1-88DAB52D753B}.Release|Any CPU.Build.0 = Release|Any CPU
{4167A6E1-C7C2-4186-8AA1-88DAB52D753B}.Release|x64.ActiveCfg = Release|Any CPU
{4167A6E1-C7C2-4186-8AA1-88DAB52D753B}.Release|x64.Build.0 = Release|Any CPU
{4167A6E1-C7C2-4186-8AA1-88DAB52D753B}.Release|x86.ActiveCfg = Release|Any CPU
{4167A6E1-C7C2-4186-8AA1-88DAB52D753B}.Release|x86.Build.0 = Release|Any CPU
{108E0A37-6F7E-49A1-AA81-35A540E91588}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{108E0A37-6F7E-49A1-AA81-35A540E91588}.Debug|Any CPU.Build.0 = Debug|Any CPU
{108E0A37-6F7E-49A1-AA81-35A540E91588}.Debug|x64.ActiveCfg = Debug|Any CPU
{108E0A37-6F7E-49A1-AA81-35A540E91588}.Debug|x64.Build.0 = Debug|Any CPU
{108E0A37-6F7E-49A1-AA81-35A540E91588}.Debug|x86.ActiveCfg = Debug|Any CPU
{108E0A37-6F7E-49A1-AA81-35A540E91588}.Debug|x86.Build.0 = Debug|Any CPU
{108E0A37-6F7E-49A1-AA81-35A540E91588}.Release|Any CPU.ActiveCfg = Release|Any CPU
{108E0A37-6F7E-49A1-AA81-35A540E91588}.Release|Any CPU.Build.0 = Release|Any CPU
{108E0A37-6F7E-49A1-AA81-35A540E91588}.Release|x64.ActiveCfg = Release|Any CPU
{108E0A37-6F7E-49A1-AA81-35A540E91588}.Release|x64.Build.0 = Release|Any CPU
{108E0A37-6F7E-49A1-AA81-35A540E91588}.Release|x86.ActiveCfg = Release|Any CPU
{108E0A37-6F7E-49A1-AA81-35A540E91588}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal