Browse Source

Merge pull request #75 from ebsco/FixStartupMode

Add ability to set startup type to XML file
Oleg Nenashev 10 years ago
parent
commit
072af36998
4 changed files with 93 additions and 1 deletions
  1. 1 1
      Main.cs
  2. 3 0
      README.markdown
  3. 26 0
      ServiceDescriptor.cs
  4. 63 0
      Tests/winswTests/ServiceDescriptorTests.cs

+ 1 - 1
Main.cs

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

+ 3 - 0
README.markdown

@@ -171,6 +171,9 @@ Long human-readable description of the service. This gets displayed in Windows s
 ### 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.)
 
+### 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
 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.
 

+ 26 - 0
ServiceDescriptor.cs

@@ -5,6 +5,7 @@ using System.Diagnostics;
 using System.IO;
 using System.Reflection;
 using System.Xml;
+using WMI;
 
 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>
         /// 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

+ 63 - 0
Tests/winswTests/ServiceDescriptorTests.cs

@@ -5,6 +5,9 @@ using winsw;
 
 namespace winswTests
 {
+    using System;
+    using WMI;
+
     [TestFixture]
     public class ServiceDescriptorTests
     {
@@ -41,6 +44,66 @@ namespace winswTests
             _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]
         public void VerifyWorkingDirectory()
         {