From f5f14d4811b849d65222db1a90732d37abad650d Mon Sep 17 00:00:00 2001 From: Tingluo Huang Date: Thu, 17 Oct 2019 16:33:43 -0400 Subject: [PATCH] Runner register labels during configuration (#130) * Runners will add os and architecture labels during registration * support github.localhost for dev. --- .../Capabilities/CapabilitiesManager.cs | 73 ---------------- .../RunnerCapabilitiesProvider.cs | 86 ------------------- src/Runner.Common/ExtensionManager.cs | 4 - src/Runner.Common/RunnerServer.cs | 4 +- .../Configuration/ConfigurationManager.cs | 45 ++++------ src/Runner.Listener/MessageListener.cs | 8 +- src/Runner.Worker/DiagnosticLogManager.cs | 10 --- src/Sdk/DTWebApi/WebApi/TaskAgent.cs | 41 ++------- .../DTWebApi/WebApi/TaskAgentHttpClient.cs | 3 +- .../DTWebApi/WebApi/TaskAgentJobRequest.cs | 14 ++- src/Sdk/DTWebApi/WebApi/TaskAgentSession.cs | 52 ----------- src/Test/L0/ExtensionManagerL0.cs | 13 ++- .../AgentCapabilitiesProviderTestL0.cs | 79 ----------------- .../Configuration/ConfigurationManagerL0.cs | 16 +--- src/Test/L0/Listener/MessageListenerL0.cs | 10 --- src/Test/L0/ServiceInterfacesL0.cs | 2 - 16 files changed, 52 insertions(+), 408 deletions(-) delete mode 100644 src/Runner.Common/Capabilities/CapabilitiesManager.cs delete mode 100644 src/Runner.Common/Capabilities/RunnerCapabilitiesProvider.cs delete mode 100644 src/Test/L0/Listener/Configuration/AgentCapabilitiesProviderTestL0.cs diff --git a/src/Runner.Common/Capabilities/CapabilitiesManager.cs b/src/Runner.Common/Capabilities/CapabilitiesManager.cs deleted file mode 100644 index 76bc5fca9..000000000 --- a/src/Runner.Common/Capabilities/CapabilitiesManager.cs +++ /dev/null @@ -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> GetCapabilitiesAsync(RunnerSettings settings, CancellationToken token); - } - - public sealed class CapabilitiesManager : RunnerService, ICapabilitiesManager - { - public async Task> GetCapabilitiesAsync(RunnerSettings settings, CancellationToken cancellationToken) - { - Trace.Entering(); - ArgUtil.NotNull(settings, nameof(settings)); - - // Initialize a dictionary of capabilities. - var capabilities = new Dictionary(StringComparer.OrdinalIgnoreCase); - - if (settings.SkipCapabilitiesScan) - { - Trace.Info("Skip capabilities scan."); - return capabilities; - } - - // Get the providers. - var extensionManager = HostContext.GetService(); - IEnumerable providers = - extensionManager - .GetExtensions() - ?.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()) - { - // 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> 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; - } - } -} diff --git a/src/Runner.Common/Capabilities/RunnerCapabilitiesProvider.cs b/src/Runner.Common/Capabilities/RunnerCapabilitiesProvider.cs deleted file mode 100644 index 6821e4de0..000000000 --- a/src/Runner.Common/Capabilities/RunnerCapabilitiesProvider.cs +++ /dev/null @@ -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> GetCapabilitiesAsync(RunnerSettings settings, CancellationToken cancellationToken) - { - ArgUtil.NotNull(settings, nameof(settings)); - var capabilities = new List(); - 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 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); - } - } -} diff --git a/src/Runner.Common/ExtensionManager.cs b/src/Runner.Common/ExtensionManager.cs index dbbb060ae..19682e3e5 100644 --- a/src/Runner.Common/ExtensionManager.cs +++ b/src/Runner.Common/ExtensionManager.cs @@ -39,10 +39,6 @@ namespace GitHub.Runner.Common var extensions = new List(); switch (typeof(T).FullName) { - // Listener capabilities providers. - case "GitHub.Runner.Common.Capabilities.ICapabilitiesProvider": - Add(extensions, "GitHub.Runner.Common.Capabilities.RunnerCapabilitiesProvider, Runner.Common"); - break; // Action command extensions. case "GitHub.Runner.Worker.IActionCommandExtension": Add(extensions, "GitHub.Runner.Worker.InternalPluginSetRepoPathCommandExtension, Runner.Worker"); diff --git a/src/Runner.Common/RunnerServer.cs b/src/Runner.Common/RunnerServer.cs index d987afc70..23200d46b 100644 --- a/src/Runner.Common/RunnerServer.cs +++ b/src/Runner.Common/RunnerServer.cs @@ -31,7 +31,7 @@ namespace GitHub.Runner.Common Task DeleteAgentAsync(int agentPoolId, int agentId); Task> GetAgentPoolsAsync(string agentPoolName = null, TaskAgentPoolType poolType = TaskAgentPoolType.Automation); Task> GetAgentsAsync(int agentPoolId, string agentName = null); - Task UpdateAgentAsync(int agentPoolId, TaskAgent agent); + Task ReplaceAgentAsync(int agentPoolId, TaskAgent agent); // messagequeue Task CreateAgentSessionAsync(Int32 poolId, TaskAgentSession session, CancellationToken cancellationToken); @@ -257,7 +257,7 @@ namespace GitHub.Runner.Common return _genericTaskAgentClient.GetAgentsAsync(agentPoolId, agentName, false); } - public Task UpdateAgentAsync(int agentPoolId, TaskAgent agent) + public Task ReplaceAgentAsync(int agentPoolId, TaskAgent agent) { CheckConnection(RunnerConnectionType.Generic); return _genericTaskAgentClient.ReplaceAgentAsync(agentPoolId, agent); diff --git a/src/Runner.Listener/Configuration/ConfigurationManager.cs b/src/Runner.Listener/Configuration/ConfigurationManager.cs index b544e307a..2ececb558 100644 --- a/src/Runner.Listener/Configuration/ConfigurationManager.cs +++ b/src/Runner.Listener/Configuration/ConfigurationManager.cs @@ -1,24 +1,19 @@ using GitHub.DistributedTask.WebApi; -using GitHub.Runner.Common.Capabilities; using GitHub.Runner.Common.Util; using GitHub.Services.Common; using GitHub.Services.OAuth; using GitHub.Services.WebApi; using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Linq; using System.Security.Cryptography; -using System.Security.Principal; -using System.Threading; using System.Threading.Tasks; using System.Runtime.InteropServices; using GitHub.Runner.Common; using GitHub.Runner.Sdk; using System.Net.Http; using System.Net.Http.Headers; -using System.Text; namespace GitHub.Runner.Listener.Configuration { @@ -168,7 +163,8 @@ namespace GitHub.Runner.Listener.Configuration { // Get the URL var inputUrl = command.GetUrl(); - if (!inputUrl.Contains("github.com", StringComparison.OrdinalIgnoreCase)) + if (!inputUrl.Contains("github.com", StringComparison.OrdinalIgnoreCase) && + !inputUrl.Contains("github.localhost", StringComparison.OrdinalIgnoreCase)) { runnerSettings.ServerUrl = inputUrl; // Get the credentials @@ -238,11 +234,8 @@ namespace GitHub.Runner.Listener.Configuration { runnerSettings.AgentName = command.GetAgentName(); - // Get the system capabilities. - Dictionary systemCapabilities = await HostContext.GetService().GetCapabilitiesAsync(runnerSettings, CancellationToken.None); - _term.WriteLine(); - + var agents = await _runnerServer.GetAgentsAsync(runnerSettings.PoolId, runnerSettings.AgentName); Trace.Verbose("Returns {0} agents", agents.Count); agent = agents.FirstOrDefault(); @@ -251,12 +244,12 @@ namespace GitHub.Runner.Listener.Configuration _term.WriteLine("A runner exists with the same name", ConsoleColor.Yellow); if (command.GetReplace()) { - // Update existing agent with new PublicKey, agent version and SystemCapabilities. - agent = UpdateExistingAgent(agent, publicKey, systemCapabilities); + // Update existing agent with new PublicKey, agent version. + agent = UpdateExistingAgent(agent, publicKey); try { - agent = await _runnerServer.UpdateAgentAsync(runnerSettings.PoolId, agent); + agent = await _runnerServer.ReplaceAgentAsync(runnerSettings.PoolId, agent); _term.WriteSuccessMessage("Successfully replaced the runner"); break; } @@ -275,7 +268,7 @@ namespace GitHub.Runner.Listener.Configuration else { // Create a new agent. - agent = CreateNewAgent(runnerSettings.AgentName, publicKey, systemCapabilities); + agent = CreateNewAgent(runnerSettings.AgentName, publicKey); try { @@ -435,7 +428,7 @@ namespace GitHub.Runner.Listener.Configuration { ArgUtil.Equal(RunMode.Normal, HostContext.RunMode, nameof(HostContext.RunMode)); string currentAction = string.Empty; - + _term.WriteSection("Runner removal"); try @@ -499,7 +492,7 @@ namespace GitHub.Runner.Listener.Configuration else { await _runnerServer.DeleteAgentAsync(settings.PoolId, settings.AgentId); - + _term.WriteLine(); _term.WriteSuccessMessage("Runner removed successfully"); } @@ -573,7 +566,7 @@ namespace GitHub.Runner.Listener.Configuration } - private TaskAgent UpdateExistingAgent(TaskAgent agent, RSAParameters publicKey, Dictionary systemCapabilities) + private TaskAgent UpdateExistingAgent(TaskAgent agent, RSAParameters publicKey) { ArgUtil.NotNull(agent, nameof(agent)); agent.Authorization = new TaskAgentAuthorization @@ -585,15 +578,14 @@ namespace GitHub.Runner.Listener.Configuration agent.Version = BuildConstants.RunnerPackage.Version; agent.OSDescription = RuntimeInformation.OSDescription; - foreach (KeyValuePair capability in systemCapabilities) - { - agent.SystemCapabilities[capability.Key] = capability.Value ?? string.Empty; - } + agent.Labels.Add("self-hosted"); + agent.Labels.Add(VarUtil.OS); + agent.Labels.Add(VarUtil.OSArchitecture); return agent; } - private TaskAgent CreateNewAgent(string agentName, RSAParameters publicKey, Dictionary systemCapabilities) + private TaskAgent CreateNewAgent(string agentName, RSAParameters publicKey) { TaskAgent agent = new TaskAgent(agentName) { @@ -606,10 +598,9 @@ namespace GitHub.Runner.Listener.Configuration OSDescription = RuntimeInformation.OSDescription, }; - foreach (KeyValuePair capability in systemCapabilities) - { - agent.SystemCapabilities[capability.Key] = capability.Value ?? string.Empty; - } + agent.Labels.Add("self-hosted"); + agent.Labels.Add(VarUtil.OS); + agent.Labels.Add(VarUtil.OSArchitecture); return agent; } @@ -638,7 +629,7 @@ namespace GitHub.Runner.Listener.Configuration private async Task GetTenantCredential(string githubUrl, string githubToken) { var gitHubUrl = new UriBuilder(githubUrl); - var githubApiUrl = $"https://api.github.com/repos/{gitHubUrl.Path.Trim('/')}/actions-runners/registration"; + var githubApiUrl = $"https://api.{gitHubUrl.Host}/repos/{gitHubUrl.Path.Trim('/')}/actions-runners/registration"; using (var httpClientHandler = HostContext.CreateHttpClientHandler()) using (var httpClient = new HttpClient(httpClientHandler)) { diff --git a/src/Runner.Listener/MessageListener.cs b/src/Runner.Listener/MessageListener.cs index 2c6a1d6ac..ce35f26cc 100644 --- a/src/Runner.Listener/MessageListener.cs +++ b/src/Runner.Listener/MessageListener.cs @@ -1,7 +1,5 @@ using GitHub.DistributedTask.WebApi; -using GitHub.Runner.Common.Capabilities; using GitHub.Runner.Listener.Configuration; -using GitHub.Runner.Common.Util; using GitHub.Services.Common; using System; using System.Collections.Generic; @@ -10,7 +8,6 @@ using System.Threading.Tasks; using System.Security.Cryptography; using System.IO; using System.Text; -using GitHub.Services.WebApi; using GitHub.Services.OAuth; using System.Diagnostics; using System.Runtime.InteropServices; @@ -59,9 +56,6 @@ namespace GitHub.Runner.Listener var serverUrl = _settings.ServerUrl; Trace.Info(_settings); - // Capabilities. - Dictionary systemCapabilities = await HostContext.GetService().GetCapabilitiesAsync(_settings, token); - // Create connection. Trace.Info("Loading Credentials"); var credMgr = HostContext.GetService(); @@ -75,7 +69,7 @@ namespace GitHub.Runner.Listener OSDescription = RuntimeInformation.OSDescription, }; string sessionName = $"{Environment.MachineName ?? "RUNNER"}"; - var taskAgentSession = new TaskAgentSession(sessionName, agent, systemCapabilities); + var taskAgentSession = new TaskAgentSession(sessionName, agent); string errorMessage = string.Empty; bool encounteringError = false; diff --git a/src/Runner.Worker/DiagnosticLogManager.cs b/src/Runner.Worker/DiagnosticLogManager.cs index 58df7e1de..5e84aa9fa 100644 --- a/src/Runner.Worker/DiagnosticLogManager.cs +++ b/src/Runner.Worker/DiagnosticLogManager.cs @@ -2,19 +2,9 @@ using System.Collections.Generic; using System.IO; using System.IO.Compression; -using System.Text; -using System.Runtime.InteropServices; using GitHub.DistributedTask.WebApi; -using GitHub.Runner.Common.Util; -using GitHub.Runner.Worker; -using GitHub.Runner.Common.Capabilities; -using GitHub.Services.WebApi; -using Microsoft.Win32; -using System.Diagnostics; using System.Linq; -using System.Collections.ObjectModel; using System.Globalization; -using System.Threading; using System.Threading.Tasks; using Pipelines = GitHub.DistributedTask.Pipelines; using GitHub.Runner.Common; diff --git a/src/Sdk/DTWebApi/WebApi/TaskAgent.cs b/src/Sdk/DTWebApi/WebApi/TaskAgent.cs index 99aa704af..86fe1068f 100644 --- a/src/Sdk/DTWebApi/WebApi/TaskAgent.cs +++ b/src/Sdk/DTWebApi/WebApi/TaskAgent.cs @@ -55,14 +55,9 @@ namespace GitHub.DistributedTask.WebApi m_properties = new PropertiesCollection(agentToBeCloned.m_properties); } - if (agentToBeCloned.m_systemCapabilities != null && agentToBeCloned.m_systemCapabilities.Count > 0) + if (agentToBeCloned.m_labels != null && agentToBeCloned.m_labels.Count > 0) { - m_systemCapabilities = new Dictionary(agentToBeCloned.m_systemCapabilities, StringComparer.OrdinalIgnoreCase); - } - - if (agentToBeCloned.m_userCapabilities != null && agentToBeCloned.m_userCapabilities.Count > 0) - { - m_userCapabilities = new Dictionary(agentToBeCloned.m_userCapabilities, StringComparer.OrdinalIgnoreCase); + m_labels = new HashSet(agentToBeCloned.m_labels, StringComparer.OrdinalIgnoreCase); } if (agentToBeCloned.PendingUpdate != null) @@ -152,32 +147,17 @@ namespace GitHub.DistributedTask.WebApi } /// - /// System-defined capabilities supported by this agent's host. + /// The labels of the runner /// - public IDictionary SystemCapabilities + public ISet Labels { get { - if (m_systemCapabilities == null) + if (m_labels == null) { - m_systemCapabilities = new Dictionary(StringComparer.OrdinalIgnoreCase); + m_labels = new HashSet(StringComparer.OrdinalIgnoreCase); } - return m_systemCapabilities; - } - } - - /// - /// User-defined capabilities supported by this agent's host. - /// - public IDictionary UserCapabilities - { - get - { - if (m_userCapabilities == null) - { - m_userCapabilities = new Dictionary(StringComparer.OrdinalIgnoreCase); - } - return m_userCapabilities; + return m_labels; } } @@ -214,10 +194,7 @@ namespace GitHub.DistributedTask.WebApi [DataMember(IsRequired = false, EmitDefaultValue = false, Name = "Properties")] private PropertiesCollection m_properties; - [DataMember(IsRequired = false, EmitDefaultValue = false, Name = "SystemCapabilities")] - private Dictionary m_systemCapabilities; - - [DataMember(IsRequired = false, EmitDefaultValue = false, Name = "UserCapabilities")] - private Dictionary m_userCapabilities; + [DataMember(IsRequired = false, EmitDefaultValue = false, Name = "Labels")] + private HashSet m_labels; } } diff --git a/src/Sdk/DTWebApi/WebApi/TaskAgentHttpClient.cs b/src/Sdk/DTWebApi/WebApi/TaskAgentHttpClient.cs index 908227ccb..337d55354 100644 --- a/src/Sdk/DTWebApi/WebApi/TaskAgentHttpClient.cs +++ b/src/Sdk/DTWebApi/WebApi/TaskAgentHttpClient.cs @@ -246,8 +246,7 @@ namespace GitHub.DistributedTask.WebApi PlanType = hubName, ScopeId = scopeIdentifier, PlanId = planId, - JobId = jobId, - Demands = demands, + JobId = jobId }; return QueueAgentRequestByPoolAsync(poolId, request, userState, cancellationToken); diff --git a/src/Sdk/DTWebApi/WebApi/TaskAgentJobRequest.cs b/src/Sdk/DTWebApi/WebApi/TaskAgentJobRequest.cs index 78d0ba3c6..39c6d042d 100644 --- a/src/Sdk/DTWebApi/WebApi/TaskAgentJobRequest.cs +++ b/src/Sdk/DTWebApi/WebApi/TaskAgentJobRequest.cs @@ -35,7 +35,6 @@ namespace GitHub.DistributedTask.WebApi this.PoolId = requestToBeCloned.PoolId; this.JobId = requestToBeCloned.JobId; this.JobName = requestToBeCloned.JobName; - this.Demands = new List(requestToBeCloned.Demands ?? new Demand[0]); this.LockToken = requestToBeCloned.LockToken; this.ExpectedDuration = requestToBeCloned.ExpectedDuration; this.OrchestrationId = requestToBeCloned.OrchestrationId; @@ -68,6 +67,11 @@ namespace GitHub.DistributedTask.WebApi { this.AgentSpecification = new JObject(requestToBeCloned.AgentSpecification); } + + if (requestToBeCloned.Labels != null) + { + this.Labels = new HashSet(requestToBeCloned.Labels, StringComparer.OrdinalIgnoreCase); + } } /// @@ -229,6 +233,7 @@ namespace GitHub.DistributedTask.WebApi /// /// [DataMember(Order = 16, EmitDefaultValue = false)] + [Obsolete("No more demands, use labels", true)] public IList Demands { get; @@ -386,6 +391,13 @@ namespace GitHub.DistributedTask.WebApi set; } + [DataMember(Order = 33, EmitDefaultValue = false)] + public ISet Labels + { + get; + set; + } + [IgnoreDataMember] internal Guid? LockToken { diff --git a/src/Sdk/DTWebApi/WebApi/TaskAgentSession.cs b/src/Sdk/DTWebApi/WebApi/TaskAgentSession.cs index 51d95b67e..a0835d2a1 100644 --- a/src/Sdk/DTWebApi/WebApi/TaskAgentSession.cs +++ b/src/Sdk/DTWebApi/WebApi/TaskAgentSession.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Runtime.Serialization; namespace GitHub.DistributedTask.WebApi @@ -27,29 +26,6 @@ namespace GitHub.DistributedTask.WebApi this.OwnerName = ownerName; } - /// - /// Initializes a new TaskAgentSession isntance with the specified owner name, agent, and capabilities. - /// - /// The name of the owner for this session. This should typically be the agent machine - /// The target agent for the session - /// A collection of capabilities to publish on session creation - public TaskAgentSession( - String ownerName, - TaskAgentReference agent, - IDictionary systemCapabilities) - { - this.Agent = agent; - this.OwnerName = ownerName; - - foreach (var capability in systemCapabilities) - { - if (capability.Value != null) - { - this.SystemCapabilities.Add(capability.Key, capability.Value); - } - } - } - /// /// Gets the unique identifier for this session. /// @@ -89,33 +65,5 @@ namespace GitHub.DistributedTask.WebApi get; set; } - - /// - /// Gets the collection of system capabilities used for this session. - /// - public IDictionary SystemCapabilities - { - get - { - if (m_systemCapabilities == null) - { - m_systemCapabilities = new Dictionary(StringComparer.OrdinalIgnoreCase); - } - return m_systemCapabilities; - } - } - - [OnSerializing] - private void OnSerializing(StreamingContext context) - { - if (m_systemCapabilities?.Count == 0) - { - m_systemCapabilities = null; - } - } - - - [DataMember(IsRequired = false, EmitDefaultValue = false, Name = "SystemCapabilities")] - private IDictionary m_systemCapabilities; } } diff --git a/src/Test/L0/ExtensionManagerL0.cs b/src/Test/L0/ExtensionManagerL0.cs index 46b69c750..37eb7e657 100644 --- a/src/Test/L0/ExtensionManagerL0.cs +++ b/src/Test/L0/ExtensionManagerL0.cs @@ -1,5 +1,4 @@ -using GitHub.Runner.Common.Capabilities; -using GitHub.Runner.Worker; +using GitHub.Runner.Worker; using System; using System.Collections.Generic; using System.Linq; @@ -21,12 +20,12 @@ namespace GitHub.Runner.Common.Tests manager.Initialize(tc); // Act. - List extensions = manager.GetExtensions(); + List extensions = manager.GetExtensions(); // Assert. Assert.True( - extensions.Any(x => x is RunnerCapabilitiesProvider), - $"Expected {nameof(RunnerCapabilitiesProvider)} extension to be returned as a job extension."); + extensions.Any(x => x is SetEnvCommandExtension), + $"Expected {nameof(SetEnvCommandExtension)} extension to be returned as a job extension."); } } @@ -42,9 +41,9 @@ namespace GitHub.Runner.Common.Tests manager.Initialize(tc); // Act/Assert. - AssertContains( + AssertContains( manager, - concreteType: typeof(GitHub.Runner.Common.Capabilities.RunnerCapabilitiesProvider)); + concreteType: typeof(GitHub.Runner.Worker.SetEnvCommandExtension)); } } diff --git a/src/Test/L0/Listener/Configuration/AgentCapabilitiesProviderTestL0.cs b/src/Test/L0/Listener/Configuration/AgentCapabilitiesProviderTestL0.cs deleted file mode 100644 index 28d617547..000000000 --- a/src/Test/L0/Listener/Configuration/AgentCapabilitiesProviderTestL0.cs +++ /dev/null @@ -1,79 +0,0 @@ -using GitHub.Runner.Common.Capabilities; -using GitHub.Runner.Listener.Configuration; -using Moq; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Xunit; - -namespace GitHub.Runner.Common.Tests.Listener -{ - public sealed class AgentCapabilitiesProviderTestL0 - { - [Fact] - [Trait("Level", "L0")] - [Trait("Category", "Agent")] - public async void TestGetCapabilities() - { - using (var hc = new TestHostContext(this)) - using (var tokenSource = new CancellationTokenSource()) - { - Mock configurationManager = new Mock(); - hc.SetSingleton(configurationManager.Object); - - // Arrange - var provider = new RunnerCapabilitiesProvider(); - provider.Initialize(hc); - var settings = new RunnerSettings() { AgentName = "IAmAgent007" }; - - // Act - List capabilities = await provider.GetCapabilitiesAsync(settings, tokenSource.Token); - - // Assert - Assert.NotNull(capabilities); - Capability runnerNameCapability = capabilities.SingleOrDefault(x => string.Equals(x.Name, "Runner.Name", StringComparison.Ordinal)); - Assert.NotNull(runnerNameCapability); - Assert.Equal("IAmAgent007", runnerNameCapability.Value); - } - } - - [Fact] - [Trait("Level", "L0")] - [Trait("Category", "Agent")] - public async void TestInteractiveSessionCapability() - { - using (var hc = new TestHostContext(this)) - using (var tokenSource = new CancellationTokenSource()) - { - hc.StartupType = StartupType.AutoStartup; - await VerifyInteractiveSessionCapability(hc, tokenSource.Token, true); - - hc.StartupType = StartupType.Service; - await VerifyInteractiveSessionCapability(hc, tokenSource.Token, false); - - hc.StartupType = StartupType.Manual; - await VerifyInteractiveSessionCapability(hc, tokenSource.Token, true); - } - } - - private async Task VerifyInteractiveSessionCapability(IHostContext hc, CancellationToken token, bool expectedValue) - { - // Arrange - var provider = new RunnerCapabilitiesProvider(); - provider.Initialize(hc); - var settings = new RunnerSettings() { AgentName = "IAmAgent007" }; - - // Act - List capabilities = await provider.GetCapabilitiesAsync(settings, token); - - // Assert - Assert.NotNull(capabilities); - Capability iSessionCapability = capabilities.SingleOrDefault(x => string.Equals(x.Name, "InteractiveSession", StringComparison.Ordinal)); - Assert.NotNull(iSessionCapability); - bool.TryParse(iSessionCapability.Value, out bool isInteractive); - Assert.Equal(expectedValue, isInteractive); - } - } -} diff --git a/src/Test/L0/Listener/Configuration/ConfigurationManagerL0.cs b/src/Test/L0/Listener/Configuration/ConfigurationManagerL0.cs index 0fd942764..82bdd35c6 100644 --- a/src/Test/L0/Listener/Configuration/ConfigurationManagerL0.cs +++ b/src/Test/L0/Listener/Configuration/ConfigurationManagerL0.cs @@ -1,6 +1,5 @@ using GitHub.DistributedTask.WebApi; using GitHub.Runner.Listener; -using GitHub.Runner.Common.Capabilities; using GitHub.Runner.Listener.Configuration; using GitHub.Runner.Common.Util; using GitHub.Services.WebApi; @@ -40,8 +39,6 @@ namespace GitHub.Runner.Common.Tests.Listener.Configuration #endif private Mock _rsaKeyManager; - private ICapabilitiesManager _capabilitiesManager; - // private DeploymentGroupAgentConfigProvider _deploymentGroupAgentConfigProvider; private string _expectedToken = "expectedToken"; private string _expectedServerUrl = "https://localhost"; private string _expectedAgentName = "expectedAgentName"; @@ -78,8 +75,6 @@ namespace GitHub.Runner.Common.Tests.Listener.Configuration _serviceControlManager = new Mock(); #endif - _capabilitiesManager = new CapabilitiesManager(); - var expectedAgent = new TaskAgent(_expectedAgentName) { Id = 1 }; var expectedDeploymentMachine = new DeploymentMachine() { Agent = expectedAgent, Id = _expectedDeploymentMachineId }; expectedAgent.Authorization = new TaskAgentAuthorization @@ -127,7 +122,7 @@ namespace GitHub.Runner.Common.Tests.Listener.Configuration _agentServer.Setup(x => x.GetAgentsAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(expectedAgents)); _agentServer.Setup(x => x.AddAgentAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(expectedAgent)); - _agentServer.Setup(x => x.UpdateAgentAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(expectedAgent)); + _agentServer.Setup(x => x.ReplaceAgentAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(expectedAgent)); rsa = new RSACryptoServiceProvider(2048); @@ -143,8 +138,6 @@ namespace GitHub.Runner.Common.Tests.Listener.Configuration tc.SetSingleton(_extnMgr.Object); tc.SetSingleton(_agentServer.Object); tc.SetSingleton(_locationServer.Object); - // tc.SetSingleton(_machineGroupServer.Object); - tc.SetSingleton(_capabilitiesManager); tc.SetSingleton(_runnerWebProxy.Object); tc.SetSingleton(_cert.Object); @@ -208,12 +201,7 @@ namespace GitHub.Runner.Common.Tests.Listener.Configuration // validate GetAgentPoolsAsync gets called once with automation pool type _agentServer.Verify(x => x.GetAgentPoolsAsync(It.IsAny(), It.Is(p => p == TaskAgentPoolType.Automation)), Times.Once); - // validate GetAgentPoolsAsync not called with deployment pool type - _agentServer.Verify(x => x.GetAgentPoolsAsync(It.IsAny(), It.Is(p => p == TaskAgentPoolType.Deployment)), Times.Never); - - // For build and release agent / deployment pool, tags logic should not get trigger; - // _machineGroupServer.Verify(x => - // x.UpdateDeploymentTargetsAsync(It.IsAny(), It.IsAny(), It.IsAny>()), Times.Never); + _agentServer.Verify(x => x.AddAgentAsync(It.IsAny(), It.Is(a => a.Labels.Contains("self-hosted") && a.Labels.Contains(VarUtil.OS) && a.Labels.Contains(VarUtil.OSArchitecture))), Times.Once); } } } diff --git a/src/Test/L0/Listener/MessageListenerL0.cs b/src/Test/L0/Listener/MessageListenerL0.cs index 5324a3e8d..e85ac6170 100644 --- a/src/Test/L0/Listener/MessageListenerL0.cs +++ b/src/Test/L0/Listener/MessageListenerL0.cs @@ -2,7 +2,6 @@ using GitHub.Services.Common; using GitHub.Services.WebApi; using GitHub.Runner.Listener; -using GitHub.Runner.Common.Capabilities; using GitHub.Runner.Listener.Configuration; using Moq; using System; @@ -21,7 +20,6 @@ namespace GitHub.Runner.Common.Tests.Listener private Mock _config; private Mock _agentServer; private Mock _credMgr; - private Mock _capabilitiesManager; public MessageListenerL0() { @@ -30,7 +28,6 @@ namespace GitHub.Runner.Common.Tests.Listener _config.Setup(x => x.LoadSettings()).Returns(_settings); _agentServer = new Mock(); _credMgr = new Mock(); - _capabilitiesManager = new Mock(); } private TestHostContext CreateTestContext([CallerMemberName] String testName = "") @@ -39,7 +36,6 @@ namespace GitHub.Runner.Common.Tests.Listener tc.SetSingleton(_config.Object); tc.SetSingleton(_agentServer.Object); tc.SetSingleton(_credMgr.Object); - tc.SetSingleton(_capabilitiesManager.Object); return tc; } @@ -62,8 +58,6 @@ namespace GitHub.Runner.Common.Tests.Listener tokenSource.Token)) .Returns(Task.FromResult(expectedSession)); - _capabilitiesManager.Setup(x => x.GetCapabilitiesAsync(_settings, It.IsAny())).Returns(Task.FromResult(new Dictionary())); - _credMgr.Setup(x => x.LoadCredentials()).Returns(new VssCredentials()); // Act. @@ -106,8 +100,6 @@ namespace GitHub.Runner.Common.Tests.Listener tokenSource.Token)) .Returns(Task.FromResult(expectedSession)); - _capabilitiesManager.Setup(x => x.GetCapabilitiesAsync(_settings, It.IsAny())).Returns(Task.FromResult(new Dictionary())); - _credMgr.Setup(x => x.LoadCredentials()).Returns(new VssCredentials()); // Act. @@ -153,8 +145,6 @@ namespace GitHub.Runner.Common.Tests.Listener tokenSource.Token)) .Returns(Task.FromResult(expectedSession)); - _capabilitiesManager.Setup(x => x.GetCapabilitiesAsync(_settings, It.IsAny())).Returns(Task.FromResult(new Dictionary())); - _credMgr.Setup(x => x.LoadCredentials()).Returns(new VssCredentials()); // Act. diff --git a/src/Test/L0/ServiceInterfacesL0.cs b/src/Test/L0/ServiceInterfacesL0.cs index e3e278a0d..106a9a32b 100644 --- a/src/Test/L0/ServiceInterfacesL0.cs +++ b/src/Test/L0/ServiceInterfacesL0.cs @@ -1,5 +1,4 @@ using GitHub.Runner.Listener; -using GitHub.Runner.Common.Capabilities; using GitHub.Runner.Listener.Configuration; using GitHub.Runner.Worker; using GitHub.Runner.Worker.Handlers; @@ -44,7 +43,6 @@ namespace GitHub.Runner.Common.Tests typeof(IHostContext), typeof(ITraceManager), typeof(IThrottlingReporter), - typeof(ICapabilitiesProvider) }; Validate( assembly: typeof(IHostContext).GetTypeInfo().Assembly,