Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
simplyadrian committed Apr 15, 2018
1 parent d0d0cd7 commit c3fc87f
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
Dockerfile.*
50 changes: 50 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
GIT_COMMIT=$(shell git rev-parse HEAD)
MY_DIR=$(shell basename $(CURDIR))
TEST_LABEL_KEY=ansible-role-testing
PLATFORM=ubuntu
define DOCKER_BODY
RUN apt-get update && apt-get install -y \\
gcc \\
git \\
python-dev \\
python-pip \\
libffi-dev \\
libssl-dev && \\
pip install -U distribute boto ansible==2.1.0 virtualenv awscli
# ansible 2.1.0 fail: https://github.com/ansible/ansible/issues/16015
ARG ANSIBLE_OPTIONS
ARG TEST_LABEL
ARG TEST_LABEL_KEY
ARG TEST_TAG
ARG GIT_COMMIT=unknown
LABEL $$TEST_LABEL_KEY=$$TEST_LABEL
LABEL git-commit=$$GIT_COMMIT
LABEL TEST_TAG=$$TEST_TAG
ADD tests /tmp/playbook
ADD . /tmp/playbook/roles/$$TEST_LABEL
WORKDIR /tmp/playbook
RUN ansible-playbook $$ANSIBLE_OPTIONS -i inventory test.yml -e environ=dev -e region=us-west-2 -e cw_alarms_action_arn=${CW_ALARMS_ACTION_ARN}
endef
export DOCKER_BODY

%4: TEST_TAG = 14.04
%6: TEST_TAG = 16.04

.PHONY: default
default: test16

testv%: ANSIBLE_OPTIONS = -v
test%:
( echo 'FROM ${PLATFORM}:${TEST_TAG}' ) > tests/Dockerfile.${PLATFORM}.${TEST_TAG}
echo "$$DOCKER_BODY" >> tests/Dockerfile.${PLATFORM}.${TEST_TAG}
docker build --build-arg TEST_LABEL=${MY_DIR} \
--build-arg TEST_LABEL_KEY=${TEST_LABEL_KEY} \
--build-arg GIT_COMMIT=${GIT_COMMIT} \
--build-arg TEST_TAG=${TEST_TAG} \
--build-arg ANSIBLE_OPTIONS=${ANSIBLE_OPTIONS} \
--force-rm -t ${MY_DIR}:${TEST_TAG} -f tests/Dockerfile.${PLATFORM}.${TEST_TAG} .
remove%:
docker rmi $(shell docker images -q --filter label=TEST_TAG=${TEST_TAG} --filter label=${TEST_LABEL_KEY}=${MY_DIR})
rm tests/Dockerfile.${PLATFORM}.${TEST_TAG}
clean .IGNORE: remove14 remove16
all: test14 test16
5 changes: 5 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cw_alarms_build_dir: "{{ playbook_dir }}/build"
cw_alarms_template_dir: "{{ cw_alarms_build_dir }}"
cw_alarms_template_name: "{{ cw_alarms_stack_name }}.json"
cw_alarms_stack_state: present
cw_alarms_region: "{{ region | default('us-west-2') }}"
2 changes: 2 additions & 0 deletions inventory/localhost
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[localhost]
localhost ansible_connection=local ansible_python_interpreter=python
2 changes: 2 additions & 0 deletions meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
dependencies: []
25 changes: 25 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
- name: Create necessary directories
file:
state: directory
path: "{{ item }}"
with_items:
- "{{ cw_alarms_build_dir }}"
- "{{ cw_alarms_template_dir }}"

- name: Template alarms
template:
src: cloudwatch.json.j2
dest: "{{ cw_alarms_template_dir }}/{{ cw_alarms_template_name }}"


- shell: "cat {{ cw_alarms_template_dir }}/{{ cw_alarms_template_name }}"


