Browse Source

Sketch Node.js service "building".

philliphoff-multiplatform
Phillip Hoff 5 years ago
parent
commit
a3dcd62419
  1. 12
      src/Microsoft.Tye.Core/ApplicationFactory.cs
  2. 16
      src/Microsoft.Tye.Core/ConfigModel/ConfigNode.cs
  3. 1
      src/Microsoft.Tye.Core/ConfigModel/ConfigService.cs
  4. 21
      src/Microsoft.Tye.Core/NodeServiceBuilder.cs
  5. 33
      src/Microsoft.Tye.Core/Serialization/ConfigServiceParser.cs
  6. 29
      src/tye/ApplicationBuilderExtensions.cs

12
src/Microsoft.Tye.Core/ApplicationFactory.cs

@ -337,6 +337,18 @@ namespace Microsoft.Tye
var external = new ExternalServiceBuilder(configService.Name, ServiceSource.Configuration); var external = new ExternalServiceBuilder(configService.Name, ServiceSource.Configuration);
service = external; service = external;
} }
else if (configService.Node != null)
{
string packagePath = Path.GetFullPath(Path.Combine(config.Source.Directory!.FullName, configService.Node.Package));
var node = new NodeServiceBuilder(configService.Name, packagePath, ServiceSource.Configuration)
{
Replicas = configService.Replicas ?? 1,
Script = configService.Node.Script
};
service = node;
}
else else
{ {
throw new CommandException("Unable to determine service type."); throw new CommandException("Unable to determine service type.");

16
src/Microsoft.Tye.Core/ConfigModel/ConfigNode.cs

@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.ComponentModel.DataAnnotations;
namespace Microsoft.Tye.ConfigModel
{
public class ConfigNode
{
[Required]
public string Package { get; set; } = default!;
public string? Script { get; set; } = default!;
}
}

1
src/Microsoft.Tye.Core/ConfigModel/ConfigService.cs

@ -46,5 +46,6 @@ namespace Microsoft.Tye.ConfigModel
public string? Version { get; set; } public string? Version { get; set; }
public string? Architecture { get; set; } public string? Architecture { get; set; }
public string? CloneDirectory { get; internal set; } public string? CloneDirectory { get; internal set; }
public ConfigNode? Node { get; set; }
} }
} }

21
src/Microsoft.Tye.Core/NodeServiceBuilder.cs

@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
namespace Microsoft.Tye
{
public class NodeServiceBuilder : LaunchedServiceBuilder
{
public NodeServiceBuilder(string name, string packagePath, ServiceSource source)
: base(name, source)
{
PackagePath = packagePath;
}
public string PackagePath { get; set; }
public string? Script { get; set; }
}
}

33
src/Microsoft.Tye.Core/Serialization/ConfigServiceParser.cs

@ -165,6 +165,10 @@ namespace Tye.Serialization
case "cloneDirectory": case "cloneDirectory":
service.CloneDirectory = YamlParser.GetScalarValue(key, child.Value); service.CloneDirectory = YamlParser.GetScalarValue(key, child.Value);
break; break;
case "node":
service.Node = new ConfigNode();
HandleNode((YamlMappingNode)child.Value, service.Node);
break;
default: default:
throw new TyeYamlException(child.Key.Start, CoreStrings.FormatUnrecognizedKey(key)); throw new TyeYamlException(child.Key.Start, CoreStrings.FormatUnrecognizedKey(key));
} }
@ -235,6 +239,35 @@ namespace Tye.Serialization
} }
} }
private static void HandleNode(YamlSequenceNode yamlSequenceNode, ConfigNode node)
{
foreach (var child in yamlSequenceNode.Children)
{
YamlParser.ThrowIfNotYamlMapping(child);
HandleNode((YamlMappingNode)child, node);
}
}
private static void HandleNode(YamlMappingNode yamlMappingNode, ConfigNode node)
{
foreach (var child in yamlMappingNode.Children)
{
var key = YamlParser.GetScalarValue(child.Key);
switch (key)
{
case "package":
node.Package = YamlParser.GetScalarValue("package", child.Value);
break;
case "script":
node.Script = YamlParser.GetScalarValue("script", child.Value);
break;
default:
throw new TyeYamlException(child.Key.Start, CoreStrings.FormatUnrecognizedKey(key));
}
}
}
private static void HandleServiceProbe(YamlMappingNode yamlMappingNode, ConfigProbe probe) private static void HandleServiceProbe(YamlMappingNode yamlMappingNode, ConfigProbe probe)
{ {
foreach (var child in yamlMappingNode.Children) foreach (var child in yamlMappingNode.Children)

29
src/tye/ApplicationBuilderExtensions.cs

@ -152,6 +152,35 @@ namespace Microsoft.Tye
liveness = null; liveness = null;
readiness = null; readiness = null;
} }
else if (service is NodeServiceBuilder node)
{
string executablePath = "node";
string? workingDirectory = Path.GetDirectoryName(node.PackagePath);
var input = new StringReader(File.ReadAllText(node.PackagePath));
var yaml = new YamlDotNet.RepresentationModel.YamlStream();
yaml.Load(input);
var mapping = (YamlDotNet.RepresentationModel.YamlMappingNode)yaml.Documents[0].RootNode;
var mainNode = (YamlDotNet.RepresentationModel.YamlScalarNode)mapping.Children[new YamlDotNet.RepresentationModel.YamlScalarNode("main")];
var main = mainNode.Value;
string args = $"{main}";
runInfo = new ExecutableRunInfo(executablePath, workingDirectory, args);
replicas = node.Replicas;
liveness = null;
readiness = null;
foreach (var entry in node.EnvironmentVariables)
{
env.Add(entry.ToHostingEnvironmentVariable());
}
}
else else
{ {
throw new InvalidOperationException($"Cannot figure out how to run service '{service.Name}'."); throw new InvalidOperationException($"Cannot figure out how to run service '{service.Name}'.");

Loading…
Cancel
Save