Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: redirect home #2722

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/custom-base-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ CMD [ "bash" ]
COPY my-root-ca.crt /usr/local/share/ca-certificates/my-root-ca.crt

# Set up containerbase
COPY --from=containerbase /usr/local/bin/ /usr/local/bin/
COPY --from=containerbase /usr/local/sbin/ /usr/local/sbin/
COPY --from=containerbase /usr/local/containerbase/ /usr/local/containerbase/
RUN install-containerbase

Expand Down Expand Up @@ -78,7 +78,7 @@ CMD [ "bash" ]
COPY my-root-ca.crt /usr/local/share/ca-certificates/my-root-ca.crt

# Set up containerbase
COPY --from=containerbase /usr/local/bin/ /usr/local/bin/
COPY --from=containerbase /usr/local/sbin/ /usr/local/sbin/
COPY --from=containerbase /usr/local/containerbase/ /usr/local/containerbase/
RUN install-containerbase

Expand Down Expand Up @@ -122,7 +122,7 @@ ENTRYPOINT [ "docker-entrypoint.sh" ]
CMD [ "bash" ]

# Set up containerbase
COPY --from=containerbase /usr/local/bin/ /usr/local/bin/
COPY --from=containerbase /usr/local/sbin/ /usr/local/sbin/
COPY --from=containerbase /usr/local/containerbase/ /usr/local/containerbase/
RUN install-containerbase

Expand Down
2 changes: 1 addition & 1 deletion src/cli/services/path.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface FileOwnerConfig {
@injectable()
export class PathService {
get binDir(): string {
return join(this.envSvc.rootDir, 'usr/local/bin');
return join(this.installDir, 'bin');
}

get cachePath(): string {
Expand Down
5 changes: 5 additions & 0 deletions src/cli/tools/dart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
`${this.envSvc.userHome}/.dart`,
]);
await execa('chmod', ['-R', 'g+w', `${this.envSvc.userHome}/.dart`]);

await this.pathSvc.exportEnv(
{ PUB_CACHE: `${this.pathSvc.cachePath}/.pub-cache` },
true,
);

Check warning on line 49 in src/cli/tools/dart/index.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/dart/index.ts#L45-L49

Added lines #L45 - L49 were not covered by tests
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/cli/tools/flutter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
join(this.envSvc.rootDir, '/root/.flutter'),
'{ "firstRun": false, "enabled": false }',
);

await this.pathSvc.exportEnv(
{ PUB_CACHE: `${this.pathSvc.cachePath}/.pub-cache` },
true,
);

Check warning on line 34 in src/cli/tools/flutter.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/flutter.ts#L30-L34

Added lines #L30 - L34 were not covered by tests
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/cli/tools/java/gradle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@

override async link(version: string): Promise<void> {
const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin');
await this.shellwrapper({
srcDir: src,
exports: 'GRADLE_USER_HOME=$HOME/.gradle',
});
await this.shellwrapper({ srcDir: src });

Check warning on line 55 in src/cli/tools/java/gradle.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/java/gradle.ts#L55

Added line #L55 was not covered by tests
}

override async test(_version: string): Promise<void> {
Expand Down
23 changes: 21 additions & 2 deletions src/cli/tools/java/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,27 @@
return;
}

await createMavenSettings(this.envSvc.userHome, this.envSvc.userId);
await createGradleSettings(this.envSvc.userHome, this.envSvc.userId);
// https://github.com/gradle/gradle/issues/8262
await this.pathSvc.exportEnv(
{
GRADLE_USER_HOME: path.join(this.pathSvc.homePath, '.gradle'),
MAVEN_USER_HOME: path.join(this.pathSvc.homePath, '.m2'),
},
true,
);

await createMavenSettings(this.pathSvc.homePath, this.envSvc.userId);
await createGradleSettings(this.pathSvc.homePath, this.envSvc.userId);

// compatibility with gradle and maven
await fs.symlink(
path.join(this.pathSvc.homePath, '.m2'),
path.join(this.envSvc.userHome, '.m2'),
);
await fs.symlink(
path.join(this.pathSvc.homePath, '.gradle'),
path.join(this.envSvc.userHome, '.gradle'),
);

Check warning on line 64 in src/cli/tools/java/index.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/java/index.ts#L44-L64

Added lines #L44 - L64 were not covered by tests