- name: Deploy alarms
cloudformation:
template: "{{ cw_alarms_template_dir }}/{{ cw_alarms_template_name }}"
region: "{{ cw_alarms_region }}"
stack_name: "{{ cw_alarms_stack_name }}"
state: "{{ cw_alarms_stack_state }}"
security_token: "{{ ansible_security_token | default(omit, true) }}"
region: "{{ cw_alarms_stack_region }}"
60 changes: 60 additions & 0 deletions templates/cloudwatch.json.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Resources" : {
{% for alarm in cw_alarms %}
"{{ alarm.name }}": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"AlarmDescription": "{{ alarm.description | default('') }}",
"Namespace": "{{ alarm.namespace }}",
"MetricName": "{{ alarm.metric }}",
"Dimensions": [
{% for dimension in alarm.dimensions %}
{
"Name": "{{ dimension.name }}",
"Value" : "{{ dimension.value }}"
}
{% if not loop.last %}, {% endif %}
{% endfor %}
],
"Statistic": "{{ alarm.statistic }}",
"Period": "{{ alarm.period_length }}",
"EvaluationPeriods": "{{ alarm.eval_periods }}",
"Threshold": "{{ alarm.threshold }}",
"ComparisonOperator": "{{ alarm.comparison_operator }}"
{% if alarm.unit is defined %}
,
"Unit" : "{{ alarm.unit }}"
{% endif %}
{% if alarm.actions is defined or cw_alarms_stack_actions is defined %}
,
"AlarmActions": [
{% if alarm.actions is defined %}
{% for arn in alarm.actions %}
"{{ arn }}"
{% if not loop.last %}, {% endif %}
{% endfor %}
{% if cw_alarms_stack_actions is defined %},{% endif %}
{% endif %}
{% if cw_alarms_stack_actions is defined %}
{% for arn in cw_alarms_stack_actions %}
"{{ arn }}"
{% if not loop.last %}, {% endif %}
{% endfor %}
{% endif %}
]
{% endif %}
{% if alarm.insufficient_data_actions is defined %}
,
"InsufficientDataActions": [
{% for action_arn in alarm.insufficient_data_actions %}
{{ action_arn }}
{% endfor %}
]
{% endif %}
}
}
{% if not loop.last %}, {% endif %}
{% endfor %}
}
}
2 changes: 2 additions & 0 deletions tests/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[test]
localhost ansible_connection=local
21 changes: 21 additions & 0 deletions tests/per_alarm_actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
- hosts: test
vars:
cw_alarms_stack_name: ansible-role-cfn-cloudwatch-alarms-testing
cw_alarms:
- name: "MemoryUtilizationAlarm"
namespace: "System/Linux"
metric: "MemoryUtilization"
eval_periods: 1
statistic: "Minimum"
threshold: 10
unit: "Percent"
period_length: 60
comparison_operator: "GreaterThanOrEqualToThreshold"
actions:
- "{{ cw_alarms_action_arn }}"
dimensions:
- name: "AppName"
value: "TestCustomMetric"
# ansible_security_token: "{{ lookup('env','AWS_SESSION_TOKEN') }}"
roles:
- ansible-role-cfn-cloudwatch-alarms
22 changes: 22 additions & 0 deletions tests/per_stack_actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
- hosts: test
vars:
cw_alarms_stack_name: ansible-role-cfn-cloudwatch-alarms-testing
cw_alarms_stack_actions:
- "{{ cw_alarms_action_arn }}"

cw_alarms:
- name: "MemoryUtilizationAlarm"
namespace: "System/Linux"
metric: "MemoryUtilization"
eval_periods: 1
statistic: "Minimum"
threshold: 10
unit: "Percent"
period_length: 60
comparison_operator: "GreaterThanOrEqualToThreshold"
dimensions:
- name: "AppName"
value: "TestCustomMetric"
# ansible_security_token: "{{ lookup('env','AWS_SESSION_TOKEN') }}"
roles:
- ansible-role-cfn-cloudwatch-alarms
24 changes: 24 additions & 0 deletions tests/per_stack_and_alarm_actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
- hosts: test
vars:
cw_alarms_stack_name: ansible-role-cfn-cloudwatch-alarms-testing
cw_alarms_stack_actions:
- "{{ cw_alarms_action_arn }}"

cw_alarms:
- name: "MemoryUtilizationAlarm"
namespace: "System/Linux"
metric: "MemoryUtilization"
eval_periods: 1
statistic: "Minimum"
threshold: 10
unit: "Percent"
period_length: 60
comparison_operator: "GreaterThanOrEqualToThreshold"
actions:
- "{{ cw_alarms_action_arn }}"
dimensions:
- name: "AppName"
value: "TestCustomMetric"
# ansible_security_token: "{{ lookup('env','AWS_SESSION_TOKEN') }}"
roles:
- ansible-role-cfn-cloudwatch-alarms
6 changes: 6 additions & 0 deletions tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- include: per_alarm_actions.yml
- include: per_alarm_actions.yml cw_alarms_stack_state=absent
- include: per_stack_actions.yml
- include: per_stack_actions.yml cw_alarms_stack_state=absent
- include: per_stack_and_alarm_actions.yml
- include: per_stack_and_alarm_actions.yml cw_alarms_stack_state=absent

0 comments on commit c3fc87f

Please sign in to comment.