Example #1
0
        public async Task <IActionResult> AcquireAgent([FromBody] AgentAcquireItem agentRequestItem, CancellationToken cancellationToken)
        {
            (string orchestrationId, string jobName) = ExtractRequestSourceInfo();

            using (_logger.BeginScope("Starting acquire operation for " +
                                      "Agent Id={agentId} Pool={agentPool} OrchestrationId={orchestrationId} JobName={jobName}",
                                      agentRequestItem.agentId, agentRequestItem.agentPool, orchestrationId, jobName))
            {
                // To acquire a new agent, we'll need to do the following:
                // 1. Determine what queue VSTS is asking for.
                // 2. Determine whether such a queue exists in Helix (and whether we can use it)
                // 3. If the queue exists, submit work to it.
                string queueId;
                try
                {
                    queueId = ExtractQueueId(agentRequestItem.agentSpecification);

                    if (queueId == null)
                    {
                        return(Json(new AgentInfoItem()
                        {
                            accepted = false
                        }));
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError($"Unable to extract queue id from request: {e.ToString()}");
                    return(BadRequest());
                }

                var agentSettings = agentRequestItem.agentConfiguration.agentSettings;

                _logger.LogInformation("Acquiring agent for queue {queueId}", queueId);

                var jobCreator = await GetHelixJobCreator(agentRequestItem, queueId, orchestrationId, jobName);

                if (jobCreator == null)
                {
                    return(Json(new AgentInfoItem()
                    {
                        accepted = false
                    }));
                }
                cancellationToken.ThrowIfCancellationRequested();
                Dictionary <string, string> properties = new Dictionary <string, string>
                {
                    { "AgentId", agentRequestItem.agentId },
                    { "Pool", agentRequestItem.agentPool },
                    { "OrchestrationId", orchestrationId },
                    { "JobName", jobName },
                };
                var agentInfoItem = await jobCreator.CreateJob(cancellationToken, properties);

                return(Json(agentInfoItem));
            }
        }
Example #2
0
 protected HelixJobCreator(AgentAcquireItem agentRequestItem, QueueInfo queueInfo, IHelixApi api,
                           ILoggerFactory loggerFactory, IHostingEnvironment hostingEnvironment,
                           Config configuration)
 {
     _agentRequestItem = agentRequestItem;
     _queueInfo        = queueInfo;
     _api                = api;
     _logger             = loggerFactory.CreateLogger <HelixJobCreator>();
     _configuration      = configuration;
     _hostingEnvironment = hostingEnvironment;
 }
Example #3
0
        public async Task <IActionResult> AcquireAgent([FromBody] AgentAcquireItem agentRequestItem)
        {
            // To acquire a new agent, we'll need to do the following:
            // 1. Determine what queue VSTS is asking for.
            // 2. Determine whether such a queue exists in Helix (and whether we can use it)
            // 3. If the queue exists, submit work to it.

            string queueId;

            try
            {
                queueId = ExtractQueueId(agentRequestItem.agentSpecification);

                if (queueId == null)
                {
                    return(Json(new AgentInfoItem()
                    {
                        accepted = false
                    }));
                }
            }
            catch (Exception e)
            {
                _logger.LogError($"Unable to extract queue id from request: {e.ToString()}");
                return(BadRequest());
            }
            var agentSettings = agentRequestItem.agentConfiguration.agentSettings;

            _logger.LogInformation($"Starting acquire operation. Queue={queueId} Agent Id={agentRequestItem.agentId} Pool={agentRequestItem.agentPool}");

            var jobCreator = await GetHelixJobCreator(agentRequestItem, queueId);

            if (jobCreator == null)
            {
                return(Json(new AgentInfoItem()
                {
                    accepted = false
                }));
            }
            return(Json(await jobCreator.CreateJob()));
        }
Example #4
0
        /// <summary>
        /// Retrieves a helix job creator specific to the type of queue that queueId is
        /// </summary>
        /// <param name="agentRequestItem">Acquire agent request info</param>
        /// <param name="queueId">Queue id</param>
        /// <returns>New helix job creator, null if queue info could not be obtained or the queue is not able to be used.</returns>
        private async Task <HelixJobCreator> GetHelixJobCreator(AgentAcquireItem agentRequestItem, string queueId, string orchestrationId, string jobName)
        {
            // Check the queue.
            QueueInfo queueInfo = await GetQueueInfo(queueId);

            if (queueInfo == null)
            {
                _logger.LogInformation($"Queue {queueId} does not exist");
                return(null);
            }
            else if (!queueInfo.IsAvailable.HasValue || !queueInfo.IsAvailable.Value)
            {
                _logger.LogInformation($"{queueId} exists but is not available");
                return(null);
            }
            else if (!IsAllowableQueue(queueInfo))
            {
                _logger.LogInformation($"{queueId} exists and is available but is not allowed based on the security settings of this pool provider.");
                return(null);
            }

            // Based on the os string, return the right job creator
            switch (queueInfo.OperatingSystemGroup.ToLowerInvariant())
            {
            case "windows":
                return(new HelixWindowsOSJobCreator(agentRequestItem, queueInfo, GetHelixApi(!queueInfo.IsInternalOnly.Value),
                                                    _loggerFactory, _hostingEnvironment, _configuration, orchestrationId, jobName));

            case "linux":
                return(new HelixLinuxOSJobCreator(agentRequestItem, queueInfo, GetHelixApi(!queueInfo.IsInternalOnly.Value),
                                                  _loggerFactory, _hostingEnvironment, _configuration, orchestrationId, jobName));

            case "osx":
                return(new HelixMacOSJobCreator(agentRequestItem, queueInfo, GetHelixApi(!queueInfo.IsInternalOnly.Value),
                                                _loggerFactory, _hostingEnvironment, _configuration, orchestrationId, jobName));

            default:
                throw new NotImplementedException($"Operating system group {queueInfo.OperatingSystemGroup} unexpected");
            }
        }
Example #5
0
 public HelixMacOSJobCreator(AgentAcquireItem agentRequestItem, QueueInfo queueInfo, IHelixApi api,
                             ILoggerFactory loggerFactory, IHostingEnvironment hostingEnvironment,
                             Config configuration)
     : base(agentRequestItem, queueInfo, api, loggerFactory, hostingEnvironment, configuration)
 {
 }
Example #6
0
 public HelixLinuxOSJobCreator(AgentAcquireItem agentRequestItem, QueueInfo queueInfo, IHelixApi api,
                               ILoggerFactory loggerFactory, IHostingEnvironment hostingEnvironment,
                               Config configuration, string orchestrationId, string jobName)
     : base(agentRequestItem, queueInfo, api, loggerFactory, hostingEnvironment, configuration, orchestrationId, jobName)
 {
 }