Skip to content

Commit 375f424

Browse files
committed
Added support for post commands
1 parent e7422d6 commit 375f424

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

docker-compose.override.yaml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ services:
2323
AWS_CONTAINER_CREDENTIALS_RELATIVE_URI: "/creds"
2424
ECS_CONTAINER_METADATA_URI: http://169.254.170.2/v3/containers/files-sidecar
2525
AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION:-eu-west-1}
26+
context: plain
2627
ECS_CONFIG_CONTENT: |
2728
2829
files:
@@ -42,9 +43,6 @@ services:
4243
source:
4344
Ssm:
4445
ParameterName: /cicd/shared/kms/arn
45-
commands:
46-
post:
47-
- file /opt/files/ssm.txt
4846
4947
/opt/files/secret.txt:
5048
source:
@@ -58,18 +56,36 @@ services:
5856
5957
/opt/files/test_ecs_properties.yaml:
6058
content: |
61-
{{ ecs_container_metadata() | to_yaml }}
59+
{{ ecs_container_metadata() | to_yaml | safe }}
6260
context: jinja2
6361
/opt/files/test_ecs_properties.json:
6462
content: |
65-
{{ ecs_task_metadata() | tojson }}
63+
{{ ecs_task_metadata() | to_json | safe }}
6664
# HELLO
6765
{{ ecs_container_metadata('ImageID')}}
6866
context: jinja2
69-
67+
ignore_if_failed: true
68+
commands:
69+
post:
70+
- ls /opt/files
71+
- cat /opt/files/test_ecs_properties.json
72+
- chown 1000:1000 -R /opt/files
7073
depends_on:
7174
- ecs-local-endpoints
7275

76+
confirm:
77+
image: public.ecr.aws/amazonlinux/amazonlinux:2
78+
container_name: testing
79+
volumes:
80+
- localshared:/opt/
81+
command:
82+
- ls
83+
- -l
84+
- /opt
85+
depends_on:
86+
- files-sidecar
87+
88+
7389
volumes:
7490
localshared:
7591
driver: local

ecs_files_composer/files_mgmt.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def handler(self, iam_override=None, session_override=None):
7272
self.write_content(is_template=False)
7373
self.set_unix_settings()
7474
if self.commands and self.commands.post:
75-
warnings.warn("Commands are not yet implemented", Warning)
75+
self.exec_post_commands()
7676

7777
def handle_sources(self, iam_override=None, session_override=None):
7878
"""
@@ -222,6 +222,28 @@ def set_unix_settings(self):
222222
else:
223223
raise
224224

225+
def exec_post_commands(self):
226+
227+
commands = self.commands.post.__root__
228+
for command in commands:
229+
cmd = command
230+
if isinstance(command, str):
231+
cmd = command.split(" ")
232+
LOG.info(f"{self.path} - {cmd}")
233+
try:
234+
res = subprocess.run(
235+
cmd,
236+
universal_newlines=True,
237+
stdout=subprocess.PIPE,
238+
stderr=subprocess.PIPE,
239+
shell=False,
240+
)
241+
except subprocess.CalledProcessError:
242+
if self.ignore_if_failed:
243+
LOG.error(res.stderr)
244+
else:
245+
raise
246+
225247
def write_content(self, is_template=True, as_bytes=False, bytes_content=None):
226248
"""
227249
Function to write the content retrieved to path.

0 commit comments

Comments
 (0)