Skip to content

Commit

Permalink
rpmbuild: specify snippets to mock config via copr-rpmbuild config file
Browse files Browse the repository at this point in the history
This allows us to specify tpm fs size to rpmbuild in order to be able to
automatically generate its size for performance builders.

See fedora-copr#3268
  • Loading branch information
nikromen committed Sep 17, 2024
1 parent 2bdec45 commit 2d5ca8a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 2 deletions.
2 changes: 2 additions & 0 deletions rpmbuild/copr-rpmbuild.spec
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ BuildRequires: %{python}-requests
BuildRequires: %{python_pfx}-jinja2
BuildRequires: %{python_pfx}-specfile >= 0.21.0
BuildRequires: python3-backoff >= 1.9.0
BuildRequires: python3-pyyaml

BuildRequires: /usr/bin/argparse-manpage
BuildRequires: python-rpm-macros
Expand All @@ -57,6 +58,7 @@ Requires: %{python_pfx}-munch
Requires: %{python}-requests
Requires: %{python_pfx}-specfile >= 0.21.0
Requires: python3-backoff >= 1.9.0
Requires: python3-pyyaml

Requires: mock >= 5.0
Requires: git
Expand Down
9 changes: 9 additions & 0 deletions rpmbuild/copr_rpmbuild/builders/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
get_mock_uniqueext,
GentlyTimeoutedPopen,
macros_for_task,
mock_snippet_for_tags,
)
from ..config import Config

log = logging.getLogger("__main__")

Expand Down Expand Up @@ -42,6 +44,10 @@ def __init__(self, task, sourcedir, resultdir, config):
self.macros = macros_for_task(task, config)
self.uniqueext = get_mock_uniqueext()
self.allow_user_ssh = task.get("allow_user_ssh")
self.tags = task.get("tags", [])

self.copr_rpmbuild_config = Config()
self.copr_rpmbuild_config.load_config()

def run(self):
open(self.logfile, 'w').close() # truncate logfile
Expand Down Expand Up @@ -82,6 +88,9 @@ def render_config_template(self):
copr_build_id=self.build_id,
isolation=self.isolation,
macros=self.macros,
mock_snippet=mock_snippet_for_tags(
self.copr_rpmbuild_config.job_tag_to_mock_snippets, self.tags
),
)

def produce_srpm(self, spec, sources, resultdir):
Expand Down
17 changes: 17 additions & 0 deletions rpmbuild/copr_rpmbuild/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
import yaml

CONFIG_PATH = "/etc/copr-rpmbuild/copr-rpmbuild.yaml",

class Config:
def __init__(self):
self.job_tag_to_mock_snippets = []

def load_config(self):
if not os.path.exists(CONFIG_PATH):
return

with open(CONFIG_PATH, "r") as file:
config_data = yaml.safe_load(file) or {}

self.job_tag_to_mock_snippets = config_data.get('job_tag_to_mock_snippets', [])
13 changes: 13 additions & 0 deletions rpmbuild/copr_rpmbuild/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,16 @@ def package_version(name):
return pkg_resources.require(name)[0].version
except pkg_resources.DistributionNotFound:
return "git"


def mock_snippet_for_tags(job_tag_to_mock_snippets, tags):
"""
Return mock snippets as string separated by newlines for a given
list of tags.
"""
mock_snippets = []
for tag in tags:
snippet = job_tag_to_mock_snippets.get(tag)
if snippet:
mock_snippets.append(snippet)
return "\n".join(mock_snippets)
15 changes: 13 additions & 2 deletions rpmbuild/copr_rpmbuild/providers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

from copr_common.request import SafeRequest
from copr_rpmbuild.helpers import CONF_DIRS
from copr_rpmbuild.helpers import run_cmd
from copr_rpmbuild.helpers import run_cmd, mock_snippet_for_tags
from copr_rpmbuild.config import Config


log = logging.getLogger("__main__")
Expand Down Expand Up @@ -51,6 +52,16 @@ def __init__(self, source_dict, config, macros=None, task=None):
if e.errno != errno.EEXIST:
raise

self.copr_rpmbuild_config = Config()
self.copr_rpmbuild_config.load_config()

# Mock snippets to render in the mock config
self.mock_snippet = ""
if self.task is not None:
self.mock_snippet = mock_snippet_for_tags(
self.copr_rpmbuild_config.job_tag_to_mock_snippets, self.task.get("tags")
)

# Change home directory to workdir and create .rpmmacros there
os.environ["HOME"] = self.workdir
self.create_rpmmacros()
Expand Down Expand Up @@ -130,7 +141,7 @@ def render_mock_config_template(self, template_name):
"""
jinja_env = Environment(loader=FileSystemLoader(CONF_DIRS))
template = jinja_env.get_template(template_name)
return template.render(macros=self.macros)
return template.render(macros=self.macros, mock_snippet=self.mock_snippet)

def produce_srpm(self):
"""
Expand Down
1 change: 1 addition & 0 deletions rpmbuild/copr_rpmbuild/providers/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def render_mock_config_template(self, *_args):
chroot=self.chroot,
repos=self.repos,
macros=self.macros,
mock_snippet=self.mock_snippet,
)

def produce_srpm(self):
Expand Down
2 changes: 2 additions & 0 deletions rpmbuild/mock-custom-build.cfg.j2
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ config_opts["root"] = "copr-custom-" + config_opts["root"]
# /bin/mock calls (when tmpfs_enable is on).
config_opts['plugin_conf']['tmpfs_opts']['keep_mounted'] = True

{{ mock_snippet }}

{%- for key, value in macros.items() %}
config_opts['macros']['{{ key }}'] = '{{ value }}'
{%- endfor %}
Expand Down
2 changes: 2 additions & 0 deletions rpmbuild/mock.cfg.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ config_opts.setdefault('plugin_conf', {})
config_opts['plugin_conf'].setdefault('tmpfs_opts', {})
config_opts['plugin_conf']['tmpfs_opts']['keep_mounted'] = True

{{ mock_snippet }}

{% if buildroot_pkgs %}
config_opts['chroot_additional_packages'] = '{{ buildroot_pkgs| join(" ") }}'
{% endif %}
Expand Down

0 comments on commit 2d5ca8a

Please sign in to comment.