const version = await resolveLatestJavaLtsVersion(
this.httpSvc,
Expand Down
86 changes: 54 additions & 32 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 @@ -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"
Expand Down Expand Up @@ -91,28 +73,68 @@ if [[ "$(find /usr/local/share/ca-certificates/ -name "*.crt" -type f -printf '.
fi

function link_tools () {
ln -sf /usr/local/containerbase/bin/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
ln -sf /usr/local/containerbase/bin/install-apt.sh /usr/local/bin/install-apt
ln -sf /usr/local/containerbase/bin/containerbase-cli /usr/local/bin/containerbase-cli
ln -sf /usr/local/containerbase/bin/containerbase-cli /usr/local/bin/install-gem
ln -sf /usr/local/containerbase/bin/containerbase-cli /usr/local/bin/install-npm
ln -sf /usr/local/containerbase/bin/containerbase-cli /usr/local/bin/install-tool
ln -sf /usr/local/containerbase/bin/containerbase-cli /usr/local/bin/prepare-tool
ln -sf /usr/local/containerbase/bin/docker-entrypoint.sh /usr/local/sbin/docker-entrypoint.sh
ln -sf /usr/local/containerbase/bin/install-apt.sh /usr/local/sbin/install-apt
ln -sf /usr/local/containerbase/bin/containerbase-cli /usr/local/sbin/containerbase-cli
ln -sf /usr/local/containerbase/bin/containerbase-cli /usr/local/sbin/install-gem
ln -sf /usr/local/containerbase/bin/containerbase-cli /usr/local/sbin/install-npm
ln -sf /usr/local/containerbase/bin/containerbase-cli /usr/local/sbin/install-tool
ln -sf /usr/local/containerbase/bin/containerbase-cli /usr/local/sbin/prepare-tool

containerbase-cli --version
}
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

# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
# set home path to /opt/containerbase/home
export_env XDG_CONFIG_HOME "$(get_home_path)/.config" true
export_env XDG_DATA_HOME "$(get_home_path)/.local/share" true
export_env XDG_STATE_HOME "$(get_home_path)/.local/state" true
# set cache path to /opt/containerbase/cache
export_env XDG_CACHE_HOME "$(get_cache_path)" true
}
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_v2_tools
prepare_user

# cleanup
rm -rf /var/lib/apt/lists/* /var/log/dpkg.* /var/log/apt
13 changes: 7 additions & 6 deletions src/usr/local/containerbase/tools/v2/golang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@


function prepare_tool() {
local go_path

# go suggests: git svn bzr mercurial
apt_install bzr mercurial

export_env GOPATH "/go"
export_env CGO_ENABLED 0
export_env GOSUMDB off
go_path=$(get_home_path)/go

export_env GOPATH "${go_path}" true
export_path "\$GOPATH/bin"

mkdir -p "$GOPATH/src" "$GOPATH/bin" "$GOPATH/pkg"
mkdir -p "${go_path}/src" "${go_path}/bin" "${go_path}/pkg"

chown -R "${USER_ID}" "$GOPATH"
chmod -R g+w "$GOPATH"
chown -R "${USER_ID}" "${go_path}"
chmod -R g+w "${go_path}"
create_tool_path > /dev/null
}

Expand Down
15 changes: 3 additions & 12 deletions src/usr/local/containerbase/tools/v2/ruby.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ function prepare_tool() {
chown -R "${USER_ID}" "${USER_HOME}"/.gemrc
chmod -R g+w "${USER_HOME}"/.gemrc

export_env CP_HOME_DIR "$(get_home_path)/.cocoapods" true
export_env CP_CACHE_DIR "$(get_cache_path)/cocoapods" true

# Workaround for compatibillity for Ruby hardcoded paths
if [ "${tool_path}" != "${ROOT_DIR_LEGACY}/ruby" ]; then
ln -sf "${tool_path}" /usr/local/ruby
Expand Down Expand Up @@ -78,21 +81,9 @@ function install_tool () {
}

function link_tool () {
local tool_path
local versioned_tool_path
local ruby_minor_version

tool_path=$(find_tool_path)
versioned_tool_path=$(find_versioned_tool_path)
ruby_minor_version="${MAJOR}.${MINOR}.0"

reset_tool_env
# export ruby varsreset_tool_env
{
printf -- "if [ \"\${EUID}\" != 0 ] && [ -z \"\$GEM_HOME\" ]; then\n"
printf -- " export GEM_HOME=\"%s/.gem/ruby/%s\"\n" "${USER_HOME}" "${ruby_minor_version}"
printf -- "fi\n"
} >> "$(find_tool_env)"

shell_wrapper ruby "${versioned_tool_path}/bin"
shell_wrapper gem "${versioned_tool_path}/bin"
Expand Down
3 changes: 2 additions & 1 deletion src/usr/local/containerbase/utils/constants.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ export ENV_FILE=/usr/local/etc/env
export BASH_RC=/etc/bash.bashrc
# defines the root directory where tools will be installed
export ROOT_DIR=/usr/local
# defines the directory where symlinks to tools will be installed
# defines the directory where shims to tools will be installed
export BIN_DIR=/usr/local/bin
export LIB_DIR=/usr/local/lib
# defines the directory where user tools will be installed
# shellcheck disable=SC2153
export USER_HOME="/home/${USER_NAME}"
Expand Down
18 changes: 11 additions & 7 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 All @@ -58,7 +60,10 @@ function setup_directories () {
mkdir -p -m 775 "$(get_version_path)"
# contains the wrapper and symlinks for the tools
# shellcheck disable=SC2174
mkdir -p -m 775 "$(get_bin_path)"
mkdir -p -m 775 "${install_dir}/bin"
# contains nodejs files and maybe others
# shellcheck disable=SC2174
mkdir -p -m 775 "${install_dir}/lib"
# contains the certificates for the tools
# shellcheck disable=SC2174
mkdir -p -m 775 "$(get_ssl_path)"
Expand All @@ -69,11 +74,10 @@ function setup_directories () {
# shellcheck disable=SC2174
mkdir -p -m 775 "$(get_home_path)"

# if the bin path exists and does not have 775, force it
if [ "$(stat --format '%a' "$(get_bin_path)")" -ne 775 ]; then
echo "Forcing 775 on '$(get_bin_path)' ..."
chmod 775 "$(get_bin_path)"
fi
# symlink v2 tools bin and lib
rm -rf "${BIN_DIR}" "${LIB_DIR}"
ln -sf "${ROOT_DIR}/bin" "${BIN_DIR}"
ln -sf "${ROOT_DIR}/lib" "${LIB_DIR}"
}

# Creates the given folder path with root and user umask depending on the caller
Expand Down Expand Up @@ -101,7 +105,7 @@ function create_folder () {

# Gets the path to the bin folder
function get_bin_path () {
echo "${BIN_DIR}"
echo "${ROOT_DIR}/bin"
}

# Gets the path to the versions folder
Expand Down
4 changes: 2 additions & 2 deletions src/usr/local/containerbase/utils/linking.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

# use this if custom env is required, creates a shell wrapper to /usr/local/bin
# use this if custom env is required, creates a shell wrapper to /opt/containerbase/bin
function shell_wrapper () {
local TARGET
local SOURCE=$2
Expand Down Expand Up @@ -33,7 +33,7 @@ EOM
set_file_owner "${TARGET}" 775
}

# use this for simple symlink to /usr/local/bin
# use this for simple symlink to /opt/containerbase/bin
function link_wrapper () {
local TARGET
local SOURCE=$2
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
6 changes: 5 additions & 1 deletion test/bash/filesystem.bats
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ teardown() {
}

@test "setup directories with correct permissions" {
local TEST_ROOT_USER=0 # root
local install_dir=$(get_install_dir)

run setup_directories
Expand All @@ -122,8 +123,11 @@ teardown() {
assert [ "$(stat --format '%a' "${install_dir}/tools")" -eq 775 ]
assert [ -d "${install_dir}/versions" ]
assert [ "$(stat --format '%a' "${install_dir}/versions")" -eq 775 ]
assert [ -d "${install_dir}/bin" ]
assert [ "$(stat --format '%a' "${install_dir}/bin")" -eq 775 ]
assert [ -d "${BIN_DIR}" ]
assert [ "$(stat --format '%a' "${BIN_DIR}")" -eq 775 ]
assert [ -L "${BIN_DIR}" ]
assert [ "$(stat --format '%a' "${BIN_DIR}")" -eq 777 ]
assert [ -d "${install_dir}/env.d" ]
assert [ "$(stat --format '%a' "${install_dir}/env.d")" -eq 775 ]
assert [ -d "${install_dir}/cache" ]
Expand Down
Loading
Loading