Skip to content

Commit

Permalink
entrpoint/boot
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderegg committed Nov 4, 2024
1 parent add6a76 commit 3b10e76
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 82 deletions.
58 changes: 41 additions & 17 deletions services/director/docker/boot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,56 @@ IFS=$(printf '\n\t')

INFO="INFO: [$(basename "$0")] "

# BOOTING application ---------------------------------------------
echo "$INFO" "Booting in ${SC_BOOT_MODE} mode ..."
echo " User :$(id "$(whoami)")"
echo " Workdir :$(pwd)"
echo "$INFO" "User :$(id "$(whoami)")"
echo "$INFO" "Workdir : $(pwd)"

#
# DEVELOPMENT MODE
#
# - prints environ info
# - installs requirements in mounted volume
#
if [ "${SC_BUILD_TARGET}" = "development" ]; then
echo "$INFO" "Environment :"
printenv | sed 's/=/: /' | sed 's/^/ /' | sort
echo "$INFO" "Python :"
python --version | sed 's/^/ /'
command -v python | sed 's/^/ /'
cd services/director || exit 1
# speedup for legacy service with all essential dependencies pinned
# in this case `--no-deps` does the trick, for details see link
# https://stackoverflow.com/a/65793484/2855718
pip install --no-cache-dir --no-deps -r requirements/dev.txt
cd - || exit 1
echo "$INFO" "PIP :"
pip list | sed 's/^/ /'

cd services/autoscaling
uv pip --quiet --no-cache-dir sync requirements/dev.txt
cd -
uv pip list
fi

if [ "${SC_BOOT_MODE}" = "debug" ]; then
# NOTE: production does NOT pre-installs debugpy
uv pip install --no-cache-dir debugpy
fi

# RUNNING application ----------------------------------------
if [ "${SC_BOOT_MODE}" = "debug-ptvsd" ]; then
watchmedo auto-restart --recursive --pattern="*.py;*/src/*" --ignore-patterns="*test*;pytest_simcore/*;setup.py;*ignore*" --ignore-directories -- \
python3 -m ptvsd --host 0.0.0.0 --port 3000 -m \
simcore_service_director --loglevel="${LOGLEVEL}"
#
# RUNNING application
#

APP_LOG_LEVEL=${DIRECTOR_LOGLEVEL:-${LOG_LEVEL:-${LOGLEVEL:-INFO}}}
SERVER_LOG_LEVEL=$(echo "${APP_LOG_LEVEL}" | tr '[:upper:]' '[:lower:]')
echo "$INFO" "Log-level app/server: $APP_LOG_LEVEL/$SERVER_LOG_LEVEL"

