Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions deploy/scaleset-listener@.service
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions doc/changelog/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
`<prefix>/runners/<type>/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
Expand Down
33 changes: 30 additions & 3 deletions lib/listener-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---------------------------------------------------------
Expand Down Expand Up @@ -306,14 +308,26 @@ 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.
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) --------------------------------------
Expand Down Expand Up @@ -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

Expand All @@ -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')
Expand Down
31 changes: 27 additions & 4 deletions test/bats/integration/deploy_listener.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;;
Expand Down Expand Up @@ -374,18 +377,25 @@ 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 </dev/null
[ "${status}" -eq 0 ]
_systemctl_called daemon-reload
_systemctl_called restart scaleset-listener@gpu.service
_systemctl_called restart scaleset-listener@cpu.service
[ "$(stat -c %a "${PREFIX}/runners/idle/home")" = 700 ]
}

@test "redeploy keeps an explicit RUNNER_MANAGED_BY without appending an override" {
Expand Down Expand Up @@ -533,8 +543,10 @@ EOF
run _deploy
[ "${status}" -eq 0 ]
[ -d "${PREFIX}/runners/gpu/work" ]
[ -d "${PREFIX}/runners/gpu/home" ]
local dir
for dir in "${PREFIX}/runners" "${PREFIX}/runners/gpu" "${PREFIX}/runners/gpu/work"; do
for dir in "${PREFIX}/runners" "${PREFIX}/runners/gpu" "${PREFIX}/runners/gpu/work" \
"${PREFIX}/runners/gpu/home"; do
grep -A4 -xF 'name=chown' "${CAP}" | grep -qxF "${dir}"
done
grep -A4 -xF 'name=chown' "${CAP}" | grep -qxF 'ci-runner:ci-runner'
Expand All @@ -550,6 +562,17 @@ EOF
[ ! -d "${PREFIX}/runners" ]
}

@test "a dry run leaves an existing runner tree without creating work or home" {
mkdir -p "${PREFIX}/runners/gpu"
run bash -c "'${SCRIPT}' --dry-run --config '${CONFIG}' --type gpu \
--org-url https://github.com/acme --prefix '${PREFIX}' --etc '${ETC}'" </dev/null
[ "${status}" -eq 0 ]
! grep -qxF 'name=usermod' "${CAP}" || false
! grep -qxF 'name=chown' "${CAP}" || false
[ ! -e "${PREFIX}/runners/gpu/work" ]
[ ! -e "${PREFIX}/runners/gpu/home" ]
}

@test "a rerun warns on stderr to rotate the token from a non-0600 environment file" {
local file="${ETC}/gpu.env" token='existing-fixture-token'
install -d -m 0755 "${ETC}"
Expand Down Expand Up @@ -736,7 +759,7 @@ _assert_migration_failure_note() {
[ -d "${runner_home}/work" ]
[ "${runner_home}" = "${PREFIX}/runners/gpu" ]
grep -qxF "ReadWritePaths=${PREFIX}/runners/%i" "${unit}"
grep -qxF "Environment=HOME=${PREFIX}/runners/%i" "${unit}"
grep -qxF "Environment=HOME=${PREFIX}/runners/%i/home" "${unit}"
grep -qxF "ExecStart=${PREFIX}/bin/scaleset-listener" "${unit}"
grep -qxF "WorkingDirectory=${PREFIX}/listener" "${unit}"
grep -qxF "EnvironmentFile=${ETC}/%i.env" "${unit}"
Expand Down
27 changes: 27 additions & 0 deletions test/bats/integration/listener_reap.bats
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,33 @@ inv() {
[ -x "${SCRIPT}" ]
}

# #186: the listener unit sets HOME to a subdir of RUNNER_HOME. reap.sh sources
# lib/common.sh, whose SEC-3 guard refuses RUNNER_HOME==HOME; with the unit's
# shape the sweep must run, and with HOME==RUNNER_HOME it must still refuse.
@test "reap.sh passes SEC-3 with HOME a subdir of RUNNER_HOME, as the listener unit sets it (#186)" {
local unit="${ROOT}/deploy/scaleset-listener@.service"
local unit_home runner_home work_root
unit_home=$(sed -n 's|^Environment=HOME=/opt/github-runner-listener/||p' "${unit}")
[ -n "${unit_home}" ]
unit_home="${WORK}/${unit_home//%i/gpu}"
runner_home="${WORK}/runners/gpu"
work_root="${runner_home}/work"
mkdir -p "${unit_home}" "${work_root}/jit-orphan.XYZ"
inv orphanid orphan
run env HOME="${unit_home}" RUNNER_HOME="${runner_home}" RUNNER_WORK_ROOT="${work_root}" "${SCRIPT}"
[ "${status}" -eq 0 ]
grep -qxF -- 'orphanid' "${RM_CAP}"
[ ! -d "${work_root}/jit-orphan.XYZ" ]
}

@test "reap.sh still refuses to run when HOME equals RUNNER_HOME (#186)" {
inv orphanid orphan
run env HOME="${RUNNER_HOME}" "${SCRIPT}"
[ "${status}" -ne 0 ]
[[ "${output}" == *"refusing RUNNER_HOME"* ]]
[ ! -e "${RM_CAP}" ]
}

@test "reap.sh removes an orphan container and prunes its stale temp dir (#105/#106)" {
inv orphanid orphan
mkdir -p "${RUNNER_WORK_ROOT}/jit-orphan.XYZ"
Expand Down
21 changes: 21 additions & 0 deletions test/bats/unit/common.bats
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ setup() {
[ "${status}" -ne 0 ]
}

@test "sourcing common.sh still refuses RUNNER_HOME equal to HOME" {
run bash -c "HOME=/x/y RUNNER_HOME=/x/y source '${LIB}'"
[ "${status}" -ne 0 ]
[[ "${output}" == *refusing* ]]
}

# #186: the listener unit sets HOME and the environment file sets RUNNER_HOME;
# reap.sh sources common.sh under exactly that pair, so it must pass SEC-3.
# Both values are derived from the unit template so this follows the unit.
@test "sourcing common.sh passes SEC-3 with HOME and RUNNER_HOME as the listener unit sets them (#186)" {
local unit="${BATS_TEST_DIRNAME}/../../../deploy/scaleset-listener@.service"
local unit_home runner_home
unit_home=$(sed -n 's/^Environment=HOME=//p' "${unit}" | sed 's/%i/gpu/g')
runner_home=$(sed -n 's/^ReadWritePaths=//p' "${unit}" | sed 's/%i/gpu/g')
[ -n "${unit_home}" ]
[ "${runner_home}" = /opt/github-runner-listener/runners/gpu ]
run bash -c "HOME='${unit_home}' RUNNER_HOME='${runner_home}' source '${LIB}'"
[ "${status}" -eq 0 ]
[[ "${output}" != *refusing* ]]
}

@test "sourcing common.sh accepts a non-existent absolute RUNNER_HOME (first install)" {
run bash -c "RUNNER_HOME=/tmp/does-not-exist-yet/runners source '${LIB}'"
[ "${status}" -eq 0 ]
Expand Down
30 changes: 26 additions & 4 deletions test/bats/unit/listener_deploy.bats
Original file line number Diff line number Diff line change
Expand Up @@ -193,30 +193,50 @@ teardown() { rm -rf "${WORK}"; }
LISTENER_PREFIX='${WORK}/opt'; LISTENER_ETC='${WORK}/etc'; listener_deploy_paths
LISTENER_RUNNER_HOME=\$LISTENER_PREFIX/runners/gpu
LISTENER_WORK_ROOT=\$LISTENER_RUNNER_HOME/work
LISTENER_HOME=\$LISTENER_RUNNER_HOME/home
_chown() { echo \"CHOWN \$*\"; }
listener_ensure_work_root ci-runner
"
[ "${status}" -eq 0 ]
[ -d "${WORK}/opt/runners/gpu/work" ]
# The unit's HOME (#186): a private 0700 dir inside the runner home.
[ -d "${WORK}/opt/runners/gpu/home" ]
[ "$(stat -c %a "${WORK}/opt/runners/gpu/home")" = 700 ]
local dir
for dir in runners runners/gpu runners/gpu/work; do
for dir in runners runners/gpu runners/gpu/work runners/gpu/home; do
[[ "${output}" == *"CHOWN ci-runner:ci-runner"*"${WORK}/opt/${dir}"* ]]
done
[[ "${output}" != *"CHOWN -R"* ]]
}

@test "listener_ensure_work_root is safe to re-run on an existing tree (idempotent)" {
mkdir -p "${WORK}/opt/runners/gpu/work/jit-leftover"
mkdir -p "${WORK}/opt/runners/gpu/work/jit-leftover" "${WORK}/opt/runners/gpu/home/.docker"
run bash -c "
source '${LIB}'
LISTENER_PREFIX='${WORK}/opt'; LISTENER_ETC='${WORK}/etc'; listener_deploy_paths
LISTENER_RUNNER_HOME=\$LISTENER_PREFIX/runners/gpu
LISTENER_WORK_ROOT=\$LISTENER_RUNNER_HOME/work
LISTENER_HOME=\$LISTENER_RUNNER_HOME/home
_chown() { :; }
listener_ensure_work_root ci-runner
"
[ "${status}" -eq 0 ]
# Existing job directories are left alone.
[ -d "${WORK}/opt/runners/gpu/work/jit-leftover" ]
[ -d "${WORK}/opt/runners/gpu/home/.docker" ]
}

@test "listener_ensure_home rejects a symbolic link and preserves the target mode" {
mkdir -m 0755 "${WORK}/target"
ln -s "${WORK}/target" "${WORK}/home"
run bash -c '
source "$1"
_chown() { :; }
listener_ensure_home service-user "$2"
' bash "${LIB}" "${WORK}/home"
[ "${status}" -ne 0 ]
[[ "${output}" == *"listener HOME is a symbolic link"* ]]
[ "$(stat -c %a "${WORK}/target")" = 755 ]
}

# --- the environment file (the secret) -------------------------------------
Expand Down Expand Up @@ -1077,8 +1097,10 @@ teardown() { rm -rf "${WORK}"; }
[ "$(sed -n 's/^ReadWritePaths=//p' "${unit}")" = '/opt/github-runner-listener/runners/%i' ]
# HOME inside the writable tree: with ProtectHome the docker CLI's credential
# helper (docker-credential-pass) cannot stat ~/.password-store and every
# image pull exits 126.
[ "$(sed -n 's/^Environment=HOME=//p' "${unit}")" = '/opt/github-runner-listener/runners/%i' ]
# image pull exits 126. It is a subdir, not RUNNER_HOME itself, because SEC-3
# in lib/common.sh refuses RUNNER_HOME==HOME and orphan reaping died (#186).
[ "$(sed -n 's/^Environment=HOME=//p' "${unit}")" = '/opt/github-runner-listener/runners/%i/home' ]
[ "$(sed -n 's/^Environment=HOME=//p' "${unit}")" != "$(sed -n 's/^ReadWritePaths=//p' "${unit}")" ]
for setting in \
'User=ci-runner' 'Group=ci-runner' \
'WorkingDirectory=/opt/github-runner-listener/listener' \
Expand Down