diff --git a/src/usr/local/containerbase/bin/install-containerbase.sh b/src/usr/local/containerbase/bin/install-containerbase.sh index f94f6bcdfd..16951db4ba 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,17 +87,46 @@ 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 ln -sf /usr/local/sbin/install-containerbase /usr/local/bin/install-containerbase } -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 150a34135f..c5f6495d82 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 30f65de22d..6aa593406f 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 c80ff4b5af..4cd4ca8a50 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 c9749fbb90..881ac764c4 100644 --- a/test/node/Dockerfile +++ b/test/node/Dockerfile @@ -20,7 +20,11 @@ RUN set -ex; ls -la /usr/local/bin/; ls -la /usr/local/sbin; RUN install-containerbase -RUN set -ex; ls -la /usr/local/bin/; ls -la /usr/local/sbin; ls -la /opt/containerbase/bin; +RUN set -ex; \ + ls -la /home/; \ + ls -la /opt/containerbase/; \ + ls -la /opt/containerbase/home; \ + true COPY --chown=1000:0 test/node/test test RUN chmod -R 775 /test @@ -74,7 +78,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 @@ -120,7 +124,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 @@ -137,13 +141,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; @@ -281,7 +285,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