diff --git a/src/usr/local/containerbase/bin/install-containerbase.sh b/src/usr/local/containerbase/bin/install-containerbase.sh index bf2f81b99a..0537040d2e 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}" @@ -39,22 +41,11 @@ require_root 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 - -# Set up user and home directory -createUser +# setup directories for v2 tools -# 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}" +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 export_env DEBIAN_FRONTEND "noninteractive" export_env LC_ALL "C.UTF-8" @@ -99,17 +90,39 @@ 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 # 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) + + # 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 5cfe3543ed..d3d5c93bf4 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 @@ -115,7 +119,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 @@ -132,13 +136,13 @@ RUN install-tool node v18.19.0 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.21 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; @@ -276,7 +280,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