Пример #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
}
Пример #2
0
// ListClusters will return a slice of ECS Clusters
func ListClusters(svc *ecs.ECS) ([]*string, error) {
	var clusters []*string

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

	for {
		resp, err := svc.ListClusters(reqParams)

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

		// Expand slice of clusters and append to our comprehensive list
		clusters = append(clusters, resp.ClusterArns...)

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

	}

	return clusters, nil
}
Пример #3
0
func GetEc2InstanceIdsFromContainerInstances(ecs_obj *ecs.ECS, cluster string, container_instances []string, debug bool) ([]string, error) {
	params := &ecs.DescribeContainerInstancesInput{
		Cluster:            aws.String(cluster),
		ContainerInstances: aws.StringSlice(container_instances),
	}
	resp, err := ecs_obj.DescribeContainerInstances(params)

	if err != nil {
		return []string{}, fmt.Errorf("Cannot retrieve ECS container instance information: %s", FormatAwsError(err))
	}

	if len(resp.ContainerInstances) <= 0 {
		return []string{}, fmt.Errorf("No ECS container instances found with specified filter - cluster:", cluster, "- instances:", strings.Join(container_instances, ", "))
	}

	var result []string
	for _, value := range resp.ContainerInstances {
		if *value.Status == "ACTIVE" {
			result = append(result, *value.Ec2InstanceId)
		} else {
			if debug == true {
				fmt.Println(*value.Ec2InstanceId, "is not in an ACTIVE state, excluded from results.")
			}
		}
	}

	if len(result) == 0 {
		return []string{}, fmt.Errorf("No running ECS container instances found in result set, cannot proceed.")
	}
	return result, nil
}
Пример #4
0
// GetContainerInstances will return a slice of ECS Container Instances within a cluster
func GetContainerInstances(svc *ecs.ECS, ec2svc *ec2.EC2, cluster *string) (containerInstances []*ecs.ContainerInstance, instances []*ec2.Reservation, e error) {

	var ciArns []*string

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

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

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

		// Expand slice of container instances and append to our comprehensive list
		ciArns = append(ciArns, resp.ContainerInstanceArns...)

		// 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
	ciResponse, err := svc.DescribeContainerInstances(&ecs.DescribeContainerInstancesInput{
		Cluster:            cluster,
		ContainerInstances: ciArns,
	})

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

	var instanceIds []*string
	for _, k := range ciResponse.ContainerInstances {
		instanceIds = append(instanceIds, k.Ec2InstanceId)
	}

	// Create a map of container instances by ci arn...
	// Note: Will work for <= 1000 instances w/o having to use NextToken
	ec2Resp, err := ec2svc.DescribeInstances(&ec2.DescribeInstancesInput{
		InstanceIds: instanceIds,
	})

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

	return ciResponse.ContainerInstances, ec2Resp.Reservations, nil
}
Пример #5
0
// describeEc2Instances Describes EC2 instances by container istance ID
func describeEc2Instances(svc *ecs.ECS, cluster *string, containerInstances []*string) (*ec2.DescribeInstancesOutput, error) {

	params := &ecs.DescribeContainerInstancesInput{
		Cluster:            cluster,
		ContainerInstances: containerInstances,
	}
	ins, err := svc.DescribeContainerInstances(params)
	if err != nil {
		return nil, err
	}
	insfail := cli.Failure(ins.Failures, err)
	if insfail != nil {
		return nil, insfail
	}
	var ec2Instances = make([]*string, len(ins.ContainerInstances))
	for i, v := range ins.ContainerInstances {
		ec2Instances[i] = v.Ec2InstanceId
	}
	ec2client := ec2.New(sess.InitSession())
	ec2params := &ec2.DescribeInstancesInput{
		DryRun:      aws.Bool(false),
		InstanceIds: ec2Instances,
	}
	return ec2client.DescribeInstances(ec2params)
}
Пример #6
0
//
// Function to print the details of a service's task definition, since it's got a lot of fiddly details.
//
func PrintTaskDefinition(awsConn *ecs.ECS, taskDefinition *string, verboseFlag bool) {
	// Fetch the details of the task definition.
	taskDef, err := awsConn.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
		TaskDefinition: taskDefinition,
	})
	CheckError(fmt.Sprintf("fetching Task Definition for %s", *taskDefinition), err)
	fmt.Println("  - Task Definition:", *taskDefinition)
	fmt.Println("    - Family:", *taskDef.TaskDefinition.Family)
	for _, containerDef := range taskDef.TaskDefinition.ContainerDefinitions {
		fmt.Println("    - Container Definition:")
		fmt.Println("      - Image:", *containerDef.Image)
		if verboseFlag {
			fmt.Println("      - CPU:", *containerDef.Cpu)
			fmt.Println("      - Memory:", *containerDef.Memory)
		}
		for _, portMap := range containerDef.PortMappings {
			fmt.Println("      - Container Port", *portMap.ContainerPort, ": Host Port", *portMap.HostPort)
		}
		if len(containerDef.Command) > 0 {
			fmt.Printf("      - Command: %v\n", containerDef.Command)
		}
		if len(containerDef.EntryPoint) > 0 {
			fmt.Printf("      - Entry Point: %v\n", containerDef.EntryPoint)
		}
		if (len(containerDef.Environment) > 0) && (verboseFlag) {
			fmt.Println("      - Environment:")
			for _, envVariable := range containerDef.Environment {
				fmt.Println("       ", *envVariable.Name, "=", *envVariable.Value)
			}
		}
	}
}
Пример #7
0
// ListClusters List ECS clusters
func ListClusters(svc *ecs.ECS, maxResults *int64) (*ecs.ListClustersOutput, error) {
	params := &ecs.ListClustersInput{
		MaxResults: maxResults,
	}
	resp, err := svc.ListClusters(params)
	if err != nil {
		return nil, err
	}
	return resp, nil
}
Пример #8
0
// DeleteCluster Removes an ECS cluster
func DeleteCluster(svc *ecs.ECS, name *string) (*ecs.DeleteClusterOutput, error) {
	params := &ecs.DeleteClusterInput{
		Cluster: name,
	}
	resp, err := svc.DeleteCluster(params)
	if err != nil {
		return nil, err
	}
	return resp, nil
}
Пример #9
0
// findTaskArn
// finds the first task definition in a cluster and service.
//
// Returns task ARN
//
// TODO:
//   filter the resulting tasks by essential: true
//   and only return the essential?
//   either that or assume that the new image will
//   somewhat match the old image, and use that to pick
//   one of the tasks.
func findTaskArn(svc *ecs.ECS, clusterArn string, serviceArn string) (string, error) {
	params := &ecs.DescribeServicesInput{
		Services: []*string{
			aws.String(serviceArn),
		},
		Cluster: aws.String(clusterArn),
	}
	resp, err := svc.DescribeServices(params)
	return *resp.Services[0].TaskDefinition, err
}
Пример #10
0
func instanceIds(e *ecspkg.ECS, containerInstances []*string) []*string {
	output, err := e.DescribeContainerInstances(&ecspkg.DescribeContainerInstancesInput{
		ContainerInstances: containerInstances,
	})
	log.Check(err)
	var ids []*string
	for _, i := range output.ContainerInstances {
		ids = append(ids, i.Ec2InstanceId)
	}
	return ids
}
Пример #11
0
// ListTaskDefs Returns a list of task definitions that are registered to your account. You can filter the results by family name with the family parameter or by status with the status parameter.
func ListTaskDefs(svc *ecs.ECS, familyPrefix *string, status *string) (*ecs.ListTaskDefinitionsOutput, error) {
	params := &ecs.ListTaskDefinitionsInput{
		FamilyPrefix: familyPrefix,
		Status:       status,
	}
	resp, err := svc.ListTaskDefinitions(params)
	if err != nil {
		return resp, err
	}
	return resp, nil
}
Пример #12
0
// StopTask Stops a task
func StopTask(svc *ecs.ECS, task *string, cluster *string) (*ecs.StopTaskOutput, error) {
	params := &ecs.StopTaskInput{
		Task:    task,
		Cluster: cluster,
	}
	resp, err := svc.StopTask(params)
	if err != nil {
		return nil, err
	}
	return resp, nil
}
Пример #13
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
}
Пример #14
0
// DescribeCluster will return a Cluster (detail struct)
func DescribeCluster(svc *ecs.ECS, cluster *string) (*ecs.Cluster, error) {
	// Get cluster details for all the things...
	reqParams := &ecs.DescribeClustersInput{
		Clusters: []*string{cluster},
	}

	resp, err := svc.DescribeClusters(reqParams)

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

	return resp.Clusters[0], err
}
Пример #15
0
// StartTask Starts a new task in ECS
func StartTask(svc *ecs.ECS, taskDef *string, containerInstances []*string, cluster *string, startedBy *string, overrides *ecs.TaskOverride) (*ecs.StartTaskOutput, error) {
	params := &ecs.StartTaskInput{
		Cluster:            cluster,
		ContainerInstances: containerInstances,
		TaskDefinition:     taskDef,
		StartedBy:          startedBy,
		Overrides:          overrides,
	}
	resp, err := svc.StartTask(params)
	if err != nil {
		return nil, err
	}
	return resp, err
}
Пример #16
0
// Verify that the ECS service exists.
func VerifyServiceExists(ecs_obj *ecs.ECS, cluster string, service string) error {
	params := &ecs.DescribeServicesInput{
		Cluster: &cluster,
		Services: []*string{ // Required
			aws.String(service), // Required
		},
	}
	_, err := ecs_obj.DescribeServices(params)

	if err != nil {
		return fmt.Errorf("Cannot verify if ECS service exists: %s", FormatAwsError(err))
	}
	return nil
}
Пример #17
0
// findClusterArn finds a cluster's arn from a more human
// readable name
func findClusterArn(svc *ecs.ECS, clusterName string) (string, error) {
	params := &ecs.ListClustersInput{}
	clusters, err := svc.ListClusters(params)
	if err != nil {
		return "", err
	}
	for _, clusterId := range clusters.ClusterArns {
		var pattern string = `/` + clusterName + `$`
		matched, _ := regexp.MatchString(pattern, *clusterId)
		if matched {
			return *clusterId, nil
		}
	}
	return "", errors.New("Could not find cluster with name: " + clusterName)
}
Пример #18
0
// setImage
// Create a new Task Definition based on an existing
// ARN, and a new image.
//
// Returns new task's ARN
//
func setImage(svc *ecs.ECS, taskArn string, image string) (string, error) {
	params := &ecs.DescribeTaskDefinitionInput{TaskDefinition: aws.String(taskArn)}
	resp, err := svc.DescribeTaskDefinition(params)
	if err != nil {
		return "", err
	}
	task := resp.TaskDefinition
	task.ContainerDefinitions[0].Image = &image
	regResp, err := svc.RegisterTaskDefinition(&ecs.RegisterTaskDefinitionInput{
		Family:               task.Family,
		ContainerDefinitions: task.ContainerDefinitions,
		Volumes:              task.Volumes,
	})
	return *regResp.TaskDefinition.TaskDefinitionArn, nil
}
Пример #19
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
}
Пример #20
0
// findServiceArn finds a service arn from a more human
// readable name
func findServiceArn(svc *ecs.ECS, clusterArn string, serviceName string) (string, error) {
	params := &ecs.ListServicesInput{
		Cluster: aws.String(clusterArn),
	}
	services, err := svc.ListServices(params)
	if err != nil {
		return "", err
	}
	for _, serviceId := range services.ServiceArns {
		var pattern string = `/` + serviceName + `$`
		matched, _ := regexp.MatchString(pattern, *serviceId)
		if matched {
			return *serviceId, nil
		}
	}
	return "", errors.New("Could not find service with name: " + serviceName)
}
Пример #21
0
func containerInstances(e *ecspkg.ECS, cluster *string) []*string {
	instances := []*string{}
	next := aws.String("")
	for next != nil {
		input := ecspkg.ListContainerInstancesInput{
			Cluster: cluster,
		}
		if next != nil && *next != "" {
			input.NextToken = next
		}
		output, err := e.ListContainerInstances(&input)
		log.Check(err)
		instances = append(instances, output.ContainerInstanceArns...)
		next = output.NextToken
	}
	return instances
}
Пример #22
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
}
Пример #23
0
// Verify that the ECS cluster exists.
func VerifyClusterExists(ecs_obj *ecs.ECS, cluster string) error {
	params := &ecs.DescribeClustersInput{
		Clusters: []*string{
			aws.String(cluster),
		},
	}
	clusters, err := ecs_obj.DescribeClusters(params)

	if err != nil {
		return fmt.Errorf("Cannot verify if ECS cluster exists: %s", FormatAwsError(err))
	}
	if len(clusters.Clusters) == 0 {
		return fmt.Errorf("Error: ECS Cluster '%s' does not exist, cannot proceed.\n", cluster)
	}
	if len(clusters.Clusters) != 1 {
		return fmt.Errorf("Error: Unexpected number of ECS Clusters returned when searching for '%s'. Received: %+v\n", cluster, clusters.Clusters)
	}
	return nil
}
Пример #24
0
// RegisterTask Register a new version of the task definition
func RegisterTask(svc *ecs.ECS, params *ecs.RegisterTaskDefinitionInput) (*ecs.RegisterTaskDefinitionOutput, error) {
	return svc.RegisterTaskDefinition(params)
}
Пример #25
0
// ListTasks Returns a list of tasks for a specified cluster.
func ListTasks(svc *ecs.ECS, params *ecs.ListTasksInput) (*ecs.ListTasksOutput, error) {
	return svc.ListTasks(params)
}