Created
February 27, 2018 22:11
-
-
Save aidansteele/1cfe17e5287dd63f9fd7648322ecb66d to your computer and use it in GitHub Desktop.
sort ecs tasks in a cluster by memory reservation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import boto3 | |
import json | |
import sys | |
import os | |
import itertools | |
def chunks(l, n): | |
for i in range(0, len(l), n): | |
yield l[i:i + n] | |
def describe_all_tasks(**kwargs): | |
ecs = boto3.client('ecs') | |
paged_arns = chunks(kwargs['tasks'], 100) | |
for arns in paged_arns: | |
kwargs['tasks'] = arns | |
resp = ecs.describe_tasks(**kwargs) | |
yield from resp['tasks'] | |
def base_taskdef_name(arn): | |
bits = arn.split('/') | |
name_rev = bits[-1] | |
bits = name_rev.split(':') | |
return bits[0] | |
if sys.stdout.isatty(): | |
os.execlp("bash", "bash", "-c", "%s %s %s | jq ." % (sys.executable, __file__, " ".join(sys.argv[1:]))) | |
ecs = boto3.client('ecs') | |
cluster = sys.argv[1] | |
paginator = ecs.get_paginator('list_tasks') | |
it = paginator.paginate(cluster=cluster, desiredStatus='RUNNING') | |
arns = sum([page['taskArns'] for page in it], []) | |
tasks = list(describe_all_tasks(cluster=cluster, tasks=arns)) | |
tasks.sort(key=lambda task: task['taskDefinitionArn']) | |
grouped = [(k, list(g)) for (k, g) in itertools.groupby(tasks, key=lambda task: base_taskdef_name(task['taskDefinitionArn']))] | |
memSum = [(arn, int(tasks[0]['memory']) * len(tasks)) for (arn, tasks) in grouped] | |
memSum.sort(key=lambda taskMem: -1 * taskMem[1]) | |
taskmap = dict(memSum) | |
print(json.dumps(taskmap, default=str)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment