tgstation-server 6.12.3
The /tg/station 13 server suite
Loading...
Searching...
No Matches
InstallationExtensions.cs
Go to the documentation of this file.
2{
3 using System;
4 using System.IO;
5 using System.ServiceProcess;
6
8
9 using WixToolset.Dtf.WindowsInstaller;
10
14 public static class InstallationExtensions
15 {
20 const string CanonicalPackageName = "tgstation-server";
21
27 [CustomAction]
28 public static ActionResult DetachStopTgsServiceIfRunning(Session session)
29 {
30 if (session == null)
31 throw new ArgumentNullException(nameof(session));
32
33 try
34 {
35 session.Log("Begin DetachStopTgsServiceIfRunning");
36 ServiceController serviceController = null;
37
38 session.Log($"Searching for {CanonicalPackageName} service...");
39 try
40 {
41 foreach (var controller in ServiceController.GetServices())
42 if (controller.ServiceName == CanonicalPackageName)
43 serviceController = controller;
44 else
45 controller.Dispose();
46
47 if (serviceController == null || serviceController.Status != ServiceControllerStatus.Running)
48 {
49 session.Log($"{CanonicalPackageName} service not found. Continuing.");
50 return ActionResult.Success;
51 }
52
53 var commandId = PipeCommands.GetServiceCommandId(
55 .Value;
56
57 session.Log($"{CanonicalPackageName} service found. Sending command \"{PipeCommands.CommandDetachingShutdown}\" ({commandId})...");
58
59 serviceController.ExecuteCommand(commandId);
60
61 session.Log($"Command sent. Waiting for {CanonicalPackageName} service to stop...");
62
63 serviceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromMinutes(1));
64
65 var stopped = serviceController.Status == ServiceControllerStatus.Stopped;
66 session.Log($"{CanonicalPackageName} stopped {(stopped ? String.Empty : "un")}successfully.");
67
68 return stopped
69 ? ActionResult.Success
70 : ActionResult.NotExecuted;
71 }
72 finally
73 {
74 serviceController?.Dispose();
75 }
76 }
77 catch (Exception ex)
78 {
79 session.Log($"Exception in DetachStopTgsServiceIfRunning:{Environment.NewLine}{ex}");
80 return ActionResult.Failure;
81 }
82 }
83
89 [CustomAction]
90 public static ActionResult ApplyProductionAppsettingsIfNonExistant(Session session)
91 {
92 if (session == null)
93 throw new ArgumentNullException(nameof(session));
94
95 try
96 {
97 session.Log("Begin ApplyProductionAppsettingsIfNonExistant");
98 var programDataDirectory = Path.Combine(
99 Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
101 var initialAppSettingsPath = Path.Combine(programDataDirectory, "appsettings.Initial.yml");
102 var productionAppSettingsPath = Path.Combine(programDataDirectory, "appsettings.Production.yml");
103
104 var initialAppSettingsExists = File.Exists(initialAppSettingsPath);
105 var productionAppSettingsExists = File.Exists(productionAppSettingsPath);
106
107 if (productionAppSettingsExists)
108 session.Log("appsettings.Production.yml present");
109 else
110 session.Log("appsettings.Production.yml NOT present");
111
112 if (!initialAppSettingsExists)
113 session.Log("appsettings.Initial.yml NOT present!");
114 else
115 {
116 session.Log("appsettings.Initial.yml present");
117 if (!productionAppSettingsExists)
118 {
119 session.Log("Copying initial settings to production settings...");
120 File.Copy(initialAppSettingsPath, productionAppSettingsPath);
121 return ActionResult.Success;
122 }
123 }
124
125 return ActionResult.NotExecuted;
126 }
127 catch (Exception ex)
128 {
129 session.Log($"Exception in ApplyProductionAppsettingsIfNonExistant:{Environment.NewLine}{ex}");
130 return ActionResult.Failure;
131 }
132 }
133 }
134}
Values able to be passed via the update file path.
static ? int GetServiceCommandId(string command)
Gets the int value of a given command .
const string CommandDetachingShutdown
Stops the server ASAP, detaching the watchdog for any running instances.
static ActionResult ApplyProductionAppsettingsIfNonExistant(Session session)
Attempts to copy the initial config to the production config if necessary.
static ActionResult DetachStopTgsServiceIfRunning(Session session)
Attempts to detach stop the existing tgstation-server service if it exists.