From 853e5aeec6ec71a518907eabed01e6bbcd31ceb2 Mon Sep 17 00:00:00 2001 From: Buddhika Chathuranga Date: Mon, 10 Aug 2020 16:53:54 +0530 Subject: [PATCH] Support both XML and YAML configuration + Remove support for *.vshost files and folder hierarchy lookup which used to be an undocumented development features (#641) * Sync with upstream (#3) * Add a Dependabot configuration (#558) * Add a Dependabot configuration * Update dependabot.yml * Bump NUnit3TestAdapter from 3.16.0 to 3.16.1 (#561) Bumps [NUnit3TestAdapter](https://github.com/nunit/nunit3-vs-adapter) from 3.16.0 to 3.16.1. - [Release notes](https://github.com/nunit/nunit3-vs-adapter/releases) - [Commits](https://github.com/nunit/nunit3-vs-adapter/compare/V3.16...V3.16.1) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump Microsoft.NET.Test.Sdk from 16.4.0 to 16.6.1 (#563) Bumps [Microsoft.NET.Test.Sdk](https://github.com/microsoft/vstest) from 16.4.0 to 16.6.1. - [Release notes](https://github.com/microsoft/vstest/releases) - [Commits](https://github.com/microsoft/vstest/compare/v16.4.0...v16.6.1) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump ilmerge from 3.0.29 to 3.0.40 (#559) * Bump ilmerge from 3.0.29 to 3.0.40 Bumps [ilmerge](https://github.com/dotnet/ILMerge) from 3.0.29 to 3.0.40. - [Release notes](https://github.com/dotnet/ILMerge/releases) - [Commits](https://github.com/dotnet/ILMerge/commits) Signed-off-by: dependabot[bot] * Define $(ILMergeVersion) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Next Turn <45985406+NextTurn@users.noreply.github.com> * Bump coverlet.collector from 1.2.0 to 1.3.0 (#560) Bumps [coverlet.collector](https://github.com/coverlet-coverage/coverlet) from 1.2.0 to 1.3.0. - [Release notes](https://github.com/coverlet-coverage/coverlet/releases) - [Commits](https://github.com/coverlet-coverage/coverlet/commits) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump ilmerge from 3.0.40 to 3.0.41 (#571) Bumps [ilmerge](https://github.com/dotnet/ILMerge) from 3.0.40 to 3.0.41. - [Release notes](https://github.com/dotnet/ILMerge/releases) - [Commits](https://github.com/dotnet/ILMerge/commits) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Oleg Nenashev Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Next Turn <45985406+NextTurn@users.noreply.github.com> * Update winsw to work with both XML and YAML config Add logic to select configuration type Remove redundant codes for config file finding * Update ConfigType.cs file encoding * Reference yamlonfiguration.md file from the Readme.md * Refactor yaml and xml support Update naming conventions in Readme.md Remove ConfigType.cs * Refactors * Update Program.cs * Remove recursive look for configurations file Remove trimming .vshost extension in executable file name * Update Program.cs Co-authored-by: Oleg Nenashev Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Next Turn <45985406+NextTurn@users.noreply.github.com> --- README.md | 4 +-- src/Core/ServiceWrapper/Program.cs | 23 ++++++++++++++++- src/Core/ServiceWrapper/WrapperService.cs | 5 ---- src/Core/WinSWCore/ServiceDescriptor.cs | 28 +-------------------- src/Core/WinSWCore/ServiceDescriptorYaml.cs | 27 ++------------------ 5 files changed, 27 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index cd3558f..e4550a6 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Binaries are available [here](https://repo.jenkins-ci.org/releases/com/sun/winsw ## Usage -WinSW is being managed by configuration files: [Main XML configuration file](doc/xmlConfigFile.md) and [EXE configuration file](doc/exeConfigFile.md). +WinSW is being managed by configuration files: [Main XML configuration file](doc/xmlConfigFile.md) and [Main YAML configuration file](doc/yamlConfigFile.md). Your renamed *WinSW.exe* binary also accepts the following commands: @@ -59,7 +59,7 @@ User documentation: * [Installation guide](doc/installation.md) - Describes the installation process for different systems and .NET versions * Configuration: * [Main XML configuration file](doc/xmlConfigFile.md) - * [EXE configuration file](doc/exeConfigFile.md) + * [Main YAML configuration file](doc/yamlConfigFile.md) * [Logging and error reporting](doc/loggingAndErrorReporting.md) * [Extensions](doc/extensions/extensions.md) * Use-cases: diff --git a/src/Core/ServiceWrapper/Program.cs b/src/Core/ServiceWrapper/Program.cs index 05152e8..3ea852a 100644 --- a/src/Core/ServiceWrapper/Program.cs +++ b/src/Core/ServiceWrapper/Program.cs @@ -21,6 +21,7 @@ using log4net.Layout; using WinSW.Configuration; using WinSW.Logging; using WinSW.Native; +using WinSW.Util; using WMI; using ServiceType = WMI.ServiceType; @@ -64,7 +65,7 @@ namespace WinSW bool inConsoleMode = argsArray.Length > 0; // If descriptor is not specified, initialize the new one (and load configs from there) - descriptor ??= new ServiceDescriptor(); + descriptor ??= GetServiceDescriptor(); // Configure the wrapper-internal logging. // STDOUT and STDERR of the child process will be handled independently. @@ -658,6 +659,26 @@ namespace WinSW } } + private static IWinSWConfiguration GetServiceDescriptor() + { + var executablePath = new DefaultWinSWSettings().ExecutablePath; + var baseName = Path.GetFileNameWithoutExtension(executablePath); + + var d = new DirectoryInfo(Path.GetDirectoryName(executablePath)); + + if (File.Exists(Path.Combine(d.FullName, baseName + ".xml"))) + { + return new ServiceDescriptor(baseName, d); + } + + if (File.Exists(Path.Combine(d.FullName, baseName + ".yml"))) + { + return new ServiceDescriptorYaml(baseName, d).Configurations; + } + + throw new FileNotFoundException($"Unable to locate { baseName }.[xml|yml] file within executable directory"); + } + private static void PrintHelp() { Console.WriteLine("A wrapper binary that can be used to host executables as Windows services"); diff --git a/src/Core/ServiceWrapper/WrapperService.cs b/src/Core/ServiceWrapper/WrapperService.cs index 5b53bba..04ba60d 100644 --- a/src/Core/ServiceWrapper/WrapperService.cs +++ b/src/Core/ServiceWrapper/WrapperService.cs @@ -73,11 +73,6 @@ namespace WinSW eventLogProvider.Service = this; } - public WrapperService() - : this(new ServiceDescriptor()) - { - } - /// /// Process the file copy instructions, so that we can replace files that are always in use while /// the service runs. diff --git a/src/Core/WinSWCore/ServiceDescriptor.cs b/src/Core/WinSWCore/ServiceDescriptor.cs index 4d31d83..78d8872 100644 --- a/src/Core/WinSWCore/ServiceDescriptor.cs +++ b/src/Core/WinSWCore/ServiceDescriptor.cs @@ -41,34 +41,8 @@ namespace WinSW // Currently there is no opportunity to alter the executable path public virtual string ExecutablePath => Defaults.ExecutablePath; - public ServiceDescriptor() + public ServiceDescriptor(string baseName, DirectoryInfo d) { - // find co-located configuration xml. We search up to the ancestor directories to simplify debugging, - // as well as trimming off ".vshost" suffix (which is used during debugging) - // Get the first parent to go into the recursive loop - string p = this.ExecutablePath; - string baseName = Path.GetFileNameWithoutExtension(p); - if (baseName.EndsWith(".vshost")) - { - baseName = baseName.Substring(0, baseName.Length - 7); - } - - DirectoryInfo d = new DirectoryInfo(Path.GetDirectoryName(p)); - while (true) - { - if (File.Exists(Path.Combine(d.FullName, baseName + ".xml"))) - { - break; - } - - if (d.Parent is null) - { - throw new FileNotFoundException("Unable to locate " + baseName + ".xml file within executable directory or any parents"); - } - - d = d.Parent; - } - this.BaseName = baseName; this.BasePath = Path.Combine(d.FullName, this.BaseName); diff --git a/src/Core/WinSWCore/ServiceDescriptorYaml.cs b/src/Core/WinSWCore/ServiceDescriptorYaml.cs index 0dec860..332b78b 100644 --- a/src/Core/WinSWCore/ServiceDescriptorYaml.cs +++ b/src/Core/WinSWCore/ServiceDescriptorYaml.cs @@ -7,35 +7,12 @@ namespace WinSW { public class ServiceDescriptorYaml { - public readonly YamlConfiguration Configurations = new YamlConfiguration(); + public readonly YamlConfiguration Configurations; public static DefaultWinSWSettings Defaults { get; } = new DefaultWinSWSettings(); - public ServiceDescriptorYaml() + public ServiceDescriptorYaml(string baseName, DirectoryInfo d) { - string p = Defaults.ExecutablePath; - string baseName = Path.GetFileNameWithoutExtension(p); - if (baseName.EndsWith(".vshost")) - { - baseName = baseName.Substring(0, baseName.Length - 7); - } - - DirectoryInfo d = new DirectoryInfo(Path.GetDirectoryName(p)); - while (true) - { - if (File.Exists(Path.Combine(d.FullName, baseName + ".yml"))) - { - break; - } - - if (d.Parent is null) - { - throw new FileNotFoundException("Unable to locate " + baseName + ".yml file within executable directory or any parents"); - } - - d = d.Parent; - } - var basepath = Path.Combine(d.FullName, baseName); using (var reader = new StreamReader(basepath + ".yml"))