-
Notifications
You must be signed in to change notification settings - Fork 865
/
ServiceControlManager.cs
80 lines (61 loc) · 3.35 KB
/
ServiceControlManager.cs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Linq;
using Microsoft.VisualStudio.Services.Agent.Util;
namespace Microsoft.VisualStudio.Services.Agent.Listener.Configuration
{
public class ServiceControlManager : AgentService
{
public void CalculateServiceName(AgentSettings settings, string serviceNamePattern, string serviceDisplayNamePattern, out string serviceName, out string serviceDisplayName)
{
ArgUtil.NotNull(settings, nameof(settings));
Trace.Entering();
serviceName = string.Empty;
serviceDisplayName = string.Empty;
Uri accountUri = new Uri(settings.ServerUrl);
string accountName = string.Empty;
if (accountUri.Host.Equals("dev.azure.com", StringComparison.OrdinalIgnoreCase))
{
accountName = accountUri.AbsolutePath.Split('/', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
}
else
{
accountName = accountUri.Host.Split('.').FirstOrDefault();
}
if (string.IsNullOrEmpty(accountName))
{
throw new InvalidOperationException(StringUtil.Loc("CannotFindHostName", settings.ServerUrl));
}
string resourceCollection = settings.PoolName ?? settings.EnvironmentName;
serviceName = StringUtil.Format(serviceNamePattern, accountName, resourceCollection, settings.AgentName);
if (serviceName.Length > 80)
{
Trace.Verbose($"Calculated service name is too long (> 80 chars). Trying again by calculating a shorter name.");
int exceededCharLength = serviceName.Length - 80;
string accountNameSubstring = StringUtil.SubstringPrefix(accountName, 25);
exceededCharLength -= accountName.Length - accountNameSubstring.Length;
string poolNameSubstring = StringUtil.SubstringPrefix(settings.PoolName, 25);
string environmentNameSubstring = StringUtil.SubstringPrefix(settings.EnvironmentName, 25);
if (settings.PoolName != null)
{
exceededCharLength -= settings.PoolName.Length - poolNameSubstring.Length;
}
if (settings.EnvironmentName != null)
{
exceededCharLength -= settings.EnvironmentName.Length - environmentNameSubstring.Length;
}
string agentNameSubstring = settings.AgentName;
// Only trim agent name if it's really necessary
if (exceededCharLength > 0)
{
agentNameSubstring = StringUtil.SubstringPrefix(settings.AgentName, settings.AgentName.Length - exceededCharLength);
}
string resourceCollectionSubstring = poolNameSubstring ?? environmentNameSubstring;
serviceName = StringUtil.Format(serviceNamePattern, accountNameSubstring, resourceCollectionSubstring, agentNameSubstring);
}
serviceDisplayName = StringUtil.Format(serviceDisplayNamePattern, accountName, resourceCollection, settings.AgentName);
Trace.Info($"Service name '{serviceName}' display name '{serviceDisplayName}' will be used for service configuration.");
}
}
}