Commit 9359cf97 authored by Alain Takoudjou's avatar Alain Takoudjou

Add tasks to upload ansible logs files

parent aa2e627c
- include: uploadlog.yml
- include: rerun.yml - include: rerun.yml
- name: Create /etc/opt dir - name: Create /etc/opt dir
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
- name: Add a periodical update on the VM - name: Add a periodical update on the VM
cron: name="Update vm bootstrap" cron: name="Update vm bootstrap"
minute="*/12" minute="*/12"
job="bash -lc /usr/local/bin/vm-bootstrap-update > /var/log/vm-bootstrap.cron.log" job="bash -lc /usr/local/bin/vm-bootstrap-update >> /var/log/vm-bootstrap.log"
- name: Check if /opt/slapos.playbook already exists - name: Check if /opt/slapos.playbook already exists
file: path=/opt/slapos.playbook/ state=directory file: path=/opt/slapos.playbook/ state=directory
......
---
- name: Add upload script
template: src=upload-script.j2 dest=/usr/local/bin/ansible-upload-vm-logs mode=755
- 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"
#!/usr/bin/env python
import os
import sys
import requests
class uploader():
log_path = '/var/log/ansible/hosts'
url = ''
def __init__(self, url='http://10.0.2.100/'):
self.url = url
def upload_file(self, file_name, content, override=False):
values = {'path': file_name, 'content': content}
if override:
values['clear'] = '1'
result = requests.post(self.url, data=values)
return result
def upload_result(self):
for filename in os.listdir(self.log_path):
filepath = os.path.join(self.log_path, filename)
to_path = 'ansible/%s' % filename
if os.path.exists(filepath) and os.path.isfile(filepath):
content = ""
with open(filepath, 'r') as fd:
content = ','.join(fd.readlines())
r = self.upload_file(to_path, '[%s]' % content, True)
print 'Content uploaded to %s' % to_path, r
def readFileFrom(self, f, lastPosition, limit=20000):
"""
Returns the last lines of file `f`, from position lastPosition.
and the last position
limit = max number of characters to read
"""
BUFSIZ = 1024
f.seek(0, 2)
btes = f.tell()
block = -1
data = ""
length = btes
truncated = False # True if a part of log data has been truncated
#if (lastPosition <= 0 and length > limit) or (length - lastPosition > limit):
# lastPosition = length - limit
# truncated = True
if lastPosition > btes:
lastPosition = 0
size = btes - lastPosition
while btes > lastPosition:
if abs(block * BUFSIZ) <= size:
# Seek back one whole BUFSIZ
f.seek(block * BUFSIZ, 2)
data = f.read(BUFSIZ) + data
else:
margin = abs(block * BUFSIZ) - size
if length < BUFSIZ:
f.seek(0, 0)
else:
seek = block * BUFSIZ + margin
f.seek(seek, 2)
data = f.read(BUFSIZ - margin) + data
btes -= BUFSIZ
block -= 1
f.close()
return {
'data': data,
'position': length,
'truncated': truncated
}
if __name__ == "__main__":
ansible_log = "/var/log/vm-bootstrap.log"
url = "http://10.0.2.100/"
if len(sys.argv) < 3:
print "Use: %s upload_url file_to_upload" % sys.argv[0]
print "Default: %s %s %s" % (sys.argv[0], url, ansible_log)
if len(sys.argv) >= 2:
url = sys.argv[1]
if len(sys.argv) >= 3:
ansible_log = sys.argv[2]
uploader = uploader()
state_file = "/opt/.ansible_log.state"
log_destination = "ansible/vm-bootstrap.log"
current_state = 0
if os.path.exists(state_file):
with open(state_file, 'r') as f:
current_state = int(f.read())
if os.path.exists(ansible_log):
f = open(ansible_log, 'r')
content = uploader.readFileFrom(f, current_state)
f.close()
if content['data']:
r = uploader.upload_file(log_destination, content['data'])
print 'Content uploaded to %s' % log_destination, r
with open(state_file, 'w') as f:
f.write(str(content['position']))
else:
print 'No Ansible log file found in %s.' % ansible_log
# Post Ansible execution result
uploader.upload_result()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment