示例#1
0
    def create_copr_project(self, chroots, description, instructions, owner,
                            project):

        try:
            self.copr_client.project_proxy.add(
                ownername=owner,
                projectname=project,
                chroots=chroots,
                description=(description or
                             "Continuous builds initiated by packit service.\n"
                             "For more info check out https://packit.dev/"),
                contact="https://github.com/packit-service/packit/issues",
                # don't show project on Copr homepage
                unlisted_on_hp=True,
                # delete project after the specified period of time
                delete_after_days=60,
                instructions=instructions
                or "You can check out the upstream project"
                f"{self.upstream_local_project.git_url} to find out how to consume these"
                "builds. This copr project is created and handled by the packit project"
                "(https://packit.dev/).",
            )
        except CoprException as ex:
            error = (
                f"Cannot create a new Copr project "
                f"(owner={owner} project={project} chroots={chroots}): {ex}")
            logger.error(error)
            raise PackitCoprProjectException(error, ex)
示例#2
0
    def create_copr_project(
        self,
        chroots: List[str],
        description: str,
        instructions: str,
        owner: str,
        project: str,
        list_on_homepage: bool = False,
        preserve_project: bool = False,
        additional_packages: Optional[List[str]] = None,
        additional_repos: Optional[List[str]] = None,
    ) -> None:

        try:
            self.copr_client.project_proxy.add(
                ownername=owner,
                projectname=project,
                chroots=chroots,
                description=(description or
                             "Continuous builds initiated by packit service.\n"
                             "For more info check out https://packit.dev/"),
                contact="https://github.com/packit/packit/issues",
                # don't show project on Copr homepage by default
                unlisted_on_hp=not list_on_homepage,
                # delete project after the specified period of time
                delete_after_days=60 if not preserve_project else None,
                additional_repos=additional_repos,
                instructions=instructions
                or "You can check out the upstream project "
                f"{self.upstream_local_project.git_url} to find out how to consume these builds. "
                f"This copr project is created and handled by the packit project "
                "(https://packit.dev/).",
            )
            # TODO: additional_packages
        except CoprException as ex:
            # TODO: Remove once Copr doesn't throw for existing projects or new
            # API endpoint is established.
            if "You already have a project named" in ex.result.error:
                # race condition between workers
                logger.debug(
                    f"Copr project ({owner}/{project}) is already present.")
                return

            error = (
                f"Cannot create a new Copr project "
                f"(owner={owner} project={project} chroots={chroots}): {ex}")
            logger.error(error)
            logger.error(ex.result)
            raise PackitCoprProjectException(error, ex)
示例#3
0
    def create_copr_project_if_not_exists(
        self,
        project: str,
        chroots: List[str],
        owner: str = None,
        description: str = None,
        instructions: str = None,
    ) -> None:
        """
        Create a project in copr if it does not exists.

        Raises PackitCoprException on any problems.
        """
        owner = owner or self.configured_owner

        if not owner:
            raise PackitCoprException(
                f"Copr owner not set. Use Copr config file or `--owner` when calling packit CLI."
            )

        try:
            copr_proj = self.copr_client.project_proxy.get(ownername=owner,
                                                           projectname=project)
            # make sure or project has chroots set correctly
            if set(copr_proj.chroot_repos.keys()) != set(chroots):
                logger.info(f"Updating targets on project {owner}/{project}")
                logger.debug(f"old = {set(copr_proj.chroot_repos.keys())}")
                logger.debug(f"new = {set(chroots)}")
                self.copr_client.project_proxy.edit(
                    owner,
                    project,
                    chroots=chroots,
                    description=description,
                    instructions=instructions,
                )
        except CoprNoResultException as ex:
            if owner != self.configured_owner:
                raise PackitCoprProjectException(
                    f"Copr project {owner}/{project} not found.") from ex

            logger.info(
                f"Copr project {owner}/{project} not found. Creating new.")
            self.create_copr_project(chroots, description, instructions, owner,
                                     project)
示例#4
0
    def create_copr_project(
        self,
        chroots: List[str],
        description: str,
        instructions: str,
        owner: str,
        project: str,
        list_on_homepage: bool = False,
        preserve_project: bool = False,
        additional_packages: List[str] = None,
        additional_repos: List[str] = None,
    ) -> None:

        try:
            self.copr_client.project_proxy.add(
                ownername=owner,
                projectname=project,
                chroots=chroots,
                description=(
                    description
                    or "Continuous builds initiated by packit service.\n"
                    "For more info check out https://packit.dev/"
                ),
                contact="https://github.com/packit/packit/issues",
                # don't show project on Copr homepage by default
                unlisted_on_hp=not list_on_homepage,
                # delete project after the specified period of time
                delete_after_days=60 if not preserve_project else None,
                additional_repos=additional_repos,
                instructions=instructions
                or "You can check out the upstream project "
                f"{self.upstream_local_project.git_url} to find out how to consume these builds. "
                f"This copr project is created and handled by the packit project "
                "(https://packit.dev/).",
            )
            # TODO: additional_packages
        except CoprException as ex:
            error = (
                f"Cannot create a new Copr project "
                f"(owner={owner} project={project} chroots={chroots}): {ex}"
            )
            logger.error(error)
            raise PackitCoprProjectException(error, ex)
