Example #1
1
//
// Given a service, fetches the tasks associated with it and returns them in an array.
//
func getServiceTasks(awsConn *ecs.ECS, clusterName string, serviceName string) []*ecs.Task {
	taskOutput, err := awsConn.ListTasks(&ecs.ListTasksInput{
		Cluster:     &clusterName,
		ServiceName: &serviceName,
	})
	CheckError("fetching task list for service", err)
	var serviceTasks []*ecs.Task
	if len(taskOutput.TaskArns) > 0 {
		taskInfo, err := awsConn.DescribeTasks(&ecs.DescribeTasksInput{
			Tasks:   taskOutput.TaskArns,
			Cluster: &clusterName,
		})
		CheckError("fetching task data for service", err)
		serviceTasks = taskInfo.Tasks
	}
	return serviceTasks
}
Example #2
0
// DescribeTasks Describes an ECS task
func DescribeTasks(svc *ecs.ECS, taskArns []*string, cluster *string) (*ecs.DescribeTasksOutput, error) {
	if len(taskArns) == 0 {
		return nil, errors.New("Tasks can not be blank")
	}
	params := &ecs.DescribeTasksInput{
		Tasks:   taskArns,
		Cluster: cluster,
	}
	resp, err := svc.DescribeTasks(params)
	if err != nil {
		return nil, err
	}
	return resp, nil
}
Example #3
0
func GetContainerInstanceArnsForService(ecs_obj *ecs.ECS, cluster string, service string, local_container_instance_arn string, debug bool) ([]string, error) {
	// Fetch a task list based on the service name.
	list_tasks_params := &ecs.ListTasksInput{
		Cluster:     &cluster,
		ServiceName: &service,
	}
	list_tasks_resp, list_tasks_err := ecs_obj.ListTasks(list_tasks_params)

	if list_tasks_err != nil {
		return []string{}, fmt.Errorf("Cannot retrieve ECS task list: %s", FormatAwsError(list_tasks_err))
	}

	if len(list_tasks_resp.TaskArns) <= 0 {
		return []string{}, fmt.Errorf("No ECS tasks found with specified filter - cluster: ", cluster, ", service:", service)
	}

	// Describe the tasks retrieved above.
	describe_tasks_params := &ecs.DescribeTasksInput{
		Cluster: &cluster,
		Tasks:   list_tasks_resp.TaskArns,
	}
	describe_tasks_resp, describe_tasks_err := ecs_obj.DescribeTasks(describe_tasks_params)

	if describe_tasks_err != nil {
		return []string{}, fmt.Errorf("Cannot retrieve ECS task details:", FormatAwsError(describe_tasks_err))
	}

	if len(describe_tasks_resp.Tasks) <= 0 {
		return []string{}, fmt.Errorf("No ECS task details found with specified filter - tasks:", strings.Join(aws.StringValueSlice(list_tasks_resp.TaskArns), ", "))
	}

	var result []string
	for _, value := range describe_tasks_resp.Tasks {
		if *value.LastStatus == "RUNNING" && *value.ContainerInstanceArn != local_container_instance_arn {
			result = append(result, *value.ContainerInstanceArn)
		} else {
			if debug == true {
				fmt.Println(*value.ContainerInstanceArn, "is not in a RUNNING state, or is this instance (we dont return ourself). Excluded from results.")
			}
		}
	}

	if len(result) == 0 {
		return []string{}, fmt.Errorf("No ECS task results found in RUNNING state, no ECS container instances to return.")
	}
	return result, nil
}
Example #4
0
// GetTasks will return a slice of ECS Tasks within a cluster
func GetTasks(svc *ecs.ECS, cluster *string) ([]*ecs.Task, error) {

	var taskArns []*string

	// List clusters
	reqParams := &ecs.ListTasksInput{
		Cluster:    cluster,
		MaxResults: aws.Int64(100),
		NextToken:  aws.String(""),
	}

	// Loop through tokens until no more results remain
	for {
		resp, err := svc.ListTasks(reqParams)

		// Error check
		if err != nil {
			return nil, fmt.Errorf("ecs.ListTasks: %s", err.Error())
		}

		// Expand slice of tasks and append to our comprehensive list
		taskArns = append(taskArns, resp.TaskArns...)

		// Cycle token
		if resp.NextToken != nil {
			reqParams.NextToken = resp.NextToken
		} else {
			// Kill loop ... out of response pages
			break
		}

	}

	// Describe the tasks that we just got back
	resp, err := svc.DescribeTasks(&ecs.DescribeTasksInput{
		Cluster: cluster,
		Tasks:   taskArns,
	})

	if err != nil {
		return nil, fmt.Errorf("ecs.DescribeTasks: %s", err.Error())
	}

	return resp.Tasks, nil
}