Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Copr build to focus on chroot #356

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ ignore=docs,tests
disable=R0205,C0209,W1514,R1735
min-similarity-lines=10
max-line-length=120
max-locals=20
58 changes: 48 additions & 10 deletions obal/data/module_utils/obal.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@
from .koji_wrapper import koji, KojiCommandError # pylint:disable=import-error,no-name-in-module


def macro_lookup(command, scl=None, dist=None, macros=None):
"""run a macro lookup command"""
if dist:
command += ['--define', 'dist %s' % dist]
else:
command += ['--undefine', 'dist']

if scl:
command += ['--define', 'scl %s' % scl]

if macros is not None:
for (macro, value) in macros.items():
command += ['--define', '%s %s' % (macro, value)]

return subprocess.check_output(command, universal_newlines=True)


def specfile_macro_lookup(specfile, macro_str, scl=None, dist=None, macros=None):
"""expand a given macro from a specfile"""
command = [
Expand All @@ -21,19 +38,20 @@ def specfile_macro_lookup(specfile, macro_str, scl=None, dist=None, macros=None)
specfile
]

if dist:
command += ['--define', '"dist %s"' % dist]
else:
command += ['--undefine', 'dist']
return macro_lookup(command, scl=scl, dist=dist, macros=macros)

if scl:
command += ['--define', '"scl %s"' % scl]

if macros is not None:
for (macro, value) in macros.items():
command += ['--define', '"%s %s"' % (macro, value)]
def srpm_macro_lookup(srpm, macro_str, scl=None, dist=None, macros=None):
"""expand a given macro from an srpm"""
command = [
'rpmquery',
'--queryformat',
macro_str,
'--package',
srpm
evgeni marked this conversation as resolved.
Show resolved Hide resolved
]

return subprocess.check_output(' '.join(command), universal_newlines=True, shell=True)
return macro_lookup(command, scl=scl, dist=dist, macros=macros)


def get_changelog_evr(specfile):
Expand All @@ -54,21 +72,41 @@ def get_specfile_evr(specfile):
return specfile_macro_lookup(specfile, '%{evr}')


def get_srpm_evr(srpm):
"""get the EVR from the source header of the srpm"""
return srpm_macro_lookup(srpm, '%{evr}')


def get_specfile_name(specfile, scl=None):
"""get the name from the specfile"""
return specfile_macro_lookup(specfile, '%{name}', scl=scl)


def get_srpm_name(srpm, scl=None):
"""get the name from the srpm"""
return srpm_macro_lookup(srpm, '%{name}', scl=scl)


def get_specfile_nevr(specfile, scl=None, dist=None, macros=None):
"""get the name, epoch, version and release from the specfile"""
return specfile_macro_lookup(specfile, '%{nevr}', scl=scl, dist=dist, macros=macros)


def get_srpm_nevr(srpm, scl=None, dist=None, macros=None):
"""get the name, epoch, version and release from the srpm"""
return srpm_macro_lookup(srpm, '%{nevr}', scl=scl, dist=dist, macros=macros)


def get_specfile_nvr(specfile, scl=None, dist=None, macros=None):
"""get the name, version and release from the specfile"""
return specfile_macro_lookup(specfile, '%{nvr}', scl=scl, dist=dist, macros=macros)


def get_srpm_nvr(srpm, scl=None, dist=None, macros=None):
"""get the name, version and release from the srpm"""
return srpm_macro_lookup(srpm, '%{nvr}', scl=scl, dist=dist, macros=macros)


def get_whitelist_status(build_command, tag, package):
"""
Get whitelist status of a given package within a tag.
Expand Down
103 changes: 82 additions & 21 deletions obal/data/modules/copr_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,117 @@
"""

import re
import json

from subprocess import CalledProcessError
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
from ansible.module_utils.obal import get_srpm_name, get_srpm_nevr # pylint:disable=import-error,no-name-in-module


def get_package_info(module, user, project, package, config_file=None):
"""
Fetch package info from Copr
"""
command = [
'get-package',
full_name(user, project),
'--name',
package,
'--with-all-builds'
]

try:
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) in error.message:
info = {}
else:
module.fail_json(msg='Retrieval of package from Copr failed', command=command, output=error.message,
repo_name=full_name(user, project), package=package)

return info

def build_exists(nevr, package_info, chroot=None):
"""
Determine if build exists in Copr for a given NEVR
"""
exists = False

if 'builds' in package_info:
chroots = [
build for build in package_info['builds']
if chroot in build['chroots']
]
successful_builds = [
build for build in chroots
if build['state'] == 'succeeded'
]
successful_nevrs = [
"{}-{}".format(build['source_package']['name'], build['source_package']['version'])
for build in successful_builds
]

exists = nevr in set(successful_nevrs)

return exists

def main():
"""
Release a package to Copr
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),
chroot=dict(type='str', required=False),
config_file=dict(type='str', required=False),
force=dict(type='bool', required=False, default=False)
)
)

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

command = [
'build',
full_name(user, project),
srpm
]
try:
package_name = get_srpm_name(srpm)
nevr = get_srpm_nevr(srpm)
except CalledProcessError as error:
module.fail_json(msg="{} / {}".format(error.stdout, error.stderr), command=error.cmd, output=error.output)

if not wait:
command.append('--nowait')
package_info = get_package_info(module, user, project, package_name, config_file)

if chroots:
for chroot in chroots:
command.extend(['--chroot', chroot])
if force or not build_exists(nevr, package_info, chroot):
command = [
'build',
full_name(user, project),
srpm,
'--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)
if not wait:
command.append('--nowait')

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+(.+)\s*', output, re.MULTILINE)
builds = re.findall(r'^Created builds:\s(\d+)', output, re.MULTILINE)
build_urls = re.findall(r'^Build was added to.+:\n^\s+(.+)\s*', output, re.MULTILINE)
builds = re.findall(r'^Created builds:\s(\d+)', output, re.MULTILINE)

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


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion obal/data/modules/copr_build_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def main():
successful_nevrs = ("{}-{}".format(package, build['source_package']['version']) for build in successful_builds)
exists = nevr in successful_nevrs

module.exit_json(exists=exists)
module.exit_json(info=package_info, exists=exists)


if __name__ == '__main__':
Expand Down
Loading
Loading