예제 #1
0
    def get_copr_builds(self, number_of_builds: int = 5) -> List:
        """
        Get the copr builds of this project done by packit.
        :return: list of builds
        """
        client = CoprClient.create_from_config_file()

        projects = [
            project.name
            for project in reversed(client.project_proxy.get_list(ownername="packit"))
            if project.name.startswith(
                f"{self.upstream_local_project.namespace}-{self.upstream_local_project.repo_name}-"
            )
        ][:5]

        builds: List = []
        for project in projects:
            builds += client.build_proxy.get_list(
                ownername="packit", projectname=project
            )

        logger.debug("Copr builds fetched.")
        return [(build.id, build.projectname, build.state) for build in builds][
            :number_of_builds
        ]
예제 #2
0
def main(srpm, copr_conf=DEFAULT_COPR_CONF, project=DEFAULT_PROJECT):
    """Query COPR info."""
    if not os.path.isfile(srpm):
        print("Error: The given SRPM is not a file:\n%s" % srpm)
        sys.exit(1)

    client = Client.create_from_config_file(copr_conf)

    print(datetime.datetime.now())
    try:
        build_id = launch_build(client, project, srpm)
    except Exception as e:
        mention_expiration_on_creds(copr_conf)
        raise e

    # 2018-03-05: adding sleep to let builds ramp up
    # this after a {'state': ['Not a valid choice.']} exception
    reps = 6
    for naptime in [5] * reps + [10] * reps + [30] * reps:
        time.sleep(naptime)
        tasks = get_build_tasks(client, build_id)
        if tasks:
            break

    check_build_status(client, build_id, tasks)
    check_test_chroot(tasks)
    print()
    print(datetime.datetime.now())
예제 #3
0
def check_copr_build(build_id: int) -> bool:
    """
    Check the copr_build with given id and refresh the status if needed.

    Used in the babysit task.

    :param build_id: id of the copr_build (CoprBuildModel.build.id)
    :return: True if in case of successful run, False when we need to retry
    """
    logger.debug(f"Getting copr build ID {build_id} from DB.")
    builds = CoprBuildModel.get_all_by_build_id(build_id)
    if not builds:
        logger.warning(f"Copr build {build_id} not in DB.")
        return True

    copr_client = CoprClient.create_from_config_file()
    build_copr = copr_client.build_proxy.get(build_id)

    if not build_copr.ended_on:
        logger.info("The copr build is still in progress.")
        return False

    logger.info(f"The status is {build_copr.state!r}.")

    for build in builds:
        if build.status != "pending":
            logger.info(f"DB state says {build.status!r}, "
                        "things were taken care of already, skipping.")
            continue
        chroot_build = copr_client.build_chroot_proxy.get(
            build_id, build.target)
        event = CoprBuildEvent(
            topic=FedmsgTopic.copr_build_finished.value,
            build_id=build_id,
            build=build,
            chroot=build.target,
            status=(COPR_API_SUCC_STATE if chroot_build.state
                    == COPR_SUCC_STATE else COPR_API_FAIL_STATE),
            owner=build.owner,
            project_name=build.project_name,
            pkg=build_copr.source_package.get(
                "name", ""),  # this seems to be the SRPM name
            timestamp=chroot_build.ended_on,
        )

        job_configs = get_config_for_handler_kls(
            handler_kls=CoprBuildEndHandler,
            event=event,
            package_config=event.get_package_config(),
        )

        for job_config in job_configs:
            CoprBuildEndHandler(
                package_config=event.package_config,
                job_config=job_config,
                data=EventData.from_event_dict(event.get_dict()),
                copr_event=event,
            ).run()
    return True
예제 #4
0
 def get_client(cls):
     try:
         client = Client.create_from_config_file()
     except (CoprNoConfigException, CoprConfigException) as e:
         raise RebaseHelperError(
             'Missing or invalid copr configuration file') from e
     else:
         return client
예제 #5
0
    def get_available_chroots() -> list:
        """
        Gets available copr chroots. Uses cache to avoid repetitive url fetching.

        :return: list of valid chroots
        """

        client = CoprClient.create_from_config_file()
        return list(
            filter(
                lambda chroot: not chroot.startswith("_"),
                client.mock_chroot_proxy.get_list().keys(),
            ))
