From 066ae2d53a0a23641cad6ed043b0fb8cdd0b9f56 Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Tue, 13 Feb 2024 15:38:37 +0100 Subject: [PATCH] feat!: redirect home --- .../bin/install-containerbase.sh | 61 +++++++++++-------- .../local/containerbase/utils/filesystem.sh | 2 + src/usr/local/containerbase/utils/user.sh | 3 +- .../local/containerbase/utils/v2/overrides.sh | 4 ++ test/node/Dockerfile | 10 +-- 5 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/usr/local/containerbase/bin/install-containerbase.sh b/src/usr/local/containerbase/bin/install-containerbase.sh index 0c635181f..dd00a43ca 100755 --- a/src/usr/local/containerbase/bin/install-containerbase.sh +++ b/src/usr/local/containerbase/bin/install-containerbase.sh @@ -21,6 +21,8 @@ fi # shellcheck source=/dev/null . /usr/local/containerbase/util.sh +# shellcheck source=/dev/null +. /usr/local/containerbase/utils/v2/overrides.sh if [[ -n "${BASH_ENV}" && "${BASH_ENV}" != "${ENV_FILE}" ]]; then echo "Wrong BASH_ENV defined - skipping: ${BASH_ENV}" @@ -42,26 +44,6 @@ setup_env_files echo "APT::Install-Recommends \"false\";" | tee -a /etc/apt/apt.conf.d/containerbase.conf echo "APT::Get::Install-Suggests \"false\";" | tee -a /etc/apt/apt.conf.d/containerbase.conf -# happens on ubuntu noble -if grep 'ubuntu:x:1000:' /etc/passwd > /dev/null; then - echo "User already exists, deleting" >&2 - userdel -r ubuntu -fi - -# Set up user and home directory -createUser - -# create env helper paths -mkdir /usr/local/env.d -su "${USER_NAME}" -c "mkdir -p \"/home/${USER_NAME}/\"{env.d,bin}" - -if [[ "$PATH" =~ (^|:)"/home/${USER_NAME}/bin"(:|$) ]]; then - echo "export PATH=\"/home/${USER_NAME}/bin:\${PATH}\"" >> "$ENV_FILE" -fi - -# OpenShift -chmod -R g+w "/home/${USER_NAME}" - export_env DEBIAN_FRONTEND "noninteractive" export_env LC_ALL "C.UTF-8" export_env LANG "C.UTF-8" @@ -105,11 +87,10 @@ link_tools # do this at the end as we are overwriting certain env vars and functions -function prepare_v2_tools () { - # setup directories for v2 tools - # shellcheck source=/dev/null - . /usr/local/containerbase/utils/v2/overrides.sh +function prepare_system () { + echo "Setting up system" + # setup directories for v2 tools setup_directories # compability with current custom images @@ -123,7 +104,37 @@ function prepare_v2_tools () { # set cache path to /opt/containerbase/cache export_env XDG_CACHE_HOME "$(get_cache_path)" true } -prepare_v2_tools +prepare_system + +function prepare_user() { + echo "Setting up user" + local install_dir + install_dir=$(get_install_dir) + + # happens on ubuntu noble + if grep 'ubuntu:x:1000:' /etc/passwd > /dev/null; then + echo "User already exists, deleting" >&2 + userdel -r ubuntu + fi + + # Set up user and home directory + createUser "${install_dir}/home" + + # create symlink for compabillity + ln -sf "${install_dir}/home" "/home/${USER_NAME}" + + # create env helper paths + mkdir /usr/local/env.d + su "${USER_NAME}" -c "mkdir -p \"/home/${USER_NAME}/\"{env.d,bin}" + + if [[ "$PATH" =~ (^|:)"/home/${USER_NAME}/bin"(:|$) ]]; then + echo "export PATH=\"/home/${USER_NAME}/bin:\${PATH}\"" >> "$ENV_FILE" + fi + + # OpenShift + chmod -R g+w "${install_dir}/home" +} +prepare_user # cleanup rm -rf /var/lib/apt/lists/* /var/log/dpkg.* /var/log/apt diff --git a/src/usr/local/containerbase/utils/filesystem.sh b/src/usr/local/containerbase/utils/filesystem.sh index a6f112e14..a2379f4e2 100644 --- a/src/usr/local/containerbase/utils/filesystem.sh +++ b/src/usr/local/containerbase/utils/filesystem.sh @@ -46,6 +46,8 @@ function setup_directories () { local install_dir install_dir=$(get_install_dir) + echo "Setting up directories in ${install_dir}" + mkdir -p "${install_dir}" # contains the installed tools # shellcheck disable=SC2174 diff --git a/src/usr/local/containerbase/utils/user.sh b/src/usr/local/containerbase/utils/user.sh index 30f65de22..6aa593406 100644 --- a/src/usr/local/containerbase/utils/user.sh +++ b/src/usr/local/containerbase/utils/user.sh @@ -1,8 +1,9 @@ #!/bin/bash function createUser() { + local home_dir=${1} # Set up user and home directory with access to users in the root group (0) # https://docs.openshift.com/container-platform/3.6/creating_images/guidelines.html#use-uid groupadd --gid "${USER_ID}" "${USER_NAME}"; - useradd --uid "${USER_ID}" --gid "${PRIMARY_GROUP_ID}" --groups "0,${USER_ID}" --shell /bin/bash --create-home "${USER_NAME}" + useradd --uid "${USER_ID}" --gid "${PRIMARY_GROUP_ID}" --groups "0,${USER_ID}" --shell /bin/bash --home-dir "${home_dir}" --create-home "${USER_NAME}" } diff --git a/src/usr/local/containerbase/utils/v2/overrides.sh b/src/usr/local/containerbase/utils/v2/overrides.sh index c80ff4b5a..4cd4ca8a5 100644 --- a/src/usr/local/containerbase/utils/v2/overrides.sh +++ b/src/usr/local/containerbase/utils/v2/overrides.sh @@ -10,6 +10,10 @@ export ROOT_DIR_LEGACY="${ROOT_DIR}" # shellcheck disable=SC2168,SC2034 export ROOT_DIR=/opt/containerbase +# we need to set the user home directory to the new root directory +# shellcheck disable=SC2153 +export USER_HOME="${ROOT_DIR}/home" + # get path location DIR="${BASH_SOURCE%/*}" if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi diff --git a/test/node/Dockerfile b/test/node/Dockerfile index 2e510e729..45f54385b 100644 --- a/test/node/Dockerfile +++ b/test/node/Dockerfile @@ -71,7 +71,7 @@ ARG APT_HTTP_PROXY RUN npm install -g yarn RUN set -ex; \ - [ "$(command -v yarn)" = "/home/${USER_NAME}/.npm-global/bin/yarn" ] && echo "works" || exit 1; \ + [ "$(command -v yarn)" = "/opt/containerbase/home/.npm-global/bin/yarn" ] && echo "works" || exit 1; \ yarn --version; RUN set -ex; cd a; yarn; [[ -d node_modules/.bin ]] || exit 1 @@ -117,7 +117,7 @@ RUN set -ex; cd a; pnpm i SHELL [ "/bin/sh", "-c" ] RUN set -ex; \ pnpm --version \ - [ "$(command -v pnpm)" = "/home/${USER_NAME}/bin/pnpm" ] && echo "works" || exit 1; + [ "$(command -v pnpm)" = "/opt/containerbase/home/bin/pnpm" ] && echo "works" || exit 1; #-------------------------------------- # test: node 18 @@ -134,13 +134,13 @@ RUN install-tool node v18.20.3 RUN npm install -g yarn RUN set -ex; \ - [ "$(command -v yarn)" = "/home/${USER_NAME}/.npm-global/bin/yarn" ] && echo "works" || exit 1; \ + [ "$(command -v yarn)" = "/opt/containerbase/home/.npm-global/bin/yarn" ] && echo "works" || exit 1; \ yarn --version; # renovate: datasource=npm RUN install-tool yarn 1.22.22 RUN set -ex; \ - [ "$(command -v yarn)" = "/home/${USER_NAME}/.npm-global/bin/yarn" ] && echo "works" || exit 1; \ + [ "$(command -v yarn)" = "/opt/containerbase/home/.npm-global/bin/yarn" ] && echo "works" || exit 1; \ yarn --version; @@ -278,7 +278,7 @@ ARG APT_HTTP_PROXY RUN npm install -g yarn RUN set -ex; \ - [ "$(command -v yarn)" = "/home/${USER_NAME}/.npm-global/bin/yarn" ] && echo "works" || exit 1; \ + [ "$(command -v yarn)" = "/opt/containerbase/home/.npm-global/bin/yarn" ] && echo "works" || exit 1; \ yarn --version; RUN set -ex; ls -lah /test/a; cd a; yarn