Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/service multi families assignment #701

Merged
merged 8 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ecs_composex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

__author__ = """John Preston"""
__email__ = "[email protected]"
__version__ = "0.23.27-rc1"
__version__ = "0.23.27-rc2"
8 changes: 8 additions & 0 deletions ecs_composex/common/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ def disable_rollback(self) -> bool:
def ecs_cluster(self) -> EcsCluster:
return self._ecs_cluster

@property
def family_names(self) -> list[str]:
return [_family.name for _family in self.families.values()]

@property
def service_names(self) -> list[str]:
return [_service.name for _service in self.services]

@ecs_cluster.setter
def ecs_cluster(self, value: EcsCluster) -> None:
self._ecs_cluster = value
Expand Down
86 changes: 58 additions & 28 deletions ecs_composex/compose/x_resources/services_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
if TYPE_CHECKING:
from ecs_composex.common.settings import ComposeXSettings
from ecs_composex.mods_manager import XResourceModule
from ecs_composex.compose.compose_services import ComposeService

from compose_x_common.compose_x_common import keyisset, set_else_none

Expand All @@ -33,7 +34,7 @@ def __init__(
settings: ComposeXSettings,
):
self.services = []
self.families_targets = []
self.families_targets: list = []
self.families_scaling = []
self.arn_parameter = None
super().__init__(name, definition, module, settings)
Expand All @@ -52,27 +53,50 @@ def debug_families_targets(self):
else:
LOG.debug(f"Applies to all services of {family[0].name}")

def handle_families_targets_expansion(self, service, settings):
@staticmethod
def define_service_to_associate(
service_name: str, family_name: str, settings: ComposeXSettings
) -> ComposeService:
for _f_service in settings.families[family_name].services:
if _f_service.name == service_name:
_associated_service = _f_service
break
else:
raise AttributeError(
"Family {} does not have a service named {}: {}".format(
family_name,
service_name,
[svc.name for svc in settings.families[family_name].services],
)
)
return _associated_service

def handle_families_targets_expansion_list(
self, service_name: str, service_def, settings: ComposeXSettings
):
"""
Method to list all families and services that are targets of the resource.
Allows to implement family and service level association to resource

:param dict service: Service definition in compose file
:param ecs_composex.common.settings.ComposeXSettings settings: Execution settings
"""
name_key = get_setting_key("name", service)
access_key = get_setting_key("access", service)
the_service = [s for s in settings.services if s.name == service[name_key]][0]
name_key = get_setting_key("name", service_def)
access_key = get_setting_key("access", service_def)
the_service = [s for s in settings.services if s.name == service_def[name_key]][
0
]
for family_name in the_service.families:
family_name = NONALPHANUM.sub("", family_name)
if family_name not in [f[0].name for f in self.families_targets]:
self.families_targets.append(
(
settings.families[family_name],
False,
[the_service],
service[access_key],
service,
[
self.define_service_to_associate(
service_name, family_name, settings
)
],
set_else_none(service_def[access_key], service_def, {}),
service_def,
)
)

Expand Down Expand Up @@ -103,18 +127,16 @@ def set_services_targets_from_list(self, settings):
f"{self.module.res_key}.{self.name} - Family {service_name} has already been added. Skipping"
)
elif service_name in [s.name for s in settings.services]:
self.handle_families_targets_expansion(service, settings)
self.handle_families_targets_expansion_list(
service_name, service, settings
)

def handle_families_targets_expansion_dict(
self, service_name, service, settings
self, service_name: str, service_def: dict, settings: ComposeXSettings
) -> None:
"""
Method to list all families and services that are targets of the resource.
Allows to implement family and service level association to resource

:param str service_name:
:param dict service: Service definition in compose file
:param ecs_composex.common.settings.ComposeXSettings settings: Execution settings
"""
for svc in settings.services:
if svc.name == service_name:
Expand All @@ -132,20 +154,24 @@ def handle_families_targets_expansion_dict(
(
settings.families[family_name],
False,
[the_service],
service["Access"] if keyisset("Access", service) else {},
service,
[
self.define_service_to_associate(
service_name, family_name, settings
)
],
set_else_none("Access", service_def, {}),
service_def,
)
)

def set_services_targets_from_dict(self, settings):
def set_services_targets_from_dict(self, settings: ComposeXSettings):
"""
Deals with services set as a dict
"""
for service_name, service_def in self.services.items():
if service_name in [
family.name for family in settings.families.values()
] and service_name not in [tgt[0].name for tgt in self.families_targets]:
if service_name in settings.family_names and service_name not in [
tgt[0].name for tgt in self.families_targets
]:
for family in settings.families.values():
if family.name == service_name:
break
Expand All @@ -156,9 +182,7 @@ def set_services_targets_from_dict(self, settings):
family,
True,
family.services,
service_def["Access"]
if keyisset("Access", service_def)
else {},
set_else_none("Access", service_def, {}),
service_def,
)
)
Expand All @@ -168,7 +192,7 @@ def set_services_targets_from_dict(self, settings):
LOG.debug(
f"{self.module.res_key}.{self.name} - Family {service_name} has already been added. Skipping"
)
elif service_name in [s.name for s in settings.services]:
elif service_name in settings.service_names:
self.handle_families_targets_expansion_dict(
service_name, service_def, settings
)
Expand All @@ -186,6 +210,12 @@ def set_services_targets(self, settings):
LOG.debug(f"{self.module.res_key}.{self.name} No Services defined.")
return
if isinstance(self.services, list):
from warnings import warn

