Skip to content

Commit 969b7af

Browse files
committed
Autoformat files under cloudwatch_agent
Signed-off-by: chenwany <[email protected]>
1 parent 89b3490 commit 969b7af

File tree

3 files changed

+41
-56
lines changed

3 files changed

+41
-56
lines changed

cookbooks/aws-parallelcluster-config/files/default/cloudwatch_agent/cloudwatch_log_configs_util.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import os
1919
import shutil
2020
import sys
21-
import jsonschema
2221

22+
import jsonschema
2323

2424
DEFAULT_SCHEMA_PATH = os.path.realpath(os.path.join(os.path.curdir, "cloudwatch_log_files_schema.json"))
2525
SCHEMA_PATH = os.environ.get("CW_LOGS_CONFIGS_SCHEMA_PATH", DEFAULT_SCHEMA_PATH)
@@ -37,15 +37,13 @@ def parse_args():
3737
"""Parse command line args."""
3838
parser = argparse.ArgumentParser(
3939
description="Validate of add new CloudWatch log configs.",
40-
epilog="If neither --input-json nor --input-file are used, this script will validate the existing config."
40+
epilog="If neither --input-json nor --input-file are used, this script will validate the existing config.",
4141
)
4242
add_group = parser.add_mutually_exclusive_group()
4343
add_group.add_argument(
4444
"--input-file", type=argparse.FileType("r"), help="Path to file containing configs for log files to add."
4545
)
46-
add_group.add_argument(
47-
"--input-json", type=json.loads, help="String containing configs for log files to add."
48-
)
46+
add_group.add_argument("--input-json", type=json.loads, help="String containing configs for log files to add.")
4947
return parser.parse_args()
5048

5149

