diff --git a/.gitignore b/.gitignore index 7bbc71c..b7bd82f 100644 --- a/.gitignore +++ b/.gitignore @@ -99,3 +99,6 @@ ENV/ # mypy .mypy_cache/ + +# vim undo files +*.un~ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f29453b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: python +sudo: required +python: + - "2.7" +install: + - pip install -r requirements.txt +script: + - make check +services: + - docker diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4a60ba7 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ + +syntax: + pycodestyle --ignore=E265,E501 callback_plugins/grafana_annotations.py + +container: + tools/start_grafana.sh + +check: container + GRAFANA_API_TOKEN='$(shell python tools/get_or_create_token.py)' ansible-playbook test.yml + #TODO: Check annotations are available in grafana + #TODO: use a test framework like unittest diff --git a/callback_plugins/grafana_annotations.py b/callback_plugins/grafana_annotations.py index d504336..d127644 100644 --- a/callback_plugins/grafana_annotations.py +++ b/callback_plugins/grafana_annotations.py @@ -1,6 +1,20 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +import os +import json +import socket +import getpass +from datetime import datetime + +try: + import httplib +except ImportError: + # Python 3 + import http.client as httplib + +from ansible.plugins.callback import CallbackBase + DOCUMENTATION = ''' callback: grafana_annotations type: notification @@ -33,21 +47,6 @@ - name: GRAFANA_API_TOKEN ''' -import os -import json -import socket -import getpass -from datetime import datetime - -try: - import httplib -except ImportError: - # Python 3 - import http.client as httplib - -from ansible.plugins.callback import CallbackBase - - PLAYBOOK_START_TXT = """\ Started playbook {playbook} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f519463 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +ansible==2.4 +pycodestyle==2.3.1 diff --git a/test.yml b/test.yml index 6036f64..8e86e5b 100644 --- a/test.yml +++ b/test.yml @@ -3,3 +3,4 @@ tasks: - debug: msg: "Hello world" + - command: sleep 2 diff --git a/tools/get_or_create_token.py b/tools/get_or_create_token.py new file mode 100644 index 0000000..9055fba --- /dev/null +++ b/tools/get_or_create_token.py @@ -0,0 +1,44 @@ +import httplib +import sys +import json +import argparse +from base64 import b64encode + +URI = "/api/auth/keys" + +parser = argparse.ArgumentParser() +parser.add_argument("--user", default="admin", help="login for basic authentication") +parser.add_argument("--passwd", default="admin", help="password for basic authentication") +parser.add_argument("--host", default="127.0.0.1", help="Grafana host address") +parser.add_argument("--port", default="3000", help="Grafana listen port") +args = parser.parse_args() + +cred = "%s:%s" % (args.user, args.passwd) +HEADERS = {"Content-Type": "application/json", "Authorization": "Basic %s" % b64encode(cred).decode('ascii')} + +http = httplib.HTTPConnection(args.host, args.port) +http.request("GET", URI, headers=HEADERS) +response = http.getresponse() +if response.status != 200: + print response.read() +tokens = json.loads(response.read()) + +token_id = None +for token in tokens: + if token.get("name") == "ansible-module": + token_id = token.get('id') +if token_id: + http = httplib.HTTPConnection(args.host, args.port) + http.request("DELETE", "%s/%d" % (URI, token_id), headers=HEADERS) + response = http.getresponse() + if response.status != 200: + print "Failed to remove 'ansible-module' token [%s]" % response.read() + sys.exit(1) + +http = httplib.HTTPConnection(args.host, args.port) +http.request("POST", URI, '{"name": "ansible-module", "role": "Editor"}', headers=HEADERS) +response = http.getresponse() +if response.status != 200: + print "Failed to create 'ansible-module' token" + sys.exit(1) +print json.loads(response.read())["key"] diff --git a/tools/start_grafana.sh b/tools/start_grafana.sh new file mode 100755 index 0000000..a4fcaf8 --- /dev/null +++ b/tools/start_grafana.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +CONTAINER=$(docker ps | grep 'grafana/grafana'|awk '{print $1}') +[ -n "$CONTAINER" ] && echo "Container already running" && exit 0 + +docker pull grafana/grafana +[ $? -ne 0 ] && exit 1 + +docker run -d --name=grafana -p 3000:3000 grafana/grafana +sleep 2 +exit $?