warn(
"Services list will be deprecated in future versions. Use Services objects instead.",
DeprecationWarning,
)
self.set_services_targets_from_list(settings)
elif isinstance(self.services, dict):
self.set_services_targets_from_dict(settings)
Expand Down
72 changes: 28 additions & 44 deletions ecs_composex/events/events_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
from ecs_composex.common.logging import LOG
from ecs_composex.common.stacks import ComposeXStack
from ecs_composex.common.troposphere_tools import build_template
from ecs_composex.compose.x_resources.helpers import (
set_lookup_resources,
set_new_resources,
set_resources,
)
from ecs_composex.compose.x_resources.services_resources import ServicesXResource
from ecs_composex.ecs.ecs_params import CLUSTER_NAME, FARGATE_VERSION

Expand All @@ -34,36 +29,37 @@ class Rule(ServicesXResource):
Class to define an Event Rule
"""

def handle_families_targets_expansion(self, service, settings):
def handle_families_targets_expansion_list(
self, service_name: str, service_def: dict, settings: ComposeXSettings
):
"""
Method to list all families and services that are targets of the resource.
Allows to implement family and service level association to resource

:param dict service: Service definition in compose file
:param ecs_composex.common.settings.ComposeXSettings settings: Execution settings
"""
the_service = [s for s in settings.services if s.name == service["name"]][0]
the_service = [s for s in settings.services if s.name == service_def["name"]][0]
for family_name in the_service.families:
family_name = NONALPHANUM.sub("", family_name)
if family_name not in [f[0].name for f in self.families_targets]:
self.families_targets.append(
(
settings.families[family_name],
False,
[the_service],
service["TaskCount"],
service,
[
self.define_service_to_associate(
service_name, family_name, settings
)
],
service_def["TaskCount"],
service_def,
)
)

def set_services_targets_from_list(self, settings):
def set_services_targets_from_list(self, settings: ComposeXSettings):
"""
Override method to map services and families targets of the services defined specifically for
events
TargetStructure:
(family, family_wide, services[], access)

:param ecs_composex.common.settings.ComposeXSettings settings:
:return:
"""
if not self.services:
Expand All @@ -90,18 +86,16 @@ def set_services_targets_from_list(self, settings):
f"The family {service_name} has already been added. Skipping"
)
elif service_name in [s.name for s in settings.services]:
self.handle_families_targets_expansion(service, settings)
self.handle_families_targets_expansion_list(
service_name, service, settings
)

def handle_families_targets_expansion_dict(
self, service_name, service, settings
self, service_name: str, service_def: dict, settings: ComposeXSettings
) -> None:
"""
Method to list all families and services that are targets of the resource.
Allows to implement family and service level association to resource

:param str service_name:
:param dict service: Service definition in compose file
:param ecs_composex.common.settings.ComposeXSettings settings: Execution settings
"""
the_service = [s for s in settings.services if s.name == service_name][0]
for family_name in the_service.families:
Expand All @@ -111,19 +105,18 @@ def handle_families_targets_expansion_dict(
(
settings.families[family_name],
False,
[the_service],
service["TaskCount"],
service,
[
self.define_service_to_associate(
service_name, family_name, settings
)
],
service_def["TaskCount"],
service_def,
)
)

def set_services_targets_from_dict(self, settings):
"""
Deals with services set as a dict

:param settings:
:return:
"""
def set_services_targets_from_dict(self, settings: ComposeXSettings) -> None:
"""Deals with services set as a dict"""
for service_name, service_def in self.services.items():
if service_name in settings.families and service_name not in [
f[0].name for f in self.families_targets
Expand Down Expand Up @@ -155,23 +148,17 @@ class XStack(ComposeXStack):
"""

def __init__(
self, title, settings: ComposeXSettings, module: XResourceModule, **kwargs
self, title: str, settings: ComposeXSettings, module: XResourceModule, **kwargs
):
"""
Method to initialize the XStack for Events

:param str title: title for the stack
:param ecs_composex.common.settings.ComposeXSettings settings: Execution settings
:param dict kwargs:
"""
"""Initialize the XStack for Events"""
if module.lookup_resources:
warnings.warn(
f"{module.res_key} does not support Lookup/Use. You can only create new resources"
)

if module.new_resources:
stack_template = build_template(
"Events rules for ComposeX",
"Events rules for Compose-X",
[CLUSTER_NAME, FARGATE_VERSION],
)
super().__init__(title, stack_template, **kwargs)
Expand All @@ -181,6 +168,3 @@ def __init__(

else:
self.is_void = True

# for resource in module.resources_list:
# resource.stack = self
1 change: 0 additions & 1 deletion ecs_composex/resource_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ def map_resource_env_vars_to_family_service_environment(
:param tuple target:
:param XResource resource:
"""

for svc in target[2]:
if svc in target[0].managed_sidecars:
continue
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ecs_composex"
version = "0.23.27-rc1"
version = "0.23.27-rc2"
description = "Manage, Configure and Deploy your services and AWS services and applications from your docker-compose definition"
authors = ["John Preston <[email protected]>"]
maintainers = ["John Preston <[email protected]>"]
Expand Down Expand Up @@ -82,7 +82,7 @@ sphinx-material = "^0.0.35"
github_url = "https://github.com/compose-x/ecs_composex"

[tool.tbump.version]
current = "0.23.27-rc1"
current = "0.23.27-rc2"
regex = '''
(?P<major>\d+)
\.
Expand Down