예제 #6
0
def babysit_copr_build(self, build_id: int):
    """ check status of a copr build and update it in DB """
    logger.debug(f"getting copr build ID {build_id} from DB")
    builds = CoprBuild.get_all_by_build_id(build_id)
    if builds:
        copr_client = CoprClient.create_from_config_file()
        build_copr = copr_client.build_proxy.get(build_id)

        if not build_copr.ended_on:
            logger.info("The copr build is still in progress")
            self.retry()
        logger.info(f"The status is {build_copr.state}")

        # copr doesn't tell status of how a build in the chroot went:
        #   https://bugzilla.redhat.com/show_bug.cgi?id=1813227
        for build in builds:
            if build.status != "pending":
                logger.info(
                    f"DB state says {build.status}, "
                    "things were taken care of already, skipping."
                )
                continue
            event = CoprBuildEvent(
                topic=FedmsgTopic.copr_build_finished.value,
                build_id=build_id,
                build={},
                chroot=build.target,
                status=(
                    COPR_API_SUCC_STATE
                    if build_copr.state == COPR_SUCC_STATE
                    else COPR_API_FAIL_STATE
                ),
                owner=build.owner,
                project_name=build.project_name,
                pkg=build_copr.source_package.get(
                    "name", ""
                ),  # this seems to be the SRPM name
                build_pg=build,
            )
            CoprBuildEndHandler(
                ServiceConfig.get_service_config(), job_config=None, event=event
            ).run()
    else:
        logger.warning(f"Copr build {build_id} not in DB.")
예제 #7
0
 def get_copr_client(self) -> CoprClient:
     """Not static because of the flex-mocking."""
     return CoprClient.create_from_config_file()
예제 #8
0
파일: api.py 프로젝트: dustymabe/packit
 def copr(self):
     if self._copr is None:
         self._copr = CoprClient.create_from_config_file()
     return self._copr
예제 #9
0
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT

import enum
import sentry_sdk
import time

from copr.v3 import Client
from datetime import datetime, timedelta, date
from os import getenv

from github import InputGitAuthor
from ogr.services.github import GithubService
from ogr.abstract import PullRequest

copr = Client.create_from_config_file()
service = GithubService(token=getenv("GITHUB_TOKEN"))
project = service.get_project(repo="hello-world", namespace="packit")
user = InputGitAuthor(name="Release Bot",
                      email="*****@*****.**")


class Trigger(str, enum.Enum):
    comment = "comment"
    pr_opened = "pr_opened"
    push = "push"


