mirror of https://github.com/winsw/winsw
Read username/password & working dir from XML
It is possible to specify the useraccount (and password) that the service will run as. In addition, the working directory for the service can be supplied.pull/22/head
parent
380c329b0a
commit
7aa4018451
13
Main.cs
13
Main.cs
|
@ -502,18 +502,27 @@ namespace winsw
|
|||
if (args[0] == "install")
|
||||
{
|
||||
string username=null, password=null;
|
||||
if (args.Count > 1 && args[1] == "/p") {
|
||||
if (args.Count > 1 && args[1] == "/p")
|
||||
{
|
||||
// we expected username/password on stdin
|
||||
Console.Write("Username: ");
|
||||
username = Console.ReadLine();
|
||||
Console.Write("Password: ");
|
||||
password = ReadPassword();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (d.HasServiceAccount())
|
||||
{
|
||||
username = d.ServiceAccountUser;
|
||||
password = d.ServiceAccountPassword;
|
||||
}
|
||||
}
|
||||
|
||||
svc.Create (
|
||||
d.Id,
|
||||
d.Caption,
|
||||
"\"" + ServiceDescriptor.ExecutablePath + "\"",
|
||||
"\"" + d.ExecutablePath + "\"",
|
||||
WMI.ServiceType.OwnProcess,
|
||||
ErrorControl.UserNotified,
|
||||
StartMode.Automatic,
|
||||
|
|
|
@ -240,3 +240,17 @@ If the service keeps failing and it goes beyond the number of `<onfailure>` conf
|
|||
This optional element controls the timing in which Windows SCM resets the failure count. For example, if you specify `<resetfailure>1 hour</resetfailure>` and your service continues to run longer than one hour, then the failure count is reset to zero. This affects the behaviour of the failure actions (see `<onfailure>` above).
|
||||
|
||||
In other words, this is the duration in which you consider the service has been running successfully. Defaults to 1 day.
|
||||
|
||||
### Service account
|
||||
It is possible to specify the useraccount (and password) that the service will run as. To do this, specify a `<serviceaccount>` element like this:
|
||||
|
||||
<serviceaccount>
|
||||
<domain>YOURDOMAIN</domain>
|
||||
<user>useraccount</user>
|
||||
<password>Pa55w0rd</password>
|
||||
</serviceaccount>
|
||||
|
||||
### Working directory
|
||||
Some services need to run with a working directory specified. To do this, specify a `<workingdirectory>` element like this:
|
||||
|
||||
<workingdirectory>C:\application</workingdirectory>
|
|
@ -22,7 +22,7 @@ namespace winsw
|
|||
/// </summary>
|
||||
public class ServiceDescriptor
|
||||
{
|
||||
private readonly XmlDocument dom = new XmlDocument();
|
||||
protected readonly XmlDocument dom = new XmlDocument();
|
||||
|
||||
/// <summary>
|
||||
/// Where did we find the configuration file?
|
||||
|
@ -37,7 +37,7 @@ namespace winsw
|
|||
/// </summary>
|
||||
public readonly string BaseName;
|
||||
|
||||
public static string ExecutablePath
|
||||
public virtual string ExecutablePath
|
||||
{
|
||||
get
|
||||
{
|
||||
|
@ -157,7 +157,7 @@ namespace winsw
|
|||
{
|
||||
get
|
||||
{
|
||||
return SingleElement("stopexecutable",true);
|
||||
return SingleElement("stopexecutable");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -511,5 +511,55 @@ namespace winsw
|
|||
}
|
||||
}
|
||||
|
||||
protected string GetServiceAccountPart(string subNodeName)
|
||||
{
|
||||
var node = dom.SelectSingleNode("//serviceaccount");
|
||||
|
||||
if (node != null)
|
||||
{
|
||||
var subNode = node.SelectSingleNode(subNodeName);
|
||||
if (subNode != null)
|
||||
{
|
||||
return subNode.InnerText;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
protected string serviceAccountDomain
|
||||
{
|
||||
get{
|
||||
return GetServiceAccountPart("domain");
|
||||
}
|
||||
}
|
||||
|
||||
protected string serviceAccountName
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetServiceAccountPart("user");
|
||||
}
|
||||
}
|
||||
|
||||
public string ServiceAccountPassword
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetServiceAccountPart("password");
|
||||
}
|
||||
}
|
||||
|
||||
public string ServiceAccountUser
|
||||
{
|
||||
get { return (serviceAccountDomain ?? "NULL") + @"\" + (serviceAccountName ?? "NULL"); }
|
||||
}
|
||||
|
||||
public bool HasServiceAccount()
|
||||
{
|
||||
return !string.IsNullOrEmpty(serviceAccountDomain) && !string.IsNullOrEmpty(serviceAccountName);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1,36 @@
|
|||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("winswTests")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Microsoft")]
|
||||
[assembly: AssemblyProduct("winswTests")]
|
||||
[assembly: AssemblyCopyright("Copyright © Microsoft 2013")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("01ee65cd-18ae-4f3b-8eac-c9f790d5f24e")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
|
@ -0,0 +1,78 @@
|
|||
using NUnit.Framework;
|
||||
using winsw;
|
||||
|
||||
namespace winswTests
|
||||
{
|
||||
|
||||
public class ServiceDescriptorExtended : ServiceDescriptor
|
||||
{
|
||||
|
||||
public ServiceDescriptorExtended(string descriptorXml)
|
||||
{
|
||||
LoadTestXml(descriptorXml);
|
||||
}
|
||||
|
||||
private void LoadTestXml(string xml)
|
||||
{
|
||||
dom.LoadXml(xml);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[TestFixture]
|
||||
public class ServiceDescriptorTests
|
||||
{
|
||||
|
||||
private ServiceDescriptorExtended extendedServiceDescriptor;
|
||||
|
||||
private const string ExpectedWorkingDirectory = @"Z:\Path\SubPath";
|
||||
private const string Username = "User";
|
||||
private const string Password = "Password";
|
||||
private const string Domain = "Domain";
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
const string SeedXml = "<service>"
|
||||
+ "<id>service.exe</id>"
|
||||
+ "<name>Service</name>"
|
||||
+ "<description>The service.</description>"
|
||||
+ "<executable>node.exe</executable>"
|
||||
+ "<arguments>My Arguments</arguments>"
|
||||
+ "<logmode>rotate</logmode>"
|
||||
+ "<serviceaccount>"
|
||||
+ "<domain>" + Domain + "</domain>"
|
||||
+ "<user>" + Username + "</user>"
|
||||
+ "<password>" + Password + "</password>"
|
||||
+ "</serviceaccount>"
|
||||
+ "<workingdirectory>"
|
||||
+ ExpectedWorkingDirectory
|
||||
+ "</workingdirectory>"
|
||||
+ @"<logpath>C:\logs</logpath>"
|
||||
+ "</service>";
|
||||
|
||||
extendedServiceDescriptor = new ServiceDescriptorExtended(SeedXml);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void VerifyWorkingDirectory()
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("_extendedServiceDescriptor.WorkingDirectory :: " + extendedServiceDescriptor.WorkingDirectory);
|
||||
Assert.That(extendedServiceDescriptor.WorkingDirectory, Is.EqualTo(ExpectedWorkingDirectory));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void VerifyUsername()
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("_extendedServiceDescriptor.WorkingDirectory :: " + extendedServiceDescriptor.WorkingDirectory);
|
||||
Assert.That(extendedServiceDescriptor.ServiceAccountUser, Is.EqualTo(Domain + "\\" + Username));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void VerifyPassword()
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("_extendedServiceDescriptor.WorkingDirectory :: " + extendedServiceDescriptor.WorkingDirectory);
|
||||
Assert.That(extendedServiceDescriptor.ServiceAccountPassword, Is.EqualTo(Password));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProductVersion>9.0.30729</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{93843402-842B-44B4-B303-AEE829BE0B43}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>winswTests</RootNamespace>
|
||||
<AssemblyName>winswTests</AssemblyName>
|
||||
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="nunit.framework, Version=2.6.2.12296, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\Lib\nunit.framework.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ServiceDescriptorTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\winsw.csproj">
|
||||
<Project>{0DE77F55-ADE5-43C1-999A-0BC81153B039}</Project>
|
||||
<Name>winsw</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
|
@ -50,6 +50,8 @@ namespace WMI
|
|||
// ReturnValue Create(bool desktopInteract, string displayName, int errorControl, string loadOrderGroup, string loadOrderGroupDependencies, string name, string pathName, string serviceDependencies, string serviceType, string startMode, string startName, string startPassword);
|
||||
void Create(string name, string displayName, string pathName, ServiceType serviceType, ErrorControl errorControl, StartMode startMode, bool desktopInteract, string startName, string startPassword, string[] serviceDependencies);
|
||||
|
||||
void Create(string name, string displayName, string pathName, ServiceType serviceType, ErrorControl errorControl, StartMode startMode, bool desktopInteract, string[] serviceDependencies);
|
||||
|
||||
Win32Service Select(string name);
|
||||
}
|
||||
|
||||
|
|
12
winsw.sln
12
winsw.sln
|
@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 10.00
|
|||
# Visual C# Express 2008
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "winsw", "winsw.csproj", "{0DE77F55-ADE5-43C1-999A-0BC81153B039}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "winswTests", "Tests\winswTests\winswTests.csproj", "{93843402-842B-44B4-B303-AEE829BE0B43}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -23,6 +25,16 @@ Global
|
|||
{0DE77F55-ADE5-43C1-999A-0BC81153B039}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
||||
{0DE77F55-ADE5-43C1-999A-0BC81153B039}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||
{0DE77F55-ADE5-43C1-999A-0BC81153B039}.Release|Win32.ActiveCfg = Release|Any CPU
|
||||
{93843402-842B-44B4-B303-AEE829BE0B43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{93843402-842B-44B4-B303-AEE829BE0B43}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{93843402-842B-44B4-B303-AEE829BE0B43}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
|
||||
{93843402-842B-44B4-B303-AEE829BE0B43}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
|
||||
{93843402-842B-44B4-B303-AEE829BE0B43}.Debug|Win32.ActiveCfg = Debug|Any CPU
|
||||
{93843402-842B-44B4-B303-AEE829BE0B43}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{93843402-842B-44B4-B303-AEE829BE0B43}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{93843402-842B-44B4-B303-AEE829BE0B43}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
||||
{93843402-842B-44B4-B303-AEE829BE0B43}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||
{93843402-842B-44B4-B303-AEE829BE0B43}.Release|Win32.ActiveCfg = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
Loading…
Reference in New Issue