if [ "${SC_BOOT_MODE}" = "debug" ]; then
reload_dir_packages=$(find /devel/packages -maxdepth 3 -type d -path "*/src/*" ! -path "*.*" -exec echo '--reload-dir {} \' \;)

exec sh -c "
cd services/autoscaling/src/simcore_service_director && \
python -m debugpy --listen 0.0.0.0:${DIRECTOR_REMOTE_DEBUGGING_PORT} -m uvicorn main:the_app \
--host 0.0.0.0 \
--reload \
$reload_dir_packages
--reload-dir . \
--log-level \"${SERVER_LOG_LEVEL}\"
"
else
exec simcore-service-director --loglevel="${LOGLEVEL}"
exec uvicorn simcore_service_director.main:the_app \
--host 0.0.0.0 \
--log-level "${SERVER_LOG_LEVEL}"
fi
124 changes: 59 additions & 65 deletions services/director/docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#!/bin/sh
#
# - Executes *inside* of the container upon start as --user [default root]
# - Notice that the container *starts* as --user [default root] but
# *runs* as non-root user [scu]
#
set -o errexit
set -o nounset

Expand All @@ -10,86 +15,75 @@ ERROR="ERROR: [$(basename "$0")] "

# Read self-signed SSH certificates (if applicable)
#
# In case the director must access a docker registry in a secure way using
# In case clusters-keeper must access a docker registry in a secure way using
# non-standard certificates (e.g. such as self-signed certificates), this call is needed.
# It needs to be executed as root.
# It needs to be executed as root. Also required to any access for example to secure rabbitmq.
update-ca-certificates

# This entrypoint script:
#
# - Executes *inside* of the container upon start as --user [default root]
# - Notice that the container *starts* as --user [default root] but
# *runs* as non-root user [scu]
#
echo "$INFO" "Entrypoint for stage ${SC_BUILD_TARGET} ..."
echo "$INFO" "User :$(id "$(whoami)")"
echo "$INFO" "Workdir :$(pwd)"
echo scuUser :"$(id scu)"

if [ "${SC_BUILD_TARGET}" = "development" ]
then
# NOTE: expects docker run ... -v $(pwd):/devel/services/director
DEVEL_MOUNT=/devel/services/director
echo "$INFO" "User :$(id "$(whoami)")"
echo "$INFO" "Workdir : $(pwd)"
echo "$INFO" "User : $(id scu)"
echo "$INFO" "python : $(command -v python)"
echo "$INFO" "pip : $(command -v pip)"

stat $DEVEL_MOUNT > /dev/null 2>&1 || \
(echo "$ERROR" "You must mount '$DEVEL_MOUNT' to deduce user and group ids" && exit 1) # FIXME: exit does not stop script
#
# DEVELOPMENT MODE
# - expects docker run ... -v $(pwd):$SC_DEVEL_MOUNT
# - mounts source folders
# - deduces host's uid/gip and assigns to user within docker
#
if [ "${SC_BUILD_TARGET}" = "development" ]; then
echo "$INFO" "development mode detected..."
stat "${SC_DEVEL_MOUNT}" >/dev/null 2>&1 ||
(echo "$ERROR" "You must mount '$SC_DEVEL_MOUNT' to deduce user and group ids" && exit 1)

echo "setting correct user id/group id..."
HOST_USERID=$(stat --format=%u "${DEVEL_MOUNT}")
HOST_GROUPID=$(stat --format=%g "${DEVEL_MOUNT}")
CONT_GROUPNAME=$(getent group "${HOST_GROUPID}" | cut --delimiter=: --fields=1)
if [ "$HOST_USERID" -eq 0 ]
then
echo "Warning: Folder mounted owned by root user... adding $SC_USER_NAME to root..."
adduser "$SC_USER_NAME" root
echo "$INFO" "setting correct user id/group id..."
HOST_USERID=$(stat --format=%u "${SC_DEVEL_MOUNT}")
HOST_GROUPID=$(stat --format=%g "${SC_DEVEL_MOUNT}")
CONT_GROUPNAME=$(getent group "${HOST_GROUPID}" | cut --delimiter=: --fields=1)
if [ "$HOST_USERID" -eq 0 ]; then
echo "$WARNING" "Folder mounted owned by root user... adding $SC_USER_NAME to root..."
adduser "$SC_USER_NAME" root
else
echo "$INFO" "Folder mounted owned by user $HOST_USERID:$HOST_GROUPID-'$CONT_GROUPNAME'..."
# take host's credentials in $SC_USER_NAME
if [ -z "$CONT_GROUPNAME" ]; then
echo "$WARNING" "Creating new group grp$SC_USER_NAME"
CONT_GROUPNAME=grp$SC_USER_NAME
addgroup --gid "$HOST_GROUPID" "$CONT_GROUPNAME"
else
echo "Folder mounted owned by user $HOST_USERID:$HOST_GROUPID-'$CONT_GROUPNAME'..."
# take host's credentials in $SC_USER_NAME
if [ -z "$CONT_GROUPNAME" ]
then
echo "Creating new group my$SC_USER_NAME"
CONT_GROUPNAME=my$SC_USER_NAME
addgroup --gid "$HOST_GROUPID" "$CONT_GROUPNAME"
else
echo "group already exists"
fi
echo "adding $SC_USER_NAME to group $CONT_GROUPNAME..."
adduser "$SC_USER_NAME" "$CONT_GROUPNAME"

echo "changing $SC_USER_NAME:$SC_USER_NAME ($SC_USER_ID:$SC_USER_ID) to $SC_USER_NAME:$CONT_GROUPNAME ($HOST_USERID:$HOST_GROUPID)"
usermod --uid "$HOST_USERID" --gid "$HOST_GROUPID" "$SC_USER_NAME"

echo "Changing group properties of files around from $SC_USER_ID to group $CONT_GROUPNAME"
find / -path /proc -prune -o -group "$SC_USER_ID" -exec chgrp --no-dereference "$CONT_GROUPNAME" {} \;
# change user property of files already around
echo "Changing ownership properties of files around from $SC_USER_ID to group $CONT_GROUPNAME"
find / -path /proc -prune -o -user "$SC_USER_ID" -exec chown --no-dereference "$SC_USER_NAME" {} \;
echo "$INFO" "group already exists"
fi
fi
echo "$INFO" "Adding $SC_USER_NAME to group $CONT_GROUPNAME..."
adduser "$SC_USER_NAME" "$CONT_GROUPNAME"

echo "$WARNING" "Changing ownership [this could take some time]"
echo "$INFO" "Changing $SC_USER_NAME:$SC_USER_NAME ($SC_USER_ID:$SC_USER_ID) to $SC_USER_NAME:$CONT_GROUPNAME ($HOST_USERID:$HOST_GROUPID)"
usermod --uid "$HOST_USERID" --gid "$HOST_GROUPID" "$SC_USER_NAME"

if [ "${SC_BOOT_MODE}" = "debug-ptvsd" ]
then
# NOTE: production does NOT pre-installs ptvsd
python3 -m pip install ptvsd
echo "$INFO" "Changing group properties of files around from $SC_USER_ID to group $CONT_GROUPNAME"
find / -path /proc -prune -o -group "$SC_USER_ID" -exec chgrp --no-dereference "$CONT_GROUPNAME" {} \;
# change user property of files already around
echo "$INFO" "Changing ownership properties of files around from $SC_USER_ID to group $CONT_GROUPNAME"
find / -path /proc -prune -o -user "$SC_USER_ID" -exec chown --no-dereference "$SC_USER_NAME" {} \;
fi
fi

# Appends docker group if socket is mounted
DOCKER_MOUNT=/var/run/docker.sock
if stat $DOCKER_MOUNT > /dev/null 2>&1
then
echo "$INFO detected docker socket is mounted, adding user to group..."
GROUPID=$(stat --format=%g $DOCKER_MOUNT)
GROUPNAME=scdocker
if stat $DOCKER_MOUNT >/dev/null 2>&1; then
echo "$INFO detected docker socket is mounted, adding user to group..."
GROUPID=$(stat --format=%g $DOCKER_MOUNT)
GROUPNAME=scdocker

if ! addgroup --gid "$GROUPID" $GROUPNAME > /dev/null 2>&1
then
echo "$WARNING docker group with $GROUPID already exists, getting group name..."
# if group already exists in container, then reuse name
GROUPNAME=$(getent group "${GROUPID}" | cut --delimiter=: --fields=1)
echo "$WARNING docker group with $GROUPID has name $GROUPNAME"
fi
adduser "$SC_USER_NAME" "$GROUPNAME"
if ! addgroup --gid "$GROUPID" $GROUPNAME >/dev/null 2>&1; then
echo "$WARNING docker group with $GROUPID already exists, getting group name..."
# if group already exists in container, then reuse name
GROUPNAME=$(getent group "${GROUPID}" | cut --delimiter=: --fields=1)
echo "$WARNING docker group with $GROUPID has name $GROUPNAME"
fi
adduser "$SC_USER_NAME" "$GROUPNAME"
fi

echo "$INFO Starting $* ..."
Expand Down

0 comments on commit 3b10e76

Please sign in to comment.