示例#5
0
    def create_copr_project_if_not_exists(
        self,
        project: str,
        chroots: List[str],
        owner: str = None,
        description: str = None,
        instructions: str = None,
        list_on_homepage: Optional[bool] = False,
        preserve_project: Optional[bool] = False,
        additional_packages: List[str] = None,
        additional_repos: List[str] = None,
        request_admin_if_needed: bool = False,
    ) -> None:
        """
        Create a project in copr if it does not exists.

        Raises PackitCoprException on any problems.
        """
        logger.info(f"Trying to get {owner}/{project} Copr project. "
                    "The project will be created if it does not exist.")
        try:
            copr_proj = self.copr_client.project_proxy.get(ownername=owner,
                                                           projectname=project)
        except CoprNoResultException as ex:
            if owner != self.configured_owner:
                raise PackitCoprProjectException(
                    f"Copr project {owner}/{project} not found.") from ex

            logger.info(
                f"Copr project '{owner}/{project}' not found. Creating new.")
            self.create_copr_project(
                chroots=chroots,
                description=description,
                instructions=instructions,
                owner=owner,
                project=project,
                list_on_homepage=list_on_homepage,
                preserve_project=preserve_project,
                additional_packages=additional_packages,
                additional_repos=additional_repos,
            )
            return
        except CoprRequestException as ex:
            logger.debug(repr(ex))
            logger.error(
                f"We were not able to get copr project {owner}/{project}: {ex}"
            )
            raise

        delete_after_days: Optional[int] = (None
                                            if preserve_project is None else
                                            -1 if preserve_project else 60)

        fields_to_change = self.get_fields_to_change(
            copr_proj=copr_proj,
            additional_repos=additional_repos,
            chroots=chroots,
            description=description,
            instructions=instructions,
            list_on_homepage=list_on_homepage,
            delete_after_days=delete_after_days,
        )

        if fields_to_change:
            logger.info(f"Updating copr project '{owner}/{project}'")
            for field, (old, new) in fields_to_change.items():
                logger.debug(f"{field}: {old} -> {new}")

            try:
                kwargs: Dict[str, Any] = {
                    arg_name: new
                    for arg_name, (old, new) in fields_to_change.items()
                }
                logger.debug(f"Copr edit arguments: {kwargs}")
                self.copr_client.project_proxy.edit(ownername=owner,
                                                    projectname=project,
                                                    **kwargs)
            except CoprRequestException as ex:
                if "Only owners and admins may update their projects." in str(
                        ex):
                    if request_admin_if_needed:
                        logger.info(
                            f"Admin permissions are required "
                            f"in order to be able to edit project settings. "
                            f"Requesting the admin rights for the copr '{owner}/{project}' project."
                        )
                        self.copr_client.project_proxy.request_permissions(
                            ownername=owner,
                            projectname=project,
                            permissions={"admin": True},
                        )
                    else:
                        logger.warning(
                            f"Admin permissions are required for copr '{owner}/{project}' project"
                            f"in order to be able to edit project settings. "
                            f"You can make a request by specifying --request-admin-if-needed "
                            f"when using Packit CLI.")
                raise PackitCoprSettingsException(
                    f"Copr project update failed for '{owner}/{project}' project.",
                    fields_to_change=fields_to_change,
                ) from ex
示例#6
0
    def create_copr_project_if_not_exists(
        self,
        project: str,
        chroots: List[str],
        owner: str = None,
        description: str = None,
        instructions: str = None,
        list_on_homepage: bool = False,
        preserve_project: bool = False,
        additional_packages: List[str] = None,
        additional_repos: List[str] = None,
        update_additional_values: bool = True,
    ) -> None:
        """
        Create a project in copr if it does not exists.

        Raises PackitCoprException on any problems.
        """
        try:
            copr_proj = self.copr_client.project_proxy.get(ownername=owner,
                                                           projectname=project)
            # make sure or project has chroots set correctly
            # we can also update other settings
            if set(copr_proj.chroot_repos.keys()) != set(chroots):
                logger.info(f"Updating copr project '{owner}/{project}'")
                logger.debug(
                    f"old targets = {set(copr_proj.chroot_repos.keys())}")
                logger.debug(f"new targets = {set(chroots)}")

                if not update_additional_values:
                    delete_after_days = None
                elif preserve_project:
                    delete_after_days = -1
                else:
                    delete_after_days = 60

                self.copr_client.project_proxy.edit(
                    ownername=owner,
                    projectname=project,
                    chroots=chroots,
                    description=description,
                    instructions=instructions,
                    unlisted_on_hp=not list_on_homepage
                    if update_additional_values else None,
                    additional_repos=additional_repos,
                    delete_after_days=delete_after_days,
                )
                # TODO: additional_packages
        except CoprNoResultException as ex:
            if owner != self.configured_owner:
                raise PackitCoprProjectException(
                    f"Copr project {owner}/{project} not found.") from ex

            logger.info(
                f"Copr project '{owner}/{project}' not found. Creating new.")
            self.create_copr_project(
                chroots=chroots,
                description=description,
                instructions=instructions,
                owner=owner,
                project=project,
                list_on_homepage=list_on_homepage,
                preserve_project=preserve_project,
                additional_packages=additional_packages,
                additional_repos=additional_repos,
            )