fix: pass env vars to systemd-run via --setenv (v0.3.5)

- Previous fix failed because systemd-run does not inherit env vars by default
- Added explicit --setenv flags for SKD_UPDATE_* variables
- Bumped version to 0.3.5
This commit is contained in:
2026-01-16 12:14:48 +01:00
parent bf7d1e466a
commit 806d768bbc
4 changed files with 21 additions and 16 deletions

View File

@ -199,30 +199,33 @@ def report_status(
def _run_async(script_path: Path, settings: Settings) -> None:
env = os.environ.copy()
env["SKD_UPDATE_SERVICE_URL"] = settings.update_service_url
env["SKD_UPDATE_PROJECT_ID"] = settings.update_project_id
env["SKD_UPDATE_TOKEN"] = _get_fresh_token(settings)
env["SKD_UPDATE_STATUS_FILE"] = settings.update_status_file
env["SKD_UPDATE_LOG_FILE"] = settings.update_log_file
# Explicitly gather the env vars we need to pass
env_vars = {
"SKD_UPDATE_SERVICE_URL": settings.update_service_url,
"SKD_UPDATE_PROJECT_ID": settings.update_project_id,
"SKD_UPDATE_TOKEN": _get_fresh_token(settings),
"SKD_UPDATE_STATUS_FILE": settings.update_status_file,
"SKD_UPDATE_LOG_FILE": settings.update_log_file,
}
# Use systemd-run to detach the update process from the current service unit.
# This ensures the script survives 'systemctl stop skd'.
# We use --unit to give it a predictable name prefix (though unique suffix is added)
# and --scope (or --service) to create a new unit.
# Since we need root (and likely run as root), this should work.
# Note: --collect ensures garbage collection of the transient unit.
# Construct systemd-run command with --setenv for each variable
cmd = [
"systemd-run",
"--unit=skd-update",
"--collect",
"--description=Safe Kiddo Update Process",
str(script_path),
]
for key, val in env_vars.items():
if val: # Only pass if not empty
cmd.append(f"--setenv={key}={val}")
cmd.append(str(script_path))
# We don't pass 'env' parameter to Popen because systemd-run ignores it
# for the target process (it only uses it for itself, but we use --setenv).
subprocess.Popen(
cmd,
env=env,
cwd="/",
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,