Skip to content

Commit

Permalink
New service name convention
Browse files Browse the repository at this point in the history
  • Loading branch information
juliobbv committed Nov 25, 2019
1 parent e0aacfa commit 446acd6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/Runner.Listener/Configuration/OsxServiceControlManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace GitHub.Runner.Listener.Configuration
public class OsxServiceControlManager : ServiceControlManager, ILinuxServiceControlManager
{
// This is the name you would see when you do `systemctl list-units | grep runner`
private const string _svcNamePattern = "actions.runner.{0}.{1}.{2}";
private const string _svcDisplayPattern = "GitHub Actions Runner ({0}.{1}.{2})";
private const string _svcNamePattern = "actions.runner.{0}.{1}";
private const string _svcDisplayPattern = "GitHub Actions Runner ({0}.{1})";
private const string _shTemplate = "darwin.svc.sh.template";
private const string _svcShName = "svc.sh";

Expand Down
27 changes: 13 additions & 14 deletions src/Runner.Listener/Configuration/ServiceControlManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,35 @@ public void CalculateServiceName(RunnerSettings settings, string serviceNamePatt
serviceDisplayName = string.Empty;

Uri accountUri = new Uri(settings.ServerUrl);
string accountName = string.Empty;
string repoOrOrgName = string.Empty;

if (accountUri.Host.EndsWith(".githubusercontent.com", StringComparison.OrdinalIgnoreCase))
{
accountName = accountUri.AbsolutePath.Split('/', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
Uri gitHubUrl = new Uri(settings.GitHubUrl);

// Use the "NWO part" from the GitHub URL path. Replace known invalid characters
repoOrOrgName = gitHubUrl.AbsolutePath.Trim('/').Replace('/', '-').Replace('\\', '-');
}
else
{
accountName = accountUri.Host.Split('.').FirstOrDefault();
repoOrOrgName = accountUri.AbsolutePath.Split('/', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
}

if (string.IsNullOrEmpty(accountName))
if (string.IsNullOrEmpty(repoOrOrgName))
{
throw new InvalidOperationException($"Cannot find GitHub organization name from server url: '{settings.ServerUrl}'");
throw new InvalidOperationException($"Cannot find GitHub repository/organization name from server url: '{settings.ServerUrl}'");
}

serviceName = StringUtil.Format(serviceNamePattern, accountName, settings.PoolName, settings.AgentName);
serviceName = StringUtil.Format(serviceNamePattern, repoOrOrgName, 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 repoOrOrgNameSubstring = StringUtil.SubstringPrefix(repoOrOrgName, 45);

exceededCharLength -= settings.PoolName.Length - poolNameSubstring.Length;
exceededCharLength -= repoOrOrgName.Length - repoOrOrgNameSubstring.Length;

string runnerNameSubstring = settings.AgentName;

Expand All @@ -77,10 +76,10 @@ public void CalculateServiceName(RunnerSettings settings, string serviceNamePatt
runnerNameSubstring = StringUtil.SubstringPrefix(settings.AgentName, settings.AgentName.Length - exceededCharLength);
}

serviceName = StringUtil.Format(serviceNamePattern, accountNameSubstring, poolNameSubstring, runnerNameSubstring);
serviceName = StringUtil.Format(serviceNamePattern, repoOrOrgNameSubstring, runnerNameSubstring);
}

serviceDisplayName = StringUtil.Format(serviceDisplayNamePattern, accountName, settings.PoolName, settings.AgentName);
serviceDisplayName = StringUtil.Format(serviceDisplayNamePattern, repoOrOrgName, settings.PoolName, settings.AgentName);

Trace.Info($"Service name '{serviceName}' display name '{serviceDisplayName}' will be used for service configuration.");
}
Expand Down
4 changes: 2 additions & 2 deletions src/Runner.Listener/Configuration/SystemdControlManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace GitHub.Runner.Listener.Configuration
public class SystemDControlManager : ServiceControlManager, ILinuxServiceControlManager
{
// This is the name you would see when you do `systemctl list-units | grep runner`
private const string _svcNamePattern = "actions.runner.{0}.{1}.{2}.service";
private const string _svcDisplayPattern = "GitHub Actions Runner ({0}.{1}.{2})";
private const string _svcNamePattern = "actions.runner.{0}.{1}.service";
private const string _svcDisplayPattern = "GitHub Actions Runner ({0}.{1})";
private const string _shTemplate = "systemd.svc.sh.template";
private const string _shName = "svc.sh";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class WindowsServiceControlManager : ServiceControlManager, IWindowsServi
{
public const string WindowsServiceControllerName = "RunnerService.exe";

private const string ServiceNamePattern = "actionsrunner.{0}.{1}.{2}";
private const string ServiceDisplayNamePattern = "GitHub Actions Runner ({0}.{1}.{2})";
private const string ServiceNamePattern = "actions.runner.{0}.{1}";
private const string ServiceDisplayNamePattern = "GitHub Actions Runner ({0}.{1})";

private INativeWindowsServiceHelper _windowsServiceHelper;
private ITerminal _term;
Expand Down
54 changes: 27 additions & 27 deletions src/Test/L0/ServiceControlManagerL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public void CalculateServiceName()
{
RunnerSettings settings = new RunnerSettings();

settings.AgentName = "runner";
settings.ServerUrl = "https://12345678901234567890123456789012345678901234567890.exampledomain.com";
settings.PoolName = "Default";
settings.AgentName = "thisiskindofalongrunnername1";
settings.ServerUrl = "https://example.githubusercontent.com/12345678901234567890123456789012345678901234567890";
settings.GitHubUrl = "https://github.com/myorganizationexample/myrepoexample";

string serviceNamePattern = "actionsrunner.{0}.{1}.{2}";
string serviceDisplayNamePattern = "GitHub Actions Runner ({0}.{1}.{2})";
string serviceNamePattern = "actions.runner.{0}.{1}";
string serviceDisplayNamePattern = "GitHub Actions Runner ({0}.{1})";

using (TestHostContext hc = CreateTestContext())
{
Expand All @@ -40,10 +40,10 @@ public void CalculateServiceName()
Assert.Equal(79, serviceName.Length);

// Verify nothing has been shortened out
Assert.Equal("actionsrunner", serviceNameParts[0]);
Assert.Equal("12345678901234567890123456789012345678901234567890", serviceNameParts[1]);
Assert.Equal("Default", serviceNameParts[2]);
Assert.Equal("runner", serviceNameParts[3]);
Assert.Equal("actions", serviceNameParts[0]);
Assert.Equal("runner", serviceNameParts[1]);
Assert.Equal("myorganizationexample-myrepoexample", serviceNameParts[2]); // '/' has been replaced with '-'
Assert.Equal("thisiskindofalongrunnername1", serviceNameParts[3]);
}
}

Expand All @@ -54,12 +54,12 @@ public void CalculateServiceName80Chars()
{
RunnerSettings settings = new RunnerSettings();

settings.AgentName = "runner1";
settings.ServerUrl = "https://12345678901234567890123456789012345678901234567890.exampledomain.com";
settings.PoolName = "Default";
settings.AgentName = "thisiskindofalongrunnername12";
settings.ServerUrl = "https://example.githubusercontent.com/12345678901234567890123456789012345678901234567890";
settings.GitHubUrl = "https://github.com/myorganizationexample/myrepoexample";

string serviceNamePattern = "actionsrunner.{0}.{1}.{2}";
string serviceDisplayNamePattern = "GitHub Actions Runner ({0}.{1}.{2})";
string serviceNamePattern = "actions.runner.{0}.{1}";
string serviceDisplayNamePattern = "GitHub Actions Runner ({0}.{1})";

using (TestHostContext hc = CreateTestContext())
{
Expand All @@ -79,10 +79,10 @@ public void CalculateServiceName80Chars()
var serviceNameParts = serviceName.Split('.');

// Verify nothing has been shortened out
Assert.Equal("actionsrunner", serviceNameParts[0]);
Assert.Equal("12345678901234567890123456789012345678901234567890", serviceNameParts[1]);
Assert.Equal("Default", serviceNameParts[2]);
Assert.Equal("runner1", serviceNameParts[3]);
Assert.Equal("actions", serviceNameParts[0]);
Assert.Equal("runner", serviceNameParts[1]);
Assert.Equal("myorganizationexample-myrepoexample", serviceNameParts[2]); // '/' has been replaced with '-'
Assert.Equal("thisiskindofalongrunnername12", serviceNameParts[3]);
}
}

Expand All @@ -93,12 +93,12 @@ public void CalculateServiceNameLimitsServiceNameTo80Chars()
{
RunnerSettings settings = new RunnerSettings();

settings.AgentName = "thisisaverylongrunnernamewithlotsofchars";
settings.ServerUrl = "https://12345678901234567890123456789012345678901234567890.exampledomain.com";
settings.PoolName = "thisisaverylongpoolnamewithlotsofchars";
settings.AgentName = "thisisareallyreallylongbutstillvalidagentname";
settings.ServerUrl = "https://example.githubusercontent.com/12345678901234567890123456789012345678901234567890";
settings.GitHubUrl = "https://github.com/myreallylongorganizationexample/myreallylongrepoexample";

string serviceNamePattern = "actionsrunner.{0}.{1}.{2}";
string serviceDisplayNamePattern = "GitHub Actions Runner ({0}.{1}.{2})";
string serviceNamePattern = "actions.runner.{0}.{1}";
string serviceDisplayNamePattern = "GitHub Actions Runner ({0}.{1})";

using (TestHostContext hc = CreateTestContext())
{
Expand All @@ -118,10 +118,10 @@ public void CalculateServiceNameLimitsServiceNameTo80Chars()
var serviceNameParts = serviceName.Split('.');

// Verify that each component has been shortened to a sensible length
Assert.Equal("actionsrunner", serviceNameParts[0]); // Never shortened
Assert.Equal("1234567890123456789012345", serviceNameParts[1]); // 25 chars
Assert.Equal("thisisaverylongpoolnamewi", serviceNameParts[2]); // 25 chars
Assert.Equal("thisisaverylon", serviceNameParts[3]); // Remainder of unused chars
Assert.Equal("actions", serviceNameParts[0]); // Never shortened
Assert.Equal("runner", serviceNameParts[1]); // Never shortened
Assert.Equal("myreallylongorganizationexample-myreallylongr", serviceNameParts[2]); // First 45 chars, '/' has been replaced with '-'
Assert.Equal("thisisareallyreally", serviceNameParts[3]); // Remainder of unused chars
}
}

Expand Down

0 comments on commit 446acd6

Please sign in to comment.