@@ -97,12 +95,11 @@ def _validate_timestamp_keys(input_json):
9795
if log_config.get("timestamp_format_key") not in valid_keys:
9896
_fail(
9997
"Log config with log_stream_name {log_stream_name} and file_path {file_path} contains an invalid "
100-
"timestamp_format_key: {timestamp_format_key}. Valid values are {valid_keys}"
101-
.format(
98+
"timestamp_format_key: {timestamp_format_key}. Valid values are {valid_keys}".format(
10299
log_stream_name=log_config.get("log_stream_name"),
103100
file_path=log_config.get("file_path"),
104101
timestamp_format_key=log_config.get("timestamp_format_key"),
105-
valid_keys=", ".join(valid_keys)
102+
valid_keys=", ".join(valid_keys),
106103
)
107104
)
108105

cookbooks/aws-parallelcluster-config/files/default/cloudwatch_agent/write_cloudwatch_agent_json.py

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,64 +13,55 @@
1313

1414
import yaml
1515

16-
AWS_CLOUDWATCH_CFG_PATH = '/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json'
16+
AWS_CLOUDWATCH_CFG_PATH = "/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json"
1717

1818

1919
def parse_args():
2020
"""Parse CL args and return an argparse.Namespace."""
21-
parser = argparse.ArgumentParser(
22-
description='Create the cloudwatch agent config file'
21+
parser = argparse.ArgumentParser(description="Create the cloudwatch agent config file")
22+
parser.add_argument("--config", help="Path to JSON file describing logs that should be monitored", required=True)
23+
parser.add_argument(
24+
"--platform", help="OS family of this instance", choices=["amazon", "centos", "ubuntu"], required=True
25+
)
26+
parser.add_argument("--log-group", help="Name of the log group", required=True)
27+
parser.add_argument(
28+
"--node-role",
29+
required=True,
30+
choices=["HeadNode", "ComputeFleet"],
31+
help="Role this node plays in the cluster " "(i.e., is it a compute node or the head node?)",
32+
)
33+
parser.add_argument("--scheduler", required=True, choices=["slurm", "awsbatch", "plugin"], help="Scheduler")
34+
parser.add_argument(
35+
"--cluster-config-path",
36+
required=False,
37+
help="Cluster configuration path",
2338
)
24-
parser.add_argument('--config',
25-
help='Path to JSON file describing logs that should be monitored',
26-
required=True)
27-
parser.add_argument('--platform',
28-
help='OS family of this instance',
29-
choices=['amazon', 'centos', 'ubuntu'],
30-
required=True)
31-
parser.add_argument('--log-group',
32-
help='Name of the log group',
33-
required=True)
34-
parser.add_argument('--node-role',
35-
required=True,
36-
choices=['HeadNode', 'ComputeFleet'],
37-
help='Role this node plays in the cluster '
38-
'(i.e., is it a compute node or the head node?)')
39-
parser.add_argument('--scheduler',
40-
required=True,
41-
choices=['slurm', 'awsbatch', 'plugin'],
42-
help='Scheduler')
43-
parser.add_argument('--cluster-config-path',
44-
required=False,
45-
help='Cluster configuration path',
46-
)
4739
return parser.parse_args()
4840

4941

5042
def gethostname():
5143
"""Return hostname of this instance."""
52-
return socket.gethostname().split('.')[0]
44+
return socket.gethostname().split(".")[0]
5345

5446

5547
def write_config(config):
5648
"""Write config to AWS_CLOUDWATCH_CFG_PATH."""
57-
with open(AWS_CLOUDWATCH_CFG_PATH, 'w+') as output_config_file:
49+
with open(AWS_CLOUDWATCH_CFG_PATH, "w+") as output_config_file:
5850
json.dump(config, output_config_file, indent=4)
5951

6052

6153
def add_log_group_name_params(log_group_name, configs):
6254
"""Add a "log_group_name": log_group_name to every config."""
6355
for config in configs:
64-
config.update({'log_group_name': log_group_name})
56+
config.update({"log_group_name": log_group_name})
6557
return configs
6658

6759

6860
def add_instance_log_stream_prefixes(configs):
6961
"""Prefix all log_stream_name fields with instance identifiers."""
7062
for config in configs:
71-
config['log_stream_name'] = '{host}.{{instance_id}}.{log_stream_name}'.format(
72-
host=gethostname(),
73-
log_stream_name=config['log_stream_name']
63+
config["log_stream_name"] = "{host}.{{instance_id}}.{log_stream_name}".format(
64+
host=gethostname(), log_stream_name=config["log_stream_name"]
7465
)
7566
return configs
7667

@@ -83,17 +74,17 @@ def read_data(config_path):
8374

8475
def select_configs_for_scheduler(configs, scheduler):
8576
"""Filter out from configs those entries whose 'schedulers' list does not contain scheduler."""
86-
return [config for config in configs if scheduler in config['schedulers']]
77+
return [config for config in configs if scheduler in config["schedulers"]]
8778

8879

8980
def select_configs_for_node_role(configs, node_role):
9081
"""Filter out from configs those entries whose 'node_roles' list does not contain node_role."""
91-
return [config for config in configs if node_role in config['node_roles']]
82+
return [config for config in configs if node_role in config["node_roles"]]
9283

9384

9485
def select_configs_for_platform(configs, platform):
9586
"""Filter out from configs those entries whose 'platforms' list does not contain platform."""
96-
return [config for config in configs if platform in config['platforms']]
87+
return [config for config in configs if platform in config["platforms"]]
9788

9889

9990
def get_node_info():
@@ -151,8 +142,8 @@ def add_scheduler_plugin_log(config_data, cluster_config_path):
151142
"""Add custom log files to config data if log files specified in scheduler plugin."""
152143
cluster_config = load_config(cluster_config_path)
153144
if (
154-
get_dict_value(cluster_config, "Scheduling.SchedulerSettings.SchedulerDefinition.Monitoring.Logs.Files")
155-
and get_dict_value(cluster_config, "Scheduling.Scheduler") == "plugin"
145+
get_dict_value(cluster_config, "Scheduling.SchedulerSettings.SchedulerDefinition.Monitoring.Logs.Files")
146+
and get_dict_value(cluster_config, "Scheduling.Scheduler") == "plugin"
156147
):
157148
log_files = get_dict_value(
158149
cluster_config, "Scheduling.SchedulerSettings.SchedulerDefinition.Monitoring.Logs.Files"
@@ -178,26 +169,22 @@ def add_scheduler_plugin_log(config_data, cluster_config_path):
178169
def add_timestamps(configs, timestamps_dict):
179170
"""For each config, set its timestamp_format field based on its timestamp_format_key field."""
180171
for config in configs:
181-
config['timestamp_format'] = timestamps_dict[config['timestamp_format_key']]
172+
config["timestamp_format"] = timestamps_dict[config["timestamp_format_key"]]
182173
return configs
183174

184175

185176
def filter_output_fields(configs):
186177
"""Remove fields that are not required by CloudWatch agent config file."""
187-
desired_keys = ['log_stream_name', 'file_path', 'timestamp_format', 'log_group_name']
178+
desired_keys = ["log_stream_name", "file_path", "timestamp_format", "log_group_name"]
188179
return [{desired_key: config[desired_key] for desired_key in desired_keys} for config in configs]
189180

190181

191182
def create_config(log_configs):
192183
"""Return a dict representing the structure of the output JSON."""
193184
return {
194185
"logs": {
195-
"logs_collected": {
196-
"files": {
197-
"collect_list": log_configs
198-
}
199-
},
200-
"log_stream_name": "{host}.{{instance_id}}.default-log-stream".format(host=gethostname())
186+
"logs_collected": {"files": {"collect_list": log_configs}},
187+
"log_stream_name": "{host}.{{instance_id}}.default-log-stream".format(host=gethostname()),
201188
}
202189
}
203190

@@ -217,13 +204,13 @@ def main():
217204
config_data = read_data(args.config)
218205
if args.cluster_config_path:
219206
config_data = add_scheduler_plugin_log(config_data, args.cluster_config_path)
220-
log_configs = select_logs(config_data['log_configs'], args)
221-
log_configs = add_timestamps(log_configs, config_data['timestamp_formats'])
207+
log_configs = select_logs(config_data["log_configs"], args)
208+
log_configs = add_timestamps(log_configs, config_data["timestamp_formats"])
222209
log_configs = add_log_group_name_params(args.log_group, log_configs)
223210
log_configs = add_instance_log_stream_prefixes(log_configs)
224211
log_configs = filter_output_fields(log_configs)
225212
write_config(create_config(log_configs))
226213

227214

228-
if __name__ == '__main__':
215+
if __name__ == "__main__":
229216
main()

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ commands =
1616
[vars]
1717
code_dirs =
1818
cookbooks/aws-parallelcluster-config/files/default/dcv/ \
19+
cookbooks/aws-parallelcluster-config/files/default/cloudwatch_agent/ \
1920
cookbooks/aws-parallelcluster-slurm/files/default/head_node_slurm/slurm/ \
2021
test/unit/
2122

0 commit comments

Comments
 (0)