Skip to content

Commit

Permalink
Consolidate runners functions
Browse files Browse the repository at this point in the history
Consolidate runners functions into a sigle file
to avoid code duplication.
  • Loading branch information
mmartinv committed Nov 20, 2023
1 parent 60c96fe commit 97b515f
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 311 deletions.
82 changes: 82 additions & 0 deletions osbuild/util/runners.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import os.path
import platform
import shutil
import subprocess
import sys


def ldconfig(*dirs):
# ld.so.conf must exist, or `ldconfig` throws a warning
subprocess.run(["touch", "/etc/ld.so.conf"], check=True)

if len(dirs) > 0:
with open("/etc/ld.so.conf", "w", encoding="utf8") as f:
for d in dirs:
f.write(f"{d}\n")
f.flush()

subprocess.run(["ldconfig"], check=True)


def sysusers():
try:
subprocess.run(
["systemd-sysusers"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True,
)
except subprocess.CalledProcessError as error:
sys.stderr.write(error.stdout)
sys.exit(1)


def tmpfiles():
# Allow systemd-tmpfiles to return non-0. Some packages want to create
# directories owned by users that are not set up with systemd-sysusers.
subprocess.run(["systemd-tmpfiles", "--create"], check=False)


def nsswitch():
# the default behavior is fine, but using nss-resolve does not
# necessarily work in a non-booted container, so make sure that
# is not configured.
try:
os.remove("/etc/nsswitch.conf")
except FileNotFoundError:
pass


def python_alternatives():
"""/usr/bin/python3 is a symlink to /etc/alternatives/python3, which points
to /usr/bin/python3.6 by default. Recreate the link in /etc, so that
shebang lines in stages and assemblers work.
"""
os.makedirs("/etc/alternatives", exist_ok=True)
try:
os.symlink("/usr/bin/python3.6", "/etc/alternatives/python3")
except FileExistsError:
pass


def sequoia():
# This provides a default set of crypto-policies which is important for
# re-enabling SHA1 support with rpm (so we can cross-build CentOS-Stream-9
# images).
os.makedirs("/etc/crypto-policies", exist_ok=True)
shutil.copytree(
"/usr/share/crypto-policies/back-ends/DEFAULT", "/etc/crypto-policies/back-ends"
)


def quirks():
# Platform specific quirks
env = os.environ.copy()

if platform.machine() == "aarch64":
# Work around a bug in qemu-img on aarch64 that can lead to qemu-img
# hangs when more then one coroutine is use (which is the default)
# See https://bugs.launchpad.net/qemu/+bug/1805256
env["OSBUILD_QEMU_IMG_COROUTINES"] = "1"

return env
46 changes: 8 additions & 38 deletions runners/org.osbuild.centos9
Original file line number Diff line number Diff line change
@@ -1,48 +1,18 @@
#!/usr/bin/python3

import os
import subprocess
import sys

import osbuild.api


def ldconfig():
# ld.so.conf must exist, or `ldconfig` throws a warning
subprocess.run(["touch", "/etc/ld.so.conf"], check=True)
subprocess.run(["ldconfig"], check=True)


def sysusers():
try:
subprocess.run(["systemd-sysusers"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True)
except subprocess.CalledProcessError as error:
sys.stderr.write(error.stdout)
sys.exit(1)


def tmpfiles():
# Allow systemd-tmpfiles to return non-0. Some packages want to create
# directories owned by users that are not set up with systemd-sysusers.
subprocess.run(["systemd-tmpfiles", "--create"], check=False)


def nsswitch():
# the default behavior is fine, but using nss-resolve does not
# necessarily work in a non-booted container, so make sure that
# is not configured.
try:
os.remove("/etc/nsswitch.conf")
except FileNotFoundError:
pass

from osbuild import api
from osbuild.util import runners

if __name__ == "__main__":
with osbuild.api.exception_handler():
ldconfig()
sysusers()
tmpfiles()
nsswitch()
with api.exception_handler():
runners.ldconfig()
runners.sysusers()
runners.tmpfiles()
runners.nsswitch()

r = subprocess.run(sys.argv[1:], check=False)

sys.exit(r.returncode)
46 changes: 8 additions & 38 deletions runners/org.osbuild.fedora30
Original file line number Diff line number Diff line change
@@ -1,48 +1,18 @@
#!/usr/bin/python3

import os
import subprocess
import sys

import osbuild.api


def ldconfig():
# ld.so.conf must exist, or `ldconfig` throws a warning
subprocess.run(["touch", "/etc/ld.so.conf"], check=True)
subprocess.run(["ldconfig"], check=True)


def sysusers():
try:
subprocess.run(["systemd-sysusers"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True)
except subprocess.CalledProcessError as error:
sys.stderr.write(error.stdout)
sys.exit(1)


def tmpfiles():
# Allow systemd-tmpfiles to return non-0. Some packages want to create
# directories owned by users that are not set up with systemd-sysusers.
subprocess.run(["systemd-tmpfiles", "--create"], check=False)


def nsswitch():
# the default behavior is fine, but using nss-resolve does not
# necessarily work in a non-booted container, so make sure that
# is not configured.
try:
os.remove("/etc/nsswitch.conf")
except FileNotFoundError:
pass

from osbuild import api
from osbuild.util import runners

if __name__ == "__main__":
with osbuild.api.exception_handler():
ldconfig()
sysusers()
tmpfiles()
nsswitch()
with api.exception_handler():
runners.ldconfig()
runners.sysusers()
runners.tmpfiles()
runners.nsswitch()

r = subprocess.run(sys.argv[1:], check=False)

sys.exit(r.returncode)
58 changes: 8 additions & 50 deletions runners/org.osbuild.fedora38
Original file line number Diff line number Diff line change
@@ -1,60 +1,18 @@
#!/usr/bin/python3

import os
import shutil
import subprocess
import sys

import osbuild.api


def ldconfig():
# ld.so.conf must exist, or `ldconfig` throws a warning
subprocess.run(["touch", "/etc/ld.so.conf"], check=True)
subprocess.run(["ldconfig"], check=True)


def sysusers():
try:
subprocess.run(["systemd-sysusers"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True)
except subprocess.CalledProcessError as error:
sys.stderr.write(error.stdout)
sys.exit(1)


def tmpfiles():
# Allow systemd-tmpfiles to return non-0. Some packages want to create
# directories owned by users that are not set up with systemd-sysusers.
subprocess.run(["systemd-tmpfiles", "--create"], check=False)


def nsswitch():
# the default behavior is fine, but using nss-resolve does not
# necessarily work in a non-booted container, so make sure that
# is not configured.
try:
os.remove("/etc/nsswitch.conf")
except FileNotFoundError:
pass


def sequoia():
# This provides a default set of crypto-policies which is important for
# re-enabling SHA1 support with rpm (so we can cross-build CentOS-Stream-9
# images).
os.makedirs("/etc/crypto-policies", exist_ok=True)
shutil.copytree(
"/usr/share/crypto-policies/back-ends/DEFAULT", "/etc/crypto-policies/back-ends"
)

from osbuild import api
from osbuild.util import runners

if __name__ == "__main__":
with osbuild.api.exception_handler():
ldconfig()
sysusers()
tmpfiles()
nsswitch()
sequoia()
with api.exception_handler():
runners.ldconfig()
runners.sysusers()
runners.tmpfiles()
runners.nsswitch()
runners.sequoia()

r = subprocess.run(sys.argv[1:], check=False)
sys.exit(r.returncode)
30 changes: 5 additions & 25 deletions runners/org.osbuild.rhel7
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
#!/usr/bin/python3

import os
import subprocess
import sys

import osbuild.api


def ldconfig():
# ld.so.conf must exist, or `ldconfig` throws a warning
with open("/etc/ld.so.conf", "w", encoding="utf8") as f:
# qemu-img needs `libiscsi`, which is located in /usr/lib64/iscsi
f.write("/usr/lib64/iscsi\n")
f.flush()
subprocess.run(["ldconfig"], check=True)


def nsswitch():
# the default behavior is fine, but using nss-resolve does not
# necessarily work in a non-booted container, so make sure that
# is not configured.
try:
os.remove("/etc/nsswitch.conf")
except FileNotFoundError:
pass

from osbuild import api
from osbuild.util import runners

if __name__ == "__main__":
with osbuild.api.exception_handler():
ldconfig()
nsswitch()
with api.exception_handler():
runners.ldconfig("/usr/lib64/iscsi")
runners.nsswitch()

r = subprocess.run(sys.argv[1:], check=False)
sys.exit(r.returncode)
57 changes: 8 additions & 49 deletions runners/org.osbuild.rhel81
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,8 @@ import os
import subprocess
import sys

import osbuild.api


def ldconfig():
# ld.so.conf must exist, or `ldconfig` throws a warning
subprocess.run(["touch", "/etc/ld.so.conf"], check=True)
subprocess.run(["ldconfig"], check=True)


def sysusers():
try:
subprocess.run(["systemd-sysusers"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True)
except subprocess.CalledProcessError as error:
sys.stderr.write(error.stdout)
sys.exit(1)


def tmpfiles():
# Allow systemd-tmpfiles to return non-0. Some packages want to create
# directories owned by users that are not set up with systemd-sysusers.
subprocess.run(["systemd-tmpfiles", "--create"], check=False)


def nsswitch():
# the default behavior is fine, but using nss-resolve does not
# necessarily work in a non-booted container, so make sure that
# is not configured.
try:
os.remove("/etc/nsswitch.conf")
except FileNotFoundError:
pass
from osbuild import api
from osbuild.util import runners


def os_release():
Expand All @@ -60,26 +31,14 @@ def os_release():
f.write('BUG_REPORT_URL="https://bugzilla.redhat.com/"\n')


def python_alternatives():
"""/usr/bin/python3 is a symlink to /etc/alternatives/python3, which points
to /usr/bin/python3.6 by default. Recreate the link in /etc, so that
shebang lines in stages and assemblers work.
"""
os.makedirs("/etc/alternatives", exist_ok=True)
try:
os.symlink("/usr/bin/python3.6", "/etc/alternatives/python3")
except FileExistsError:
pass


if __name__ == "__main__":
with osbuild.api.exception_handler():
ldconfig()
sysusers()
tmpfiles()
nsswitch()
with api.exception_handler():
runners.ldconfig()
runners.sysusers()
runners.tmpfiles()
runners.nsswitch()
os_release()
python_alternatives()
runners.python_alternatives()

r = subprocess.run(sys.argv[1:], check=False)
sys.exit(r.returncode)
Loading

0 comments on commit 97b515f

Please sign in to comment.