diff --git a/deploy/scaleset-listener@.service b/deploy/scaleset-listener@.service index b5aa6c3..bf15c9a 100644 --- a/deploy/scaleset-listener@.service +++ b/deploy/scaleset-listener@.service @@ -58,8 +58,11 @@ ReadWritePaths=/opt/github-runner-listener/runners/%i # HOME inside the same writable tree. ProtectHome hides /home/ci-runner, and on # a host with docker-credential-pass installed the docker CLI then fails every # image pull (cannot stat ~/.password-store) with exit 126. A HOME it can read -# turns that into "no stored credentials" and an anonymous pull. -Environment=HOME=/opt/github-runner-listener/runners/%i +# turns that into "no stored credentials" and an anonymous pull. It is a subdir +# (the deploy creates it 0700 for the service user), not RUNNER_HOME itself: +# lib/common.sh (SEC-3) refuses RUNNER_HOME==HOME, which made every orphan +# reap fail (#186). +Environment=HOME=/opt/github-runner-listener/runners/%i/home [Install] WantedBy=multi-user.target diff --git a/doc/changelog/CHANGELOG.md b/doc/changelog/CHANGELOG.md index fb2d45a..de0ebc4 100644 --- a/doc/changelog/CHANGELOG.md +++ b/doc/changelog/CHANGELOG.md @@ -217,6 +217,12 @@ versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Fixed +- **Orphan reaping works again** (#186): since #168 the listener unit set + `HOME` to the runner home itself, so the SEC-3 guard (RUNNER_HOME must not + equal HOME) refused every orphan sweep and stranded containers and `jit-*` + work dirs were no longer cleaned up. The unit's `HOME` is now + `/runners//home`. Redeploy to create these directories with + mode 0700 for the service user before restarting listeners. - **Declared job sizes are charged and released exactly once** (#177): a job cancelled while waiting for a worker slot no longer leaves its cores and GB committed; a job admitted before the first successful host probe diff --git a/lib/listener-deploy.sh b/lib/listener-deploy.sh index 7f129b5..cb20740 100644 --- a/lib/listener-deploy.sh +++ b/lib/listener-deploy.sh @@ -108,6 +108,8 @@ listener_type_paths() { LISTENER_UNIT_NAME="scaleset-listener@${LISTENER_TYPE}.service" LISTENER_RUNNER_HOME="${LISTENER_PREFIX}/runners/${LISTENER_TYPE}" LISTENER_WORK_ROOT="${LISTENER_RUNNER_HOME}/work" + # The unit's Environment=HOME (#186): a subdir, never RUNNER_HOME itself. + LISTENER_HOME="${LISTENER_RUNNER_HOME}/home" } # --- the admin tool --------------------------------------------------------- @@ -306,6 +308,17 @@ listener_ensure_engine_access() { # --- the runner state tree --------------------------------------------------- +# The unit's private HOME lives inside RUNNER_HOME so SEC-3 can permit reaping. +listener_ensure_home() { + local user=$1 home=$2 + if [[ -L ${home} ]]; then + printf 'FAIL: listener HOME is a symbolic link: %s\n' "${home}" >&2 + return 1 + fi + install -d -m 0700 "${home}" + _chown "${user}:${user}" "${home}" +} + # systemd requires the writable directory to exist before starting the unit. # Own the shared parent too so the service user can traverse it. Avoid # recursive ownership changes because jobs and other types already live here. @@ -313,7 +326,8 @@ listener_ensure_work_root() { local user=$1 parent="${LISTENER_PREFIX}/runners" install -d -m 0750 "${parent}" "${LISTENER_RUNNER_HOME}" "${LISTENER_WORK_ROOT}" _chown "${user}:${user}" "${parent}" "${LISTENER_RUNNER_HOME}" "${LISTENER_WORK_ROOT}" - echo " work root: ${LISTENER_WORK_ROOT} (owned by ${user})" + listener_ensure_home "${user}" "${LISTENER_HOME}" + echo " work root: ${LISTENER_WORK_ROOT}, home ${LISTENER_HOME} (owned by ${user})" } # --- the environment file (the secret) -------------------------------------- @@ -452,7 +466,7 @@ listener_install_unit() { } listener_deploy_template() ( - local src=$1 user=$2 rendered prefix etc replacing=0 active_units unit + local src=$1 user=$2 rendered prefix etc replacing=0 active_units unit env_file type runner_home rendered=$(mktemp) trap 'rm -f -- "${rendered}"' EXIT @@ -468,7 +482,20 @@ listener_deploy_template() ( if listener_unit_current "${rendered}" "${LISTENER_UNIT_PATH}"; then echo " systemd unit: ${LISTENER_UNIT_PATH} already current, skipped" else - [[ ! -f ${LISTENER_UNIT_PATH} ]] || replacing=1 + if [[ -f ${LISTENER_UNIT_PATH} ]]; then + replacing=1 + # The shared template also changes HOME for installed sibling instances. + # Prepare their directories before installing it, including inactive units. + for env_file in "${LISTENER_ETC}"/*.env; do + [[ -f ${env_file} && ${env_file} != "${LISTENER_ENV_FILE}" ]] || continue + type=${env_file##*/} + type=${type%.env} + listener_valid_type_name "${type}" || continue + runner_home="${LISTENER_PREFIX}/runners/${type}" + [[ -d ${runner_home} ]] || continue + listener_ensure_home "${user}" "${runner_home}/home" + done + fi listener_install_unit "${rendered}" "${LISTENER_UNIT_PATH}" if (( replacing )); then active_units=$(_systemctl list-units --type=service --state=active --no-legend --plain --full --no-pager 'scaleset-listener@*.service') diff --git a/test/bats/integration/deploy_listener.bats b/test/bats/integration/deploy_listener.bats index 3a26678..19d992f 100644 --- a/test/bats/integration/deploy_listener.bats +++ b/test/bats/integration/deploy_listener.bats @@ -98,6 +98,9 @@ case "$1" in disable) rm -f "${STUB_STATE}/$2.enabled"; exit 0 ;; start|restart) [ "$2" = "${STUB_FAIL_START_UNIT:-}" ] && exit 1 + if [ "$2" = "${STUB_CHECK_HOME_UNIT:-}" ]; then + [ "$(stat -c %a "${STUB_CHECK_HOME_DIR}")" = 700 ] || exit 1 + fi : > "${STUB_STATE}/$2.started"; exit 0 ;; stop) rm -f "${STUB_STATE}/$2.started"; exit 0 ;; *) exit 0 ;; @@ -374,11 +377,17 @@ EOF grep -qxF 'RUNNER_MANAGED_BY=github-runner-listener-gpu' "${ETC}/gpu.env" } -@test "redeploy restarts other active instances after replacing the shared template" { +@test "redeploy prepares sibling homes before restarting active instances with the shared template" { run _deploy --skip-github [ "${status}" -eq 0 ] + local unit="${LISTENER_UNIT_DIR}/scaleset-listener@.service" + sed -i '/^Environment=HOME=/s|/home$||' "${unit}" + mkdir -p "${PREFIX}/runners/cpu/work" "${PREFIX}/runners/idle/home" + chmod 0755 "${PREFIX}/runners/idle/home" + touch "${ETC}/cpu.env" "${ETC}/idle.env" touch "${STUB_STATE}/scaleset-listener@cpu.service.started" - printf '\n# Older installed template\n' >> "${LISTENER_UNIT_DIR}/scaleset-listener@.service" + export STUB_CHECK_HOME_UNIT=scaleset-listener@cpu.service + export STUB_CHECK_HOME_DIR="${PREFIX}/runners/cpu/home" : > "${CAP}" run _deploy_host --type gpu --skip-github