diff --git a/module_build_service/builder.py b/module_build_service/builder.py index e9e74b28..151e72ce 100644 --- a/module_build_service/builder.py +++ b/module_build_service/builder.py @@ -1038,7 +1038,7 @@ class CoprModuleBuilder(GenericBuilder): else: return self.build_srpm(artifact_name, source) - def build_srpm(self, artifact_name, source): + def build_srpm(self, artifact_name, source, build_id=None): if not self.__prep: raise RuntimeError("Buildroot is not prep-ed") @@ -1047,9 +1047,7 @@ class CoprModuleBuilder(GenericBuilder): if response.output != "ok": log.error(response.error) - # Since we don't have implemented messaging support in copr yet, - # let's just assume that the build is finished by now - return response.data["ids"][0], koji.BUILD_STATES["COMPLETE"], response.message, None + return response.data["ids"][0], koji.BUILD_STATES["BUILDING"], response.message, None def _wait_until_all_builds_are_finished(self, module): while True: diff --git a/module_build_service/messaging.py b/module_build_service/messaging.py index fad73253..b7413ec5 100644 --- a/module_build_service/messaging.py +++ b/module_build_service/messaging.py @@ -27,12 +27,13 @@ import json import os import re +import kobo.rpmlib try: from inspect import signature except ImportError: from funcsigs import signature -from module_build_service import log +from module_build_service import log, conf class BaseMessage(object): @@ -160,7 +161,7 @@ class BaseMessage(object): regex_pattern = re.compile( (r'(?P' + categories_re + r')(?:\.)' r'(?Pbuild|repo|module)(?:(?:\.)' - r'(?Pstate))?(?:\.)(?Pchange|done)$')) + r'(?Pstate|build))?(?:\.)(?Pchange|done|end)$')) regex_results = re.search(regex_pattern, topic) if regex_results: @@ -214,6 +215,13 @@ class BaseMessage(object): msg_obj = MBSModule( msg_id, msg_inner_msg.get('id'), msg_inner_msg.get('state')) + elif conf.system == category == 'copr' and object == 'build': + build = msg_inner_msg.get('build') + status = msg_inner_msg.get('status') + pkg = msg_inner_msg.get('pkg') + what = msg_inner_msg.get('what') + msg_obj = CoprBuildEnd(msg_id, build, status, pkg, what) + # If the message matched the regex and is important to the app, # it will be returned if msg_obj: @@ -263,6 +271,31 @@ class KojiRepoChange(BaseMessage): self.repo_tag = repo_tag +class CoprBuildEnd(object): + """ A wrapper class that transforms copr message attributes + to a KojiBuildChange message object + :param msg_id: the id of the msg (e.g. 2016-SomeGUID) + :param build_id: the id of the build (e.g. 264382) + :param status: the new build state + (see http://copr-backend.readthedocs.io/package/constants.html#backend.constants.BuildStatus ) + :param pkg: the full name of what is being built + (e.g. mutt-kz-1.5.23.1-1.20150203.git.c8504a8a.fc21) + :param state_reason: the optional reason as to why the state changed + """ + def __new__(cls, msg_id, build_id, status, pkg, what=None): + nvr = kobo.rpmlib.parse_nvra(pkg) + return KojiBuildChange( + msg_id=msg_id, + build_id=build_id, + task_id=build_id, + build_new_state=status, + build_name=nvr["name"], + build_version=nvr["version"], + build_release=".".join(s for s in [nvr["release"], nvr["epoch"], nvr["arch"]] if s), + state_reason=what, + ) + + class MBSModule(BaseMessage): """ A class that inherits from BaseMessage to provide a message object for a module event generated by module_build_service @@ -377,7 +410,7 @@ def _amq_publish(topic, msg, conf, service): _messaging_backends = { 'fedmsg': { 'publish': _fedmsg_publish, - 'services': ['buildsys', 'mbs'] + 'services': ['buildsys', 'mbs', 'copr'] }, 'amq': { 'publish': _amq_publish, diff --git a/module_build_service/scheduler/handlers/modules.py b/module_build_service/scheduler/handlers/modules.py index 7a2a46a6..eb0b15ff 100644 --- a/module_build_service/scheduler/handlers/modules.py +++ b/module_build_service/scheduler/handlers/modules.py @@ -154,7 +154,7 @@ def wait(config, session, msg): tag = None dependencies = [] - if conf.system == "mock": + if conf.system in ["mock", "copr"]: # In case of mock, we do not try to get anything from pdc, # just generate our own koji_tag to identify the module in messages. tag = '-'.join(['module', module_info['name'], diff --git a/tests/test_messaging.py b/tests/test_messaging.py new file mode 100644 index 00000000..b6e810b5 --- /dev/null +++ b/tests/test_messaging.py @@ -0,0 +1,90 @@ +# Copyright (c) 2017 Red Hat, Inc. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# +# Written by Jakub Kadlcik + + +import unittest +from module_build_service import messaging, conf + + +class TestFedmsgMessaging(unittest.TestCase): + + def test_buildsys_state_change(self): + # https://fedora-fedmsg.readthedocs.io/en/latest/topics.html#id134 + buildsys_state_change_msg = { + 'msg': { + 'attribute': 'state', + 'build_id': 614503, + 'instance': 'primary', + 'name': 'plasma-systemsettings', + 'new': 1, + 'old': 0, + 'owner': 'dvratil', + 'release': '1.fc23', + 'task_id': 9053697, + 'version': '5.2.1' + }, + 'msg_id': '2015-51be4c8e-8ab6-4dcb-ac0d-37b257765c71', + 'timestamp': 1424789698.0, + 'topic': 'org.fedoraproject.prod.buildsys.build.state.change' + } + + topic = 'org.fedoraproject.prod.buildsys.build.state.change' + msg = messaging.BaseMessage.from_fedmsg(topic, buildsys_state_change_msg) + + self.assertEqual(msg.build_id, 614503) + self.assertEqual(msg.build_new_state, 1) + + def test_copr_build_end(self): + # http://fedora-fedmsg.readthedocs.io/en/latest/topics.html#copr-build-end + copr_build_end_msg = { + 'msg': { + 'build': 100, + 'chroot': 'fedora-20-x86_64', + 'copr': 'mutt-kz', + 'ip': '172.16.3.3', + 'pid': 12010, + 'pkg': 'mutt-kz-1.5.23.1-1.20150203.git.c8504a8a.fc21', + 'status': 1, + 'user': 'fatka', + 'version': '1.5.23.1-1.20150203.git.c8504a8a.fc21', + 'what': 'build end: user:fatka copr:mutt-kz build:100 ip:172.16.3.3 pid:12010 status:1', + 'who': 'worker-2' + }, + 'msg_id': '2013-b05a323d-37ee-4396-9635-7b5dfaf5441b', + 'timestamp': 1383956707.634, + 'topic': 'org.fedoraproject.prod.copr.build.end', + 'username': 'copr' + } + + conf.set_item("system", "copr") + topic = 'org.fedoraproject.prod.copr.build.end' + msg = messaging.BaseMessage.from_fedmsg(topic, copr_build_end_msg) + self.assertIsInstance(msg, messaging.KojiBuildChange) + self.assertEqual(msg.msg_id, '2013-b05a323d-37ee-4396-9635-7b5dfaf5441b') + self.assertEqual(msg.build_id, 100) + self.assertEqual(msg.task_id, 100) + self.assertEqual(msg.build_new_state, 1) + self.assertEqual(msg.build_name, 'mutt-kz') + self.assertEqual(msg.build_version, '1.5.23.1') + self.assertEqual(msg.build_release, '1.20150203.git.c8504a8a.fc21') + self.assertEqual(msg.state_reason, + 'build end: user:fatka copr:mutt-kz build:100 ip:172.16.3.3 pid:12010 status:1')