From c6210cdf0d9763a1959f560c64f4224688a010a1 Mon Sep 17 00:00:00 2001 From: "John \"Preston\" Mille" Date: Mon, 26 Aug 2024 15:35:22 +0100 Subject: [PATCH] Added optional --s3-prefix for render to indicate one wants to have the TemplateURL point to S3 --- ecs_composex/cli.py | 7 +++++++ ecs_composex/common/files.py | 10 +++++++++- ecs_composex/common/settings.py | 6 ++++++ ecs_composex/common/stacks/__init__.py | 10 ++++++++-- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/ecs_composex/cli.py b/ecs_composex/cli.py index f48a7027..fc259e01 100644 --- a/ecs_composex/cli.py +++ b/ecs_composex/cli.py @@ -120,6 +120,13 @@ def main_parser(): help="Bucket name to upload the templates to", dest="BucketName", ) + base_command_parser.add_argument( + "--s3-prefix", + type=str, + default="", + help="Path within the bucket in which to store the files. Use with caution, override possible", + dest="S3PrefixPath", + ) base_command_parser.add_argument( "--role-arn", dest=ComposeXSettings.arn_arg, diff --git a/ecs_composex/common/files.py b/ecs_composex/common/files.py index 9292a823..d757dde3 100644 --- a/ecs_composex/common/files.py +++ b/ecs_composex/common/files.py @@ -4,6 +4,14 @@ """ Functions to manage a template and wheter it should be stored in S3 """ + +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from ecs_composex.common.settings import ComposeXSettings + import pprint from os.path import abspath @@ -160,7 +168,7 @@ def __init__( def __repr__(self): return self.file_path - def upload(self, settings): + def upload(self, settings: ComposeXSettings): """ Method to handle uploading the files to S3. """ diff --git a/ecs_composex/common/settings.py b/ecs_composex/common/settings.py index 84c950ba..4edb8a0a 100644 --- a/ecs_composex/common/settings.py +++ b/ecs_composex/common/settings.py @@ -77,6 +77,7 @@ class ComposeXSettings: command_arg = "command" bucket_arg = "BucketName" + bucket_prefix_path_arg: str = "S3PrefixPath" input_file_arg = "DockerComposeXFile" output_dir_arg = "OutputDirectory" format_arg = "TemplateFormat" @@ -148,6 +149,11 @@ def __init__( self.bucket_name = ( None if not keyisset(self.bucket_arg, kwargs) else kwargs[self.bucket_arg] ) + self.bucket_prefix_path = set_else_none(self.bucket_prefix_path_arg, kwargs, "") + if self.bucket_prefix_path and self.bucket_prefix_path.startswith("/"): + self.bucket_prefix_path = self.bucket_prefix_path[1:] + if self.bucket_prefix_path.endswith("/"): + self.bucket_prefix_path = self.bucket_prefix_path[:-1] self.volumes = [] self.services = [] self.secrets = [] diff --git a/ecs_composex/common/stacks/__init__.py b/ecs_composex/common/stacks/__init__.py index 7baa33f1..aacda554 100644 --- a/ecs_composex/common/stacks/__init__.py +++ b/ecs_composex/common/stacks/__init__.py @@ -236,13 +236,13 @@ def render_parameters_list_cfn(self): ) return params - def render(self, settings): + def render(self, settings: ComposeXSettings): """ Function to use when the template is finalized and can be uploaded to S3. """ LOG.debug(f"Rendering {self.title}") self.DependsOn = list(set(self.DependsOn)) - template_file = FileArtifact( + template_file: FileArtifact = FileArtifact( file_name=self.file_name, template=self.stack_template, settings=settings, @@ -251,6 +251,12 @@ def render(self, settings): template_file.define_body() template_file.write(settings) setattr(self, "TemplateURL", template_file.file_path) + if not settings.upload and settings.bucket_name and settings.bucket_prefix_path: + setattr( + self, + "TemplateURL", + f"https://s3.amazonaws.com/{settings.bucket_name}/{settings.bucket_prefix_path}/{template_file.file_name}", + ) if settings.upload: template_file.upload(settings) setattr(self, "TemplateURL", template_file.url)