Skip to content

Commit

Permalink
Refactor Copr support into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ehelms committed Apr 7, 2023
1 parent d1f114b commit 7761102
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 48 deletions.
23 changes: 23 additions & 0 deletions obal/data/module_utils/copr_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
A copr-cli wrapper
"""
from subprocess import check_output, CalledProcessError

class CoprCliCommandError(Exception):
"""Raised when copr-cli command fails"""
def __init__(self, message, command):
self.message = message
self.command = command
super(CoprCliCommandError, self).__init__(message) #pylint: disable-all

def copr_cli(command, executable=None):
"""
Run a copr-cli command
"""
if executable is None:
executable = 'copr-cli'

try:
return check_output([executable] + command, universal_newlines=True)
except CalledProcessError as error:
raise CoprCliCommandError(error.output, error.cmd)
44 changes: 44 additions & 0 deletions obal/data/modules/copr_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Release a package to Copr
"""

import re

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.copr_wrapper import copr_cli, CoprCliCommandError # pylint:disable=import-error,no-name-in-module


def main():
"""
Build a package in koji
"""
module = AnsibleModule(
argument_spec=dict(
srpm=dict(type='path', required=True),
repo_name=dict(type='str', required=True),
wait=dict(type='bool', required=False, default=True)
)
)

srpm = module.params['srpm']
repo_name = module.params['repo_name']
wait = module.params['wait']

command = ['build', repo_name, srpm]

if not wait:
command.append('--nowait')

try:
output = copr_cli(command)
except CoprCliCommandError as error:
module.fail_json(msg='Copr build failed', command=error.command, output=error.message,
repo_name=repo_name, srpm=srpm)

builds = re.findall(r'^Build was added to.+:\n^\s(.+)', output, re.MULTILINE)

module.exit_json(changed=True, output=output, builds=builds)


if __name__ == '__main__':
main()
54 changes: 54 additions & 0 deletions obal/data/modules/copr_chroot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Manage a chroot in a Copr repository
"""

import re

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.copr_wrapper import copr_cli, CoprCliCommandError # pylint:disable=import-error,no-name-in-module


def main():
"""
Manage a chroot in a Copr repository
"""
module = AnsibleModule(
argument_spec=dict(
repo_name=dict(type='str', required=True),
chroot=dict(type='str', required=True),
external_repos=dict(type='list', required=False, default=[]),
buildroot_packages=dict(type='list', required=False, default=[]),
modules=dict(type='list', required=False, default=[]),
)
)

repo_name = module.params['repo_name']
chroot = module.params['chroot']
external_repos = module.params['external_repos']
buildroot_packages = module.params['buildroot_packages']
modules = module.params['modules']

command = [
'edit-chroot',
"%s/%s" % (repo_name, chroot)
]

if external_repos:
command.extend(['--repos', external_repos.join(' ')])

if buildroot_packages:
command.extend(['--packages', buildroot_packages.join(' ')])

if modules:
command.extend(['--modules', modules.join(',')])

try:
output = copr_cli(command)
except CoprCliCommandError as error:
module.fail_json(msg='Copr chroot edit failed', command=' '.join(error.command), output=error.message)

module.exit_json(changed=True, output=output)


if __name__ == '__main__':
main()
59 changes: 59 additions & 0 deletions obal/data/modules/copr_repo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
Create a repository in Copr
"""

import re

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.copr_wrapper import copr_cli, CoprCliCommandError # pylint:disable=import-error,no-name-in-module


def main():
"""
Create a repository in Copr
"""
module = AnsibleModule(
argument_spec=dict(
repo_name=dict(type='str', required=True),
chroots=dict(type='list', required=True),
description=dict(type='str', required=False),
unlisted_on_homepage=dict(type='bool', required=False, default=True),
delete_after_days=dict(type='str', required=False, default=None),
)
)

repo_name = module.params['repo_name']
chroots = module.params['chroots']
description = module.params['description']
unlisted_on_homepage = module.params['unlisted_on_homepage']
delete_after_days = module.params['delete_after_days']

if not description:
description = repo_name

command = [
'create',
repo_name,
'--description',
"'%s'" % description
]

