From d94dbf121a975df016248b182f80356e8df39dfb Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Fri, 31 May 2024 13:38:06 +0200 Subject: [PATCH] feat!: redirect folders to `/opt/containerbase` (#2090) BREAKING CHANGE: Containerbase binaries are moved, read custom image guide. The paths `/usr/local/{bin,lib}` are now symlinks to `/opt/comntainerbase/{bin,lib}`. --- docs/custom-base-image.md | 6 +++--- src/cli/services/path.service.ts | 2 +- .../containerbase/bin/install-containerbase.sh | 17 ++++++++++------- src/usr/local/containerbase/utils/constants.sh | 3 ++- src/usr/local/containerbase/utils/filesystem.sh | 16 +++++++++------- src/usr/local/containerbase/utils/linking.sh | 4 ++-- .../local/{bin => sbin}/install-containerbase | 0 test/bash/filesystem.bats | 6 +++++- test/bash/linking.bats | 2 ++ test/bash/util.sh | 8 +++++--- test/latest/Dockerfile | 6 +++--- 11 files changed, 42 insertions(+), 28 deletions(-) rename src/usr/local/{bin => sbin}/install-containerbase (100%) diff --git a/docs/custom-base-image.md b/docs/custom-base-image.md index 00e9e888c..47891d600 100644 --- a/docs/custom-base-image.md +++ b/docs/custom-base-image.md @@ -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 @@ -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 @@ -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 diff --git a/src/cli/services/path.service.ts b/src/cli/services/path.service.ts index a31a62286..ed75ba6dc 100644 --- a/src/cli/services/path.service.ts +++ b/src/cli/services/path.service.ts @@ -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 envFile(): string { diff --git a/src/usr/local/containerbase/bin/install-containerbase.sh b/src/usr/local/containerbase/bin/install-containerbase.sh index 5f93c784b..17bdc9f91 100755 --- a/src/usr/local/containerbase/bin/install-containerbase.sh +++ b/src/usr/local/containerbase/bin/install-containerbase.sh @@ -91,13 +91,13 @@ 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 } @@ -111,6 +111,9 @@ function prepare_v2_tools () { . /usr/local/containerbase/utils/v2/overrides.sh setup_directories + + # compability with current custom images + ln -sf /usr/local/sbin/install-containerbase /usr/local/bin/install-containerbase } prepare_v2_tools diff --git a/src/usr/local/containerbase/utils/constants.sh b/src/usr/local/containerbase/utils/constants.sh index 0f7e7bbf6..26c032b94 100644 --- a/src/usr/local/containerbase/utils/constants.sh +++ b/src/usr/local/containerbase/utils/constants.sh @@ -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}" diff --git a/src/usr/local/containerbase/utils/filesystem.sh b/src/usr/local/containerbase/utils/filesystem.sh index 35dd98f92..a6f112e14 100644 --- a/src/usr/local/containerbase/utils/filesystem.sh +++ b/src/usr/local/containerbase/utils/filesystem.sh @@ -58,7 +58,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)" @@ -69,11 +72,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 @@ -101,7 +103,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 diff --git a/src/usr/local/containerbase/utils/linking.sh b/src/usr/local/containerbase/utils/linking.sh index 60d83f4e7..6dd0438b4 100644 --- a/src/usr/local/containerbase/utils/linking.sh +++ b/src/usr/local/containerbase/utils/linking.sh @@ -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 @@ -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 diff --git a/src/usr/local/bin/install-containerbase b/src/usr/local/sbin/install-containerbase similarity index 100% rename from src/usr/local/bin/install-containerbase rename to src/usr/local/sbin/install-containerbase diff --git a/test/bash/filesystem.bats b/test/bash/filesystem.bats index f9dd9324a..7d9761cc4 100644 --- a/test/bash/filesystem.bats +++ b/test/bash/filesystem.bats @@ -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 @@ -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" ] diff --git a/test/bash/linking.bats b/test/bash/linking.bats index d7082312f..b9c53a837 100644 --- a/test/bash/linking.bats +++ b/test/bash/linking.bats @@ -12,6 +12,8 @@ setup() { # load test overwrites load "$TEST_DIR/util.sh" + mkdir "${ROOT_DIR}/bin" + setup_directories } diff --git a/test/bash/util.sh b/test/bash/util.sh index e285e8cac..a7e1de60e 100644 --- a/test/bash/util.sh +++ b/test/bash/util.sh @@ -7,6 +7,7 @@ export REPO_DIR="${TEST_DIR}/../.." export CONTAINERBASE_DIR="${REPO_DIR}/src/usr/local/containerbase" export ROOT_DIR="${TEST_ROOT_DIR}/root" export BIN_DIR="${TEST_ROOT_DIR}/bin" +export LIB_DIR="${TEST_ROOT_DIR}/lib" export USER_HOME="${TEST_ROOT_DIR}/user" export ENV_FILE="${TEST_ROOT_DIR}/env" @@ -29,9 +30,10 @@ function link_cli_tool () { if [[ "${ARCHITECTURE}" = "aarch64" ]];then arch=arm64 fi - export PATH="${BIN_DIR}:${PATH}" - ln -sf "${REPO_DIR}/dist/cli/containerbase-cli-${arch}" "${BIN_DIR}/containerbase-cli" + export PATH="${TEST_ROOT_DIR}/sbin:${PATH}" + ln -sf "${REPO_DIR}/dist/cli/containerbase-cli-${arch}" "${TEST_ROOT_DIR}/sbin/containerbase-cli" } -mkdir -p "${BIN_DIR}" "${ROOT_DIR}" +# ensure directories exist +mkdir -p "${TEST_ROOT_DIR}"/{sbin,root,user} link_cli_tool diff --git a/test/latest/Dockerfile b/test/latest/Dockerfile index 7147cea2f..83ab14a56 100644 --- a/test/latest/Dockerfile +++ b/test/latest/Dockerfile @@ -123,7 +123,7 @@ SHELL ["/bin/bash" , "-c"] COPY --from=build /test/ca.pem /usr/local/share/ca-certificates/renovate-ca.crt # Set up containerbase -COPY --from=build /usr/local/bin/ /usr/local/bin/ +COPY --from=build /usr/local/sbin/ /usr/local/sbin/ COPY --from=build /usr/local/containerbase/ /usr/local/containerbase/ RUN install-containerbase @@ -179,11 +179,11 @@ RUN install-tool node v20.14.0 RUN install-tool docker v26.1.3 #-------------------------------------- -# test: bin path has 775 +# test: bin path has 777 #-------------------------------------- FROM base as testd -RUN [ $(stat --format '%a' "/usr/local/bin") -eq 775 ] +RUN [ $(stat --format '%a' "/usr/local/bin") -eq 777 ] RUN prepare-tool all