-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathServiceControlNewInstance.cs
More file actions
142 lines (119 loc) · 5.03 KB
/
ServiceControlNewInstance.cs
File metadata and controls
142 lines (119 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
namespace ServiceControlInstaller.Engine.Instances
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
using Configuration.ServiceControl;
using Services;
using Setup;
using Validation;
public class ServiceControlNewInstance : ServiceControlInstallableBase, IServiceControlInstance
{
public static ServiceControlNewInstance CreateWithDefaultPersistence()
{
const string persisterUsedForBrandNewInstances = StorageEngineNames.RavenDB;
return CreateWithPersistence(persisterUsedForBrandNewInstances);
}
public static ServiceControlNewInstance CreateWithPersistence(string persistence)
{
var persistenceManifest = ServiceControlPersisters.GetPrimaryPersistence(persistence);
return new ServiceControlNewInstance(persistenceManifest);
}
public ServiceControlNewInstance(PersistenceManifest persistenceManifest)
{
Version = Constants.CurrentVersion;
PersistenceManifest = persistenceManifest;
}
public override void WriteConfigurationFile()
{
var appConfig = new ServiceControlAppConfig(this);
appConfig.Save();
}
public PersistenceManifest PersistenceManifest { get; }
public override string DirectoryName => "ServiceControl";
public List<RemoteInstanceSetting> RemoteInstances { get; set; } = [];
public void AddRemoteInstance(string apiUri)
{
//Secondary instance can be configured with * or + as the hostname to ensure
//that the api is bound to all available network interfaces. This is problematic
//from the main instance perspective as it requires concrete hostname.
//Remote instances settings are added only via UI when all instances are deployed
//on the same machine so it is safe to replace * or + with localhost.
if (apiUri.Contains("*") || apiUri.Contains("+"))
{
apiUri = apiUri.Replace("*", "localhost")
.Replace("+", "localhost");
}
if (RemoteInstances.All(x => string.Compare(x.ApiUri, apiUri, StringComparison.InvariantCultureIgnoreCase) != 0))
{
RemoteInstances.Add(new RemoteInstanceSetting
{
ApiUri = apiUri
});
}
}
[XmlElement(typeof(XmlNullableTimeSpan))]
public TimeSpan? AuditRetentionPeriod { get; set; }
[XmlElement(typeof(XmlTimeSpan))]
public TimeSpan ErrorRetentionPeriod { get; set; }
internal override WindowsServiceDetails GetWindowsServiceDetails()
{
return new WindowsServiceDetails
{
ServiceAccount = ServiceAccount,
ServiceAccountPwd = ServiceAccountPwd,
DisplayName = DisplayName,
Name = Name,
ImagePath = $"\"{Path.Combine(InstallPath, Constants.ServiceControlExe)}\"",
ServiceDescription = ServiceDescription
};
}
protected override void RunSetup()
{
InstanceSetup.Run(this);
}
protected override void ValidateMaintenancePort()
{
DatabaseMaintenancePortValidator.Validate(this);
}
protected override void ValidateQueueNames()
{
QueueNameValidator.Validate(this);
}
protected override void ValidateServiceAccount()
{
ServiceAccountValidation.Validate(this);
}
protected override void ValidateConnectionString()
{
ConnectionStringValidator.Validate(this);
}
public static ServiceControlNewInstance Load(string path)
{
ServiceControlNewInstance instanceData;
var serializer = new XmlSerializer(typeof(ServiceControlNewInstance));
using (var stream = File.OpenRead(path))
{
instanceData = (ServiceControlNewInstance)serializer.Deserialize(stream);
}
var doc = new XmlDocument();
doc.Load(path);
if (doc.SelectSingleNode("/ServiceControlInstanceMetadata/ForwardErrorMessages") == null)
{
throw new InvalidDataException("The supplied file is using an old format. Which is no longer supported.");
}
if (doc.SelectSingleNode("/ServiceControlInstanceMetadata/AuditRetentionPeriod") == null)
{
throw new InvalidDataException("The supplied file is using an old format. Which is no longer supported.");
}
if (doc.SelectSingleNode("/ServiceControlInstanceMetadata/ErrorRetentionPeriod") == null)
{
throw new InvalidDataException("The supplied file is using an old format. Which is no longer supported.");
}
return instanceData;
}
}
}