Skip to content

Commit

Permalink
feat!: redirect folders to /opt/containerbase (#2090)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Containerbase binaries are moved, read custom image guide.
The paths `/usr/local/{bin,lib}` are now symlinks to `/opt/comntainerbase/{bin,lib}`.
  • Loading branch information
viceice committed Jul 3, 2024
1 parent 173a98b commit 66a7bdc
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 29 deletions.
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
17 changes: 10 additions & 7 deletions src/usr/local/containerbase/bin/install-containerbase.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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

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
27 changes: 19 additions & 8 deletions src/usr/local/containerbase/utils/filesystem.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ function create_versioned_tool_path () {
# Will set up the general folder structure for the whole containerbase installation
function setup_directories () {
local install_dir
local home_path
install_dir=$(get_install_dir)
home_path=$(get_home_path)

mkdir -p "${install_dir}"
# contains the installed tools
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 @@ -67,13 +72,19 @@ function setup_directories () {
mkdir -p -m 775 "$(get_cache_path)"
# contains the home for the tools
# shellcheck disable=SC2174
mkdir -p -m 775 "$(get_home_path)"
mkdir -p -m 775 "${home_path}"
# shellcheck disable=SC2174
mkdir -p -m 775 "${home_path}"/{.config,.local}

# 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}"

# symlink known user folders
ln -sf "${USER_HOME}/.config" "${home_path}/.config"
ln -sf "${USER_HOME}/.local" "${home_path}/.local"
ln -sf "${USER_HOME}/.cache" "$(get_cache_path)"
}

# Creates the given folder path with root and user umask depending on the caller
Expand Down Expand Up @@ -101,7 +112,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
File renamed without changes.
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
2 changes: 2 additions & 0 deletions test/bash/linking.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ setup() {
# load test overwrites
load "$TEST_DIR/util.sh"

mkdir "${ROOT_DIR}/bin"

setup_directories
}

Expand Down
8 changes: 5 additions & 3 deletions test/bash/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
6 changes: 3 additions & 3 deletions test/latest/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,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

Expand Down Expand Up @@ -181,11 +181,11 @@ RUN install-tool node v20.15.0
RUN install-tool docker v27.0.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

Expand Down

0 comments on commit 66a7bdc

Please sign in to comment.