diff --git a/cookbooks/aws-parallelcluster-environment/files/cloudwatch/cloudwatch_agent_config_util.py b/cookbooks/aws-parallelcluster-environment/files/cloudwatch/cloudwatch_agent_config_util.py index c51093a53..80da1ddec 100644 --- a/cookbooks/aws-parallelcluster-environment/files/cloudwatch/cloudwatch_agent_config_util.py +++ b/cookbooks/aws-parallelcluster-environment/files/cloudwatch/cloudwatch_agent_config_util.py @@ -20,6 +20,8 @@ import sys import jsonschema +from jinja2 import FileSystemLoader +from jinja2.sandbox import SandboxedEnvironment DEFAULT_SCHEMA_PATH = os.path.realpath(os.path.join(os.path.curdir, "cloudwatch_agent_config_schema.json")) SCHEMA_PATH = os.environ.get("CW_LOGS_CONFIGS_SCHEMA_PATH", DEFAULT_SCHEMA_PATH) @@ -47,6 +49,15 @@ def parse_args(): return parser.parse_args() +def render_jinja_template(template_file_path, **kwargs): + file_loader = FileSystemLoader(str(os.path.dirname(template_file_path))) + env = SandboxedEnvironment(loader=file_loader) + rendered_template = env.get_template(os.path.basename(template_file_path)).render(**kwargs) + with open(template_file_path, "w", encoding="utf-8") as f: + f.write(rendered_template) + return template_file_path + + def get_input_json(args): """Either load the input JSON data from a file, or returned the JSON parsed on the CLI.""" if args.input_file: @@ -59,7 +70,10 @@ def get_input_json(args): def _read_json_at(path): """Read the JSON file at path.""" try: - with open(path, encoding="utf-8") as input_file: + config_args = { + "default_platforms": ["amazon", "centos", "redhat", "rocky", "ubuntu"], + } + with open(render_jinja_template(path, **config_args), encoding="utf-8") as input_file: return json.load(input_file) except FileNotFoundError: _fail(f"No file exists at {path}") diff --git a/cookbooks/aws-parallelcluster-environment/test/controls/cloudwatch_spec.rb b/cookbooks/aws-parallelcluster-environment/test/controls/cloudwatch_spec.rb index 01aef2eb2..89d500f9b 100644 --- a/cookbooks/aws-parallelcluster-environment/test/controls/cloudwatch_spec.rb +++ b/cookbooks/aws-parallelcluster-environment/test/controls/cloudwatch_spec.rb @@ -64,7 +64,7 @@ describe file('/usr/local/bin/cloudwatch_agent_config_util.py') do it { should exist } - its('sha256sum') { should eq 'b816b4891a5e8f1e7ac94616db7927f7955ba72a8f53ec1b320402a2ac9c9b7f' } + its('sha256sum') { should eq '321b1a5e4886588a6f5501e4b1fe00b506ecab54640e529cfb9f407ad03dd289' } its('owner') { should eq 'root' } its('group') { should eq 'root' } its('mode') { should cmp '0644' }