From 84fa11355ee7b47edb53b3652938164324a48a4e Mon Sep 17 00:00:00 2001 From: John Preston <1236150+JohnPreston@users.noreply.github.com> Date: Fri, 31 May 2024 05:49:57 +0100 Subject: [PATCH] compose - depends_on as object (#757) --- ecs_composex/compose/compose_services/__init__.py | 7 ++++++- ecs_composex/ecs/ecs_family/__init__.py | 14 ++++++++++---- .../firelens_config_sidecar.py | 2 +- .../firelens_managed_sidecar_service.py | 2 +- ecs_composex/ecs/ecs_stack.py | 2 +- ecs_composex/ecs/managed_sidecars/__init__.py | 4 ++-- .../create_lookup_services_mappings_cloudmap.yml | 8 ++++---- 7 files changed, 25 insertions(+), 14 deletions(-) diff --git a/ecs_composex/compose/compose_services/__init__.py b/ecs_composex/compose/compose_services/__init__.py index 3e0cf4667..b43fa5cf8 100644 --- a/ecs_composex/compose/compose_services/__init__.py +++ b/ecs_composex/compose/compose_services/__init__.py @@ -133,7 +133,12 @@ def __init__( self.cfn_environment = ( import_env_variables(self.environment) if self.environment else NoValue ) - self.depends_on = set_else_none("depends_on", self.definition, [], False) + self.depends_on: dict = set_else_none("depends_on", self.definition, {}, False) + if isinstance(self.depends_on, list): + services_names = [_s_name for _s_name in self.depends_on] + self.depends_on: dict = {} + for service_name in services_names: + self.depends_on[service_name] = {"condition": "service_started"} self.docker_labels: dict = {} self.import_docker_labels(self._definition) diff --git a/ecs_composex/ecs/ecs_family/__init__.py b/ecs_composex/ecs/ecs_family/__init__.py index a477c7f6c..2e9f7adef 100644 --- a/ecs_composex/ecs/ecs_family/__init__.py +++ b/ecs_composex/ecs/ecs_family/__init__.py @@ -78,7 +78,7 @@ def __init__(self, services: list[ComposeService], family_name): self.managed_sidecars = [] self.name = family_name self.family_hostname = self.name.replace("_", "-").lower() - self.services_depends_on = [] + self.services_depends_on: dict = {} self.template = set_template(self) self.stack: ServiceStack = ServiceStack( self.logical_name, @@ -585,9 +585,15 @@ def set_services_to_services_dependencies(self): """ for service in self.services: if service.depends_on: - for service_depends_on in service.depends_on: - if service_depends_on not in self.services_depends_on: - self.services_depends_on.append(service_depends_on) + if not isinstance(service.depends_on, dict): + raise TypeError( + "Service depends_on not a dict", + service.name, + service.depends_on, + ) + for service_name, condition_def in service.depends_on.items(): + if service_name not in self.services_depends_on: + self.services_depends_on[service_name] = {} @property def task_ephemeral_storage(self) -> int: diff --git a/ecs_composex/ecs/ecs_firelens/ecs_firelens_advanced/firelens_config_sidecar.py b/ecs_composex/ecs/ecs_firelens/ecs_firelens_advanced/firelens_config_sidecar.py index da2d4110f..02590ab29 100644 --- a/ecs_composex/ecs/ecs_firelens/ecs_firelens_advanced/firelens_config_sidecar.py +++ b/ecs_composex/ecs/ecs_firelens/ecs_firelens_advanced/firelens_config_sidecar.py @@ -102,4 +102,4 @@ def __init__( patch_fluent_service( fluent_service, shared_volume, name, shared_volume.name, mount_path ) - fluent_service.depends_on.append(self.name) + fluent_service.depends_on[self.name] = {"condition": "service_started"} diff --git a/ecs_composex/ecs/ecs_firelens/firelens_managed_sidecar_service.py b/ecs_composex/ecs/ecs_firelens/firelens_managed_sidecar_service.py index c41d7fbb1..55a2b88c0 100644 --- a/ecs_composex/ecs/ecs_firelens/firelens_managed_sidecar_service.py +++ b/ecs_composex/ecs/ecs_firelens/firelens_managed_sidecar_service.py @@ -186,7 +186,7 @@ def set_as_dependency_to_family_services(self, is_dependency: bool = True) -> No if service is self: continue if self.name not in service.depends_on: - service.depends_on.append(self.name) + service.depends_on[self.name] = {"condition": "service_healthy"} LOG.info( f"{self.family.name}.{service.name} - Added {self.name} as startup dependency" ) diff --git a/ecs_composex/ecs/ecs_stack.py b/ecs_composex/ecs/ecs_stack.py index 536834aa1..3b00224f4 100644 --- a/ecs_composex/ecs/ecs_stack.py +++ b/ecs_composex/ecs/ecs_stack.py @@ -76,7 +76,7 @@ def handle_families_dependencies( """ for family_def in families_post: _family_title, _family_name = family_def - for family_name in settings.families[_family_title].services_depends_on: + for family_name in settings.families[_family_title].services_depends_on.keys(): for __family_title, __family_def in settings.families.items(): if __family_def.name == family_name: family_title = __family_title diff --git a/ecs_composex/ecs/managed_sidecars/__init__.py b/ecs_composex/ecs/managed_sidecars/__init__.py index 849028587..a377c8d22 100644 --- a/ecs_composex/ecs/managed_sidecars/__init__.py +++ b/ecs_composex/ecs/managed_sidecars/__init__.py @@ -65,13 +65,13 @@ def set_as_dependency_to_family_services(self, is_dependency: bool = False) -> N for service in self.family.ordered_services: if is_dependency: if self.name not in service.depends_on: - service.depends_on.append(self.name) + service.depends_on[self.name] = {"condition": "service_started"} LOG.info( f"{self.family.name}.{service.name} - Added {self.name} as startup dependency" ) else: if service.name not in self.depends_on: - self.depends_on.append(service.name) + self.depends_on[service.name] = {"condition": "service_started"} LOG.info( f"{self.family.name}.{self.name} - Added {service.name} as startup dependency" ) diff --git a/use-cases/dynamodb/create_lookup_services_mappings_cloudmap.yml b/use-cases/dynamodb/create_lookup_services_mappings_cloudmap.yml index 30f5351a8..8f29f232e 100644 --- a/use-cases/dynamodb/create_lookup_services_mappings_cloudmap.yml +++ b/use-cases/dynamodb/create_lookup_services_mappings_cloudmap.yml @@ -82,8 +82,8 @@ x-dynamodb: tableC: Lookup: Tags: - - name: tableC - - createdbycomposex: 'True' + name: tableC + createdbycomposex: 'True' Services: app03: Access: RW @@ -101,8 +101,8 @@ x-dynamodb: tableD: Lookup: Tags: - - name: tableC - - createdbycomposex: 'False' + name: tableC + createdbycomposex: 'False' Services: app03: Access: RW