forked from fedora-copr/copr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rpmbuild, frontend: activate Red Hat subscription on demand
Fix fedora-copr#2132
- Loading branch information
Showing
5 changed files
with
137 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#! /usr/bin/python3 | ||
|
||
""" | ||
Final checks that the builder machine is ready to be used | ||
Everything printed to STDOUT will be redirected to the copr-backend logs, | ||
STDERR will be ignored. | ||
""" | ||
|
||
import os | ||
import sys | ||
import time | ||
from fnmatch import fnmatch | ||
from subprocess import run, PIPE | ||
from copr_rpmbuild.config import Config | ||
|
||
|
||
def check_mock_config(chroot): | ||
""" | ||
Does the mock config for this chroot exist? | ||
""" | ||
if chroot == "srpm-builds": | ||
return | ||
|
||
config = "/etc/mock/{}.cfg".format(chroot) | ||
if os.path.isfile(config): | ||
return | ||
|
||
print("Chroot config {} not found".format(config)) | ||
sys.exit(1) | ||
|
||
|
||
def subscription_required(chroot): | ||
""" | ||
Is subscription required for this task? | ||
""" | ||
config = Config() | ||
config.load_config() | ||
|
||
for pattern in config.rhsm: | ||
if fnmatch(chroot, pattern): | ||
return True | ||
return False | ||
|
||
|
||
def active_subscription(): | ||
""" | ||
Is subscription active on this system? | ||
""" | ||
# This implementation requires root privileges which we fortunately have | ||
# when calling this script. In case this function needs to be re-written to | ||
# be used under a normal user, it could be done by checking the existence of | ||
# the `/etc/pki/consumer/cert.pem` file. However, it will break in the | ||
# following corner cases: | ||
# - The system is registered and then halted for a long time and then | ||
# booted again after the entitlement is no longer valid | ||
# - The system is unregistered from the server (but not from the client | ||
# itself), making all the client files stale | ||
cmd = ["subscription-manager", "status"] | ||
return run(cmd, stdout=PIPE, stderr=PIPE, check=False) == 0 | ||
|
||
|
||
def wait_for_subscription(timeout=600): | ||
""" | ||
Wait until this system has an active subscription | ||
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. | ||
""" | ||
start = time.time() | ||
attempt = 1 | ||
while True: | ||
print("Checking Red Hat subscription (attempt #{0})".format(attempt)) | ||
if active_subscription(): | ||
print("Red Hat subscription active") | ||
return | ||
if time.time() > start + timeout: | ||
print("Waiting for Red Hat subscription timeouted!") | ||
sys.exit(1) | ||
time.sleep(30) | ||
attempt += 1 | ||
|
||
|
||
def main(): | ||
""" | ||
The entrypoint for this script | ||
""" | ||
try: | ||
chroot = sys.argv[1] | ||
check_mock_config(chroot) | ||
if subscription_required(chroot): | ||
wait_for_subscription() | ||
except RuntimeError as ex: | ||
print(ex) | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,8 @@ | |
# cute | ||
# multiline | ||
# snippet | ||
# | ||
# Chroots that require active Red Hat subscription | ||
# rhsm: | ||
# - rhel-* | ||
# - epel-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters