Skip to content
Snippets Groups Projects
Commit 73bacc48 authored by Stéphane Diemer's avatar Stéphane Diemer
Browse files

Handle systemd service and timer in tester script | refs #33688

parent 85274931
No related branches found
No related tags found
No related merge requests found
...@@ -133,6 +133,8 @@ MEDIAIMPORT_USER='' ...@@ -133,6 +133,8 @@ MEDIAIMPORT_USER=''
MEDIAIMPORT_PASSWD='' MEDIAIMPORT_PASSWD=''
# -- Tester config -- # -- Tester config --
# enable systemd timer to execute tests by default
TESTER_ENABLE_SYSTEMD_TIMER='1'
# separate values with commas # separate values with commas
TESTER_IGNORED_TESTS='' TESTER_IGNORED_TESTS=''
# separate values with commas # separate values with commas
......
[Unit]
Description=Envsetup tests service
[Service]
Type=simple
EnvironmentFile=/etc/environment
ExecStart=/bin/bash -c -- 'if [ -f /root/envsetup/tests/tester.py ]; then mkdir -p /root/envsetup/tests/logs; /root/envsetup/tests/tester.py -e >/root/envsetup/tests/logs/tester_service.log 2>&1; fi'
[Unit]
Description=Envsetup tests timer
[Timer]
OnCalendar=*-*-* 6:15:00
[Install]
WantedBy=timers.target
...@@ -10,7 +10,6 @@ import base64 ...@@ -10,7 +10,6 @@ import base64
import datetime import datetime
import glob import glob
import os import os
import re
import socket import socket
import subprocess import subprocess
import sys import sys
...@@ -18,7 +17,9 @@ import time ...@@ -18,7 +17,9 @@ import time
import uuid import uuid
from utilities.config import load_conf, get_conf from utilities.config import load_conf, get_conf
from utilities.logging import strip_colors, escape_html
from utilities.os import get_dir from utilities.os import get_dir
from utilities.systemd import check_systemd_setup
OUT_OF_SUPPORT_TEXT = '''\033[93mWarning: OUT_OF_SUPPORT_TEXT = '''\033[93mWarning:
The system is out of support, UbiCast will not be notified if errors are detected. The system is out of support, UbiCast will not be notified if errors are detected.
...@@ -45,24 +46,6 @@ sys.stdout = Logger(sys.stdout, log_buffer) ...@@ -45,24 +46,6 @@ sys.stdout = Logger(sys.stdout, log_buffer)
sys.stderr = sys.stdout sys.stderr = sys.stdout
def strip_colors(text):
return re.sub(r'\033\[[\d;]+m', '', text)
def escape(text):
html = text.strip()
html = html.replace('<', '&lt;')
html = html.replace('>', '&gt;')
html = html.replace('\033[90m', '<span style="color: gray;">')
html = html.replace('\033[91m', '<span style="color: red;">')
html = html.replace('\033[92m', '<span style="color: green;">')
html = html.replace('\033[93m', '<span style="color: orange;">')
html = html.replace('\033[94m', '<span style="color: blue;">')
html = html.replace('\033[95m', '<span style="color: purple;">')
html = strip_colors(html)
return html
def raid_idle(): def raid_idle():
idle = True idle = True
devs = glob.glob('/sys/block/md*/md/sync_action') devs = glob.glob('/sys/block/md*/md/sync_action')
...@@ -135,6 +118,8 @@ class Tester(): ...@@ -135,6 +118,8 @@ class Tester():
self.logs_dir = os.path.join(self.root_dir, 'logs') self.logs_dir = os.path.join(self.root_dir, 'logs')
os.makedirs(self.logs_dir, exist_ok=True) os.makedirs(self.logs_dir, exist_ok=True)
print('Logs dir is "%s".' % self.logs_dir) print('Logs dir is "%s".' % self.logs_dir)
# Check systemd service and timer
check_systemd_setup()
# Run tests # Run tests
now, failures, out_of_support, log_content, html_report = self.run_tests(tests) now, failures, out_of_support, log_content, html_report = self.run_tests(tests)
if args.send_email: if args.send_email:
...@@ -426,11 +411,11 @@ class Tester(): ...@@ -426,11 +411,11 @@ class Tester():
html_cell = 'th' if not html_report else 'td' html_cell = 'th' if not html_report else 'td'
html_report += '\n <tr>' html_report += '\n <tr>'
for i, val in enumerate(row): for i, val in enumerate(row):
html_report += ' <%s>%s</%s>' % (html_cell, escape(val), html_cell) html_report += ' <%s>%s</%s>' % (html_cell, escape_html(val), html_cell)
html_report += ' </tr>' html_report += ' </tr>'
html_report = '<table border="1">%s\n</table>' % html_report html_report = '<table border="1">%s\n</table>' % html_report
if out_of_support: if out_of_support:
html_report = '<p>' + escape(OUT_OF_SUPPORT_TEXT) + '</p>\n' + html_report html_report = '<p>' + escape_html(OUT_OF_SUPPORT_TEXT) + '</p>\n' + html_report
# Store locally results # Store locally results
now = datetime.datetime.utcnow() now = datetime.datetime.utcnow()
history_file = os.path.join(self.logs_dir, 'tests_history.txt') history_file = os.path.join(self.logs_dir, 'tests_history.txt')
......
#!/usr/bin/env python3
'''
EnvSetup systemd handler.
'''
import os
import subprocess
from .config import get_conf
def check_systemd_setup():
'''
Add envsetup timer and service and enable them if enabled in configuration.
Confiugration key is TESTER_ENABLE_SYSTEMD_TIMER.
'''
# Remove old files
deprecated_files = (
'/etc/systemd/system/envsetup-tests.service',
'/etc/systemd/system/envsetup-tests.timer',
'/etc/systemd/system/ubicast-config.service'
)
for path in deprecated_files:
if os.path.exists(path):
os.remove(path)
print('Removed deprecated file: "%s".' % path)
# Write systemd files if needed
template_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'systemd')
files_to_write = (
('/lib/systemd/system/envsetup-tests.service', os.path.join(template_dir, 'envsetup-tests.service')),
('/lib/systemd/system/envsetup-tests.timer', os.path.join(template_dir, 'envsetup-tests.timer')),
)
for path, template in files_to_write:
content = ''
if os.path.exists(path):
with open(path, 'r') as fo:
content = fo.read()
with open(template, 'r') as fo:
expected = fo.read()
if not content or content != expected:
# Replace file if different
with open(path, 'w') as fo:
fo.write(expected)
print('File "%s" updated.' % path)
else:
print('File "%s" is already up to date.' % path)
# Enable systemd timer if needed
if get_conf('TESTER_ENABLE_SYSTEMD_TIMER') == '1':
print('Checking status of envsetup systemd timer...')
p = subprocess.run(['systemctl', 'is-enabled', 'envsetup-tests.timer'])
if p.returncode != 0:
subprocess.run(['systemctl', 'enable', 'envsetup-tests.timer'])
subprocess.run(['systemctl', 'restart', 'envsetup-tests.timer'])
print('Enabled "envsetup-tests.timer" in systemd.')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment