Commit 780f9780 by Alain Takoudjou

Merge branch 'logging' into 'master'

Merge Logging to master

Ansible post logs and result of tasks to default http server

See merge request !9
2 parents 9359cf97 f0da81b1
#!/usr/bin/python
import os
import sys
from subprocess import Popen, PIPE
def check_ping(host_list):
for host in host_list:
process = Popen("ping -c 1 %s" % host, shell=True, stdout=PIPE)
result = process.communicate()[0]
if process.returncode == 0:
continue
raise Exception('PING fail: host at %s didn\'t send response.\n%s' % (
host, result))
def ping_cluster(hpath):
if os.path.exists(hpath):
for content in open(hpath, 'r').readlines():
if content:
items = content.strip().split(' ')
check_ping(items)
if __name__ == "__main__":
if len(sys.argv) < 3:
print "Use: %s TYPE [HOST LIST] OR [HOST-FILE]" % sys.argv[0]
print "ex: %s host google.com slapos.org; %s cluster /tmp/hosts" % (
sys.argv[0], sys.argv[0])
exit(1)
if sys.argv[1] == 'host':
check_ping(sys.argv[2:])
elif sys.argv[1] == 'cluster':
ping_cluster(sys.argv[2])
......@@ -46,7 +46,7 @@
- name: stat /tmp/hosts
stat: path=/tmp/hosts
register: hostname_file
register: hosts_file
- name: Format hosts
script: format_hosts /tmp/hosts tl.teralab-datascience.fr
......@@ -57,3 +57,12 @@
- name: adding entry from workspace
lineinfile: dest=/etc/resolv.conf line="nameserver 10.200.218.1"
- name: ping current host
script: ping host {{ lookup('file', '/etc/opt/ipv4') }} {{ lookup('file', '/etc/opt/hostname') }}
when: hostname_file.stat.exists == True
- name: ping cluster hosts
script: ping cluster /tmp/hosts
when: hosts_file.stat.exists == True
ignore_errors: True
......@@ -5,5 +5,5 @@
- name: Add a periodical upload of logs and result
cron: name="Upload ansible files to http server"
minute="*/7"
job="bash -lc /usr/local/bin/ansible-upload-vm-logs http://10.0.2.100/ /var/log/vm-bootstrap.log > /var/log/ansible-upload.log"
job="/usr/local/bin/ansible-upload-vm-logs http://10.0.2.100/ /var/log/vm-bootstrap.log > /var/log/ansible-upload.log"
......@@ -8,9 +8,11 @@ class uploader():
log_path = '/var/log/ansible/hosts'
url = ''
host = '127.0.0.1'
def __init__(self, url='http://10.0.2.100/'):
def __init__(self, url='http://10.0.2.100/', host='127.0.0.1'):
self.url = url
self.host = host
def upload_file(self, file_name, content, override=False):
values = {'path': file_name, 'content': content}
......@@ -20,6 +22,7 @@ class uploader():
return result
def upload_result(self):
has_failure = False
for filename in os.listdir(self.log_path):
filepath = os.path.join(self.log_path, filename)
to_path = 'ansible/%s' % filename
......@@ -29,6 +32,12 @@ class uploader():
content = ','.join(fd.readlines())
r = self.upload_file(to_path, '[%s]' % content, True)
print 'Content uploaded to %s' % to_path, r
if filename.endswith('FAILED'):
has_failure = True
if not has_failure:
to_path = 'ansible/%s_FAILED' % self.host
r = self.upload_file(to_path, '[]', True)
print 'Content uploaded to %s' % to_path, r
def readFileFrom(self, f, lastPosition, limit=20000):
"""
......@@ -83,7 +92,7 @@ if __name__ == "__main__":
if len(sys.argv) >= 3:
ansible_log = sys.argv[2]
uploader = uploader()
uploader = uploader(url=url)
state_file = "/opt/.ansible_log.state"
log_destination = "ansible/vm-bootstrap.log"
current_state = 0
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!