-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b882c7
commit 84e4ce1
Showing
10 changed files
with
328 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,29 @@ | ||
# SPDX-License-Identifier: MPL-2.0 | ||
# Copyright 2020-2022 John Mille <[email protected]> | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Union | ||
|
||
if TYPE_CHECKING: | ||
from ecs_composex.common.settings import ComposeXSettings | ||
from ecs_composex.ecs.ecs_family import ComposeFamily | ||
from ecs_composex.efs.efs_stack import Efs | ||
|
||
from compose_x_common.compose_x_common import keyisset | ||
from troposphere import GetAtt, Ref | ||
from troposphere.ecs import AuthorizationConfig, EFSVolumeConfiguration, Volume | ||
from troposphere.efs import AccessPoint, CreationInfo, PosixUser, RootDirectory | ||
from troposphere.iam import PolicyType | ||
|
||
from ecs_composex.common.logging import LOG | ||
from ecs_composex.common.troposphere_tools import add_parameters | ||
from ecs_composex.common.troposphere_tools import add_parameters, add_resource | ||
from ecs_composex.ecs.ecs_params import TASK_T | ||
from ecs_composex.efs.efs_params import FS_ARN, FS_ID, FS_MNT_PT_SG_ID, FS_PORT | ||
from ecs_composex.rds_resources_settings import handle_new_tcp_resource | ||
from ecs_composex.rds_resources_settings import ( | ||
add_security_group_ingress, | ||
handle_new_tcp_resource, | ||
) | ||
|
||
|
||
def get_volumes(task_definition): | ||
|
@@ -86,7 +98,7 @@ def add_task_iam_access_to_access_point(family, access_points, efs): | |
}, | ||
Roles=[family.iam_manager.task_role.name], | ||
) | ||
family.template.add_resource(policy) | ||
add_resource(family.template, policy) | ||
|
||
|
||
def add_efs_definition_to_target_family(new_efs, target): | ||
|
@@ -137,7 +149,7 @@ def override_service_volume(new_efs, fs_id, target, access_points, volumes): | |
Path=mount_pt.ContainerPath, | ||
), | ||
) | ||
target[0].template.add_resource(sub_service_specific_access_point) | ||
add_resource(target[0].template, sub_service_specific_access_point) | ||
access_points.append(sub_service_specific_access_point) | ||
volumes.append( | ||
Volume( | ||
|
@@ -212,40 +224,56 @@ def override_efs_settings(new_efs, target, fs_id_parameter, access_points, volum | |
) | ||
|
||
|
||
def expand_family_with_efs_volumes(efs_root_stack_title, new_efs, settings): | ||
def looked_up_efs_family_hook( | ||
efs: Efs, family: ComposeFamily, settings: ComposeXSettings | ||
) -> None: | ||
sg_id = efs.add_attribute_to_another_stack(family.stack, FS_MNT_PT_SG_ID, settings) | ||
add_parameters(family.template, [sg_id["ImportParameter"]]) | ||
add_security_group_ingress( | ||
family.stack, efs.logical_name, Ref(sg_id["ImportParameter"]), 2049 | ||
) | ||
family.stack.Parameters.update( | ||
{sg_id["ImportParameter"].title: sg_id["ImportValue"]} | ||
) | ||
|
||
|
||
def expand_family_with_efs_volumes( | ||
efs_root_stack_title: str, efs: Efs, settings: ComposeXSettings | ||
): | ||
""" | ||
Function to add the EFS Volume definition to the task definition for the service to use. | ||
:param efs_root_stack_title: Root stack title for EFS | ||
:param new_efs: | ||
:param ecs_composex.common.settings.ComposeXSettings settings: | ||
:return: | ||
""" | ||
fs_id_parameter = new_efs.attributes_outputs[FS_ID]["ImportParameter"] | ||
fs_id_getatt = new_efs.attributes_outputs[FS_ID]["ImportValue"] | ||
for target in new_efs.families_targets: | ||
if target[0].service_compute.launch_type == "EXTERNAL": | ||
fs_id_parameter = efs.attributes_outputs[FS_ID]["ImportParameter"] | ||
fs_id_getatt = efs.attributes_outputs[FS_ID]["ImportValue"] | ||
for target in efs.families_targets: | ||
family: ComposeFamily = target[0] | ||
if family.service_compute.launch_type == "EXTERNAL": | ||
LOG.warning( | ||
f"x-efs - {target[0].name} - When using EXTERNAL Launch Type, networking settings cannot be set." | ||
f"x-efs - {family.name} - When using EXTERNAL Launch Type, networking settings cannot be set." | ||
) | ||
return | ||
if efs.lookup: | ||
looked_up_efs_family_hook(efs, target[0], settings) | ||
access_points = [] | ||
target[0].stack.Parameters.update({fs_id_parameter.title: fs_id_getatt}) | ||
add_parameters(target[0].template, [fs_id_parameter]) | ||
task_definition = target[0].template.resources[TASK_T] | ||
family.stack.Parameters.update({fs_id_parameter.title: fs_id_getatt}) | ||
add_parameters(family.template, [fs_id_parameter]) | ||
task_definition = family.template.resources[TASK_T] | ||
efs_config_kwargs = {"FilesystemId": Ref(fs_id_parameter)} | ||
if ( | ||
new_efs.parameters | ||
and keyisset("EnforceIamAuth", new_efs.parameters) | ||
efs.parameters | ||
and keyisset("EnforceIamAuth", efs.parameters) | ||
or [service.user for service in target[2]] | ||
): | ||
add_efs_definition_to_target_family(new_efs, target) | ||
efs_access_point = target[0].template.add_resource( | ||
add_efs_definition_to_target_family(efs, target) | ||
efs_access_point = add_resource( | ||
family.template, | ||
AccessPoint( | ||
f"{new_efs.logical_name}{target[0].logical_name}EfsAccessPoint", | ||
f"{efs.logical_name}{family.logical_name}EfsAccessPoint", | ||
FileSystemId=Ref(fs_id_parameter), | ||
) | ||
), | ||
) | ||
if not efs_access_point: | ||
continue | ||
access_points.append(efs_access_point) | ||
efs_config_kwargs.update( | ||
{ | ||
|
@@ -257,24 +285,16 @@ def expand_family_with_efs_volumes(efs_root_stack_title, new_efs, settings): | |
) | ||
efs_volume_definition = Volume( | ||
EFSVolumeConfiguration=EFSVolumeConfiguration(**efs_config_kwargs), | ||
Name=new_efs.volume.volume_name, | ||
Name=efs.volume.volume_name, | ||
) | ||
volumes = get_volumes(task_definition) | ||
volumes.append(efs_volume_definition) | ||
override_efs_settings(new_efs, target, fs_id_parameter, access_points, volumes) | ||
add_task_iam_access_to_access_point(target[0], access_points, new_efs) | ||
override_efs_settings(efs, target, fs_id_parameter, access_points, volumes) | ||
add_task_iam_access_to_access_point(family, access_points, efs) | ||
|
||
|
||
def efs_to_ecs(resources, services_stack, res_root_stack, settings): | ||
""" | ||
Function to associate back the EFS FS to services. | ||
:param resources: | ||
:param services_stack: | ||
:param res_root_stack: | ||
:param ecs_composex.common.settings.ComposeXSettings settings: | ||
:return: | ||
""" | ||
"""Function to associate back the EFS FS to services.""" | ||
for resource_name, resource in resources.items(): | ||
LOG.info(f"{resource.module.res_key}.{resource_name} - Linking to services") | ||
if not resource.mappings and resource.cfn_resource: | ||
|
@@ -285,3 +305,5 @@ def efs_to_ecs(resources, services_stack, res_root_stack, settings): | |
settings=settings, | ||
) | ||
expand_family_with_efs_volumes(res_root_stack.title, resource, settings) | ||
else: | ||
expand_family_with_efs_volumes(None, resource, settings) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.