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 Jul 8, 2023
1 parent bd886fb commit 2f82d21
Show file tree
Hide file tree
Showing 21 changed files with 479 additions and 126 deletions.
5 changes: 4 additions & 1 deletion obal/data/module_utils/copr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ def __init__(self, message, command):
self.command = command
super(CoprCliCommandError, self).__init__(message) #pylint: disable-all

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

if config_file:
command = ['--config', config_file] + command

try:
return check_output([executable] + command, universal_newlines=True, stderr=STDOUT)
except CalledProcessError as error:
Expand Down
54 changes: 54 additions & 0 deletions obal/data/modules/check_copr_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Check if a build already exists in Copr
"""

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


def main():
"""
Check if a build already exists in Copr
"""
module = AnsibleModule(
argument_spec=dict(
user=dict(type='str', required=True),
project=dict(type='str', required=True),
nvr=dict(type='str', required=True),
package=dict(type='str', required=True),
config_file=dict(type='str', required=False),
)
)

user = module.params['user']
project = module.params['project']
nvr = module.params['nvr']
package = module.params['package']
config_file = module.params['config_file']

command = [
'get-package',
full_name(user, project),
'--name',
package,
'--with-all-builds'
]

try:
package_info = json.loads(copr_cli(command, config_file=config_file))
except CoprCliCommandError as error:
if "Error: No package with name {} in copr {}".format(package, project):
module.exit_json(changed=True, exists=False)
else:
module.fail_json(msg='Retrieval of package from Copr failed', command=command, output=error.message,
repo_name=full_name(user, project), package=package)

builds = (build for build in package_info['builds'] if build['state'] == 'succeeded')
exists = any("{}-{}".format(package, build['source_package']['version']) == nvr for build in builds)

module.exit_json(changed=not exists, exists=exists)


if __name__ == '__main__':
main()
61 changes: 61 additions & 0 deletions obal/data/modules/copr_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
Release a package to Copr
"""

import re

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


def main():
"""
Release a package to Copr
"""
module = AnsibleModule(
argument_spec=dict(
user=dict(type='str', required=True),
project=dict(type='str', required=True),
srpm=dict(type='path', required=True),
wait=dict(type='bool', required=False, default=False),
chroots=dict(type='list', required=False),
config_file=dict(type='str', required=False),
)
)

user = module.params['user']
project = module.params['project']
srpm = module.params['srpm']
wait = module.params['wait']
chroots = module.params['chroots']
config_file = module.params['config_file']

command = [
'build',
full_name(user, project),
srpm
]

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

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

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

build_urls = re.findall(r'^Build was added to.+:\n^\s(.+)', output, re.MULTILINE)
build_urls = [url.strip() for url in build_urls]

builds = re.findall(r'^Created builds:\s(\d+)', output, re.MULTILINE)

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


if __name__ == '__main__':
main()
21 changes: 9 additions & 12 deletions obal/data/modules/copr_chroot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ def main():
user=dict(type='str', required=True),
project=dict(type='str', required=True),
chroot=dict(type='str', required=True),
external_repos=dict(type='list', required=False),
buildroot_packages=dict(type='list', required=False),
modules=dict(type='list', required=False),
external_repos=dict(type='list', required=False, default=[]),
buildroot_packages=dict(type='list', required=False, default=[]),
modules=dict(type='list', required=False, default=[]),
config_file=dict(type='str', required=False),
)
)

Expand All @@ -27,23 +28,19 @@ def main():
external_repos = module.params['external_repos']
buildroot_packages = module.params['buildroot_packages']
modules = module.params['modules']
config_file = module.params['config_file']

command = [
'edit-chroot',
"{}/{}".format(full_name(user, project), chroot)
]

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

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

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

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

