Commit 0c62baab authored by Thomas Gambier's avatar Thomas Gambier Committed by Thomas Gambier

vm-bootstrap: use python3 for ansible-upload-vm-logs on recent distros

python-requests package doesn't exist anymore on Ubuntu 20.04 so we
convert the upload script to support both python2 and python3 and we use
python3 in Debian >= 10 and Ubuntu >= 20.04.

This let us clean up the imt-vm-bootstrap playbook for Debian 10 (remove
manual installation of python-requests package).
parent e2a16016
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
roles: roles:
- { role: vm-bootstrap, startup_playbook_id: imt-vm-bootstrap.yml } - { role: vm-bootstrap, startup_playbook_id: imt-vm-bootstrap.yml }
- { role: package, package_name: python-requests, package_state: present, when: ansible_distribution == "Debian" and ansible_distribution_major_version|int >= 10}
- ntp - ntp
- { role: vm-disks, vd_disk: b, data_n: 1, when: vd_list.stdout.find("vdb") != -1 } - { role: vm-disks, vd_disk: b, data_n: 1, when: vd_list.stdout.find("vdb") != -1 }
- { role: vm-disks, vd_disk: c, data_n: 2, when: vd_list.stdout.find("vdc") != -1 } - { role: vm-disks, vd_disk: c, data_n: 2, when: vd_list.stdout.find("vdc") != -1 }
......
...@@ -2,8 +2,19 @@ ...@@ -2,8 +2,19 @@
- name: Add upload script - name: Add upload script
template: src=upload-script.j2 dest=/usr/local/bin/ansible-upload-vm-logs mode=755 template: src=upload-script.j2 dest=/usr/local/bin/ansible-upload-vm-logs mode=755
- name: Add a periodical upload of logs and result - name: Add a periodical upload of logs and result (python 3)
cron: name="Upload ansible files to http server"
minute="*/5"
job="python3 /usr/local/bin/ansible-upload-vm-logs http://10.0.2.100/ /var/log/vm-bootstrap.log > /var/log/ansible-upload.log 2>&1"
cron_file=ansible-upload-log user="root"
when: (ansible_distribution == "Debian" and ansible_distribution_major_version|int >= 10) or
(ansible_distribution == "Ubuntu" and ansible_distribution_major_version|int >= 20)
- name: Add a periodical upload of logs and result (python 2)
cron: name="Upload ansible files to http server" cron: name="Upload ansible files to http server"
minute="*/5" minute="*/5"
job="/usr/local/bin/ansible-upload-vm-logs http://10.0.2.100/ /var/log/vm-bootstrap.log > /var/log/ansible-upload.log 2>&1" job="/usr/local/bin/ansible-upload-vm-logs http://10.0.2.100/ /var/log/vm-bootstrap.log > /var/log/ansible-upload.log 2>&1"
cron_file=ansible-upload-log user="root" cron_file=ansible-upload-log user="root"
when: (ansible_distribution == "Debian" and ansible_distribution_major_version|int < 10) or
(ansible_distribution == "Ubuntu" and ansible_distribution_major_version|int < 20) or
(not ansible_distribution == "Debian" and not ansible_distribution == "Ubuntu")
...@@ -31,53 +31,26 @@ class uploader(): ...@@ -31,53 +31,26 @@ class uploader():
with open(filepath, 'r') as fd: with open(filepath, 'r') as fd:
content = ','.join(fd.readlines()) content = ','.join(fd.readlines())
r = self.upload_file(to_path, '[%s]' % content, True) r = self.upload_file(to_path, '[%s]' % content, True)
print 'Content uploaded to %s' % to_path, r print('Content uploaded to %s' % to_path, r)
if filename.endswith('FAILED'): if filename.endswith('FAILED'):
has_failure = True has_failure = True
if not has_failure: if not has_failure:
to_path = 'ansible/%s_FAILED' % self.host to_path = 'ansible/%s_FAILED' % self.host
r = self.upload_file(to_path, '[]', True) r = self.upload_file(to_path, '[]', True)
print 'Content uploaded to %s' % to_path, r print('Content uploaded to %s' % to_path, r)
def readFileFrom(self, f, lastPosition, limit=20000): def readFileFrom(self, f, last_position):
""" """
Returns the last lines of file `f`, from position lastPosition. Returns the last lines of file `f`, from position lastPosition.
and the last position and the last position
limit = max number of characters to read
""" """
BUFSIZ = 1024 f.seek(last_position)
f.seek(0, 2) data = f.read()
btes = f.tell() new_position = 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() f.close()
return { return {
'data': data, 'data': data,
'position': length, 'position': new_position
'truncated': truncated
} }
if __name__ == "__main__": if __name__ == "__main__":
...@@ -85,8 +58,8 @@ if __name__ == "__main__": ...@@ -85,8 +58,8 @@ if __name__ == "__main__":
ansible_log = "/var/log/vm-bootstrap.log" ansible_log = "/var/log/vm-bootstrap.log"
url = "http://10.0.2.100/" url = "http://10.0.2.100/"
if len(sys.argv) < 3: if len(sys.argv) < 3:
print "Use: %s upload_url file_to_upload" % sys.argv[0] print("Use: %s upload_url file_to_upload" % sys.argv[0])
print "Default: %s %s %s" % (sys.argv[0], url, ansible_log) print("Default: %s %s %s" % (sys.argv[0], url, ansible_log))
if len(sys.argv) >= 2: if len(sys.argv) >= 2:
url = sys.argv[1] url = sys.argv[1]
if len(sys.argv) >= 3: if len(sys.argv) >= 3:
...@@ -106,11 +79,11 @@ if __name__ == "__main__": ...@@ -106,11 +79,11 @@ if __name__ == "__main__":
f.close() f.close()
if content['data']: if content['data']:
r = uploader.upload_file(log_destination, content['data']) r = uploader.upload_file(log_destination, content['data'])
print 'Content uploaded to %s' % log_destination, r print('Content uploaded to %s' % log_destination, r)
with open(state_file, 'w') as f: with open(state_file, 'w') as f:
f.write(str(content['position'])) f.write(str(content['position']))
else: else:
print 'No Ansible log file found in %s.' % ansible_log print('No Ansible log file found in %s.' % ansible_log)
# Post Ansible execution result # Post Ansible execution result
uploader.upload_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