def print_services(services): header = ['Service Name', 'Task Def', 'Launch Type/CPs', 'Desired', 'Running', 'Pending', 'Status', 'Created', 'Deployments (des/pend/run)'] def format_deployments(deployments): result = [] for d in deployments: updated_at = humanize.naturaltime(datetime.datetime.now(datetime.timezone.utc) - d['updatedAt']) result.append("{}/{}/{} {}".format(d['desiredCount'], d['pendingCount'], d['runningCount'], updated_at)) msg = " ".join(result) if len(msg)> 80: return msg[0:80] + "..." return msg data = [[ s['serviceName'], s['taskDefinition'].split(":task-definition/")[-1], s.get('launchType', " ".join(["{} ({})".format(x['capacityProvider'], x['weight']) for x in s.get("capacityProviderStrategy", [])])), s['desiredCount'], s['runningCount'], s['pendingCount'], s['status'], humanize.naturaltime(datetime.datetime.now(datetime.timezone.utc) - s['createdAt']), format_deployments(s['deployments']), ] for s in services] print_table(header, data)
def print_container_repos(repos): header = ['Name', 'Latest', 'Recent Tags', 'Image Count', 'Untagged Count'] def format_latest_image(images): latest = sorted([i for i in images if 'latest' in i['tags']], key=lambda x:x['date']) return "({}) {}".format(latest[0]['digest'].split("sha256:")[-1][0:8], humanize.naturaltime(datetime.datetime.now(datetime.timezone.utc) - latest[0]['date']).rjust(16)) if latest else "" def format_recent_tag_images(images): tagged = sorted([i for i in images if 'latest' not in i['tags'] and i['tags']], key=lambda x: x['date'])[0:3] if not tagged: return "" tagged = ["({}) [{}] {}".format(t['digest'].split("sha256:")[-1][0:8], ",".join(t['tags']), humanize.naturaltime(datetime.datetime.now(datetime.timezone.utc) - t['date'])) for t in tagged] return ", ".join(tagged) data = [[ r['name'], format_latest_image(r['images']), format_recent_tag_images(r['images']), r['total'], r['total_untagged'] ] for r in repos] print_table(header, data)
def print_task_def_list(task_def_ids): header = ['Task Def'] data = [[ td.split(':task-definition/')[1] ] for td in task_def_ids[0:5]] print_table(header, data)
def print_task_definitions_by_service(task_definitions): header = ['Service', 'Earliest', 'Oldest', 'Total'] data = [[ td['name'], td['first'], td['last'], td['total'], ] for td in task_definitions] print_table(header, data)
def print_clusters_info(clusters_info): header = ['Region', 'Cluster', 'Container Instances', 'Running Tasks', 'Active Services'] data = [[ c['clusterArn'].split("arn:aws:ecs:")[1].split(":")[0], c['clusterName'], c['registeredContainerInstancesCount'], c['runningTasksCount'], c['activeServicesCount'], ] for c in clusters_info] print_table(header, data)
def print_task_events(events, max_rows=10): header = ['Age', 'Message'] def format_msg(msg): if len(msg)> 100: return msg[0:100] + "..." return msg data = [[ humanize.naturaltime(datetime.datetime.now(datetime.timezone.utc) - e['createdAt']), format_msg(e['message']), ] for e in events][0:max_rows] print_table(header, data)
def print_ec2_instances(instances): header = ['Name', 'AvailabilityZone', 'PrivateIpAddress', 'ImageId', 'InstanceType', 'InstanceId', 'State', 'Age'] data = [[ i['Name'] if i['Name'] else '', i['AvailabilityZone'], i['PrivateIpAddress'], i['ImageId'], i['InstanceType'], i['InstanceId'], i['State'], humanize.naturaltime(datetime.datetime.now(datetime.timezone.utc) - i['LaunchTime']), ] for i in instances] data = sorted(data,key=lambda x:x[0]) print_table(header, data)
def print_container_instances(instances): header = ['Cluster', 'ContainerInstance', 'Ec2InstanceId', 'Name', 'Private IP', 'State', 'AmiId', 'Type', 'Zone', 'Status', 'Tasks', 'Pending', 'CPU', 'Mem'] data = [[ i['cluster'], i['containerInstanceArn'].split(':container-instance/')[1], i['ec2InstanceId'], i['ec2Detail']['Name'], i['ec2Detail']['PrivateIpAddress'], i['ec2Detail']['State'], i['ecs.ami-id'], i['ecs.instance-type'], i['ecs.availability-zone'], i['status'], i['runningTasksCount'], i['pendingTasksCount'], "{}/{}".format((i['registered.CPU'] - i['remaining.CPU']), i['registered.CPU']), "{}/{}".format((i['registered.MEMORY'] - i['remaining.MEMORY']), i['registered.MEMORY']), ] for i in instances] print_table(header, data)
def print_tasks(tasks): header = ['Group', 'TaskDef', 'Ports', 'Name', 'IP', 'Zone', 'Instance', 'Connectivity', 'connectivityAt', 'memory', 'Desired', 'Health', 'Status'] def format_container_ports(containers): result = [] for c in containers: for p in c.get('networkBindings', []): result.append("{}->{}".format(p['containerPort'], p['hostPort'])) return " ".join(result) data = [] for t in tasks: item = [ t['group'], t['taskDefinitionArn'].split(':task-definition/')[1], format_container_ports(t['containers']), t['container_instance']['ec2Detail']['Name'] if "container_instance" in t and t['container_instance'] else "NA", t['container_instance']['ec2Detail']['PrivateIpAddress'] if "container_instance" in t and t['container_instance'] else "NA", t['container_instance']['ecs.availability-zone'] if "container_instance" in t and t['container_instance'] else "NA", t['container_instance']['ecs.instance-type'] if "container_instance" in t and t['container_instance'] else "NA", t.get('connectivity', ''), humanize.naturaltime(datetime.datetime.now(datetime.timezone.utc) - t['connectivityAt']) if 'connectivityAt' in t else "", t['memory'], t['desiredStatus'], t['healthStatus'], t['lastStatus'], ] data.append(item) print_table(header, data)