From 4d8c9a0d39fb1bf4eddb784f0423dfa6fdbaec46 Mon Sep 17 00:00:00 2001 From: Helena Greebe Date: Tue, 14 Nov 2023 15:30:24 -0500 Subject: [PATCH] Add common cloudwatch agent module --- .../cloudwatch/cloudwatch_agent_common_utils.py | 17 +++++++++++++++++ .../cloudwatch/cloudwatch_agent_config_util.py | 17 ++--------------- .../cloudwatch/write_cloudwatch_agent_json.py | 17 ++--------------- .../test/controls/cloudwatch_spec.rb | 12 ++++++++++-- 4 files changed, 31 insertions(+), 32 deletions(-) create mode 100644 cookbooks/aws-parallelcluster-environment/files/cloudwatch/cloudwatch_agent_common_utils.py diff --git a/cookbooks/aws-parallelcluster-environment/files/cloudwatch/cloudwatch_agent_common_utils.py b/cookbooks/aws-parallelcluster-environment/files/cloudwatch/cloudwatch_agent_common_utils.py new file mode 100644 index 0000000000..be81ef142b --- /dev/null +++ b/cookbooks/aws-parallelcluster-environment/files/cloudwatch/cloudwatch_agent_common_utils.py @@ -0,0 +1,17 @@ +import os + +from jinja2 import FileSystemLoader +from jinja2.sandbox import SandboxedEnvironment + +CONFIG_ARGS = { + "default_platforms": ["amazon", "centos", "redhat", "rocky", "ubuntu"], +} + + +def render_jinja_template(template_file_path): + 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(**CONFIG_ARGS) + with open(template_file_path, "w", encoding="utf-8") as f: + f.write(rendered_template) + return template_file_path 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 d19e17ce3e..41ed03820f 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,8 +20,7 @@ import sys import jsonschema -from jinja2 import FileSystemLoader -from jinja2.sandbox import SandboxedEnvironment +from cloudwatch_agent_common_utils import render_jinja_template 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) @@ -49,15 +48,6 @@ 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: @@ -82,10 +72,7 @@ def _read_json_at(path): def _read_jinja_template_at(path): """Read the JSON file at path.""" try: - config_args = { - "default_platforms": ["amazon", "centos", "redhat", "rocky", "ubuntu"], - } - with open(render_jinja_template(path, **config_args), encoding="utf-8") as input_file: + with open(render_jinja_template(path), 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/files/cloudwatch/write_cloudwatch_agent_json.py b/cookbooks/aws-parallelcluster-environment/files/cloudwatch/write_cloudwatch_agent_json.py index 87e3d54b64..f14bd9997d 100644 --- a/cookbooks/aws-parallelcluster-environment/files/cloudwatch/write_cloudwatch_agent_json.py +++ b/cookbooks/aws-parallelcluster-environment/files/cloudwatch/write_cloudwatch_agent_json.py @@ -11,8 +11,7 @@ import os import socket -from jinja2 import FileSystemLoader -from jinja2.sandbox import SandboxedEnvironment +from cloudwatch_agent_common_utils import render_jinja_template AWS_CLOUDWATCH_CFG_PATH = "/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json" DEFAULT_METRICS_COLLECTION_INTERVAL = 60 @@ -64,21 +63,9 @@ def add_instance_log_stream_prefixes(configs): return configs -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 read_data(config_path): """Read in log configuration data from config_path.""" - config_args = { - "default_platforms": ["amazon", "centos", "redhat", "rocky", "ubuntu"], - } - with open(render_jinja_template(config_path, **config_args), encoding="utf-8") as infile: + with open(render_jinja_template(config_path), encoding="utf-8") as infile: return json.load(infile) diff --git a/cookbooks/aws-parallelcluster-environment/test/controls/cloudwatch_spec.rb b/cookbooks/aws-parallelcluster-environment/test/controls/cloudwatch_spec.rb index 7b1c9d5615..928216b7b6 100644 --- a/cookbooks/aws-parallelcluster-environment/test/controls/cloudwatch_spec.rb +++ b/cookbooks/aws-parallelcluster-environment/test/controls/cloudwatch_spec.rb @@ -40,7 +40,7 @@ describe file('/usr/local/bin/write_cloudwatch_agent_json.py') do it { should exist } - its('sha256sum') { should eq 'fb50eccd7b18db7c4b3212260b4f88a018f3ae9dd0ddf472bc476e0fff26c79d' } + its('sha256sum') { should eq 'e528db6e875ed28e1ed0caf5e2e4cb9542e744ce9ebfadafa594272542fe7ea2' } its('owner') { should eq 'root' } its('group') { should eq 'root' } its('mode') { should cmp '0755' } @@ -64,7 +64,15 @@ describe file('/usr/local/bin/cloudwatch_agent_config_util.py') do it { should exist } - its('sha256sum') { should eq '767896cff89142865397757f970cf73e7453982ffdc584bfadc788c77991e82b' } + its('sha256sum') { should eq '55125b14b8b5dba4b694b07f2ece008a6607ceb24ce0ae784a92affe34bd78bd' } + its('owner') { should eq 'root' } + its('group') { should eq 'root' } + its('mode') { should cmp '0644' } + end + + describe file('/usr/local/bin/cloudwatch_agent_config_util.py') do + it { should exist } + its('sha256sum') { should eq 'b65d53caf3d69f723324c4339f44cd8662a5c63ad8796118640738d7f6a63381' } its('owner') { should eq 'root' } its('group') { should eq 'root' } its('mode') { should cmp '0644' }