private static async Task <List <EcsTask> > DescribeTasks(
            AmazonECSClient ecsClient,
            string cluster,
            List <string> taskArns,
            CancellationToken token)
        {
            var include = new List <string> {
                TaskField.TAGS
            };
            var accumulator = new List <EcsTask>();
            // split batch into chunks of 100 requests
            var lists = taskArns
                        .Select((value, index) => new { value, index })
                        .GroupBy(x => x.index / 100)
                        .Select(x => x.Select(v => v.value).ToList());

            foreach (var arns in lists)
            {
                var response = await ecsClient.DescribeTasksAsync(new DescribeTasksRequest
                {
                    Cluster = cluster,
                    Tasks   = arns,
                    Include = include
                }, token);

                accumulator.AddRange(response.Tasks);
            }

            return(accumulator);
        }
Exemplo n.º 2
0
        // **Step 2** Look up the current container's status via AWS SDK
        public static Boolean QuerySdkTaskStatus(string cluster, string uuid)
        {
            Boolean         taskSdkStatus         = false;
            AmazonECSClient ecs                   = new AmazonECSClient();
            Task <DescribeTasksResponse> response = ecs.DescribeTasksAsync(
                new DescribeTasksRequest
            {
                Cluster = cluster,
                Tasks   = new List <string> {
                    uuid
                }
            }
                );

            // this will run until completion or until task is killed due to timeout
            while (response.Status != TaskStatus.RanToCompletion)
            {
                // failure possible if container doesn't have access to 'ecs.DescribeTasks'
                if (response.Status == TaskStatus.Faulted)
                {
                    Console.WriteLine("Error in getting a response from request.");
                    break;
                }
            }

            if (response.Result.Tasks.Count > 0)
            {
                Amazon.ECS.Model.Task currentTask = response.Result.Tasks[0];
                Console.WriteLine("Task stopped reason: {0}", currentTask.StoppedReason);

                if (!ValidTaskStopReason(currentTask.StoppedReason))
                {
                    taskSdkStatus = true;
                }
                else
                {
                    Console.WriteLine("Task has stopped with valid reason: {0}", currentTask.StoppedReason);
                }
            }

            return(taskSdkStatus);
        }
Exemplo n.º 3
0
        public static async Task <string> GetContainerHostUrlAsync(IConfiguration configuration, string scheme = "http")
        {
            try
            {
                // This implementation is for local.
                var gatewayUrl = configuration["GATEWAY_URL"];

                if (!string.IsNullOrWhiteSpace(gatewayUrl))
                {
                    return(gatewayUrl);
                }

                // Following implementation is for AWS-ECS.
                var uri = configuration["ECS_CONTAINER_METADATA_URI"];

                if (string.IsNullOrWhiteSpace(uri))
                {
                    throw new InvalidOperationException("ECS_CONTAINER_METADATA_URI does not exist.");
                }

                using (var client = new HttpClient())
                {
                    var response = await client.GetAsync(uri);

                    var content = await response.Content.ReadAsStringAsync();

                    var json    = JObject.Parse(content);
                    var cluster = json["Labels"]["com.amazonaws.ecs.cluster"].ToString();
                    var taskArn = json["Labels"]["com.amazonaws.ecs.task-arn"].ToString();
                    var ecs     = new AmazonECSClient();
                    var request = new DescribeTasksRequest {
                        Cluster = cluster, Tasks = new List <string> {
                            taskArn
                        }
                    };
                    var tasks = await ecs.DescribeTasksAsync(request);

                    var container = tasks.Tasks[0].Containers[0];
                    var address   = container.NetworkInterfaces[0].PrivateIpv4Address;

                    if (container.NetworkBindings.Any())
                    {
                        var port = container.NetworkBindings[0].HostPort;
                        return($"{scheme}://{address}:{port}/");
                    }
                    else
                    {
                        var taskDef = await ecs.DescribeTaskDefinitionAsync(
                            new DescribeTaskDefinitionRequest
                        {
                            TaskDefinition = tasks.Tasks[0].TaskDefinitionArn
                        });

                        var port = taskDef.TaskDefinition.ContainerDefinitions[0].PortMappings[0].HostPort;
                        return($"{scheme}://{address}:{port}/");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                throw;
            }
        }