def update_service(client, prod_service_name, new_td_arn): """ Update production service to use a new task definition revision. :param client obj: The boto client object. :param str prod_service_name: The getTalent service name. :param str new_td_arn: The AWS resource name for the task definition. """ try: response = client.describe_services( cluster=ecs_utils.PROD_CLUSTER_NAME, services=[prod_service_name]) ecs_utils.validate_http_status('describe_services', response) except Exception as e: print "Exception {} obtaining service description for {}".format( e.message, prod_service_name) exit(1) desired_count = response['services'][0]['desiredCount'] deployment_configuration = response['services'][0][ 'deploymentConfiguration'] try: response = client.update_service( cluster=ecs_utils.PROD_CLUSTER_NAME, service=prod_service_name, desiredCount=desired_count, taskDefinition=new_td_arn, deploymentConfiguration=deployment_configuration) ecs_utils.validate_http_status('update_service', response) except Exception as e: print "Exception {} updating service for {}".format( e.message, prod_service_name) exit(1) print "Successfully updated AWS service for {}".format(prod_service_name)
def scan_cluster(cluster): ''' Scan an ECS cluster and describe its services and task definitions. :param cluster: The cluster to inspect. :return: None. ''' services_response = ECS_CLIENT.list_services(cluster=cluster) validate_http_status('list_tasks', services_response) count = 0 while True: service_list = services_response['serviceArns'] for service in service_list: describe_service(cluster, service) count += 1 if 'nextToken' not in services_response: break services_response = ECS_CLIENT.list_services( cluster=cluster, nextToken=services_response['nextToken']) validate_http_status('list_tasks', services_response) print print "{} services found.".format(count)
def describe_service(cluster, service_arn): ''' Describe an ECS service and its task definition :param cluster: The cluster to inspect. :param service_arn: The service to describe. :return: None. ''' print response = ECS_CLIENT.describe_services(cluster=cluster, services=[service_arn]) validate_http_status('describe_services', response) print "{} {} Deployments: {}".format( response['services'][0]['serviceName'], response['services'][0]['status'], len(response['services'][0]['deployments'])) td = response['services'][0]['taskDefinition'] response = ECS_CLIENT.describe_task_definition(taskDefinition=td) validate_http_status('describe_services', response) print "Task: Family: {} Revision: {} Status: {}".format( response['taskDefinition']['family'], response['taskDefinition']['revision'], response['taskDefinition']['status']) # TODO: Add this line # print "Available Task Revsions: {} - {}".format(first_revision, last_revision) print "Image: {}".format( response['taskDefinition']['containerDefinitions'][0]['image']) print "CPU: {} Memory {}: ".format( response['taskDefinition']['containerDefinitions'][0]['cpu'], response['taskDefinition']['containerDefinitions'][0]['memory'])
def get_task_definition(client, td_name): """ Get a task definition by name. :param obj client: The boto ECS client. :param str td_name: The name of the task description. """ try: # This returns the latest ACTIVE revision task_definition = client.describe_task_definition( taskDefinition=td_name) ecs_utils.validate_http_status('get_task_description', task_definition) except Exception as e: print "Exception searching for task definition {}: {}".format( e.message, td_name) exit(1) return task_definition
def update_prod_task_definition(client, family_name, definitions): """ Create a new revision of a task definition. :param obj client: The boto ECS client. :param str family_name: The task name. :param json definitions: Container definitions (may only be one) """ try: response = client.register_task_definition( family=family_name, containerDefinitions=definitions) ecs_utils.validate_http_status('update_prod_task_definition', response) except Exception as e: print "Exception {} registering task definition for {}".format( e.message, family_name) exit(1) td = response['taskDefinition'] print "Task definition for {} updated to revision {}.".format( td['family'], td['revision']) return td['taskDefinitionArn']