Skip to content

Commit

Permalink
Added support for post commands
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnPreston committed Sep 28, 2021
1 parent e7422d6 commit 375f424
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
28 changes: 22 additions & 6 deletions docker-compose.override.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ services:
AWS_CONTAINER_CREDENTIALS_RELATIVE_URI: "/creds"
ECS_CONTAINER_METADATA_URI: http://169.254.170.2/v3/containers/files-sidecar
AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION:-eu-west-1}
context: plain
ECS_CONFIG_CONTENT: |
files:
Expand All @@ -42,9 +43,6 @@ services:
source:
Ssm:
ParameterName: /cicd/shared/kms/arn
commands:
post:
- file /opt/files/ssm.txt
/opt/files/secret.txt:
source:
Expand All @@ -58,18 +56,36 @@ services:
/opt/files/test_ecs_properties.yaml:
content: |
{{ ecs_container_metadata() | to_yaml }}
{{ ecs_container_metadata() | to_yaml | safe }}
context: jinja2
/opt/files/test_ecs_properties.json:
content: |
{{ ecs_task_metadata() | tojson }}
{{ ecs_task_metadata() | to_json | safe }}
# HELLO
{{ ecs_container_metadata('ImageID')}}
context: jinja2
ignore_if_failed: true
commands:
post:
- ls /opt/files
- cat /opt/files/test_ecs_properties.json
- chown 1000:1000 -R /opt/files
depends_on:
- ecs-local-endpoints

confirm:
image: public.ecr.aws/amazonlinux/amazonlinux:2
container_name: testing
volumes:
- localshared:/opt/
command:
- ls
- -l
- /opt
depends_on:
- files-sidecar


volumes:
localshared:
driver: local
Expand Down
24 changes: 23 additions & 1 deletion ecs_files_composer/files_mgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def handler(self, iam_override=None, session_override=None):
self.write_content(is_template=False)
self.set_unix_settings()
if self.commands and self.commands.post:
warnings.warn("Commands are not yet implemented", Warning)
self.exec_post_commands()

def handle_sources(self, iam_override=None, session_override=None):
"""
Expand Down Expand Up @@ -222,6 +222,28 @@ def set_unix_settings(self):
else:
raise

def exec_post_commands(self):

commands = self.commands.post.__root__
for command in commands:
cmd = command
if isinstance(command, str):
cmd = command.split(" ")
LOG.info(f"{self.path} - {cmd}")
try:
res = subprocess.run(
cmd,
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=False,
)
except subprocess.CalledProcessError:
if self.ignore_if_failed:
LOG.error(res.stderr)
else:
raise

def write_content(self, is_template=True, as_bytes=False, bytes_content=None):
"""
Function to write the content retrieved to path.
Expand Down

0 comments on commit 375f424

Please sign in to comment.