mirror of
https://github.com/actions/runner.git
synced 2025-12-14 22:04:58 +00:00
Runner register labels during configuration (#130)
* Runners will add os and architecture labels during registration * support github.localhost for dev.
This commit is contained in:
committed by
Christopher Johnson
parent
2f261f2c31
commit
f5f14d4811
@@ -1,73 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using GitHub.Runner.Common.Util;
|
||||
using GitHub.Runner.Sdk;
|
||||
|
||||
namespace GitHub.Runner.Common.Capabilities
|
||||
{
|
||||
[ServiceLocator(Default = typeof(CapabilitiesManager))]
|
||||
public interface ICapabilitiesManager : IRunnerService
|
||||
{
|
||||
Task<Dictionary<string, string>> GetCapabilitiesAsync(RunnerSettings settings, CancellationToken token);
|
||||
}
|
||||
|
||||
public sealed class CapabilitiesManager : RunnerService, ICapabilitiesManager
|
||||
{
|
||||
public async Task<Dictionary<string, string>> GetCapabilitiesAsync(RunnerSettings settings, CancellationToken cancellationToken)
|
||||
{
|
||||
Trace.Entering();
|
||||
ArgUtil.NotNull(settings, nameof(settings));
|
||||
|
||||
// Initialize a dictionary of capabilities.
|
||||
var capabilities = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
if (settings.SkipCapabilitiesScan)
|
||||
{
|
||||
Trace.Info("Skip capabilities scan.");
|
||||
return capabilities;
|
||||
}
|
||||
|
||||
// Get the providers.
|
||||
var extensionManager = HostContext.GetService<IExtensionManager>();
|
||||
IEnumerable<ICapabilitiesProvider> providers =
|
||||
extensionManager
|
||||
.GetExtensions<ICapabilitiesProvider>()
|
||||
?.OrderBy(x => x.Order);
|
||||
|
||||
// Add each capability returned from each provider.
|
||||
foreach (ICapabilitiesProvider provider in providers ?? new ICapabilitiesProvider[0])
|
||||
{
|
||||
foreach (Capability capability in await provider.GetCapabilitiesAsync(settings, cancellationToken) ?? new List<Capability>())
|
||||
{
|
||||
// Make sure we mask secrets in capabilities values.
|
||||
capabilities[capability.Name] = HostContext.SecretMasker.MaskSecrets(capability.Value);
|
||||
}
|
||||
}
|
||||
|
||||
return capabilities;
|
||||
}
|
||||
}
|
||||
|
||||
public interface ICapabilitiesProvider : IExtension
|
||||
{
|
||||
int Order { get; }
|
||||
|
||||
Task<List<Capability>> GetCapabilitiesAsync(RunnerSettings settings, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
public sealed class Capability
|
||||
{
|
||||
public string Name { get; }
|
||||
public string Value { get; }
|
||||
|
||||
public Capability(string name, string value)
|
||||
{
|
||||
ArgUtil.NotNullOrEmpty(name, nameof(name));
|
||||
Name = name;
|
||||
Value = value ?? string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
using GitHub.Runner.Common.Util;
|
||||
using GitHub.Runner.Sdk;
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GitHub.Runner.Common.Capabilities
|
||||
{
|
||||
public sealed class RunnerCapabilitiesProvider : RunnerService, ICapabilitiesProvider
|
||||
{
|
||||
public Type ExtensionType => typeof(ICapabilitiesProvider);
|
||||
|
||||
public int Order => 99; // Process last to override prior.
|
||||
|
||||
public Task<List<Capability>> GetCapabilitiesAsync(RunnerSettings settings, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgUtil.NotNull(settings, nameof(settings));
|
||||
var capabilities = new List<Capability>();
|
||||
Add(capabilities, "Runner.Name", settings.AgentName ?? string.Empty);
|
||||
Add(capabilities, "Runner.OS", VarUtil.OS);
|
||||
Add(capabilities, "Runner.OSArchitecture", VarUtil.OSArchitecture);
|
||||
#if OS_WINDOWS
|
||||
Add(capabilities, "Runner.OSVersion", GetOSVersionString());
|
||||
#endif
|
||||
Add(capabilities, "InteractiveSession", (HostContext.StartupType != StartupType.Service).ToString());
|
||||
Add(capabilities, "Runner.Version", BuildConstants.RunnerPackage.Version);
|
||||
Add(capabilities, "Runner.ComputerName", Environment.MachineName ?? string.Empty);
|
||||
Add(capabilities, "Runner.HomeDirectory", HostContext.GetDirectory(WellKnownDirectory.Root));
|
||||
return Task.FromResult(capabilities);
|
||||
}
|
||||
|
||||
private void Add(List<Capability> capabilities, string name, string value)
|
||||
{
|
||||
Trace.Info($"Adding '{name}': '{value}'");
|
||||
capabilities.Add(new Capability(name, value));
|
||||
}
|
||||
|
||||
private object GetHklmValue(string keyName, string valueName)
|
||||
{
|
||||
keyName = $@"HKEY_LOCAL_MACHINE\{keyName}";
|
||||
object value = Registry.GetValue(keyName, valueName, defaultValue: null);
|
||||
if (object.ReferenceEquals(value, null))
|
||||
{
|
||||
Trace.Info($"Key name '{keyName}', value name '{valueName}' is null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
Trace.Info($"Key name '{keyName}', value name '{valueName}': '{value}'");
|
||||
return value;
|
||||
}
|
||||
|
||||
private string GetOSVersionString()
|
||||
{
|
||||
// Do not use System.Environment.OSVersion.Version to resolve the OS version number.
|
||||
// It leverages the GetVersionEx function which may report an incorrect version
|
||||
// depending on the app's manifest. For details, see:
|
||||
// https://msdn.microsoft.com/library/windows/desktop/ms724451(v=vs.85).aspx
|
||||
|
||||
// Attempt to retrieve the major/minor version from the new registry values added in
|
||||
// in Windows 10.
|
||||
//
|
||||
// The registry value "CurrentVersion" is unreliable in Windows 10. It contains the
|
||||
// value "6.3" instead of "10.0".
|
||||
object major = GetHklmValue(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMajorVersionNumber");
|
||||
object minor = GetHklmValue(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMinorVersionNumber");
|
||||
string majorMinorString;
|
||||
if (major != null && minor != null)
|
||||
{
|
||||
majorMinorString = StringUtil.Format("{0}.{1}", major, minor);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Fallback to the registry value "CurrentVersion".
|
||||
majorMinorString = GetHklmValue(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion") as string;
|
||||
}
|
||||
|
||||
// Opted to use the registry value "CurrentBuildNumber" over "CurrentBuild". Based on brief
|
||||
// internet investigation, the only difference appears to be that on Windows XP "CurrentBuild"
|
||||
// was unreliable and "CurrentBuildNumber" was the correct choice.
|
||||
string build = GetHklmValue(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentBuildNumber") as string;
|
||||
return StringUtil.Format("{0}.{1}", majorMinorString, build);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,10 +39,6 @@ namespace GitHub.Runner.Common
|
||||
var extensions = new List<IExtension>();
|
||||
switch (typeof(T).FullName)
|
||||
{
|
||||
// Listener capabilities providers.
|
||||
case "GitHub.Runner.Common.Capabilities.ICapabilitiesProvider":
|
||||
Add<T>(extensions, "GitHub.Runner.Common.Capabilities.RunnerCapabilitiesProvider, Runner.Common");
|
||||
break;
|
||||
// Action command extensions.
|
||||
case "GitHub.Runner.Worker.IActionCommandExtension":
|
||||
Add<T>(extensions, "GitHub.Runner.Worker.InternalPluginSetRepoPathCommandExtension, Runner.Worker");
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace GitHub.Runner.Common
|
||||
Task DeleteAgentAsync(int agentPoolId, int agentId);
|
||||
Task<List<TaskAgentPool>> GetAgentPoolsAsync(string agentPoolName = null, TaskAgentPoolType poolType = TaskAgentPoolType.Automation);
|
||||
Task<List<TaskAgent>> GetAgentsAsync(int agentPoolId, string agentName = null);
|
||||
Task<TaskAgent> UpdateAgentAsync(int agentPoolId, TaskAgent agent);
|
||||
Task<TaskAgent> ReplaceAgentAsync(int agentPoolId, TaskAgent agent);
|
||||
|
||||
// messagequeue
|
||||
Task<TaskAgentSession> CreateAgentSessionAsync(Int32 poolId, TaskAgentSession session, CancellationToken cancellationToken);
|
||||
@@ -257,7 +257,7 @@ namespace GitHub.Runner.Common
|
||||
return _genericTaskAgentClient.GetAgentsAsync(agentPoolId, agentName, false);
|
||||
}
|
||||
|
||||
public Task<TaskAgent> UpdateAgentAsync(int agentPoolId, TaskAgent agent)
|
||||
public Task<TaskAgent> ReplaceAgentAsync(int agentPoolId, TaskAgent agent)
|
||||
{
|
||||
CheckConnection(RunnerConnectionType.Generic);
|
||||
return _genericTaskAgentClient.ReplaceAgentAsync(agentPoolId, agent);
|
||||
|
||||
Reference in New Issue
Block a user