Skip to content

Commit

Permalink
Fix environment variable name in dotnet to be consistent and also cod…
Browse files Browse the repository at this point in the history
…e cleanup for consistency (metaparticle-io#76)

* fix up examples and documentation

* fix up indenting

* renamed env vars to be conssitent and also moved braces to new lines

* Add travis for dotnet (metaparticle-io#3)

* add initial travis file

* fix up missing dash

* try again going into the right directory this time

* no need for mono and we can set the dotnet core version

* make build.sh executable

* see what directory we are on

* set to unix type

* build the examples
  • Loading branch information
srini85 authored and brendandburns committed Jan 27, 2018
1 parent 146d583 commit 06a9b70
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 40 deletions.
13 changes: 12 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ matrix:
- cd javascript/metaparticle-package
- npm install
script:
- npm test
- npm test

include:
- language: csharp
sudo: required
dotnet: 2.0.0
mono: none
before_script:
- cd dotnet
- ls
script:
- ./build.sh
13 changes: 8 additions & 5 deletions dotnet/Metaparticle.Package/Config.cs
Original file line number Diff line number Diff line change
@@ -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; }
Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions dotnet/Metaparticle.Package/ContainerExecutor.cs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
18 changes: 12 additions & 6 deletions dotnet/Metaparticle.Package/DockerExecutor.cs
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions dotnet/Metaparticle.Package/ImageBuilder.cs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
48 changes: 32 additions & 16 deletions dotnet/Metaparticle.Package/Metaparticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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":
Expand All @@ -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(" ");
}
Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
6 changes: 4 additions & 2 deletions dotnet/Metaparticle.Package/MetaparticleExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
15 changes: 10 additions & 5 deletions dotnet/Metaparticle.Package/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
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);
task.Wait();
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;
}
Expand All @@ -34,8 +37,10 @@ public static async Task<Process> 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,
Expand Down
2 changes: 1 addition & 1 deletion dotnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions dotnet/build.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 06a9b70

Please sign in to comment.