for chroot in chroots:
command.extend(['--chroot', chroot])

if unlisted_on_homepage:
command.extend(['--unlisted-on-hp', 'on'])

if delete_after_days:
command.extend(['--delete-after-days', delete_after_days])

try:
output = copr_cli(command)
except CoprCliCommandError as error:
module.fail_json(msg='Copr repository creation failed', command=' '.join(error.command), output=error.message)

module.exit_json(changed=True, output=output)


if __name__ == '__main__':
main()
55 changes: 24 additions & 31 deletions obal/data/roles/build_package/tasks/copr.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
---
- when: not build_package_scratch
block:
- name: 'Release to copr'
tito_release:
directory: "{{ inventory_dir }}/{{ package_base_dir }}/{{ inventory_hostname }}"
arguments: "{{ build_package_tito_args }}"
test: "{{ build_package_test }}"
scratch: "{{ build_package_scratch }}"
releasers: "{{ releasers }}"
releaser_arguments: "{{ build_package_tito_releaser_args }}"
register: build_package_tito_release

- name: 'Wait for tasks to finish'
include_tasks: wait.yml
when: build_package_wait|bool

- when: build_package_scratch
block:
- name: Define copr repo name
Expand All @@ -33,21 +17,30 @@
- include_role:
name: copr_repo
run_once: true
vars:
copr_repo_delete_after_days: 2

- name: Create temporary build directory
tempfile:
state: directory
suffix: srpms
register: srpm_directory
when: srpm_directory is not defined

- name: 'Build SRPM'
tito_build:
directory: "{{ inventory_dir }}/{{ package_base_dir }}/{{ inventory_hostname }}"
srpm: true
scl: "{{ scl }}"
register: srpm_build
- name: 'Build SRPM'
srpm:
package: "{{ inventory_dir }}/{{ package_base_dir }}/{{ inventory_hostname }}"
output: "{{ srpm_directory.path if 'path' in srpm_directory else srpm_directory }}"
source_location: "{{ source_location | default(omit) }}"
source_system: "{{ source_system | default(omit) }}"
register: srpm_build

- name: 'Run build'
command: >-
copr-cli build
{% if not build_package_wait | bool %}--nowait{% endif %}
{{ copr_repo_name }}
{{ srpm_build.path }}
register: build_status
- name: 'Run build'
copr_build:
repo_name: "{{ copr_repo_name }}"
srpm: "{{ srpm_build.path }}"
wait: "{{ build_package_wait }}"
register: copr_builds

- debug:
msg: "{{ build_status.stdout_lines | join('\n') }}"
- debug:
msg: "{{ copr_builds.output }}"
35 changes: 18 additions & 17 deletions obal/data/roles/copr_repo/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
---
- name: 'Create copr scratch build repo'
command: >
copr-cli
create
{{ copr_repo_name }}
--chroot {{ copr_repo_chroot }}
--description '{{ copr_repo_description }}'
--unlisted-on-hp on
--repo '{{ copr_repo_external_repos | join(' ') }}'
changed_when: false
- name: 'Create Copr repo'
copr_repo:
repo_name: "{{ copr_repo_name }}"
chroots: "{{ copr_repo_chroots | map(attribute='name') }}"
description: "{{ copr_repo_description | default(omit) }}"
delete_after_days: "{{ copr_repo_delete_after_days | default(omit) }}"
unlisted_on_homepage: "{{ copr_repo_unlisted_on_homepage | default(omit) }}"
register: create_output

- name: 'Add build packages to chroot'
command: >
copr-cli
edit-chroot
{{ copr_repo_name }}/{{ copr_repo_chroot }}
--packages '{{ copr_repo_packages | join(' ') }}'
changed_when: false
- name: Configure chroots
copr_chroot:
repo_name: "{{ copr_repo_name }}"
chroot: "{{ chroot.name }}"
external_repos: "{{ chroot.external_repos | default(omit) }}"
buildroot_packages: "{{ chroot.buildroot_packages | default(omit) }}"
modules: "{{ chroot.modules | default(omit) }}"
loop: "{{ copr_repo_chroots }}"
loop_control:
loop_var: chroot

0 comments on commit 7761102

Please sign in to comment.