class Testcase:
    def __init__(self,
                 pr: PullRequest = None,
예제 #10
0
def update_copr_builds(build_id: int,
                       builds: Iterable["CoprBuildModel"]) -> bool:
    """
    Updates the state of copr builds if they have ended.

    Args:
        build_id (int): ID of the copr build to update.
        builds (Iterable[CoprBuildModel]): List of builds corresponding to
            the given ``build_id``.

    Returns:
        bool: Whether the run was successful, False signals the need to retry.
    """
    copr_client = CoprClient.create_from_config_file()
    try:
        build_copr = copr_client.build_proxy.get(build_id)
    except copr.v3.CoprNoResultException:
        logger.info(
            f"Copr build {build_id} no longer available. Setting it to error status and "
            f"not checking it anymore.")
        for build in builds:
            build.set_status("error")
        return True

    if not build_copr.ended_on:
        logger.info("The copr build is still in progress.")
        return False

    logger.info(f"The status is {build_copr.state!r}.")

    current_time = datetime.datetime.utcnow()
    for build in builds:
        elapsed = current_time - build.build_submitted_time
        if elapsed.total_seconds() > DEFAULT_JOB_TIMEOUT:
            logger.info(
                f"The build {build_id} has been running for "
                f"{elapsed.total_seconds()}, probably an internal error"
                f"occurred. Not checking it anymore.")
            build.set_status("error")
            continue
        if build.status != "pending":
            logger.info(f"DB state says {build.status!r}, "
                        "things were taken care of already, skipping.")
            continue
        chroot_build = copr_client.build_chroot_proxy.get(
            build_id, build.target)
        event = AbstractCoprBuildEvent(
            topic=FedmsgTopic.copr_build_finished.value,
            build_id=build_id,
            build=build,
            chroot=build.target,
            status=(COPR_API_SUCC_STATE if chroot_build.state
                    == COPR_SUCC_STATE else COPR_API_FAIL_STATE),
            owner=build.owner,
            project_name=build.project_name,
            pkg=build_copr.source_package.get(
                "name", ""),  # this seems to be the SRPM name
            timestamp=chroot_build.ended_on,
        )

        job_configs = get_config_for_handler_kls(
            handler_kls=CoprBuildEndHandler,
            event=event,
            package_config=event.get_package_config(),
        )

        for job_config in job_configs:
            CoprBuildEndHandler(
                package_config=event.package_config,
                job_config=job_config,
                event=event.get_dict(),
            ).run()
    return True
예제 #11
0
def upgrade():
    bind = op.get_bind()
    session = orm.Session(bind=bind)

    db = Redis(
        host=getenv("REDIS_SERVICE_HOST", "localhost"),
        port=int(getenv("REDIS_SERVICE_PORT", "6379")),
        db=0,
        decode_responses=True,
    )

    # tasks
    keys = db.keys("celery-task-meta-*")
    for key in keys:
        data = loads(db.get(key))
        task_id = data.get("task_id")
        status = data.get("status")
        result = data.get("result")
        traceback = data.get("traceback")
        date_done = data.get("data_done")
        if isinstance(date_done, str):
            date_done = datetime.fromisoformat(date_done)

        # our table
        TaskResultUpgradeModel.add_task_result(session=session,
                                               task_id=task_id,
                                               task_result_dict=result)
        # celery table
        add_task_to_celery_table(
            session=session,
            task_id=task_id,
            status=status,
            result=result,
            traceback=traceback,
            date_done=date_done,
        )

    # whitelist
    db = PersistentDict(hash_name="whitelist")
    for account, data in db.get_all().items():
        if not isinstance(data, dict):
            continue

        status = data.get("status")
        WhitelistUpgradeModel.add_account(session=session,
                                          account_name=account,
                                          status=status)

    # installations
    for event in RedisInstallation.db().get_all().values():
        if not isinstance(event, dict):
            continue

        event = event["event_data"]
        account_login = event.get("account_login")
        account_id = event.get("account_id")
        account_url = event.get("account_url")
        account_type = event.get("account_type")
        sender_id = event.get("sender_id")
        sender_login = event.get("sender_login")

        created_at = event.get("created_at")
        if isinstance(created_at, (int, float)):
            created_at = datetime.fromtimestamp(created_at, timezone.utc)
        elif isinstance(created_at, str):
            created_at = created_at.replace("Z", "+00:00")
            created_at = datetime.fromisoformat(created_at)

        InstallationUpgradeModel.create(
            session=session,
            account_login=account_login,
            account_id=account_id,
            account_type=account_type,
            account_url=account_url,
            sender_login=sender_login,
            sender_id=sender_id,
            created_at=created_at,
        )

    #  copr-builds
    for copr_build in RedisCoprBuild.db().get_all().values():
        if not isinstance(copr_build, dict):
            continue

        project_name = copr_build.get("project")
        owner = copr_build.get("owner")
        chroots = copr_build.get("chroots")
        build_submitted_time = (
            datetime.fromisoformat(copr_build.get("build_submitted_time"))
            if copr_build.get("build_submitted_time") else None)
        build_id = copr_build.get("build_id")

        if not build_id:
            continue

        status = copr_build.get("status")
        web_url = (
            f"https://copr.fedorainfracloud.org/coprs/{owner}/{project_name}/"
            f"build/{build_id}/")

        try:
            project_name_list = project_name.split("-")
            if project_name_list[-1] == "stg":
                pr_id = int(project_name_list[-2])
            else:
                pr_id = int(project_name_list[-1])

            job_trigger = JobTriggerUpgradeModel.get_or_create(
                type=JobTriggerModelType.pull_request,
                trigger_id=pr_id,
                session=session,
            )
        except Exception:
            continue

        try:
            copr = Client.create_from_config_file()
            build = copr.build_proxy.get(build_id)
            build_submitted_time = datetime.fromtimestamp(build.submitted_on)
            build_start_time = datetime.fromtimestamp(build.started_on)
            build_finished_time = datetime.fromtimestamp(build.ended_on)

        except CoprNoResultException:
            build_submitted_time = build_submitted_time or datetime(
                2020, 1, 1, 0, 0, 0)
            build_start_time = datetime(2020, 1, 1, 0, 10, 0)
            build_finished_time = datetime(2020, 1, 1, 0, 20, 0)

        for chroot in chroots:
            CoprBuildUpgradeModel.get_or_create(
                session=session,
                build_id=str(build_id),
                project_name=project_name,
                owner=owner,
                target=chroot,
                status=status,
                job_trigger=job_trigger,
                web_url=web_url,
                build_submitted_time=build_submitted_time,
                build_start_time=build_start_time,
                build_finished_time=build_finished_time,
            )

    session.commit()