Skip to content
This repository has been archived by the owner on Aug 13, 2020. It is now read-only.

Commit

Permalink
Add ci through travis
Browse files Browse the repository at this point in the history
* Adding a 2 second sleep in the playbook to make the region annotation
visible.
* Adding a Makefile and tools allowing to trigger tests.
  • Loading branch information
rrey committed Jan 30, 2018
1 parent 1bb0838 commit 39620f8
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,6 @@ ENV/

# mypy
.mypy_cache/

# vim undo files
*.un~
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: python
sudo: required
python:
- "2.7"
install:
- pip install -r requirements.txt
script:
- make check
services:
- docker
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
29 changes: 14 additions & 15 deletions callback_plugins/grafana_annotations.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ansible==2.4
pycodestyle==2.3.1
1 change: 1 addition & 0 deletions test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
tasks:
- debug:
msg: "Hello world"
- command: sleep 2
44 changes: 44 additions & 0 deletions tools/get_or_create_token.py
Original file line number Diff line number Diff line change
@@ -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"]
11 changes: 11 additions & 0 deletions tools/start_grafana.sh
Original file line number Diff line number Diff line change
@@ -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 $?

0 comments on commit 39620f8

Please sign in to comment.