From e114fca7324aeadb3aa81274e13e84ba5d9e42f3 Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Wed, 11 Sep 2019 13:05:07 +1000 Subject: [PATCH] DCD-436: Make log group unique. --- group_vars/aws_node_local.yml | 8 ++ roles/aws_common/defaults/main.yml | 2 + .../aws_common/molecule/default/molecule.yml | 6 ++ .../aws_common/molecule/default/playbook.yml | 5 ++ .../molecule/default/tests/test_default.py | 8 ++ roles/aws_common/tasks/main.yml | 10 +++ .../templates/amazon-cloudwatch-agent.json.j2 | 88 +++++++++++++++++++ 7 files changed, 127 insertions(+) create mode 100644 roles/aws_common/templates/amazon-cloudwatch-agent.json.j2 diff --git a/group_vars/aws_node_local.yml b/group_vars/aws_node_local.yml index 6cb28f4..d6ff087 100644 --- a/group_vars/aws_node_local.yml +++ b/group_vars/aws_node_local.yml @@ -34,6 +34,14 @@ atl_product_installation_versioned: "{{ atl_product_installation_base }}/{{ atl_ atl_product_installation_current: "{{ atl_product_installation_base }}/current" atl_installer_temp: "{{ atl_installation_base }}/tmp" +atl_product_log_locations: + confluence: + - "{{ atl_product_installation_current }}/logs" + jira: + - "{{ atl_product_installation_current }}/logs" + - "{{ atl_product_home }}/logs" + stash: [] + crowd: [] # The following are imports from the environment. These are generally # set in /etc/atl by the CloudFormation template and sourced before diff --git a/roles/aws_common/defaults/main.yml b/roles/aws_common/defaults/main.yml index 8d5cef2..fbd36c7 100644 --- a/roles/aws_common/defaults/main.yml +++ b/roles/aws_common/defaults/main.yml @@ -6,3 +6,5 @@ aws_cloudwatch_agent_rpm: "https://s3.{{ aws_download_region }}.amazonaws.com/am # Mostly for molecule testing, as skip-tags doesn't work with handlers. atl_aws_agent_restart: true + +atl_aws_log_group: "{{ atl_product_edition }}-{{ atl_aws_stack_name }}" diff --git a/roles/aws_common/molecule/default/molecule.yml b/roles/aws_common/molecule/default/molecule.yml index e8b8bcf..9db2aa4 100644 --- a/roles/aws_common/molecule/default/molecule.yml +++ b/roles/aws_common/molecule/default/molecule.yml @@ -8,13 +8,19 @@ lint: platforms: - name: amazon_linux2 image: amazonlinux:2 + groups: + - aws_node_local # - name: ubuntu_lts # image: ubuntu:bionic provisioner: name: ansible lint: name: ansible-lint + inventory: + links: + group_vars: ../../../../group_vars/ verifier: name: testinfra lint: name: flake8 + enabled: false diff --git a/roles/aws_common/molecule/default/playbook.yml b/roles/aws_common/molecule/default/playbook.yml index e44015d..0eb5b02 100644 --- a/roles/aws_common/molecule/default/playbook.yml +++ b/roles/aws_common/molecule/default/playbook.yml @@ -6,6 +6,11 @@ ansible_default_ipv4: address: "9.9.9.9" ansible_ec2_instance_id: "NONE" + + atl_product_family: "jira" + atl_product_edition: "jira-software" + atl_aws_stack_name: "MY_STACK" + atl_aws_agent_restart: false roles: - role: aws_common diff --git a/roles/aws_common/molecule/default/tests/test_default.py b/roles/aws_common/molecule/default/tests/test_default.py index cf68ac4..53261bb 100644 --- a/roles/aws_common/molecule/default/tests/test_default.py +++ b/roles/aws_common/molecule/default/tests/test_default.py @@ -15,3 +15,11 @@ testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( ]) def test_package_exes(host, exe): assert host.file(exe).exists + + +def test_service_file(host): + f = host.file('/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json') + assert f.contains('"log_group_name": "jira-software-MY_STACK"') + assert f.user == 'root' + assert f.group == 'root' + assert f.mode == 0o0644 diff --git a/roles/aws_common/tasks/main.yml b/roles/aws_common/tasks/main.yml index 0f4b0cd..bb578c0 100644 --- a/roles/aws_common/tasks/main.yml +++ b/roles/aws_common/tasks/main.yml @@ -12,3 +12,13 @@ set_fact: atl_cluster_node_id: "{{ ansible_ec2_instance_id }}" atl_local_ipv4: "{{ ansible_ec2_local_ipv4 | default(ansible_default_ipv4.address) }}" + +- name: Generate CloudWatch config + template: + src: "amazon-cloudwatch-agent.json.j2" + dest: "/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json" + owner: root + group: root + mode: 0644 + notify: + - Restart CloudWatch Agent diff --git a/roles/aws_common/templates/amazon-cloudwatch-agent.json.j2 b/roles/aws_common/templates/amazon-cloudwatch-agent.json.j2 new file mode 100644 index 0000000..b33ff45 --- /dev/null +++ b/roles/aws_common/templates/amazon-cloudwatch-agent.json.j2 @@ -0,0 +1,88 @@ +{ + "agent": { + "metrics_collection_interval": 10, + "run_as_user": "root" + }, + + "logs": { + "logs_collected": { + "files": { + "collect_list": [ + {% set comma = joiner(',') %} + {% for path in atl_product_log_locations[atl_product_family] %} + {{ comma() }} + { + "file_path": "{{ path }}/*", + "log_group_name": "{{ atl_aws_log_group }}", + "log_stream_name": "{instance_id}" + } + {% endfor %} + ] + } + } + }, + + "metrics": { + "append_dimensions": { + "AutoScalingGroupName": "${aws:AutoScalingGroupName}", + "ImageId": "${aws:ImageId}", + "InstanceId": "${aws:InstanceId}", + "InstanceType": "${aws:InstanceType}" + }, + "metrics_collected": { + "cpu": { + "measurement": [ + "cpu_usage_idle", + "cpu_usage_iowait", + "cpu_usage_user", + "cpu_usage_system" + ], + "metrics_collection_interval": 10, + "totalcpu": false + }, + "disk": { + "measurement": [ + "used_percent", + "inodes_free" + ], + "metrics_collection_interval": 10, + "resources": [ + "*" + ] + }, + "diskio": { + "measurement": [ + "io_time", + "write_bytes", + "read_bytes", + "writes", + "reads" + ], + "metrics_collection_interval": 10, + "resources": [ + "*" + ] + }, + "mem": { + "measurement": [ + "mem_used_percent" + ], + "metrics_collection_interval": 10 + }, + "netstat": { + "measurement": [ + "tcp_established", + "tcp_time_wait" + ], + "metrics_collection_interval": 10 + }, + "swap": { + "measurement": [ + "swap_used_percent" + ], + "metrics_collection_interval": 10 + } + } + } + +}