Beispiel #1
0
def _validate_log_definitions(tasks: list) -> None:
    for task_dict in tasks:
        for container_definition in task_dict['containerDefinitions']:
            if not container_definition.get('logConfiguration'):
                continue

            if not container_definition['logConfiguration'].get('options'):
                continue

            options = container_definition['logConfiguration']['options']
            if not options['awslogs-group'].startswith('/ecs'):
                logger.error("log groups must start with '/ecs/' prefix: %s", options)
                raise AeropressException()

            if options['awslogs-stream-prefix'] != 'ecs':
                logger.error("logstream prefixes must be 'ecs' : %s", options)
                raise AeropressException()
Beispiel #2
0
def _validate_alarm_names(services: list) -> None:
    """
    All ecs alarm names must start with 'ecs:' prefix.
    """
    for service_dict in services:
        # TODO: Should be configurable
        if service_dict['alarm']['AlarmName'].startswith('ecs:'):
            continue

        logger.error("Alarm names must start with 'ecs:' prefix %s" %
                     service_dict)
        raise AeropressException()
Beispiel #3
0
def _load_config(root_path: Path,
                 image_url: str = None,
                 entrypoint: list = [],
                 command: list = [],
                 environment: str = None) -> list:
    logger.info('Reading yaml config files from %s', root_path)

    services = []  # type: List[Dict[str, Any]]

    # Reading yaml services definitions into a list of dictionary.
    for root, dirs, files in os.walk(root_path.as_posix()):
        for name in files:
            path = Path(os.path.join(root, name))
            with open(path.as_posix()) as f:
                _yaml_dict = yaml.safe_load(f.read())

            for key, value in _yaml_dict.items():
                # Handle service defnitions.
                for service_k, service_v in value.items():
                    if service_k != 'task':
                        continue

                    # Override defaults for container definitions.
                    for container_definition in service_v[
                            'containerDefinitions']:
                        if image_url:
                            container_definition['image'] = image_url

                        if entrypoint:
                            container_definition['entryPoint'] = entrypoint

                        if command:
                            container_definition['command'] = command

                        if environment:
                            # Environment must be in format of ... '[{"name": "foo", "value": "bar"}]'
                            container_definition['environment'] = json.loads(
                                environment)

                services.append(value)

    # Validate definitions
    if not _is_valid_config(services):
        logger.error('Config is not valid!')
        raise AeropressException()

    return services
Beispiel #4
0
def _register_task_definitions(tasks: list) -> None:
    for task_dict in tasks:
        # Create container definitions.
        container_definitions = []
        for container_definition in task_dict['containerDefinitions']:
            d = {
                'name': container_definition['name'],
                'image': container_definition['image'],
                'logConfiguration': container_definition['logConfiguration'],
                'memoryReservation': container_definition['memoryReservation'],
                'cpu': container_definition.get('cpu', 0),
                'entryPoint': container_definition.get('entryPoint', []),
                'command': container_definition.get('command', []),
                'environment': container_definition.get('environment', []),
                'portMappings': container_definition.get('portMappings', []),
                'ulimits': container_definition.get('ulimits', []),
                'mountPoints': container_definition.get('mountPoints', []),
                'links': container_definition.get('links', []),
            }
            if container_definition.get('memory'):
                if container_definition['memory'] < container_definition['memoryReservation']:
                    logger.error('memory must be equal or bigger than memoryReservation')
                    raise AeropressException()

                d['memory'] = container_definition['memory']

            container_definitions.append(d)

        logger.info('Creating task definition: %s', task_dict['family'])
        response = ecs_client.register_task_definition(
            family=task_dict['family'],
            taskRoleArn=task_dict['taskRoleArn'],
            executionRoleArn=task_dict['executionRoleArn'],
            networkMode=task_dict['networkMode'],
            containerDefinitions=container_definitions,
            requiresCompatibilities=task_dict['requiresCompatibilities'],
            volumes=task_dict.get('volumes', []),
        )
        logger.debug('Created task definition details: %s', response)
Beispiel #5
0
def deploy(services: list, service_names: list) -> None:
    selected_services = []  # type: List[Dict[str, Any]]
    for service_name in service_names:
        service_dict = _find_service(service_name, services)

        if not service_dict:
            logger.error("Service '%s' is not found! ", service_name)
            raise AeropressException()

        selected_services.append(service_dict)

    # Register task definitions.
    tasks = [
        selected_service.get('task') for selected_service in selected_services
    ]
    tasks = list(filter(None, tasks))
    if tasks:
        task.register_all(tasks)

    # Update or create all services. (We might have tasks without services, eliminating them..)
    filtered_selected_services = [
        sd for sd in selected_services if sd.get('serviceName')
    ]
    service.update_all(filtered_selected_services)