Skip to content

Commit

Permalink
feat!: redirect home
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Feb 13, 2024
1 parent fbb90aa commit cfb9bca
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 26 deletions.
51 changes: 32 additions & 19 deletions src/usr/local/containerbase/bin/install-containerbase.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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"
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/usr/local/containerbase/utils/filesystem.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/usr/local/containerbase/utils/user.sh
Original file line number Diff line number Diff line change
@@ -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}"
}
4 changes: 4 additions & 0 deletions src/usr/local/containerbase/utils/v2/overrides.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions test/node/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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;


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cfb9bca

Please sign in to comment.