mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-27 07:04:57 +01:00
Merge pull request #526 from Cyberboss/EasyInstallService
Simple command line registration for the service, everything else is manual
This commit is contained in:
@@ -1,4 +1,13 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using McMaster.Extensions.CommandLineUtils;
|
||||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.Configuration.Install;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Security.Principal;
|
||||
using System.ServiceProcess;
|
||||
using Tgstation.Server.Host.Watchdog;
|
||||
|
||||
@@ -7,12 +16,94 @@ namespace Tgstation.Server.Host.Service
|
||||
/// <summary>
|
||||
/// Contains the entrypoint for the application
|
||||
/// </summary>
|
||||
static class Program
|
||||
[ExcludeFromCodeCoverage]
|
||||
class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// The --uninstall or -u option
|
||||
/// </summary>
|
||||
[Option(ShortName = "u")]
|
||||
public bool Uninstall { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The --install or -i option
|
||||
/// </summary>
|
||||
[Option(ShortName = "i")]
|
||||
public bool Install { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Check if the running user is a system administrator
|
||||
/// </summary>
|
||||
/// <returns><see langword="true"/> if the running user is a system administrator, <see langword="false"/> otherwise</returns>
|
||||
static bool IsAdministrator()
|
||||
{
|
||||
var user = WindowsIdentity.GetCurrent();
|
||||
var principal = new WindowsPrincipal(user);
|
||||
return principal.IsInRole(WindowsBuiltInRole.Administrator);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Command line handler, always runs
|
||||
/// </summary>
|
||||
public void OnExecute()
|
||||
{
|
||||
if((Install || Uninstall) && !IsAdministrator())
|
||||
{
|
||||
//try to restart as admin
|
||||
var currentProcess = Process.GetCurrentProcess();
|
||||
var argList = Environment.GetCommandLineArgs().ToList();
|
||||
//its windows, first arg is .exe name guaranteed
|
||||
var exe = argList.First();
|
||||
argList.RemoveAt(0);
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
UseShellExecute = true,
|
||||
Verb = "runas",
|
||||
Arguments = String.Join(" ", argList),
|
||||
FileName = Assembly.GetExecutingAssembly().Location,
|
||||
WorkingDirectory = Environment.CurrentDirectory,
|
||||
};
|
||||
using (Process.Start(startInfo))
|
||||
return;
|
||||
}
|
||||
|
||||
if (Install)
|
||||
{
|
||||
if (Uninstall)
|
||||
//oh no, it's retarded...
|
||||
return;
|
||||
using (var processInstaller = new ServiceProcessInstaller())
|
||||
using (var installer = new ServiceInstaller())
|
||||
{
|
||||
processInstaller.Account = ServiceAccount.NetworkService;
|
||||
|
||||
installer.Context = new InstallContext("tgs-4-install.log", new string[] { String.Format(CultureInfo.InvariantCulture, "/assemblypath={0}", Assembly.GetEntryAssembly().Location) });
|
||||
installer.Description = "/tg/station 13 server v4 running as a windows service";
|
||||
installer.DisplayName = "/tg/station server 4";
|
||||
installer.DelayedAutoStart = true;
|
||||
installer.StartType = ServiceStartMode.Automatic;
|
||||
installer.ServicesDependedOn = new string[] { "Tcpip", "Dhcp", "Dnscache" };
|
||||
installer.ServiceName = ServerService.Name;
|
||||
installer.Parent = processInstaller;
|
||||
|
||||
var state = new ListDictionary();
|
||||
installer.Install(state);
|
||||
}
|
||||
}
|
||||
else if (Uninstall)
|
||||
using (var installer = new ServiceInstaller())
|
||||
{
|
||||
installer.Context = new InstallContext("tgs-4-uninstall.log", null);
|
||||
installer.ServiceName = ServerService.Name;
|
||||
installer.Uninstall(null);
|
||||
}
|
||||
else
|
||||
ServiceBase.Run(new ServerService(new WatchdogFactory()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Entrypoint for the application
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
static void Main() => ServiceBase.Run(new ServerService(new WatchdogFactory()));
|
||||
static int Main(string[] args) => CommandLineApplication.Execute<Program>(args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,11 @@ namespace Tgstation.Server.Host.Service
|
||||
/// </summary>
|
||||
sealed class ServerService : ServiceBase
|
||||
{
|
||||
/// <summary>
|
||||
/// The canonical windows service name
|
||||
/// </summary>
|
||||
public const string Name = "tgstation-server-4";
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="IWatchdog"/> for the <see cref="ServerService"/>
|
||||
/// </summary>
|
||||
@@ -35,7 +40,7 @@ namespace Tgstation.Server.Host.Service
|
||||
{
|
||||
if (watchdogFactory == null)
|
||||
throw new ArgumentNullException(nameof(watchdogFactory));
|
||||
ServiceName = "tgstation-server";
|
||||
ServiceName = Name;
|
||||
watchdog = watchdogFactory.CreateWatchdog();
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,7 @@
|
||||
<PropertyGroup>
|
||||
<StartupObject>Tgstation.Server.Host.Service.Program</StartupObject>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup />
|
||||
<ItemGroup>
|
||||
<Compile Include="GlobalSuppressions.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
@@ -87,5 +88,10 @@
|
||||
<Name>Tgstation.Server.Host.Watchdog</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="McMaster.Extensions.CommandLineUtils">
|
||||
<Version>2.2.4</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
Reference in New Issue
Block a user