Add ability to set startup type to XML file

pull/75/head
Nicholas Carpenter 2015-01-31 21:38:05 -05:00
parent f4c11569cb
commit 57b6d2ad48
4 changed files with 93 additions and 1 deletions

View File

@ -584,7 +584,7 @@ namespace winsw
"\"" + d.ExecutablePath + "\"", "\"" + d.ExecutablePath + "\"",
ServiceType.OwnProcess, ServiceType.OwnProcess,
ErrorControl.UserNotified, ErrorControl.UserNotified,
StartMode.Automatic, d.StartMode,
d.Interactive, d.Interactive,
username, username,
password, password,

View File

@ -171,6 +171,9 @@ Long human-readable description of the service. This gets displayed in Windows s
### executable ### executable
This element specifies the executable to be launched. It can be either absolute path, or you can just specify the executable name and let it be searched from `PATH` (although note that the services often run in a different user account and therefore it might have different `PATH` than your shell does.) This element specifies the executable to be launched. It can be either absolute path, or you can just specify the executable name and let it be searched from `PATH` (although note that the services often run in a different user account and therefore it might have different `PATH` than your shell does.)
### startmode - Optional Element
This element specifies the start mode of the Windows service. It can be one of the following values: Boot, System, Automatic, or Manual. See [MSDN](https://msdn.microsoft.com/en-us/library/aa384896%28v=vs.85%29.aspx) for details. The default is Automatic.
### depend ### depend
Specify IDs of other services that this service depends on. When service X depends on service Y, X can only run if Y is running. Specify IDs of other services that this service depends on. When service X depends on service Y, X can only run if Y is running.

View File

@ -5,6 +5,7 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Xml; using System.Xml;
using WMI;
namespace winsw namespace winsw
{ {
@ -393,6 +394,31 @@ namespace winsw
} }
} }
/// <summary>
/// Start mode of the Service
/// </summary>
public StartMode StartMode
{
get
{
var p = SingleElement("startmode", true);
if (p == null) return StartMode.Automatic; // default value
try
{
return (StartMode)Enum.Parse(typeof(StartMode), p, true);
}
catch
{
Console.WriteLine("Start mode in XML must be one of the following:");
foreach (string sm in Enum.GetNames(typeof(StartMode)))
{
Console.WriteLine(sm);
}
throw;
}
}
}
/// <summary> /// <summary>
/// True if the service should when finished on shutdown. /// True if the service should when finished on shutdown.
/// This doesn't work on some OSes. See http://msdn.microsoft.com/en-us/library/ms679277%28VS.85%29.aspx /// This doesn't work on some OSes. See http://msdn.microsoft.com/en-us/library/ms679277%28VS.85%29.aspx

View File

@ -5,6 +5,9 @@ using winsw;
namespace winswTests namespace winswTests
{ {
using System;
using WMI;
[TestFixture] [TestFixture]
public class ServiceDescriptorTests public class ServiceDescriptorTests
{ {
@ -41,6 +44,66 @@ namespace winswTests
_extendedServiceDescriptor = ServiceDescriptor.FromXML(seedXml); _extendedServiceDescriptor = ServiceDescriptor.FromXML(seedXml);
} }
[Test]
public void DefaultStartMode()
{
Assert.That(_extendedServiceDescriptor.StartMode, Is.EqualTo(StartMode.Automatic));
}
[Test]
[ExpectedException(typeof(System.ArgumentException))]
public void IncorrectStartMode()
{
const string SeedXml = "<service>"
+ "<id>service.exe</id>"
+ "<name>Service</name>"
+ "<description>The service.</description>"
+ "<executable>node.exe</executable>"
+ "<arguments>My Arguments</arguments>"
+ "<startmode>rotate</startmode>"
+ "<logmode>rotate</logmode>"
+ "<serviceaccount>"
+ "<domain>" + Domain + "</domain>"
+ "<user>" + Username + "</user>"
+ "<password>" + Password + "</password>"
+ "<allowservicelogon>" + AllowServiceAccountLogonRight + "</allowservicelogon>"
+ "</serviceaccount>"
+ "<workingdirectory>"
+ ExpectedWorkingDirectory
+ "</workingdirectory>"
+ @"<logpath>C:\logs</logpath>"
+ "</service>";
_extendedServiceDescriptor = ServiceDescriptor.FromXML(SeedXml);
Assert.That(_extendedServiceDescriptor.StartMode, Is.EqualTo(StartMode.Manual));
}
[Test]
public void ChangedStartMode()
{
const string SeedXml = "<service>"
+ "<id>service.exe</id>"
+ "<name>Service</name>"
+ "<description>The service.</description>"
+ "<executable>node.exe</executable>"
+ "<arguments>My Arguments</arguments>"
+ "<startmode>manual</startmode>"
+ "<logmode>rotate</logmode>"
+ "<serviceaccount>"
+ "<domain>" + Domain + "</domain>"
+ "<user>" + Username + "</user>"
+ "<password>" + Password + "</password>"
+ "<allowservicelogon>" + AllowServiceAccountLogonRight + "</allowservicelogon>"
+ "</serviceaccount>"
+ "<workingdirectory>"
+ ExpectedWorkingDirectory
+ "</workingdirectory>"
+ @"<logpath>C:\logs</logpath>"
+ "</service>";
_extendedServiceDescriptor = ServiceDescriptor.FromXML(SeedXml);
Assert.That(_extendedServiceDescriptor.StartMode, Is.EqualTo(StartMode.Manual));
}
[Test] [Test]
public void VerifyWorkingDirectory() public void VerifyWorkingDirectory()
{ {