Expand Down
59 changes: 59 additions & 0 deletions obal/data/modules/copr_fork.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
Fork a Copr project
"""

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


def main():
"""
Fork a Copr project
"""
module = AnsibleModule(
argument_spec=dict(
src_user=dict(type='str', required=True),
src_project=dict(type='str', required=True),
dest_user=dict(type='str', required=True),
dest_project=dict(type='str', required=True),
delete_after_days=dict(type='str', required=False, default=None),
config_file=dict(type='str', required=False),
)
)

src_user = module.params['src_user']
src_project = module.params['src_project']
dest_user = module.params['dest_user']
dest_project = module.params['dest_project']
delete_after_days = module.params['delete_after_days']
config_file = module.params['config_file']

command = [
'fork',
full_name(src_user, src_project),
full_name(dest_user, dest_project)
]

try:
fork_output = copr_cli(command, config_file=config_file)
except CoprCliCommandError as error:
module.fail_json(msg='Copr project forking failed', command=' '.join(error.command), output=error.message)

if delete_after_days:
modify_command = [
'modify',
full_name(dest_user, dest_project),
'--delete-after-days',
delete_after_days
]

try:
copr_cli(modify_command, config_file=config_file)
except CoprCliCommandError as error:
module.fail_json(msg='Copr project forking failed', command=' '.join(error.command), output=error.message)

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


if __name__ == '__main__':
main()
14 changes: 10 additions & 4 deletions obal/data/modules/copr_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ansible.module_utils.copr import copr_cli, CoprCliCommandError, full_name # pylint:disable=import-error,no-name-in-module


def project_exists(user, project, module):
def project_exists(user, project, module, config_file=None):
"""
Return true if a project already exists for a user
"""
Expand All @@ -17,7 +17,7 @@ def project_exists(user, project, module):
]

try:
project_list = copr_cli(command)
project_list = copr_cli(command, config_file=config_file)
except CoprCliCommandError as error:
module.fail_json(msg='Copr project listing failed', command=' '.join(error.command), output=error.message)

Expand All @@ -35,6 +35,8 @@ def main():
description=dict(type='str', required=False),
unlisted_on_homepage=dict(type='bool', required=False, default=False),
delete_after_days=dict(type='str', required=False),
appstream=dict(type='str', required=False, default='off'),
config_file=dict(type='str', required=False),
)
)

Expand All @@ -44,11 +46,13 @@ def main():
description = module.params['description']
unlisted_on_homepage = module.params['unlisted_on_homepage']
delete_after_days = module.params['delete_after_days']
appstream = module.params['appstream']
config_file = module.params['config_file']

if not description:
description = project

if project_exists(user, project, module):
if project_exists(user, project, module, config_file=config_file):
command = ['modify']
else:
command = ['create']
Expand All @@ -59,6 +63,8 @@ def main():
description
])

command.extend(['--appstream', appstream])

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

Expand All @@ -69,7 +75,7 @@ def main():
command.extend(['--delete-after-days', delete_after_days])

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

Expand Down
19 changes: 19 additions & 0 deletions obal/data/playbooks/release/metadata.obal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,22 @@ variables:
action: store_false
parameter: --skip-koji-whitelist-check
help: ignore koji whitelist check and release the package anyway
build_package_copr_chroots:
action: append
parameter: --copr-chroot
help: Specify a Copr chroot to release for, can be specified multiple times
build_package_copr_rebuild:
action: store_true
parameter: --copr-rebuild
help: Forces a rebuild of a package in Copr
build_package_skip_failed_build:
action: store_true
parameter: --skip-failed-build
help: Skips failing builds and continues execution
build_package_wait:
action: store_false
parameter: --nowait
help: Do not wait on builds to complete
build_package_copr_config:
parameter: --copr-config
help: Path to a Copr config file
3 changes: 3 additions & 0 deletions obal/data/playbooks/scratch/metadata.obal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ variables:
action: store_false
parameter: --skip-koji-whitelist-check
help: ignore koji whitelist check and scratch build the package anyway
build_package_copr_config:
parameter: --copr-config
help: Path to a Copr config file
2 changes: 2 additions & 0 deletions obal/data/roles/build_package/defaults/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ build_package_waitrepo: false
build_package_koji_whitelist_check: false
build_package_tito_builder:
build_package_use_koji_build: false
build_package_copr_rebuild: false
build_package_skip_failed_build: false
56 changes: 4 additions & 52 deletions obal/data/roles/build_package/tasks/copr.yml
Original file line number Diff line number Diff line change
@@ -1,53 +1,5 @@
---
- 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
set_fact:
copr_repo_name: "{{ copr_user }}/{{ build_package_scratch_repo }}-{{ 999999999 | random | to_uuid }}"
run_once: true
when: copr_repo_name is not defined

- name: 'Write copr repo name to vars file'
copy:
content: "{{ 'copr_repo: ' + copr_repo_name | to_yaml }}"
dest: "{{ obal_tmp_dir }}/copr_repo"
mode: '0640'
run_once: true

- include_role:
name: copr_repo
run_once: true

- name: 'Build SRPM'
tito_build:
directory: "{{ inventory_dir }}/{{ package_base_dir }}/{{ inventory_hostname }}"
srpm: true
scl: "{{ scl }}"
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

- debug:
msg: "{{ build_status.stdout_lines | join('\n') }}"
- include_tasks: copr_build.yml
loop: "{{ copr_projects }}"
loop_control:
loop_var: copr_project
Loading

0 comments on commit 2f82d21

Please sign in to comment.