Skip to content

Commit

Permalink
rpmbuild, frontend: activate Red Hat subscription on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostyX committed Sep 26, 2024
1 parent 8b0977a commit b1320b1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ def get_build_record(task, for_backend=False):
"sandbox": task.build.sandbox,
"background": bool(task.build.is_background),
"chroot": task.mock_chroot.name,
"tags": task.mock_chroot.tags + task.tags,
"tags": task.mock_chroot.tags + task.tags + (
# TODO just for testing purposes, add a boolean
# `models.MockChroot.subscription` column to the database?
["subscription"] if task.mock_chroot.name == "rhel-9-x86_64" else []
),
"allow_user_ssh": bool(task.build.ssh_public_keys),
}

Expand Down
5 changes: 3 additions & 2 deletions rpmbuild/copr_rpmbuild/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ def cmd_readable(cmd):
return ' '.join([shlex.quote(part) for part in cmd])


def run_cmd(cmd, cwd=".", preexec_fn=None, env=None):
def run_cmd(cmd, cwd=".", preexec_fn=None, env=None, timeout=None):
"""
Runs given command in a subprocess.
:param list(str) cmd: command to be executed and its arguments
:param str cwd: In which directory to execute the command
:param func preexec_fn: a callback invoked before exec in subprocess
:param dict env: environment variables to set for process
:param int timeout: passed down to Popen.communicate()
:raises RuntimeError
:returns munch.Munch(cmd, stdout, stderr, returncode)
Expand All @@ -55,7 +56,7 @@ def run_cmd(cmd, cwd=".", preexec_fn=None, env=None):
try:
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, preexec_fn=preexec_fn, env=env)
(stdout, stderr) = process.communicate()
(stdout, stderr) = process.communicate(timeout=timeout)
except OSError as e:
if e.errno == errno.ENOENT:
raise RuntimeError(
Expand Down
41 changes: 41 additions & 0 deletions rpmbuild/copr_rpmbuild/rhsm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Red Hat Subscription Management
Activating Red Hat subscription may take a lot of time and historically, the
subscription service used to be unreliable, so we should activate the
subscription only when necessary.
"""

import os
import shlex
from copr_rpmbuild.helpers import run_cmd


def activate_subscription():
"""
Activate Red Hat Subscription.
"""
pool = "8a85f9a17c71102f017ce611251c770f"
user = "copr-team"
# TODO Not sure where to store this password so that it is not readable from
# the builder (we give SSH access to users, so it cannot be in ENV)
password = "TODO"
name = os.environ.get("RESALLOC_NAME", "unknown-builder")
system = name.replace("_", "-")
timeout = 200

cmd = [
"/usr/local/bin/copr-rh-subscribe.sh",
"--pool-id", pool,
"--user", user,
"--pass", shlex.quote(password),
"--system", shlex.quote(system),
]
run_cmd(cmd, timeout=timeout)


def subscription_required(task):
"""
Is subscription required for this task?
"""
return "subscription" in task["tags"]
4 changes: 4 additions & 0 deletions rpmbuild/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
locate_srpm,
package_version,
)
from copr_rpmbuild.rhsm import subscription_required, activate_subscription
from six.moves.urllib.parse import urlparse, urljoin, urlencode

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -256,6 +257,9 @@ def build_rpm(args, config):
task = get_task(args, config, build_config_url_path, task_id)
log_task(task)

if subscription_required(task):
activate_subscription()

try:
source_json = {
"clone_url": task["git_repo"],
Expand Down

0 comments on commit b1320b1

Please sign in to comment.