runners: create fake machine id when needed

Create fake machine before executing the runner command to
avoid the "Failed to resolve specifiers in '/var/log/journal/%m'"
errors.
This commit is contained in:
Miguel Martín 2023-11-21 08:38:21 +01:00 committed by Simon de Vlieger
parent b3c6366135
commit 99af178c6d
7 changed files with 100 additions and 97 deletions

View file

@ -1,8 +1,10 @@
import os.path
import pathlib
import platform
import shutil
import subprocess
import sys
from contextlib import contextmanager
def ldconfig(*dirs):
@ -31,6 +33,29 @@ def sysusers():
sys.exit(1)
@contextmanager
def create_machine_id_if_needed(tree="", keep_empty=False):
"""Create a machine-id with a fake machine id if it does not exist.
The machine-id file will be delete at context exit unless specified
with 'keep_empty' variable. In that case an empty machine-id will
be kept.
"""
path = pathlib.Path(f"{tree}/etc/machine-id")
try:
if not path.exists():
path.parent.mkdir(mode=0o755, exist_ok=True)
with path.open(mode="w", encoding="utf8") as f:
# create a fake machine ID to improve reproducibility
f.write("ffffffffffffffffffffffffffffffff\n")
path.chmod(0o444)
yield
finally:
path.unlink()
if keep_empty:
path.touch()
path.chmod(0o444)
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.