periodic_tasks.py 5.09 KB
Newer Older
Mustafa Gezen's avatar
Mustafa Gezen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#  Copyright (c) 2021 The Distrobuild Authors
#
#  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
Mustafa Gezen's avatar
Mustafa Gezen committed
17
18
19
20
#  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.

21
import asyncio
22
import datetime
23
import xmlrpc
24
25

import koji
Mustafa Gezen's avatar
Mustafa Gezen committed
26
from tortoise.transactions import atomic
27

28
from distrobuild.common import tags
29
30
from distrobuild.models import Build, BuildStatus, Package
from distrobuild.session import koji_session, mbs_client
Mustafa Gezen's avatar
Mustafa Gezen committed
31
from distrobuild.settings import settings
32
33

from distrobuild_scheduler import logger
Mustafa Gezen's avatar
Mustafa Gezen committed
34
from distrobuild_scheduler.sigul import sign_koji_package
35
36


37
38
@atomic()
async def atomic_sign_unsigned_builds():
39
    builds = await Build.filter(signed=False, status=BuildStatus.SUCCEEDED).prefetch_related("package").all()
40
41
    for build in builds:
        if build.koji_id:
42
43
            koji_session.packageListAdd(tags.compose(), build.package.name, "distrobuild")

44
            should_tag = True
45
46
            build_tasks = koji_session.listBuilds(taskID=build.koji_id)
            for build_task in build_tasks:
47
48
49
50
51
52
53
54
                build_history = koji_session.queryHistory(build=build_task["build_id"])
                if "tag_listing" in build_history:
                    for tag in build_history["tag_listing"]:
                        if tag["tag.name"] == tags.compose():
                            should_tag = False

                if should_tag:
                    koji_session.tagBuild(tags.compose(), build_task["nvr"])
55

56
57
58
59
60
                build_rpms = koji_session.listBuildRPMs(build_task["build_id"])
                for rpm in build_rpms:
                    nvr_arch = "%s.%s" % (rpm["nvr"], rpm["arch"])
                    await sign_koji_package(nvr_arch)
                    koji_session.writeSignedRPM(nvr_arch, settings.sigul_key_id)
61

62
63
64
65
            build.signed = True
            await build.save()


Mustafa Gezen's avatar
Mustafa Gezen committed
66
67
68
69
70
71
72
73
74
@atomic()
async def atomic_check_build_status():
    builds = await Build.filter(status=BuildStatus.BUILDING).all()
    for build in builds:
        if build.koji_id:
            task_info = koji_session.getTaskInfo(build.koji_id, request=True)
            if task_info["state"] == koji.TASK_STATES["CLOSED"]:
                build.status = BuildStatus.SUCCEEDED
                await build.save()
75

Mustafa Gezen's avatar
Mustafa Gezen committed
76
77
78
79
80
81
82
83
84
85
                package = await Package.filter(id=build.package_id).get()
                package.last_build = datetime.datetime.now()
                await package.save()
            elif task_info["state"] == koji.TASK_STATES["CANCELED"]:
                build.status = BuildStatus.CANCELLED
                await build.save()
            elif task_info["state"] == koji.TASK_STATES["FAILED"]:
                try:
                    task_result = koji_session.getTaskResult(build.koji_id)
                    print(task_result)
86
                except (koji.BuildError, xmlrpc.client.Fault):
87
                    build.status = BuildStatus.FAILED
Mustafa Gezen's avatar
Mustafa Gezen committed
88
89
90
                except koji.GenericError:
                    build.status = BuildStatus.CANCELLED
                finally:
91
                    await build.save()
Mustafa Gezen's avatar
Mustafa Gezen committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
        elif build.mbs_id:
            build_info = await mbs_client.get_build(build.mbs_id)
            state = build_info["state_name"]
            if state == "ready":
                build.status = BuildStatus.SUCCEEDED
                await build.save()

                package = await Package.filter(id=build.package_id).get()
                package.last_build = datetime.datetime.now()
                await package.save()
            elif state == "failed":
                build.status = BuildStatus.FAILED
                await build.save()


async def check_build_status():
    while True:
        logger.debug("[*] Running periodic task: check_build_status")
110

111
112
113
114
        try:
            await atomic_check_build_status()
        except Exception as e:
            logger.error(e)
115
116
117

        # run every 5 minutes
        await asyncio.sleep(60 * 5)
118
119
120
121
122
123
124


async def sign_unsigned_builds():
    if not settings.disable_sigul:
        while True:
            logger.debug("[*] Running periodic task: sign_unsigned_builds")

125
126
127
128
            try:
                await atomic_sign_unsigned_builds()
            except Exception as e:
                logger.error(e)
129

130
        # run every 5 minutes
131
            await asyncio.sleep(60 * 5)