Skip to content

Commit

Permalink
Add common cloudwatch agent module
Browse files Browse the repository at this point in the history
  • Loading branch information
hgreebe committed Nov 14, 2023
1 parent 21655e8 commit 4d8c9a0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand All @@ -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' }
Expand Down

0 comments on commit 4d8c9a0

Please sign in to comment.