diff --git a/.travis.yml b/.travis.yml index 788fd95..94b5604 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,4 +14,15 @@ matrix: - cd javascript/metaparticle-package - npm install script: - - npm test \ No newline at end of file + - npm test + + include: + - language: csharp + sudo: required + dotnet: 2.0.0 + mono: none + before_script: + - cd dotnet + - ls + script: + - ./build.sh \ No newline at end of file diff --git a/dotnet/Metaparticle.Package/Config.cs b/dotnet/Metaparticle.Package/Config.cs index 1a582ce..d08f8c9 100644 --- a/dotnet/Metaparticle.Package/Config.cs +++ b/dotnet/Metaparticle.Package/Config.cs @@ -1,7 +1,9 @@ using System; -namespace Metaparticle.Package { - public class Config : Attribute { +namespace Metaparticle.Package +{ + public class Config : Attribute + { public bool Verbose { get; set; } public bool Quiet { get; set; } @@ -16,15 +18,16 @@ public class Config : Attribute { public string Dockerfile { get; set; } - public Config() { + public Config() + { Builder = "docker"; LoadConfigVariablesFromEnvironment(); } private void LoadConfigVariablesFromEnvironment() { - Repository = TryGetEnvironmentVariable("MP_CONFIG_REPOSITORY"); - Publish = TryGetEnvironmentVariable("MP_CONFIG_PUBLISH").ToLower() == "true"; + Repository = TryGetEnvironmentVariable("METAPARTICLE_CONFIG_REPOSITORY"); + Publish = TryGetEnvironmentVariable("METAPARTICLE_CONFIG_PUBLISH").ToLower() == "true"; } private string TryGetEnvironmentVariable(string name) diff --git a/dotnet/Metaparticle.Package/ContainerExecutor.cs b/dotnet/Metaparticle.Package/ContainerExecutor.cs index 76e7dd9..f4b5adc 100644 --- a/dotnet/Metaparticle.Package/ContainerExecutor.cs +++ b/dotnet/Metaparticle.Package/ContainerExecutor.cs @@ -1,6 +1,9 @@ using System.IO; -namespace Metaparticle.Package { - public interface ContainerExecutor { + +namespace Metaparticle.Package +{ + public interface ContainerExecutor + { string Run(string image, Metaparticle.Runtime.Config config); void Cancel(string id); diff --git a/dotnet/Metaparticle.Package/DockerExecutor.cs b/dotnet/Metaparticle.Package/DockerExecutor.cs index 6026119..34408e1 100644 --- a/dotnet/Metaparticle.Package/DockerExecutor.cs +++ b/dotnet/Metaparticle.Package/DockerExecutor.cs @@ -1,7 +1,8 @@ using System.IO; using static Metaparticle.Package.Util; -namespace Metaparticle.Package { +namespace Metaparticle.Package +{ public class DockerExecutor : ContainerExecutor { public void Cancel(string id) @@ -17,20 +18,25 @@ public string Run(string image, Metaparticle.Runtime.Config config) return idWriter.ToString().Trim(); } - public void Logs(string id, TextWriter stdout, TextWriter stderr) { + public void Logs(string id, TextWriter stdout, TextWriter stderr) + { Exec("docker", string.Format("logs -f {0}", id), stdout, stderr); } - public bool PublishRequired() { + public bool PublishRequired() + { return false; } - private string portString(int[] ports) { - if (ports == null || ports.Length == 0) { + private string portString(int[] ports) + { + if (ports == null || ports.Length == 0) + { return ""; } var pieces = new string[ports.Length]; - for (int i = 0; i < ports.Length; i++) { + for (int i = 0; i < ports.Length; i++) + { pieces[i] = string.Format("-p {0}:{1}", ports[i], ports[i]); } return string.Join(",", pieces); diff --git a/dotnet/Metaparticle.Package/ImageBuilder.cs b/dotnet/Metaparticle.Package/ImageBuilder.cs index 8360454..81276f7 100644 --- a/dotnet/Metaparticle.Package/ImageBuilder.cs +++ b/dotnet/Metaparticle.Package/ImageBuilder.cs @@ -1,7 +1,9 @@ using System.IO; -namespace Metaparticle.Package { - public interface ImageBuilder { +namespace Metaparticle.Package +{ + public interface ImageBuilder + { bool Build(string configFile, string imageName, TextWriter stdout = null, TextWriter stderr = null); bool Push(string imageName, TextWriter stdout = null, TextWriter stderr = null); diff --git a/dotnet/Metaparticle.Package/Metaparticle.cs b/dotnet/Metaparticle.Package/Metaparticle.cs index 3305bb8..403a43d 100644 --- a/dotnet/Metaparticle.Package/Metaparticle.cs +++ b/dotnet/Metaparticle.Package/Metaparticle.cs @@ -14,13 +14,16 @@ public class Driver private Config config; private RuntimeConfig runtimeConfig; - public Driver(Config config, RuntimeConfig runtimeConfig) { + public Driver(Config config, RuntimeConfig runtimeConfig) + { this.config = config; this.runtimeConfig = runtimeConfig; } - private ImageBuilder getBuilder() { - switch (config.Builder.ToLowerInvariant()) { + private ImageBuilder getBuilder() + { + switch (config.Builder.ToLowerInvariant()) + { case "docker": return new DockerBuilder(); case "aci": @@ -30,11 +33,13 @@ private ImageBuilder getBuilder() { } } - private ContainerExecutor getExecutor() { + private ContainerExecutor getExecutor() + { if (runtimeConfig == null) { return null; } - switch (runtimeConfig.Executor.ToLowerInvariant()) { + switch (runtimeConfig.Executor.ToLowerInvariant()) + { case "docker": return new DockerExecutor(); case "aci": @@ -46,12 +51,15 @@ private ContainerExecutor getExecutor() { } } - private static string getArgs(string[] args) { - if (args == null || args.Length == 0) { + private static string getArgs(string[] args) + { + if (args == null || args.Length == 0) + { return ""; } var b = new StringBuilder(); - foreach (var arg in args) { + foreach (var arg in args) + { b.Append(arg); b.Append(" "); } @@ -94,30 +102,36 @@ public void Build(string[] args) if (!string.IsNullOrEmpty(config.Version)) { imgName += ":" + config.Version; } - if (!builder.Build(dockerfilename, imgName, stdout: o, stderr: e)) { + if (!builder.Build(dockerfilename, imgName, stdout: o, stderr: e)) + { Console.Error.WriteLine("Image build failed."); return; } - if (config.Publish) { - if (!builder.Push(imgName, stdout: o, stderr: e)) { + if (config.Publish) + { + if (!builder.Push(imgName, stdout: o, stderr: e)) + { Console.Error.WriteLine("Image push failed."); return; } } - if (runtimeConfig == null) { + if (runtimeConfig == null) + { return; } var exec = getExecutor(); - if (exec.PublishRequired() && !config.Publish) { + if (exec.PublishRequired() && !config.Publish) + { Console.Error.WriteLine("Image publish is required, but image was not published. Set publish to true in the package config."); return; } var id = exec.Run(imgName, runtimeConfig); - Console.CancelKeyPress += delegate { + Console.CancelKeyPress += delegate + { exec.Cancel(id); }; @@ -146,7 +160,8 @@ private string writeDockerfile(string dir, string exe, string[] args, Config con public static bool InDockerContainer() { - switch (System.Environment.GetEnvironmentVariable("METAPARTICLE_IN_CONTAINER")) { + switch (System.Environment.GetEnvironmentVariable("METAPARTICLE_IN_CONTAINER")) + { case "true": case "1": return true; @@ -180,7 +195,8 @@ public static void Containerize(string[] args, Action main) { config = (Config) attribute; } - if (attribute is RuntimeConfig) { + if (attribute is RuntimeConfig) + { runtimeConfig = (RuntimeConfig) attribute; } } diff --git a/dotnet/Metaparticle.Package/MetaparticleExecutor.cs b/dotnet/Metaparticle.Package/MetaparticleExecutor.cs index f235b70..9bc367e 100644 --- a/dotnet/Metaparticle.Package/MetaparticleExecutor.cs +++ b/dotnet/Metaparticle.Package/MetaparticleExecutor.cs @@ -5,7 +5,8 @@ using Metaparticle.Runtime; using static Metaparticle.Package.Util; -namespace Metaparticle.Package { +namespace Metaparticle.Package +{ public class MetaparticleExecutor : ContainerExecutor { public void Cancel(string id) @@ -15,7 +16,8 @@ public void Cancel(string id) HandleErrorExec("mp-compiler", string.Format("-f {0} --delete", specFileName)); } - private void HandleErrorExec(string cmd, string args, TextWriter stdout=null) { + private void HandleErrorExec(string cmd, string args, TextWriter stdout=null) + { var err = new StringWriter(); var proc = Exec(cmd, args, stdout: stdout, stderr: err); if (proc.ExitCode != 0) { diff --git a/dotnet/Metaparticle.Package/Util.cs b/dotnet/Metaparticle.Package/Util.cs index 028df0f..a8c4262 100644 --- a/dotnet/Metaparticle.Package/Util.cs +++ b/dotnet/Metaparticle.Package/Util.cs @@ -3,8 +3,10 @@ using System.IO; using System.Diagnostics; -namespace Metaparticle.Package { - public class Util { +namespace Metaparticle.Package +{ + public class Util + { public static Process Exec(String file, String args, TextWriter stdout=null, TextWriter stderr=null) { var task = ExecAsync(file, args, stdout, stderr); @@ -12,7 +14,8 @@ public static Process Exec(String file, String args, TextWriter stdout=null, Tex return task.Result; } - public static async Task Copy(StreamReader reader, TextWriter writer) { + public static async Task Copy(StreamReader reader, TextWriter writer) + { if (reader == null || writer == null) { return; } @@ -34,8 +37,10 @@ public static async Task ExecAsync(String file, String args, TextWriter var outputTask = Copy(proc.StandardOutput, stdout); var errTask = Copy(proc.StandardError, stderr); - await Task.Run(() => { - Task.WaitAll(new []{ + await Task.Run(() => + { + Task.WaitAll(new [] + { runTask, outputTask, errTask, diff --git a/dotnet/README.md b/dotnet/README.md index d4afb6d..44db008 100644 --- a/dotnet/README.md +++ b/dotnet/README.md @@ -51,7 +51,7 @@ You can set some of the config attributes through environment variables so that E.g. ``` -set MP_CONFIG_REPOSITORY=docker.io/myrepo/myimagename:sometag +set METAPARTICLE_CONFIG_REPOSITORY=docker.io/myrepo/myimagename:sometag ``` This will set the `Repository` property that you would otherwise set in the attributes. See `Config.cs` for supported environment variable overrides. diff --git a/dotnet/build.sh b/dotnet/build.sh new file mode 100755 index 0000000..1728772 --- /dev/null +++ b/dotnet/build.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -e + +dotnet build Metaparticle.Package + +# Build examples +cd examples +for D in *; do + if [ -d "${D}" ]; then + dotnet build ${D} + fi +done