2019-05-15 20:32:50 +02:00
|
|
|
#!/usr/bin/python3
|
2018-10-05 15:26:10 +02:00
|
|
|
|
|
|
|
import argparse
|
2018-10-12 11:06:54 +02:00
|
|
|
import logging
|
2018-10-05 15:26:10 +02:00
|
|
|
import pika
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import osc
|
|
|
|
import re
|
2018-10-21 20:53:37 +02:00
|
|
|
from time import sleep
|
2018-10-10 20:59:16 +02:00
|
|
|
from osc.core import http_GET, http_POST, makeurl
|
2018-10-21 20:53:37 +02:00
|
|
|
from M2Crypto.SSL import SSLError as SSLError
|
2018-10-05 15:26:10 +02:00
|
|
|
from osclib.conf import Config
|
|
|
|
from osclib.stagingapi import StagingAPI
|
|
|
|
from lxml import etree as ET
|
2018-10-08 14:05:25 +02:00
|
|
|
from openqa_client.client import OpenQA_Client
|
2018-10-21 20:53:37 +02:00
|
|
|
from openqa_client.exceptions import ConnectionError
|
2019-05-15 20:32:50 +02:00
|
|
|
from urllib.error import HTTPError, URLError
|
|
|
|
from urllib.parse import quote_plus
|
2018-11-15 14:03:26 +01:00
|
|
|
|
|
|
|
import requests
|
2018-10-12 11:06:54 +02:00
|
|
|
from PubSubConsumer import PubSubConsumer
|
2018-10-05 15:26:10 +02:00
|
|
|
|
|
|
|
class Project(object):
|
|
|
|
def __init__(self, name):
|
2018-10-12 11:28:42 +02:00
|
|
|
self.name = name
|
2018-10-05 15:26:10 +02:00
|
|
|
Config(apiurl, name)
|
|
|
|
self.api = StagingAPI(apiurl, name)
|
|
|
|
self.staging_projects = dict()
|
2018-10-08 14:05:25 +02:00
|
|
|
self.listener = None
|
2018-11-03 18:28:57 +01:00
|
|
|
self.logger = logging.getLogger(__name__)
|
2018-10-10 20:59:16 +02:00
|
|
|
self.replace_string = self.api.attribute_value_load('OpenQAMapping')
|
2018-10-12 11:28:42 +02:00
|
|
|
|
|
|
|
def init(self):
|
2018-10-05 15:26:10 +02:00
|
|
|
for p in self.api.get_staging_projects():
|
|
|
|
if self.api.is_adi_project(p):
|
|
|
|
continue
|
|
|
|
self.staging_projects[p] = self.initial_staging_state(p)
|
2018-10-12 11:28:42 +02:00
|
|
|
self.update_staging_status(p)
|
2018-10-05 15:26:10 +02:00
|
|
|
|
|
|
|
def staging_letter(self, name):
|
|
|
|
return name.split(':')[-1]
|
|
|
|
|
|
|
|
def map_iso(self, staging_project, iso):
|
2018-10-10 20:59:16 +02:00
|
|
|
parts = self.replace_string.split('/')
|
2018-10-09 09:07:34 +02:00
|
|
|
if parts[0] != 's':
|
|
|
|
raise Exception("{}'s iso_replace_string does not start with s/".format(self.name))
|
|
|
|
old = parts[1]
|
|
|
|
new = parts[2]
|
|
|
|
new = new.replace('$LETTER', self.staging_letter(staging_project))
|
|
|
|
return re.compile(old).sub(new, iso)
|
2018-10-05 15:26:10 +02:00
|
|
|
|
|
|
|
def gather_isos(self, name, repository):
|
|
|
|
url = self.api.makeurl(['published', name, repository, 'iso'])
|
|
|
|
f = self.api.retried_GET(url)
|
|
|
|
root = ET.parse(f).getroot()
|
|
|
|
ret = []
|
|
|
|
for entry in root.findall('entry'):
|
|
|
|
if entry.get('name').endswith('iso'):
|
|
|
|
ret.append(self.map_iso(name, entry.get('name')))
|
|
|
|
return ret
|
|
|
|
|
2018-10-08 10:37:28 +02:00
|
|
|
def gather_buildid(self, name, repository):
|
|
|
|
url = self.api.makeurl(['published', name, repository], {'view': 'status'})
|
|
|
|
f = self.api.retried_GET(url)
|
|
|
|
id = ET.parse(f).getroot().find('buildid')
|
|
|
|
if id is not None:
|
|
|
|
return id.text
|
|
|
|
|
2018-10-05 15:26:10 +02:00
|
|
|
def initial_staging_state(self, name):
|
2018-10-08 10:37:28 +02:00
|
|
|
return {'isos': self.gather_isos(name, 'images'),
|
|
|
|
'id': self.gather_buildid(name, 'images')}
|
2018-10-05 15:26:10 +02:00
|
|
|
|
2018-10-08 14:05:25 +02:00
|
|
|
def fetch_openqa_jobs(self, staging, iso):
|
|
|
|
buildid = self.staging_projects[staging].get('id')
|
|
|
|
if not buildid:
|
2018-10-12 11:06:54 +02:00
|
|
|
self.logger.info("I don't know the build id of " + staging)
|
2018-10-08 14:05:25 +02:00
|
|
|
return
|
2018-10-08 20:09:10 +02:00
|
|
|
# all openQA jobs are created at the same URL
|
|
|
|
url = self.api.makeurl(['status_reports', 'published', staging, 'images', 'reports', buildid])
|
2018-10-08 14:05:25 +02:00
|
|
|
openqa = self.listener.jobs_for_iso(iso)
|
2018-10-08 20:09:10 +02:00
|
|
|
# collect job infos to pick names
|
|
|
|
openqa_infos = dict()
|
2018-10-08 14:05:25 +02:00
|
|
|
for job in openqa:
|
|
|
|
print(staging, iso, job['id'], job['state'], job['result'],
|
|
|
|
job['settings']['MACHINE'], job['settings']['TEST'])
|
2018-10-08 20:09:10 +02:00
|
|
|
openqa_infos[job['id']] = {'url': self.listener.test_url(job)}
|
|
|
|
openqa_infos[job['id']]['state'] = self.map_openqa_result(job)
|
|
|
|
openqa_infos[job['id']]['name'] = job['settings']['TEST']
|
|
|
|
openqa_infos[job['id']]['machine'] = job['settings']['MACHINE']
|
|
|
|
|
|
|
|
# make sure the names are unique
|
|
|
|
taken_names = dict()
|
|
|
|
for id in openqa_infos:
|
|
|
|
name = openqa_infos[id]['name']
|
|
|
|
if name in taken_names:
|
|
|
|
openqa_infos[id]['name'] = openqa_infos[id]['name'] + "@" + openqa_infos[id]['machine']
|
|
|
|
# the other id
|
|
|
|
id = taken_names[name]
|
|
|
|
openqa_infos[id]['name'] = openqa_infos[id]['name'] + "@" + openqa_infos[id]['machine']
|
|
|
|
taken_names[name] = id
|
|
|
|
|
|
|
|
for info in openqa_infos.values():
|
2018-11-03 18:28:57 +01:00
|
|
|
xml = self.openqa_check_xml(info['url'], info['state'], 'openqa:' + info['name'])
|
2018-10-08 15:43:45 +02:00
|
|
|
try:
|
|
|
|
http_POST(url, data=xml)
|
|
|
|
except HTTPError:
|
2018-10-12 11:06:54 +02:00
|
|
|
self.logger.error('failed to post status to ' + url)
|
2018-10-08 14:05:25 +02:00
|
|
|
|
|
|
|
def update_staging_status(self, project):
|
|
|
|
for iso in self.staging_projects[project]['isos']:
|
|
|
|
self.fetch_openqa_jobs(project, iso)
|
|
|
|
|
2018-10-05 15:26:10 +02:00
|
|
|
def update_staging_buildid(self, project, repository, buildid):
|
|
|
|
self.staging_projects[project]['id'] = buildid
|
|
|
|
self.staging_projects[project]['isos'] = self.gather_isos(project, repository)
|
2018-10-08 14:05:25 +02:00
|
|
|
self.update_staging_status(project)
|
2018-10-05 15:26:10 +02:00
|
|
|
|
|
|
|
def check_published_repo(self, project, repository, buildid):
|
|
|
|
if repository != 'images':
|
|
|
|
return
|
|
|
|
for p in self.staging_projects:
|
|
|
|
if project == p:
|
|
|
|
self.update_staging_buildid(project, repository, buildid)
|
|
|
|
|
|
|
|
def matching_project(self, iso):
|
|
|
|
for p in self.staging_projects:
|
|
|
|
if iso in self.staging_projects[p]['isos']:
|
|
|
|
return p
|
|
|
|
|
2018-10-08 14:05:25 +02:00
|
|
|
def map_openqa_result(self, job):
|
|
|
|
if job['result'] in ['passed', 'softfailed']:
|
2018-10-05 15:26:10 +02:00
|
|
|
return 'success'
|
2018-10-08 14:05:25 +02:00
|
|
|
if job['result'] == 'none':
|
|
|
|
return 'pending'
|
2018-10-05 15:26:10 +02:00
|
|
|
return 'failure'
|
|
|
|
|
2018-10-08 14:05:25 +02:00
|
|
|
def openqa_job_change(self, iso):
|
2018-10-05 15:26:10 +02:00
|
|
|
staging = self.matching_project(iso)
|
|
|
|
if not staging:
|
|
|
|
return
|
2018-10-08 14:05:25 +02:00
|
|
|
# we fetch all openqa jobs so we can avoid long job names
|
|
|
|
self.fetch_openqa_jobs(staging, iso)
|
2018-10-05 15:26:10 +02:00
|
|
|
|
2018-10-08 14:05:25 +02:00
|
|
|
def openqa_check_xml(self, url, state, name):
|
2018-10-05 15:26:10 +02:00
|
|
|
check = ET.Element('check')
|
|
|
|
se = ET.SubElement(check, 'url')
|
2018-10-08 14:05:25 +02:00
|
|
|
se.text = url
|
2018-10-05 15:26:10 +02:00
|
|
|
se = ET.SubElement(check, 'state')
|
|
|
|
se.text = state
|
|
|
|
se = ET.SubElement(check, 'name')
|
|
|
|
se.text = name
|
|
|
|
return ET.tostring(check)
|
|
|
|
|
2018-10-08 20:09:10 +02:00
|
|
|
|
2018-10-12 11:06:54 +02:00
|
|
|
class Listener(PubSubConsumer):
|
2018-11-24 14:19:00 +01:00
|
|
|
def __init__(self, amqp_prefix, openqa_url):
|
|
|
|
super(Listener, self).__init__(amqp_prefix, logging.getLogger(__name__))
|
2018-10-05 15:26:10 +02:00
|
|
|
self.projects = []
|
|
|
|
self.amqp_prefix = amqp_prefix
|
2018-10-08 14:05:25 +02:00
|
|
|
self.openqa_url = openqa_url
|
|
|
|
self.openqa = OpenQA_Client(server=openqa_url)
|
|
|
|
|
2018-10-12 11:06:54 +02:00
|
|
|
def routing_keys(self):
|
|
|
|
ret = []
|
|
|
|
for suffix in ['.obs.repo.published', '.openqa.job.done',
|
|
|
|
'.openqa.job.create', '.openqa.job.restart']:
|
|
|
|
ret.append(self.amqp_prefix + suffix)
|
|
|
|
return ret
|
2018-10-05 15:26:10 +02:00
|
|
|
|
|
|
|
def add(self, project):
|
2018-10-12 11:28:42 +02:00
|
|
|
project.listener = self
|
2018-10-05 15:26:10 +02:00
|
|
|
self.projects.append(project)
|
|
|
|
|
2018-10-12 11:28:42 +02:00
|
|
|
def start_consuming(self):
|
|
|
|
# now we are (re-)connected to the bus and need to fetch the
|
|
|
|
# initial state
|
|
|
|
for project in self.projects:
|
|
|
|
self.logger.info('Fetching ISOs of %s', project.name)
|
|
|
|
project.init()
|
|
|
|
self.logger.info('Finished fetching initial ISOs, listening')
|
|
|
|
super(Listener, self).start_consuming()
|
|
|
|
|
2018-10-08 14:05:25 +02:00
|
|
|
def jobs_for_iso(self, iso):
|
|
|
|
values = {
|
|
|
|
'iso': iso,
|
|
|
|
'scope': 'current',
|
|
|
|
'latest': '1',
|
|
|
|
}
|
|
|
|
return self.openqa.openqa_request('GET', 'jobs', values)['jobs']
|
|
|
|
|
2018-10-08 15:43:45 +02:00
|
|
|
def get_step_url(self, testurl, modulename):
|
|
|
|
failurl = testurl + '/modules/{!s}/fails'.format(quote_plus(modulename))
|
|
|
|
fails = requests.get(failurl).json()
|
|
|
|
failed_step = fails.get('first_failed_step', 1)
|
|
|
|
return "{!s}#step/{!s}/{:d}".format(testurl, modulename, failed_step)
|
|
|
|
|
2018-10-08 14:05:25 +02:00
|
|
|
def test_url(self, job):
|
2018-10-08 15:43:45 +02:00
|
|
|
url = self.openqa_url + ("/tests/%d" % job['id'])
|
|
|
|
if job['result'] == 'failed':
|
|
|
|
for module in job['modules']:
|
|
|
|
if module['result'] == 'failed':
|
|
|
|
return self.get_step_url(url, module['name'])
|
|
|
|
return url
|
2018-10-08 14:05:25 +02:00
|
|
|
|
2018-10-05 15:26:10 +02:00
|
|
|
def on_published_repo(self, payload):
|
|
|
|
for p in self.projects:
|
|
|
|
p.check_published_repo(str(payload['project']), str(payload['repo']), str(payload['buildid']))
|
2019-07-19 12:32:27 +02:00
|
|
|
# notify openQA to sync the projects - the plugin will check itself it
|
|
|
|
# the project is to be synced. For now we notify about every 'images' repo
|
|
|
|
if payload['repo'] == 'images':
|
|
|
|
self.openqa.openqa_request('PUT', 'obs_rsync', str(payload['project']), 'runs')
|
2018-10-05 15:26:10 +02:00
|
|
|
|
2018-10-08 14:05:25 +02:00
|
|
|
def on_openqa_job(self, iso):
|
2019-01-30 16:06:11 +01:00
|
|
|
self.logger.debug('openqa_job_change %s', iso)
|
2018-10-05 15:26:10 +02:00
|
|
|
for p in self.projects:
|
2018-10-08 14:05:25 +02:00
|
|
|
p.openqa_job_change(iso)
|
2018-10-05 15:26:10 +02:00
|
|
|
|
|
|
|
def on_message(self, unused_channel, method, properties, body):
|
2019-05-20 11:43:04 +02:00
|
|
|
self.acknowledge_message(method.delivery_tag)
|
2018-10-05 15:26:10 +02:00
|
|
|
if method.routing_key == '{}.obs.repo.published'.format(amqp_prefix):
|
|
|
|
self.on_published_repo(json.loads(body))
|
2018-10-08 19:47:16 +02:00
|
|
|
elif re.search(r'.openqa.', method.routing_key):
|
2018-10-08 14:05:25 +02:00
|
|
|
self.on_openqa_job(json.loads(body).get('ISO'))
|
2018-10-08 19:47:16 +02:00
|
|
|
else:
|
2018-10-12 11:06:54 +02:00
|
|
|
self.logger.warning("unknown rabbitmq message {}".format(method.routing_key))
|
2018-10-08 14:05:25 +02:00
|
|
|
|
2018-10-05 15:26:10 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Bot to sync openQA status to OBS')
|
2018-10-23 08:13:31 +02:00
|
|
|
parser.add_argument("--apiurl", '-A', type=str, help='API URL of OBS')
|
2018-10-05 15:26:10 +02:00
|
|
|
parser.add_argument('-s', '--staging', type=str, default=None,
|
|
|
|
help='staging project letter')
|
|
|
|
parser.add_argument('-f', '--force', action='store_true', default=False,
|
|
|
|
help='force the write of the comment')
|
|
|
|
parser.add_argument('-p', '--project', type=str, default='Factory',
|
|
|
|
help='openSUSE version to make the check (Factory, 13.2)')
|
|
|
|
parser.add_argument('-d', '--debug', action='store_true', default=False,
|
|
|
|
help='enable debug information')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2018-10-23 08:13:31 +02:00
|
|
|
osc.conf.get_config(override_apiurl = args.apiurl)
|
2018-10-05 15:26:10 +02:00
|
|
|
osc.conf.config['debug'] = args.debug
|
|
|
|
|
2018-10-23 08:13:31 +02:00
|
|
|
apiurl = osc.conf.config['apiurl']
|
2018-10-05 15:26:10 +02:00
|
|
|
|
|
|
|
if apiurl.endswith('suse.de'):
|
|
|
|
amqp_prefix = 'suse'
|
2018-10-08 14:05:25 +02:00
|
|
|
openqa_url = 'https://openqa.suse.de'
|
2018-10-05 15:26:10 +02:00
|
|
|
else:
|
|
|
|
amqp_prefix = 'opensuse'
|
2018-10-08 14:05:25 +02:00
|
|
|
openqa_url = 'https://openqa.opensuse.org'
|
|
|
|
|
2018-10-12 11:06:54 +02:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
2018-11-24 14:19:00 +01:00
|
|
|
l = Listener(amqp_prefix, openqa_url)
|
2018-10-10 20:59:16 +02:00
|
|
|
url = makeurl(apiurl, ['search', 'project', 'id'], {'match': 'attribute/@name="OSRT:OpenQAMapping"'})
|
|
|
|
f = http_GET(url)
|
|
|
|
root = ET.parse(f).getroot()
|
|
|
|
for entry in root.findall('project'):
|
|
|
|
l.add(Project(entry.get('name')))
|
2018-10-05 15:26:10 +02:00
|
|
|
|
2018-11-03 18:28:57 +01:00
|
|
|
try:
|
2019-03-09 08:25:26 +01:00
|
|
|
l.run(runtime=3600)
|
2018-11-03 18:28:57 +01:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
l.stop()
|