Skip to content

Commit

Permalink
rpmbuild, frontend: activate Red Hat subscription on demand
Browse files Browse the repository at this point in the history
Fix #2132
  • Loading branch information
FrostyX committed Sep 29, 2024
1 parent 8b0977a commit a4e9ee7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
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
47 changes: 47 additions & 0 deletions rpmbuild/copr_rpmbuild/rhsm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
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 wait for the
subscription only when necessary.
"""

import time
import logging
import subprocess


log = logging.getLogger("__main__")


def subscription_required(task):
"""
Is subscription required for this task?
"""
return "subscription" in task["tags"]


def active_subscription():
"""
Is subscription active on this system?
"""
cmd = ["subscription-manager", "status"]
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Check warning

Code scanning / vcs-diff-lint

active_subscription: 'subprocess.run' used without explicitly defining the value for 'check'. Warning

active_subscription: 'subprocess.run' used without explicitly defining the value for 'check'.
return proc.returncode == 0


def wait_for_subscription(timeout=86400):
"""
Wait until this system has an active subscription
"""
start = time.time()
attempt = 1
while True:
log.info("Checking Red Hat subscription (attempt #%s)", attempt)
if active_subscription():
log.info("Red Hat subscription active")
return
if time.time() > start + timeout:
raise RuntimeError("Waiting for Red Hat subscription timeouted!")
time.sleep(30)
attempt += 1
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, wait_for_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):
wait_for_subscription()

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

0 comments on commit a4e9ee7

Please sign in to comment.