prep for service dependency support, part 2

This commit is contained in:
Chris Cheetham 2020-03-25 20:31:26 -04:00
Родитель 0df0cf2f18
Коммит d874e0fa22
11 изменённых файлов: 125 добавлений и 30 удалений

Просмотреть файл

@ -47,11 +47,9 @@ namespace Steeltoe.Tooling.Controllers
/// Returns the project.
/// </summary>
/// <returns>The project.</returns>
protected Project GetProject()
protected Deployment GetDeployment()
{
var projectDir = Context.WorkingDirectory;
var projectName = Path.GetFileName(projectDir);
return new ProjectBuilder().BuildProject($"{projectDir}/{projectName}.csproj");
return new DeploymentBuilder().BuildDeployment(Context.WorkingDirectory);
}
}
}

Просмотреть файл

@ -41,10 +41,10 @@ namespace Steeltoe.Tooling.Controllers
/// </summary>
protected override void Execute()
{
var project = GetProject();
if (!Context.Registry.Images.TryGetValue(project.Framework, out var image))
var deployment = GetDeployment();
if (!Context.Registry.Images.TryGetValue(deployment.Project.Framework, out var image))
{
throw new ToolingException($"no image for framework: {project.Framework}");
throw new ToolingException($"no image for framework: {deployment.Project.Framework}");
}
var files = new string[] {"Dockerfile", "docker-compose.yml"};
@ -52,14 +52,14 @@ namespace Steeltoe.Tooling.Controllers
{
Logger.LogDebug($"writing {file}");
var template = TemplateManager.GetTemplate($"{file}.st");
template.Bind("project", project);
template.Bind("project", deployment.Project);
template.Bind("image", image);
File.WriteAllText(file, template.Render());
}
if (!_runInDocker) return;
var cli = new Cli("docker-compose", Context.Shell);
cli.Run("up --build", $"running '{project.Name}' in Docker");
cli.Run("up --build", $"running '{deployment.Project.Name}' in Docker");
}
}
}

Просмотреть файл

@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
using System.Collections.Generic;
using Steeltoe.Tooling.Models;
using YamlDotNet.Serialization;
namespace Steeltoe.Tooling.Controllers
@ -35,11 +33,8 @@ namespace Steeltoe.Tooling.Controllers
/// </summary>
protected override void Execute()
{
Dictionary<string, Project> projects = new Dictionary<string, Project>();
var project = GetProject();
projects.Add(project.Name, project);
var serializer = new SerializerBuilder().Build();
Context.Console.WriteLine(serializer.Serialize(projects));
Context.Console.WriteLine(serializer.Serialize(GetDeployment()));
}
}
}

Просмотреть файл

@ -24,7 +24,7 @@ namespace Steeltoe.Tooling.Controllers
/// </summary>
protected override void Execute()
{
var project = GetProject();
var project = GetDeployment().Project;
var cli = new Cli("docker-compose", Context.Shell);
cli.Run("down", $"stopping '{project.Name}' in Docker");
}

Просмотреть файл

