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"))