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

Avoid imp usage in monitoring and worker tests

parent 38fb2219
No related branches found
No related tags found
No related merge requests found
...@@ -4,43 +4,25 @@ ...@@ -4,43 +4,25 @@
Criticality: High Criticality: High
Checks that MediaWorker can be reached using SSH and that it can reach the tasks server Checks that MediaWorker can be reached using SSH and that it can reach the tasks server
''' '''
import imp from pathlib import Path
import os
import re import re
import subprocess import subprocess
import sys import sys
GREEN = '\033[92m' sys.path.append(str(Path(__file__).parents[1].resolve()))
RED = '\033[91m'
DEF = '\033[0m'
# pylint: disable=wrong-import-position
try: from envsetup import utils as u # noqa: E402
import mediaserver
except ImportError:
print('MediaServer is not installed, skipping test.')
sys.exit(2)
else:
print('MediaServer version: %s.' % mediaserver.__version__)
os.chdir(os.path.dirname(__file__))
if not os.path.isfile('../utils.py'):
print('conf.sh not found.')
sys.exit(1)
es_utils = imp.load_source('es_utils', '../utils.py')
conf = es_utils.load_conf()
def check_ssh(ip): def check_ssh(ip):
cmd = 'ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no root@%s ls /tmp' % ip cmd = 'ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no root@%s ls /tmp' % ip
print('Connecting to MediaWorker:\n%s' % cmd) u.log('Connecting to MediaWorker:\n%s' % cmd)
try: try:
subprocess.check_output(cmd, shell=True, timeout=5) subprocess.check_output(cmd, shell=True, timeout=5)
print('%sLogged in successfully in "%s".%s' % (GREEN, ip, DEF)) u.success('Logged in successfully in "%s".' % ip)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
print('%sFailed to login using SSH, run "ssh-copy-id %s".%s' % (RED, ip, DEF)) u.error('Failed to login using SSH, run "ssh-copy-id %s".' % ip)
return False return False
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
try: try:
...@@ -53,26 +35,10 @@ def check_ssh(ip): ...@@ -53,26 +35,10 @@ def check_ssh(ip):
return True return True
def run_tests(ip):
print('Updating envsetup tests on MediaWorker.')
cmd = 'ssh -t root@%s /root/envsetup/update_envsetup.py' % ip
os.system(cmd)
print('Running envsetup tests on MediaWorker.')
cmd = 'ssh -t root@%s /root/envsetup/tester.py' % ip
status = os.system(cmd)
if status == 0:
subprocess.check_output(cmd, shell=True, timeout=60)
print('%sAll tests completed on MediaWorker "%s".' % (GREEN, ip, DEF))
return True
else:
print('%sapt-get update failed on MediaWorker "%s".%s' % (RED, ip, DEF))
return False
def check_celerity_connectivity(ip): def check_celerity_connectivity(ip):
print('Getting celerity server url.') u.log('Getting celerity server url.')
cmd = 'ssh -t root@%s cat /etc/celerity/config.py' % ip cmd = 'ssh -t root@%s cat /etc/celerity/config.py' % ip
print(cmd) u.log(cmd)
try: try:
d = subprocess.check_output(cmd, shell=True, timeout=5, universal_newlines=True) d = subprocess.check_output(cmd, shell=True, timeout=5, universal_newlines=True)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
...@@ -80,78 +46,91 @@ def check_celerity_connectivity(ip): ...@@ -80,78 +46,91 @@ def check_celerity_connectivity(ip):
else: else:
m = re.search(r'\nSERVER_URL\s*=\s*([:\/\'\"\-\_\.\w]+)', d) m = re.search(r'\nSERVER_URL\s*=\s*([:\/\'\"\-\_\.\w]+)', d)
if not m: if not m:
print('%sFailed to get celerity tasks server url from configuration in MediaWorker "%s".%s' % (RED, ip, DEF)) u.error('Failed to get celerity tasks server url from configuration in MediaWorker "%s".' % ip)
return False return False
server_url = m.groups()[0].strip('"\' ') server_url = m.groups()[0].strip('"\' ')
print('Checking celerity connectivity.') u.log('Checking celerity connectivity.')
cmd = 'ssh -t root@%s curl -k %s' % (ip, server_url) cmd = 'ssh -t root@%s curl -k %s' % (ip, server_url)
print(cmd) u.log(cmd)
try: try:
d = subprocess.check_output(cmd, shell=True, timeout=5, universal_newlines=True) d = subprocess.check_output(cmd, shell=True, timeout=5, universal_newlines=True)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
d = '' d = ''
if 'Celerity tasks server' in d: if 'Celerity tasks server' in d:
print('%sSuccessfully reached tasks server from MediaWorker "%s".%s' % (GREEN, ip, DEF)) u.success('%sSuccessfully reached tasks server from MediaWorker "%s".%s' % ip)
return True return True
print('%sFailed to reach tasks server from MediaWorker "%s".%s' % (RED, ip, DEF)) u.error('Failed to reach tasks server from MediaWorker "%s".' % ip)
return False return False
def check_celerity_versions(ip): def check_celerity_versions(ip):
print('Checking that celerity server and worker uses the same version.') u.log('Checking that celerity server and worker uses the same version.')
try: try:
ms_out = subprocess.check_output('dpkg -s celerity-utils | grep "^Version:"', shell=True, timeout=10, universal_newlines=True) ms_out = subprocess.check_output('dpkg -s celerity-utils | grep "^Version:"', shell=True, timeout=10, universal_newlines=True)
mw_out = subprocess.check_output('ssh -t root@%s dpkg -s celerity-utils | grep "^Version:"' % ip, shell=True, timeout=10, universal_newlines=True) mw_out = subprocess.check_output('ssh -t root@%s dpkg -s celerity-utils | grep "^Version:"' % ip, shell=True, timeout=10, universal_newlines=True)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
print('%sFailed to check celerity version in MediaWorker "%s":\n%s%s' % (RED, ip, e, DEF)) u.error('Failed to check celerity version in MediaWorker "%s":\n%s' % ip, e)
return False return False
ms_out = (ms_out[len('Version:'):] if ms_out.startswith('Version:') else ms_out).strip() ms_out = (ms_out[len('Version:'):] if ms_out.startswith('Version:') else ms_out).strip()
mw_out = (mw_out[len('Version:'):] if mw_out.startswith('Version:') else mw_out).strip() mw_out = (mw_out[len('Version:'):] if mw_out.startswith('Version:') else mw_out).strip()
if ms_out != mw_out: if ms_out != mw_out:
print('%sThe celerity version in MediaWorker "%s" is not the same as in MediaServer.%s\nMediaServer version: \tn%s\nMediaWorker version: \t%s' % (RED, ip, DEF, ms_out, mw_out)) u.error('The celerity version in MediaWorker "%s" is not the same as in MediaServer.\nMediaServer version: \tn%s\nMediaWorker version: \t%s' % (ip, ms_out, mw_out))
return False return False
print('%sThe celerity version in MediaWorker "%s" is the same as in MediaServer.%s\nCurrent celerity version is: %s.' % (GREEN, ip, DEF, ms_out)) u.success('The celerity version in MediaWorker "%s" is the same as in MediaServer.\nCurrent celerity version is: %s.' % (ip, ms_out))
return True return True
def check_mediaworker_in_whitelist(ip): def run_tests(ip):
# this check is not usefull anymore because the worker uses the u.log('Updating envsetup tests on MediaWorker.')
# API to get links to resources with valid secure link token cmd = 'ssh -t root@%s /root/envsetup/update_envsetup.py' % ip
nginx_vhosts_path = '/etc/nginx/sites-enabled' subprocess.run(cmd, shell=True)
vhosts = os.listdir(nginx_vhosts_path) u.log('Running envsetup tests on MediaWorker.')
for v in vhosts: cmd = 'ssh -t root@%s /root/envsetup/tester.py' % ip
if v.endswith('.conf'): p = subprocess.run(cmd, shell=True)
vhost_path = os.path.join(nginx_vhosts_path, v) if p.returncode == 0:
with open(vhost_path, 'r') as f: subprocess.check_output(cmd, shell=True, timeout=60)
d = f.read() u.success('All tests completed on MediaWorker "%s".' % ip)
if 'msuser_whitelist' in d: return True
if ip not in d: else:
print('%sMediaWorker ip %s is not in %s whitelist%s' % (RED, ip, v, DEF)) u.error('apt-get update failed on MediaWorker "%s".' % ip)
return False return False
return True
all_ok = True def main():
tested = False try:
mediaserver_ip = conf.get('NETWORK_IP') import mediaserver
worker_ips = conf.get('CELERITY_WORKER_IP') except ImportError:
for worker_ip in worker_ips.split(','): u.log('MediaServer is not installed, skipping test.')
worker_ip = worker_ip.strip() return 2
if worker_ip and not worker_ip.startswith('127.0.') and worker_ip != mediaserver_ip: else:
tested = True u.log('MediaServer version: %s.' % mediaserver.__version__)
if not check_ssh(worker_ip):
all_ok = False all_ok = True
else: tested = False
if not check_celerity_connectivity(worker_ip): mediaserver_ip = u.get_conf('NETWORK_IP')
all_ok = False worker_ips = u.get_conf('CELERITY_WORKER_IP')
if not check_celerity_versions(worker_ip): for worker_ip in worker_ips.split(','):
worker_ip = worker_ip.strip()
if worker_ip and not worker_ip.startswith('127.0.') and worker_ip != mediaserver_ip:
tested = True
if not check_ssh(worker_ip):
all_ok = False all_ok = False
# if not run_tests(worker_ip): else:
# all_ok = False if not check_celerity_connectivity(worker_ip):
# if not check_mediaworker_in_whitelist(worker_ip): all_ok = False
# all_ok = False if not check_celerity_versions(worker_ip):
if not tested: all_ok = False
print('Celerity IP not set or running locally, skipping test.') # if not run_tests(worker_ip):
sys.exit(2) # all_ok = False
if not tested:
sys.exit(int(not all_ok)) u.log('Celerity IP not set or running locally, skipping test.')
return 2
if not all_ok:
return 1
return 0
if __name__ == '__main__':
code = main()
sys.exit(code)
...@@ -5,17 +5,15 @@ Criticality: Low ...@@ -5,17 +5,15 @@ Criticality: Low
Check that the monitoring graphs work. Check that the monitoring graphs work.
''' '''
from datetime import datetime from datetime import datetime
import imp from pathlib import Path
import os import os
import subprocess import subprocess
import sys import sys
os.chdir(os.path.dirname(__file__)) sys.path.append(str(Path(__file__).parents[1].resolve()))
if not os.path.isfile('../utils.py'):
print('The envsetup configuration was not found.') # pylint: disable=wrong-import-position
sys.exit(1) from envsetup import utils as u # noqa: E402
u = imp.load_source('es_utils', '../utils.py')
conf = u.load_conf()
MUNIN_WWW_PATH = '/var/cache/munin/www/' MUNIN_WWW_PATH = '/var/cache/munin/www/'
...@@ -61,5 +59,5 @@ def check_munin(): ...@@ -61,5 +59,5 @@ def check_munin():
if __name__ == '__main__': if __name__ == '__main__':
rc = check_munin() code = check_munin()
sys.exit(rc) sys.exit(code)
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