Skip to content

Commit a3f2d4b

Browse files
chore: run checks using celery UID/GID in container (ietf-tools#4364)
* chore: run checks as celery uid/gid in celery container * chore: add init flag to suggested beat container config
1 parent 727feab commit a3f2d4b

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

dev/celery/docker-init.sh

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,20 @@ CELERY_ROLE="${CELERY_ROLE:-worker}"
2121
cd "$WORKSPACEDIR" || exit 255
2222

2323
if [[ -n "${UPDATE_REQUIREMENTS_FROM}" ]]; then
24+
# Need to run as root in the container for this
2425
reqs_file="${WORKSPACEDIR}/${UPDATE_REQUIREMENTS_FROM}"
2526
echo "Updating requirements from ${reqs_file}..."
2627
pip install --upgrade -r "${reqs_file}"
2728
fi
2829

29-
if [[ "${CELERY_ROLE}" == "worker" ]]; then
30-
echo "Running initial checks..."
31-
/usr/local/bin/python $WORKSPACEDIR/ietf/manage.py check
32-
fi
33-
3430
CELERY_OPTS=( "${CELERY_ROLE}" )
3531
if [[ -n "${CELERY_UID}" ]]; then
36-
# ensure that some group with the necessary GID exists in container
32+
# ensure that a user with the necessary UID exists in container
3733
if ! id "${CELERY_UID}" ; then
3834
adduser --system --uid "${CELERY_UID}" --no-create-home --disabled-login "celery-user-${CELERY_UID}"
3935
fi
4036
CELERY_OPTS+=("--uid=${CELERY_UID}")
37+
CELERY_USERNAME="$(id -nu ${CELERY_UID})"
4138
fi
4239

4340
if [[ -n "${CELERY_GID}" ]]; then
@@ -46,8 +43,17 @@ if [[ -n "${CELERY_GID}" ]]; then
4643
addgroup --gid "${CELERY_GID}" "celery-group-${CELERY_GID}"
4744
fi
4845
CELERY_OPTS+=("--gid=${CELERY_GID}")
46+
CELERY_GROUP="$(getent group ${CELERY_GID} | awk -F: '{print $1}')"
4947
fi
5048

49+
run_as_celery_uid () {
50+
SU_OPTS=()
51+
if [[ -n "${CELERY_GROUP}" ]]; then
52+
SU_OPTS+=("-g" "${CELERY_GROUP}")
53+
fi
54+
su "${SU_OPTS[@]}" "${CELERY_USERNAME:-root}" -s /bin/sh -c "$@"
55+
}
56+
5157
log_term_timing_msgs () {
5258
# output periodic debug message
5359
while true; do
@@ -68,6 +74,12 @@ cleanup () {
6874
fi
6975
}
7076

77+
if [[ "${CELERY_ROLE}" == "worker" ]]; then
78+
echo "Running initial checks..."
79+
# Run checks as celery worker if one was specified
80+
run_as_celery_uid /usr/local/bin/python $WORKSPACEDIR/ietf/manage.py check
81+
fi
82+
7183
trap 'trap "" TERM; cleanup' TERM
7284
# start celery in the background so we can trap the TERM signal
7385
celery --app="${CELERY_APP:-ietf}" "${CELERY_OPTS[@]}" "$@" &

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ services:
8585
#
8686
# beat:
8787
# image: ghcr.io/ietf-tools/datatracker-celery:latest
88+
# init: true
8889
# environment:
8990
# CELERY_APP: ietf
9091
# CELERY_ROLE: beat

0 commit comments

Comments
 (0)