From bcea862090c58154a89486be17ad6d51299206b6 Mon Sep 17 00:00:00 2001 From: Rob Goodall Date: Wed, 13 Dec 2017 15:36:56 +0000 Subject: [PATCH] [GITHUB-1] Prometheus AWS CloudWatch Exporter --- .editorconfig | 11 ++ .gitignore | 21 +++ .travis.yml | 34 ++++ .version | 1 + LICENSE | 21 +++ Makefile | 111 +++++++++++++ README.md | 76 +++++++++ defaults/main.yml | 15 ++ files/prometheus_wrapper | 41 +++++ handlers/main.yml | 7 + meta/main.yml | 23 +++ requirements.txt | 1 + tasks/build.yml | 55 ++++++ tasks/configure.yml | 27 +++ tasks/main.yml | 11 ++ templates/cloudwatch.yml.j2 | 3 + .../prometheus_cloudwatch_exporter.default.j2 | 1 + ...prometheus_cloudwatch_exporter.sysvinit.j2 | 157 ++++++++++++++++++ tests/Vagrantfile | 52 ++++++ tests/ansible.cfg | 4 + tests/inventory | 1 + tests/local_requirements.yml | 4 + tests/test.yml | 27 +++ 23 files changed, 704 insertions(+) create mode 100755 .editorconfig create mode 100755 .gitignore create mode 100755 .travis.yml create mode 100644 .version create mode 100755 LICENSE create mode 100755 Makefile create mode 100755 defaults/main.yml create mode 100644 files/prometheus_wrapper create mode 100644 handlers/main.yml create mode 100755 meta/main.yml create mode 100755 requirements.txt create mode 100755 tasks/build.yml create mode 100644 tasks/configure.yml create mode 100755 tasks/main.yml create mode 100644 templates/cloudwatch.yml.j2 create mode 100644 templates/prometheus_cloudwatch_exporter.default.j2 create mode 100644 templates/prometheus_cloudwatch_exporter.sysvinit.j2 create mode 100755 tests/Vagrantfile create mode 100755 tests/ansible.cfg create mode 100755 tests/inventory create mode 100755 tests/local_requirements.yml create mode 100755 tests/test.yml diff --git a/.editorconfig b/.editorconfig new file mode 100755 index 0000000..de80aae --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true +indent_style = tab +indent_size = 4 + +[*.{yml,md,json.j2}] +indent_style = space +indent_size = 2 diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..677c241 --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +## IDEs, editors, ... +.DS_Store +.idea +.project +*.bak +*.iml +*.sublime* +*.swp +/.settings + + + + +# Build and Tests Artefacts +*.retry +/.make +/.venv_* +/meta/.galaxy_install_info +/tests/.vagrant +/tests/*.log +/tests/roles diff --git a/.travis.yml b/.travis.yml new file mode 100755 index 0000000..68c9f9a --- /dev/null +++ b/.travis.yml @@ -0,0 +1,34 @@ +--- + +group: stable +dist: trusty + +language: python +python: "2.7" + +branches: + only: + - develop + - master + +env: + - ANSIBLE_INSTALL_VERSION=2.0.2.0 + - ANSIBLE_INSTALL_VERSION=2.1.6.0 + - ANSIBLE_INSTALL_VERSION=2.2.3.0 + - ANSIBLE_INSTALL_VERSION=2.3.2.0 + - ANSIBLE_INSTALL_VERSION=2.4.1.0 + +before_install: + # Make sure everything's up to date. + - sudo apt-get update -qq + +install: + # Download dependencies + - make test_deps + +script: + - make lint + - make test_ansible + +notifications: + webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/.version b/.version new file mode 100644 index 0000000..6b3126c --- /dev/null +++ b/.version @@ -0,0 +1 @@ +v1.0 diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..f3dd894 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016-2017 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..4e77ffd --- /dev/null +++ b/Makefile @@ -0,0 +1,111 @@ +include .make + +ANSIBLE_INSTALL_VERSION ?= 2.2.3.0 +ANSIBLE_CONFIG ?= tests/ansible.cfg +ROLE_NAME ?= $(shell basename $$(pwd)) +TEST_PLAYBOOK ?= test.yml +VAGRANT_BOX ?= ubuntu/trusty64 +PATH := $(PWD)/.venv_$(ANSIBLE_INSTALL_VERSION)/bin:$(shell printenv PATH) +SHELL := env PATH=$(PATH) /bin/bash + +.DEFAULT_GOAL := help +.PHONY: help + +export ANSIBLE_CONFIG +export PATH +export VAGRANT_BOX +export TEST_PLAYBOOK + +all: test clean + +## Run tests on any file change +watch: test_deps + while sleep 1; do \ + find defaults/ handlers/ meta/ tasks/ templates/ tests/test.yml \ + | entr -d make lint vagrant; \ + done + +## Run tests +test: lint test_deps vagrant + +## Install test dependencies +test_deps: .venv_$(ANSIBLE_INSTALL_VERSION) tests/roles + +tests/roles: + mkdir -p tests/roles + ln -s ../.. tests/roles/sansible.$(ROLE_NAME) + ansible-galaxy install -p tests/roles -r tests/local_requirements.yml --ignore-errors + +## ! Executes Ansible tests using local connection +# run it ONLY from within a test VM. +# If you want to test this role, run `make test` instead. +# Example: make test_ansible +# make test_ansible TEST_PLAYBOOK=test-something-else.yml +test_ansible: test_ansible_build test_ansible_configure + cd tests && ansible-playbook \ + --inventory inventory \ + --connection local \ + --tags assert \ + $(TEST_PLAYBOOK) + +## ! Executes Ansible tests using local connection +# run it ONLY from witinh a test VM. +# Example: make test_ansible_build +# make test_ansible_build TEST_PLAYBOOK=test-something-else.yml +test_ansible_%: + cd tests && ansible-playbook \ + --inventory inventory \ + --connection local \ + --tags=$(subst test_ansible_,,$@) \ + $(TEST_PLAYBOOK) + cd tests && ansible-playbook \ + --inventory inventory \ + --connection local \ + --tags=$(subst test_ansible_,,$@) \ + $(TEST_PLAYBOOK) \ + | grep -q 'changed=0.*failed=0' \ + && (echo 'Idempotence test: pass' && exit 0) \ + || (echo 'Idempotence test: fail' && exit 1) + +## Start and (re)provisiom Vagrant test box +vagrant: + cd tests && vagrant up --no-provision + cd tests && vagrant provision + @echo "- - - - - - - - - - - - - - - - - - - - - - -" + @echo " Provisioning Successful" + @echo "- - - - - - - - - - - - - - - - - - - - - - -" + +## Execute simple Vagrant command +# Example: make vagrant_ssh +# make vagrant_halt +vagrant_%: + cd tests && vagrant $(subst vagrant_,,$@) + +## Installs a virtual environment and all python dependencies +.venv_%: + virtualenv .venv_$(ANSIBLE_INSTALL_VERSION) + .venv_$(ANSIBLE_INSTALL_VERSION)/bin/pip install -r requirements.txt --ignore-installed + .venv_$(ANSIBLE_INSTALL_VERSION)/bin/pip install ansible==$(ANSIBLE_INSTALL_VERSION) + virtualenv --relocatable .venv_$(ANSIBLE_INSTALL_VERSION) + +## lint Ansible files +lint: .venv_$(ANSIBLE_INSTALL_VERSION) + find defaults/ handlers/ meta/ tasks/ templates/ -name "*.yml" | xargs -I{} ansible-lint {} + +## Prints this help +help: + @awk -v skip=1 \ + '/^##/ { sub(/^[#[:blank:]]*/, "", $$0); doc_h=$$0; doc=""; skip=0; next } \ + skip { next } \ + /^#/ { doc=doc "\n" substr($$0, 2); next } \ + /:/ { sub(/:.*/, "", $$0); printf "\033[34m%-30s\033[0m\033[1m%s\033[0m %s\n\n", $$0, doc_h, doc; skip=1 }' \ + $(MAKEFILE_LIST) + +## Removes all downloaded dependencies +clean: + rm -rf .venv_* + rm -rf tests/roles + cd tests && (vagrant destroy || echo "skipping vagrant destroy") + +.make: + touch .make diff --git a/README.md b/README.md index e63f1d8..619bbb8 100644 --- a/README.md +++ b/README.md @@ -1 +1,77 @@ # prometheus_cloudwatch_exporter + +Master: [![Build Status](https://travis-ci.org/sansible/prometheus_cloudwatch_exporter.svg?branch=master)](https://travis-ci.org/sansible/prometheus_cloudwatch_exporter) +Develop: [![Build Status](https://travis-ci.org/sansible/prometheus_cloudwatch_exporter.svg?branch=develop)](https://travis-ci.org/sansible/prometheus_cloudwatch_exporter) + +* [ansible.cfg](#ansible-cfg) +* [Installation and Dependencies](#installation-and-dependencies) +* [Tags](#tags) +* [Examples](#examples) + +This role installs Prometheus AWS CloudWatch Exporter. + +Prometheus AWS CloudWatch Exporter makes available AWS CloudWatch metrics for collection by Prometheus server. + +For more information about Prometheus AWS CloudWatch Exporter please visit: +[http://github.com/prometheus/cloudwatch_exporter](https://github.com/prometheus/cloudwatch_exporter). + +For more information about Prometheus Server please visit: +[https://prometheus.io](https://prometheus.io). + + +## ansible.cfg + +This role is designed to work with merge "hash_behaviour". Make sure your +ansible.cfg contains these settings + +```INI +[defaults] +hash_behaviour = merge +``` + +## Installation and Dependencies + +This role will install `sansible.users_and_groups` for managing `prometheus_cloudwatch_exporter` user +and `sansible.java` to install Java, which the Prometheus AWS CloudWatch Exporter requires to run. + +To install run `ansible-galaxy install sansible.prometheus_cloudwatch_exporter` or add this to your +`roles.yml`. + +```YAWL +- name: sansible.prometheus_cloudwatch_exporter + version: v1.0 +``` + +and run `ansible-galaxy install -p ./roles -r roles.yml` + + +## Tags + +This role uses tags: **build** and **configure** + +* `build` - Installs Prometheus AWS CloudWatch Exporter and all it's dependencies. +* `configure` - Configures Prometheus AWS CloudWatch Exporter. + + +## Examples + +Simply include the role in your playbook. + +Default exporter port: 9106 + +```YAML +- name: Install and configure prometheus_cloudwatch_exporter + hosts: "somehost" + + roles: + - role: sansible.prometheus_cloudwatch_exporter + sansible_prometheus_cloudwatch_exporter: + config: + region: eu-west-1 + metrics: + - aws_namespace: AWS/ELB + aws_metric_name: RequestCount + aws_dimensions: [AvailabilityZone, LoadBalancerName] + aws_dimension_select: + LoadBalancerName: [myLB] + aws_statistics: [Sum] diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100755 index 0000000..5f3de92 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,15 @@ +--- + +sansible_prometheus_cloudwatch_exporter: + checksum: sha256:3ac6c88e45815e84b400c70112f6d2b942d90ec08d4db19f23533e7667837571 + config: + region: eu-west-1 + metrics: ~ + download_url: "http://search.maven.org/remotecontent?filepath=io/prometheus/cloudwatch/cloudwatch_exporter/0.4/cloudwatch_exporter-0.4-jar-with-dependencies.jar" + group: prometheus + opts: 9106 cloudwatch.yml + path: + install: /home/prometheus + log: /var/log/prometheus + pid: /var/run/prometheus + user: prometheus diff --git a/files/prometheus_wrapper b/files/prometheus_wrapper new file mode 100644 index 0000000..9f63681 --- /dev/null +++ b/files/prometheus_wrapper @@ -0,0 +1,41 @@ +#!/bin/bash +# +# Script to enable the creation of log files with the correct ownership. +# +DAEMON="" +DAEMON_ARGS="" +LOG="" + +usage() { echo "Usage: $0 -c [ -a \"\" ] -l " 1>&2; exit 1; } + + +while getopts ":c:a:l:" opt; do + case $opt in + c) + DAEMON=$OPTARG + ;; + a) + DAEMON_ARGS=$OPTARG + ;; + l) + LOG=$OPTARG + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; + :) + echo "Option -$OPTARG requires an argument." >&2 + exit 1 + ;; + *) + usage + ;; + esac +done +if [ ! -z "$DAEMON" ] && [ ! -z "$LOG" ]; then + exec $DAEMON $DAEMON_ARGS >"$LOG" 2>&1 +else + usage +fi +exit $? diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..4e1b13c --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,7 @@ +--- + +- name: restart prometheus cloudwatch exporter + become: yes + service: + name: prometheus-cloudwatch-exporter + state: restarted diff --git a/meta/main.yml b/meta/main.yml new file mode 100755 index 0000000..4c4b6a9 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,23 @@ +--- + +galaxy_info: + description: "Install Prometheus CloudWatch Exporter." + license: MIT + min_ansible_version: 2.0 + platforms: + - name: Ubuntu + versions: + - trusty + categories: + - development + +dependencies: + - role: sansible.users_and_groups + users_and_groups: + groups: + - name: "{{ sansible_prometheus_cloudwatch_exporter.group }}" + users: + - name: "{{ sansible_prometheus_cloudwatch_exporter.user }}" + group: "{{ sansible_prometheus_cloudwatch_exporter.group }}" + + - role: sansible.java diff --git a/requirements.txt b/requirements.txt new file mode 100755 index 0000000..5cb763f --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +ansible-lint==3.* diff --git a/tasks/build.yml b/tasks/build.yml new file mode 100755 index 0000000..abdee22 --- /dev/null +++ b/tasks/build.yml @@ -0,0 +1,55 @@ +--- + +- name: Create Prometheus CloudWatch Exporter directories + become: yes + file: + path: "{{ item }}" + state: directory + owner: "{{ sansible_prometheus_cloudwatch_exporter.user }}" + group: "{{ sansible_prometheus_cloudwatch_exporter.group }}" + mode: 0750 + with_items: + - "{{ sansible_prometheus_cloudwatch_exporter.path.install }}" + - "{{ sansible_prometheus_cloudwatch_exporter.path.install }}/bin" + - "{{ sansible_prometheus_cloudwatch_exporter.path.install }}/prometheus_cloudwatch_exporter" + - "{{ sansible_prometheus_cloudwatch_exporter.path.log }}" + - "{{ sansible_prometheus_cloudwatch_exporter.path.pid }}" + +- name: Copy wrapper shell script + become: yes + copy: + src: prometheus_wrapper + dest: "{{ sansible_prometheus_cloudwatch_exporter.path.install }}/bin/prometheus_wrapper" + owner: "{{ sansible_prometheus_cloudwatch_exporter.user }}" + group: "{{ sansible_prometheus_cloudwatch_exporter.group }}" + mode: 0755 + +- name: Download Prometheus CloudWatch Exporter JAR + become: yes + get_url: + url: "{{ sansible_prometheus_cloudwatch_exporter.download_url }}" + dest: "{{ sansible_prometheus_cloudwatch_exporter.path.install }}/prometheus_cloudwatch_exporter/{{ sansible_prometheus_cloudwatch_exporter.download_url.split('/')[-1] }}" + checksum: "{{ sansible_prometheus_cloudwatch_exporter.checksum }}" + owner: "{{ sansible_prometheus_cloudwatch_exporter.user }}" + group: "{{ sansible_prometheus_cloudwatch_exporter.group }}" + mode: 0755 + +- name: Set Prometheus CloudWatch Exporter defaults + become: yes + template: + dest: /etc/default/prometheus-cloudwatch-exporter + mode: 0644 + src: prometheus_cloudwatch_exporter.default.j2 + +- name: Make Prometheus CloudWatch Exporter a service + become: yes + template: + dest: /etc/init.d/prometheus-cloudwatch-exporter + mode: 0775 + src: prometheus_cloudwatch_exporter.sysvinit.j2 + +- name: Enable Prometheus CloudWatch Exporter to start on boot + become: yes + service: + name: prometheus-cloudwatch-exporter + enabled: yes diff --git a/tasks/configure.yml b/tasks/configure.yml new file mode 100644 index 0000000..571a9fe --- /dev/null +++ b/tasks/configure.yml @@ -0,0 +1,27 @@ +--- + +- name: Deploy Prometheus CloudWatch Exporter defaults + become: yes + template: + dest: /etc/default/prometheus-cloudwatch-exporter + mode: 0644 + src: prometheus_cloudwatch_exporter.default.j2 + notify: + - restart prometheus cloudwatch exporter + +- name: Deploy config file + become: yes + become_user: "{{ sansible_prometheus_cloudwatch_exporter.user }}" + template: + dest: "{{ sansible_prometheus_cloudwatch_exporter.path.install }}/prometheus_cloudwatch_exporter/cloudwatch.yml" + group: "{{ sansible_prometheus_cloudwatch_exporter.group }}" + owner: "{{ sansible_prometheus_cloudwatch_exporter.user }}" + src: cloudwatch.yml.j2 + notify: + - restart prometheus cloudwatch exporter + +- name: Prometheus CloudWatch Exporter service start + become: yes + service: + name: prometheus-cloudwatch-exporter + state: started diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100755 index 0000000..6582c6d --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,11 @@ +--- + +- name: Install Prometheus CloudWatch Exporter + include: build.yml + tags: + - build + +- name: Configure Prometheus CloudWatch Exporter + include: configure.yml + tags: + - configure diff --git a/templates/cloudwatch.yml.j2 b/templates/cloudwatch.yml.j2 new file mode 100644 index 0000000..bcf06bb --- /dev/null +++ b/templates/cloudwatch.yml.j2 @@ -0,0 +1,3 @@ +--- +region: {{ sansible_prometheus_cloudwatch_exporter.config.region }} +metrics: {{ sansible_prometheus_cloudwatch_exporter.config.metrics }} diff --git a/templates/prometheus_cloudwatch_exporter.default.j2 b/templates/prometheus_cloudwatch_exporter.default.j2 new file mode 100644 index 0000000..7e855d1 --- /dev/null +++ b/templates/prometheus_cloudwatch_exporter.default.j2 @@ -0,0 +1 @@ +DAEMON_ARGS="{{ sansible_prometheus_cloudwatch_exporter.opts }}" diff --git a/templates/prometheus_cloudwatch_exporter.sysvinit.j2 b/templates/prometheus_cloudwatch_exporter.sysvinit.j2 new file mode 100644 index 0000000..b11930b --- /dev/null +++ b/templates/prometheus_cloudwatch_exporter.sysvinit.j2 @@ -0,0 +1,157 @@ +#! /bin/sh +### BEGIN INIT INFO +# Provides: prometheus-cloudwatch-exporter +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Prometheus CloudWatch Exporter +# Description: Prometheus CloudWatch Exporter +### END INIT INFO + +# Please remove the "Author" lines above and replace them +# with your own name if you copy and modify this script. + +# Do NOT "set -e" + +# PATH should only include /usr/* if it runs after the mountnfs.sh script +PATH=/sbin:/usr/sbin:/bin:/usr/bin +DESC="Prometheus CloudWatch Exporter" +NAME=prometheus-cloudwatch-exporter +DAEMON="/usr/bin/java -jar {{ sansible_prometheus_cloudwatch_exporter.path.install }}/prometheus_cloudwatch_exporter/{{ sansible_prometheus_cloudwatch_exporter.download_url.split('/')[-1] }}" +DAEMON_ARGS="" +PIDFILE="{{ sansible_prometheus_cloudwatch_exporter.path.pid }}/$NAME.pid" +SCRIPTNAME=/etc/init.d/$NAME +LOG="{{ sansible_prometheus_cloudwatch_exporter.path.log }}/$NAME.log" +USER=prometheus + +# Exit if the package is not installed +[ -x "$DAEMON" ] || exit 0 + +# Read configuration variable file if it is present +[ -r /etc/default/$NAME ] && . /etc/default/$NAME + +# Load the VERBOSE setting and other rcS variables +. /lib/init/vars.sh + +# Define LSB log_* functions. +# Depend on lsb-base (>= 3.2-14) to ensure that this file is present +# and status_of_proc is working. +. /lib/lsb/init-functions + +# +# Function that starts the daemon/service +# +do_start() +{ + # Return + # 0 if daemon has been started + # 1 if daemon was already running + # 2 if daemon could not be started + start-stop-daemon --start --background -C -c $USER --user $USER --pidfile $PIDFILE --exec {{ sansible_prometheus_cloudwatch_exporter.path.install }}/bin/prometheus_wrapper -- -c "$DAEMON" -a "$DAEMON_ARGS" -l "$LOG" + # + # Exit code forced to be 0 otherwise service start tasks in Ansible fail on the first run. + # + exit $RETVAL + # Add code here, if necessary, that waits for the process to be ready + # to handle requests from services started subsequently which depend + # on this one. As a last resort, sleep for some time. +} + +# +# Function that stops the daemon/service +# +do_stop() +{ + # Return + # 0 if daemon has been stopped + # 1 if daemon was already stopped + # 2 if daemon could not be stopped + # other if a failure occurred + start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --exec $DAEMON + RETVAL="$?" + [ "$RETVAL" = 2 ] && return 2 + # Wait for children to finish too if this is a daemon that forks + # and if the daemon is only ever run from this initscript. + # If the above conditions are not satisfied then add some other code + # that waits for the process to drop all resources that could be + # needed by services started subsequently. A last resort is to + # sleep for some time. + start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON + [ "$?" = 2 ] && return 2 + # Many daemons don't delete their pidfiles when they exit. + rm -f $PIDFILE + return "$RETVAL" +} + +# +# Function that sends a SIGHUP to the daemon/service +# +do_reload() { + # + # If the daemon can reload its configuration without + # restarting (for example, when it is sent a SIGHUP), + # then implement that here. + # + start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME + return 0 +} + +case "$1" in + start) + [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" + do_start + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + stop) + [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" + do_stop + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + status) + status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? + ;; + #reload|force-reload) + # + # If do_reload() is not implemented then leave this commented out + # and leave 'force-reload' as an alias for 'restart'. + # + #log_daemon_msg "Reloading $DESC" "$NAME" + #do_reload + #log_end_msg $? + #;; + restart|force-reload) + # + # If the "reload" option is implemented then remove the + # 'force-reload' alias + # + log_daemon_msg "Restarting $DESC" "$NAME" + do_stop + case "$?" in + 0|1) + do_start + case "$?" in + 0) log_end_msg 0 ;; + 1) log_end_msg 1 ;; # Old process is still running + *) log_end_msg 1 ;; # Failed to start + esac + ;; + *) + # Failed to stop + log_end_msg 1 + ;; + esac + ;; + *) + #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2 + echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 + exit 3 + ;; +esac + diff --git a/tests/Vagrantfile b/tests/Vagrantfile new file mode 100755 index 0000000..0d4b89e --- /dev/null +++ b/tests/Vagrantfile @@ -0,0 +1,52 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +# Vagrantfile API/syntax version. Don't touch unless you know what you're doing! +VAGRANTFILE_API_VERSION = "2" + +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| + + config.vm.box = "#{ENV['VAGRANT_BOX']}" + config.vm.box_check_update = false + + config.ssh.forward_agent = true + + config.vm.provider :virtualbox do |vb| + vb.customize ["modifyvm", :id, "--memory", 1024] + vb.customize ["modifyvm", :id, "--cpus", 1] + + vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] + vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"] + end + + # Test "build" tag + config.vm.provision "ansible" do |ansible| + ansible.playbook = "#{ENV['TEST_PLAYBOOK']}" + ansible.tags = [ "build" ] + ansible.verbose = "#{ENV['VERBOSE']}" if ENV['VERBOSE'] + end + config.vm.provision "ansible" do |ansible| + ansible.playbook = "#{ENV['TEST_PLAYBOOK']}" + ansible.tags = [ "build" ] + ansible.verbose = "#{ENV['VERBOSE']}" if ENV['VERBOSE'] + end + + # Test "configure" tag + config.vm.provision "ansible" do |ansible| + ansible.playbook = "#{ENV['TEST_PLAYBOOK']}" + ansible.tags = [ "configure" ] + ansible.verbose = "#{ENV['VERBOSE']}" if ENV['VERBOSE'] + end + config.vm.provision "ansible" do |ansible| + ansible.playbook = "#{ENV['TEST_PLAYBOOK']}" + ansible.tags = [ "configure" ] + ansible.verbose = "#{ENV['VERBOSE']}" if ENV['VERBOSE'] + end + + # Test "assert" tag + config.vm.provision "ansible" do |ansible| + ansible.playbook = "#{ENV['TEST_PLAYBOOK']}" + ansible.tags = [ "assert" ] + ansible.verbose = "#{ENV['VERBOSE']}" if ENV['VERBOSE'] + end +end diff --git a/tests/ansible.cfg b/tests/ansible.cfg new file mode 100755 index 0000000..0908f0a --- /dev/null +++ b/tests/ansible.cfg @@ -0,0 +1,4 @@ +[defaults] +allow_world_readable_tmpfiles = True +hash_behaviour = merge +roles_folder = roles diff --git a/tests/inventory b/tests/inventory new file mode 100755 index 0000000..e8913bc --- /dev/null +++ b/tests/inventory @@ -0,0 +1 @@ +localhost ansible_python_interpreter="env python" diff --git a/tests/local_requirements.yml b/tests/local_requirements.yml new file mode 100755 index 0000000..affaf97 --- /dev/null +++ b/tests/local_requirements.yml @@ -0,0 +1,4 @@ +--- + +- src: sansible.users_and_groups +- src: sansible.java diff --git a/tests/test.yml b/tests/test.yml new file mode 100755 index 0000000..f3c6489 --- /dev/null +++ b/tests/test.yml @@ -0,0 +1,27 @@ +--- + +- name: Test Prometheus CloudWatch Exporter role + hosts: all + + pre_tasks: + - name: Update apt + become: yes + apt: + cache_valid_time: 1800 + update_cache: yes + tags: + - build + + roles: + - role: sansible.prometheus_cloudwatch_exporter + + post_tasks: + - name: Get Prometheus CloudWatch Exporter service state + become: yes + service: + name: prometheus-cloudwatch-exporter + state: started + register: status_check + failed_when: status_check.changed + tags: + - assert