Skip to content

Commit

Permalink
Added optional --s3-prefix for render to indicate one wants to have t…
Browse files Browse the repository at this point in the history
…he TemplateURL point to S3
  • Loading branch information
JohnPreston committed Aug 26, 2024
1 parent 4e86885 commit c6210cd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
7 changes: 7 additions & 0 deletions ecs_composex/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion ecs_composex/common/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
"""
Expand Down
6 changes: 6 additions & 0 deletions ecs_composex/common/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 = []
Expand Down
10 changes: 8 additions & 2 deletions ecs_composex/common/stacks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit c6210cd

Please sign in to comment.