@ -0,0 +1,36 @@
// Copyright 2020 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using YamlDotNet.Serialization;
namespace Steeltoe.Tooling.Models
{
/// <summary>
/// A model of a Docker deployment.
/// </summary>
public class Deployment
{
/// <summary>
/// Deployment name.
/// </summary>
[YamlMember(Alias = "name")]
public string Name { get; set; }
/// <summary>
/// Project to be deployed.
/// </summary>
[YamlMember(Alias = "project")]
public Project Project { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,39 @@
// Copyright 2020 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System.IO;
namespace Steeltoe.Tooling.Models
{
/// <summary>
/// Helper for building a Deployment.
/// </summary>
public class DeploymentBuilder
{
/// <summary>
/// Returns a deployment for the specified directory.
/// </summary>
/// <returns>deployment model</returns>
public Deployment BuildDeployment(string directory)
{
var name = Path.GetFileName(directory);
var deployment = new Deployment
{
Name = name,
Project = new ProjectBuilder().BuildProject(Path.Join(directory, $"{name}.csproj"))
};
return deployment;
}
}
}

Просмотреть файл

@ -25,7 +25,7 @@ namespace Steeltoe.Tooling.Models
/// <summary>
/// Project name.
/// </summary>
[YamlIgnore]
[YamlMember(Alias = "name")]
public string Name
{
get => _name;
@ -33,7 +33,6 @@ namespace Steeltoe.Tooling.Models
}
private string _name;
/// <summary>
/// Project file path.
/// </summary>
@ -51,5 +50,11 @@ namespace Steeltoe.Tooling.Models
/// </summary>
[YamlMember(Alias = "protocols")]
public List<Protocol> Protocols { get; set; }
/// <summary>
/// Project service dependencies to be deployed.
/// </summary>
[YamlMember(Alias = "services")]
public List<Service> Services { get; }
}
}

Просмотреть файл

@ -29,11 +29,11 @@ namespace Steeltoe.Tooling.Models
{
private static readonly ILogger Logger = Logging.LoggerFactory.CreateLogger<ProjectBuilder>();
private static readonly List<Protocol> DefaultServices = new List<Protocol>();
private static readonly List<Protocol> DefaultProtocols = new List<Protocol>();
static ProjectBuilder()
{
DefaultServices.Add(new Protocol("http", 8080));
DefaultProtocols.Add(new Protocol("http", 8080));
}
/// <summary>
@ -58,7 +58,7 @@ namespace Steeltoe.Tooling.Models
Name = Path.GetFileNameWithoutExtension(projectFile),
File = Path.GetFileName(projectFile),
Framework = GetFramework(projectFile),
Protocols = GetServices(projectFile)
Protocols = GetProtocols(projectFile)
};
return project;
}
@ -82,13 +82,13 @@ namespace Steeltoe.Tooling.Models
throw new ToolingException("could not determine framework");
}
private List<Protocol> GetServices(string projectFile)
private List<Protocol> GetProtocols(string projectFile)
{
var launchSettingsPath =
Path.Join(Path.GetDirectoryName(projectFile), "Properties", "launchSettings.json");
if (!File.Exists(launchSettingsPath))
{
return DefaultServices;
return DefaultProtocols;
}
Logger.LogDebug($"loading launch settings: {launchSettingsPath}");
@ -104,19 +104,19 @@ namespace Steeltoe.Tooling.Models
(YamlMappingNode) profiles.Children[new YamlScalarNode(Path.GetFileNameWithoutExtension(projectFile))];
if (!profile.Children.ContainsKey(new YamlScalarNode("applicationUrl")))
{
return DefaultServices;
return DefaultProtocols;
}
var urlSpec = profile.Children[new YamlScalarNode("applicationUrl")];
var urls = $"{urlSpec}".Split(';');
if (urls.Length == 0)
{
return DefaultServices;
return DefaultProtocols;
}
var services = urls.Select(url => new Uri(url)).Select(uri => new Protocol(uri.Scheme, uri.Port)).ToList();
services.Sort();
return services;
var protocols = urls.Select(url => new Uri(url)).Select(uri => new Protocol(uri.Scheme, uri.Port)).ToList();
protocols.Sort();
return protocols;
}
}
}

Просмотреть файл

@ -0,0 +1,22 @@
// Copyright 2020 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using YamlDotNet.Serialization;
namespace Steeltoe.Tooling.Models
{
public class Service
{
}
}

Просмотреть файл

@ -3,6 +3,6 @@ services:
<project.Name>:
build: .
ports:
<project.Services: {svc|- "<svc.Port>:<svc.Port>"}; separator="\n">
<project.Protocols: {protocol |- "<protocol.Port>:<protocol.Port>"}; separator="\n">
volumes:
- .:/<project.Name>