From 3b954e36c4fa3d5eea5a4afa4dcac060d590e3de Mon Sep 17 00:00:00 2001 From: Saravanan Date: Wed, 26 Aug 2020 17:51:32 +0530 Subject: [PATCH] feat: added extension class EfsVolumeConfiguration, TaskDefinition, Volume --- .../deployment/service_template_generator.py | 21 +++++++++- cloudlift/extensions/__init__.py | 0 cloudlift/extensions/troposphere/__init__.py | 0 cloudlift/extensions/troposphere/ecs.py | 41 +++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 cloudlift/extensions/__init__.py create mode 100644 cloudlift/extensions/troposphere/__init__.py create mode 100644 cloudlift/extensions/troposphere/ecs.py diff --git a/cloudlift/deployment/service_template_generator.py b/cloudlift/deployment/service_template_generator.py index ac31d725..35991f58 100644 --- a/cloudlift/deployment/service_template_generator.py +++ b/cloudlift/deployment/service_template_generator.py @@ -13,13 +13,14 @@ DeploymentConfiguration, Environment, LoadBalancer, LogConfiguration, NetworkConfiguration, PlacementStrategy, - PortMapping, Service, TaskDefinition) + PortMapping, Service, MountPoint) from troposphere.elasticloadbalancingv2 import Action, Certificate, Listener from troposphere.elasticloadbalancingv2 import LoadBalancer as ALBLoadBalancer from troposphere.elasticloadbalancingv2 import (Matcher, RedirectConfig, TargetGroup, TargetGroupAttribute) from troposphere.iam import Role +from cloudlift.extensions.troposphere.ecs import TaskDefinition, Volume, EfsVolumeConfiguration from cloudlift.config import region as region_service from cloudlift.config import get_account_id @@ -181,6 +182,13 @@ def _add_service(self, service_name, config): if config['command'] is not None: container_definition_arguments['Command'] = [config['command']] + if 'mountPoints' in config: + mountPoints = [] + for mountPoint in config['mountPoints']: + mountPoints.append(MountPoint(ContainerPath=mountPoint['containerPath'], + SourceVolume=mountPoint['sourceVolume'])) + container_definition_arguments['MountPoints'] = mountPoints + cd = ContainerDefinition(**container_definition_arguments) task_role = self.template.add_resource(Role( @@ -205,11 +213,19 @@ def _add_service(self, service_name, config): 'Cpu': str(config['fargate']['cpu']), 'Memory': str(config['fargate']['memory']) } + volumes = [] + if 'volumes' in config: + for volume in config['volumes']: + volumes.append(Volume(Name=volume['name'], + EfsVolumeConfiguration=EfsVolumeConfiguration( + FileSystemId=volume['efsFileSystemId'], + TransitEncryption='ENABLED'))) td = TaskDefinition( service_name + "TaskDefinition", Family=service_name + "Family", ContainerDefinitions=[cd], + Volumes=volumes, TaskRoleArn=Ref(task_role), **launch_type_td ) @@ -398,7 +414,8 @@ def _add_alb(self, cd, service_name, config, launch_type): self.template.add_resource(alb) target_group_name = "TargetGroup" + service_name - health_check_path = config['http_interface']['health_check_path'] if 'health_check_path' in config['http_interface'] else "/elb-check" + health_check_path = config['http_interface']['health_check_path'] if 'health_check_path' in config[ + 'http_interface'] else "/elb-check" if config['http_interface']['internal']: target_group_name = target_group_name + 'Internal' diff --git a/cloudlift/extensions/__init__.py b/cloudlift/extensions/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/cloudlift/extensions/troposphere/__init__.py b/cloudlift/extensions/troposphere/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/cloudlift/extensions/troposphere/ecs.py b/cloudlift/extensions/troposphere/ecs.py new file mode 100644 index 00000000..b5ff85f6 --- /dev/null +++ b/cloudlift/extensions/troposphere/ecs.py @@ -0,0 +1,41 @@ +from troposphere import AWSObject, Tags, AWSProperty +from troposphere.ecs import ContainerDefinition, InferenceAccelerator, PlacementConstraint, ProxyConfiguration, \ + DockerVolumeConfiguration, Host + + +class EfsVolumeConfiguration(AWSProperty): + props = { + 'FileSystemId': (str, True), + 'TransitEncryption': (str, True) + } + + +class Volume(AWSProperty): + props = { + 'DockerVolumeConfiguration': (DockerVolumeConfiguration, False), + 'EfsVolumeConfiguration': (EfsVolumeConfiguration, False), + 'Name': (str, True), + 'Host': (Host, False), + } + + +class TaskDefinition(AWSObject): + resource_type = "AWS::ECS::TaskDefinition" + + props = { + 'ContainerDefinitions': ([ContainerDefinition], False), + 'Cpu': (str, False), + 'ExecutionRoleArn': (str, False), + 'Family': (str, False), + 'InferenceAccelerators': ([InferenceAccelerator], False), + 'IpcMode': (str, False), + 'Memory': (str, False), + 'NetworkMode': (str, False), + 'PidMode': (str, False), + 'PlacementConstraints': ([PlacementConstraint], False), + 'ProxyConfiguration': (ProxyConfiguration, False), + 'RequiresCompatibilities': ([str], False), + 'Tags': (Tags, False), + 'TaskRoleArn': (str, False), + 'Volumes': ([Volume], False), + }