Skip to content
Snippets Groups Projects
test_mediaworker.py 2.65 KiB
Newer Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Criticality: High
Florent Thiery's avatar
Florent Thiery committed
Checks that MediaWorker can be reached using SSH and that it can reach the tasks server
'''
import os
import imp
import sys
import subprocess

GREEN = '\033[92m'
RED = '\033[91m'
DEF = '\033[0m'


try:
    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):
    cmd = 'ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no %s ls /tmp' % ip
    print('Connecting to MediaWorker: %s' % cmd)
    try:
        subprocess.check_output(cmd, shell=True, timeout=2)
        print('%sLogged in successfully%s' % (GREEN, DEF))
    except subprocess.CalledProcessError:
        print('%sFailed to login using SSH, run ssh-copy-id %s %s' % (RED, ip, DEF))
        return False
    return True

def run_tests(ip):
Florent Thiery's avatar
Florent Thiery committed
    print('Updating envsetup tests on MediaWorker')
Florent Thiery's avatar
Florent Thiery committed
    cmd = 'ssh -t %s cd /root/envsetup && git pull' % ip
Florent Thiery's avatar
Florent Thiery committed
    os.system(cmd)
    print('Running envsetup tests on MediaWorker')
    cmd = 'ssh -t %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, DEF))
        return True
    else:
        print('%apt-get update failed on MediaWorker%s' % (RED, DEF))
        return False

def check_celerity_connectivity(ip):
    h = conf.get('MS_SERVER_NAME')
Florent Thiery's avatar
Florent Thiery committed
    cmd = "ssh %s curl -k https://%s:6200" % (ip, h)
    print('Checking celerity connectivity: %s' % cmd)
Florent Thiery's avatar
Florent Thiery committed
    try:
        d = subprocess.check_output(cmd, shell=True, timeout=5, universal_newlines=True)
Florent Thiery's avatar
Florent Thiery committed
    except subprocess.CalledProcessError:
Florent Thiery's avatar
Florent Thiery committed
        d = ''
    if "Celerity tasks server" in d:
        print('%sSuccessfully reached tasks server%s' % (GREEN, DEF))
        return True
Florent Thiery's avatar
Florent Thiery committed
    print('%sMediaWorker %s failed to reach tasks server%s' % (RED, ip, DEF))
mediaserver_ip = conf.get('NETWORK_IP')
worker_ip = conf.get('CELERITY_WORKER_IP')
if worker_ip != '127.0.1.1' and worker_ip != mediaserver_ip:
    if not check_ssh(worker_ip):
        all_ok = False
        if not check_celerity_connectivity(worker_ip):
        if not run_tests(worker_ip):
else:
    print('Celerity IP not set or running locally, skipping test')
    sys.exit(2)
Florent Thiery's avatar
Florent Thiery committed
sys.exit(int(not all_ok))