Skip to content

Commit

Permalink
Merge pull request #7958 from abpframework/issue/5716
Browse files Browse the repository at this point in the history
Set random port when a new Microservice is created
  • Loading branch information
hikalkan authored Mar 5, 2021
2 parents 829b566 + 4d9f49d commit 12d9270
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.IO;
using System.Linq;
using Volo.Abp.Cli.Commands;

namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps
{
public class MicroserviceServiceRandomPortStep : ProjectBuildPipelineStep
{
private readonly string _defaultPort = string.Empty;
private string _tyeFileContent = null;

public MicroserviceServiceRandomPortStep(string defaultPort)
{
_defaultPort = defaultPort;
}

public override void Execute(ProjectBuildContext context)
{
var newPort = GetNewRandomPort(context);

var targetFiles = context.Files.Where(f=> f.Name.EndsWith("launchSettings.json") || f.Name.EndsWith("appsettings.json")).ToList();

foreach (var file in targetFiles)
{
file.SetContent(file.Content.Replace(_defaultPort, newPort));
}
}

private string GetNewRandomPort(ProjectBuildContext context)
{
string newPort;
var rnd = new Random();
var tryCount = 0;

do
{
newPort = rnd.Next(44350, 45350).ToString();

if (tryCount++ > 2000)
{
break;
}

} while (PortExistsForAnotherService(context, newPort));

return newPort;
}

private bool PortExistsForAnotherService(ProjectBuildContext context, string newPort)
{
return ReadTyeFileContent(context).SplitToLines().Any(l => l.Contains("port") && l.Contains(newPort));
}

private string ReadTyeFileContent(ProjectBuildContext context)
{
if (_tyeFileContent != null)
{
return _tyeFileContent;
}

var solutionFolderPath = context.BuildArgs.ExtraProperties[NewCommand.Options.OutputFolder.Short] ??
context.BuildArgs.ExtraProperties[NewCommand.Options.OutputFolder.Long] ??
Directory.GetCurrentDirectory();

var tyeFilePath = Path.Combine(solutionFolderPath, "tye.yaml");

if (!File.Exists(tyeFilePath))
{
return String.Empty;
}

_tyeFileContent = File.ReadAllText(tyeFilePath);

return _tyeFileContent;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public override IEnumerable<ProjectBuildPipelineStep> GetCustomSteps(ProjectBuil
var steps = new List<ProjectBuildPipelineStep>();

DeleteUnrelatedUiProject(context, steps);
SetRandomPortForHostProject(context, steps);
RandomizeStringEncryption(context, steps);

return steps;
Expand All @@ -54,6 +55,11 @@ private static void DeleteUnrelatedUiProject(ProjectBuildContext context, List<P
}
}

private static void SetRandomPortForHostProject(ProjectBuildContext context, List<ProjectBuildPipelineStep> steps)
{
steps.Add(new MicroserviceServiceRandomPortStep("44371"));
}

private static void RandomizeStringEncryption(ProjectBuildContext context, List<ProjectBuildPipelineStep> steps)
{
steps.Add(new RandomizeStringEncryptionStep());
Expand Down

0 comments on commit 12d9270

Please sign in to comment.