Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenneth Skovhede <kenneth@hexad.dk>2016-09-15 15:39:19 +0300
committerKenneth Skovhede <kenneth@hexad.dk>2016-09-15 15:39:19 +0300
commitafa9924f30f659b7581678d6140b81148ec53ddb (patch)
treea262ff166b0dc154c40cbdd88f2f7879a15cf8e5 /Duplicati/WindowsService
parent1d43227813af7187cda9746bffb40834c28f75d2 (diff)
Made the WindowsService self-installable with parameter support.
This fixes #1768
Diffstat (limited to 'Duplicati/WindowsService')
-rw-r--r--Duplicati/WindowsService/Program.cs45
-rw-r--r--Duplicati/WindowsService/ProjectInstaller.cs24
-rw-r--r--Duplicati/WindowsService/ServiceControl.cs11
-rw-r--r--Duplicati/WindowsService/ServiceProcessInstaller.cs13
4 files changed, 88 insertions, 5 deletions
diff --git a/Duplicati/WindowsService/Program.cs b/Duplicati/WindowsService/Program.cs
index b646861eb..17483528a 100644
--- a/Duplicati/WindowsService/Program.cs
+++ b/Duplicati/WindowsService/Program.cs
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
+using System.Configuration.Install;
using System.Linq;
+using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
@@ -11,7 +13,48 @@ namespace Duplicati.WindowsService
{
public static void Main(string[] args)
{
- ServiceBase.Run(new ServiceBase[] { new ServiceControl(args) });
+ var install = args != null && args.Where(x => string.Equals("install", x, StringComparison.OrdinalIgnoreCase)).Any();
+ var uninstall = args != null && args.Where(x => string.Equals("uninstall", x, StringComparison.OrdinalIgnoreCase)).Any();
+ var help = args != null && args.Where(x => string.Equals("help", x, StringComparison.OrdinalIgnoreCase)).Any();
+
+ if (help)
+ {
+ Console.WriteLine("This is a Windows Service wrapper tool that hosts the Duplicati.Server.exe process, letting it run as a windows service.");
+ Console.WriteLine("|To run from a console, run the Duplicati.Server.exe instead of Duplicati.WindowsService.exe");
+ Console.WriteLine();
+ Console.WriteLine("Supported commands:");
+ Console.WriteLine(" install:\r\n Installs the service");
+ Console.WriteLine(" uninstall:\r\n Uninstalls the service");
+ Console.WriteLine();
+ Console.WriteLine("Supported options for the install command:");
+ Console.WriteLine(" /localuser:\r\n Installs the service as a local user");
+ Console.WriteLine();
+ Console.WriteLine("It is possible to pass arguments to Duplicati.Server.exe, simply add them to the commandline:");
+ Console.WriteLine(" Duplicati.WindowsService.exe install --webservice-interface=loopback --log-retention=3M");
+ Console.WriteLine();
+ Console.WriteLine("To see supported options, run Duplicati.Server.exe:");
+ Console.WriteLine(" Duplicati.Server.exe help");
+ Console.WriteLine();
+ Console.WriteLine("To debug the WindowsService setup, add the --debug-service:");
+ Console.WriteLine(" Duplicati.WindowsService.exe install --debug-service");
+ }
+ else if (install || uninstall)
+ {
+ // Remove the install and uninstall flags if they are present
+ var commandline = string.Join(" ", args.Where(x => !(string.Equals("install", x, StringComparison.OrdinalIgnoreCase) || string.Equals("uninstall", x, StringComparison.OrdinalIgnoreCase))));
+ var selfexec = Assembly.GetExecutingAssembly().Location;
+
+
+ // --uninstall + --install = reinstall
+ if (uninstall)
+ ManagedInstallerClass.InstallHelper(new string[] { "/u", selfexec });
+ if (install)
+ ManagedInstallerClass.InstallHelper(new string[] { "/commandline=" + commandline, selfexec });
+ }
+ else
+ {
+ ServiceBase.Run(new ServiceBase[] { new ServiceControl(args) });
+ }
}
}
}
diff --git a/Duplicati/WindowsService/ProjectInstaller.cs b/Duplicati/WindowsService/ProjectInstaller.cs
index 4ab6fb84d..eec0bf56b 100644
--- a/Duplicati/WindowsService/ProjectInstaller.cs
+++ b/Duplicati/WindowsService/ProjectInstaller.cs
@@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
+using System.Text;
using System.Linq;
using System.Threading.Tasks;
@@ -18,5 +19,28 @@ namespace Duplicati.WindowsService
new ServiceInstaller()
});
}
+
+ public override void Install(IDictionary stateSaver)
+ {
+ var commandline = Context.Parameters["commandline"];
+ if (!string.IsNullOrWhiteSpace(commandline))
+ {
+ var rawpath = Context.Parameters["assemblypath"];
+ var path = new StringBuilder(rawpath);
+ if (!rawpath.StartsWith("\"", StringComparison.Ordinal) || !rawpath.EndsWith("\"", StringComparison.Ordinal))
+ {
+ path.Insert(0, '"');
+ path.Append('"');
+ }
+
+ path.Append(" ");
+ path.Append(commandline);
+
+ Context.Parameters["assemblypath"] = path.ToString();
+ }
+
+ base.Install(stateSaver);
+ }
+
}
}
diff --git a/Duplicati/WindowsService/ServiceControl.cs b/Duplicati/WindowsService/ServiceControl.cs
index 61133cd11..b6eddc9e0 100644
--- a/Duplicati/WindowsService/ServiceControl.cs
+++ b/Duplicati/WindowsService/ServiceControl.cs
@@ -33,9 +33,10 @@ namespace Duplicati.WindowsService
m_eventLog.Source = LOG_SOURCE;
m_eventLog.Log = LOG_NAME;
- m_cmdargs = args;
- m_verbose_messages = m_cmdargs != null && m_cmdargs.Where(x => string.Equals("--debug-service", x, StringComparison.OrdinalIgnoreCase)).Any();
- }
+ m_verbose_messages = args != null && args.Any(x => string.Equals("--debug-service", x, StringComparison.OrdinalIgnoreCase));
+ m_cmdargs = (args ?? new string[0]).Where(x => !string.Equals("--debug-service", x, StringComparison.OrdinalIgnoreCase)).ToArray();
+
+ }
protected override void OnStart(string[] args)
{
@@ -54,6 +55,8 @@ namespace Duplicati.WindowsService
private void DoStart(string[] args)
{
+ var startargs = (args ?? new string[0]).Union(m_cmdargs ?? new string[0]).ToArray();
+
if (m_verbose_messages)
m_eventLog.WriteEntry("Starting...");
lock(m_lock)
@@ -72,7 +75,7 @@ namespace Duplicati.WindowsService
m_eventLog.WriteEntry("Starting runner...");
m_runner = new Runner(
- m_cmdargs,
+ startargs,
() =>
{
if (m_verbose_messages)
diff --git a/Duplicati/WindowsService/ServiceProcessInstaller.cs b/Duplicati/WindowsService/ServiceProcessInstaller.cs
index 2537fdd57..cc90447ab 100644
--- a/Duplicati/WindowsService/ServiceProcessInstaller.cs
+++ b/Duplicati/WindowsService/ServiceProcessInstaller.cs
@@ -12,5 +12,18 @@ namespace Duplicati.WindowsService
{
this.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
}
+
+ public override void Install(System.Collections.IDictionary stateSaver)
+ {
+ var localuser = Context.Parameters["localuser"];
+ if (localuser != null)
+ {
+ this.Account = System.ServiceProcess.ServiceAccount.User;
+ this.Username = localuser;
+ }
+
+
+ base.Install(stateSaver);
+ }
}
}