Skip to content

Commit

Permalink
feat: auto home handling (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice authored May 18, 2021
1 parent 52156b6 commit db6bb46
Show file tree
Hide file tree
Showing 20 changed files with 79 additions and 52 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ARG USER_NAME=user
#--------------------------------------
# Image: base
#--------------------------------------
FROM ubuntu:focal@sha256:cf31af331f38d1d7158470e095b132acd126a7180a54f263d386da88eb681d93
FROM ubuntu:20.04@sha256:cf31af331f38d1d7158470e095b132acd126a7180a54f263d386da88eb681d93

ARG USER_ID
ARG USER_NAME
Expand All @@ -30,4 +30,3 @@ RUN install-buildpack

# renovate: datasource=github-tags lookupName=git/git
RUN install-tool git v2.31.1

11 changes: 8 additions & 3 deletions src/usr/local/bin/install-buildpack
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ if [[ -z "${BASH_ENV+x}" ]]; then
fi



# env helper, loads tool specific env
cat >> $BASH_ENV <<- EOM
export BUILDPACK=1 USER_NAME="${USER_NAME}" USER_ID="${USER_ID}"
export BUILDPACK=1 USER_NAME="${USER_NAME}" USER_ID="${USER_ID}" USER_HOME="/home/${USER_NAME}"
# openshift override unknown user home
if [ "\${EUID}" != 0 ]; then
export HOME="\${USER_HOME}"
fi
if [ -d /usr/local/env.d ]; then
for i in /usr/local/env.d/*.sh; do
if [ -r \$i ]; then
Expand Down Expand Up @@ -64,10 +69,10 @@ su ${USER_NAME} -c 'mkdir -p /home/${USER_NAME}/{env.d,bin}'
chmod g+w /home/${USER_NAME}/{,env.d,bin}



export_env DEBIAN_FRONTEND "noninteractive"
export_env LC_ALL "C.UTF-8"
export_env LANG "C.UTF-8"

# install basic required packages
apt_install \
ca-certificates \
Expand Down
7 changes: 7 additions & 0 deletions src/usr/local/buildpack/tools/docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
set -e

require_root


if [[ -d "/usr/local/bin/${TOOL_NAME}" ]]; then
echo "Skipping, already installed"
exit 0
fi

groupadd -g 999 docker
usermod -aG docker ${USER_NAME}

Expand Down
2 changes: 1 addition & 1 deletion src/usr/local/buildpack/tools/dotnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if [[ ! "${MAJOR}" || ! "${MINOR}" || ! "${PATCH}" ]]; then
exit 1
fi

DOTNET_INSTALL_DIR=/usr/local/dotnet/${TOOL_VERSION}
DOTNET_INSTALL_DIR=/usr/local/${TOOL_NAME}/${TOOL_VERSION}

if [[ -d "${DOTNET_INSTALL_DIR}" ]]; then
echo "Skipping, already installed"
Expand Down
11 changes: 8 additions & 3 deletions src/usr/local/buildpack/tools/elixir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ set -e
require_root
check_command erl

if [[ -d "/usr/local/${TOOL_NAME}/${TOOL_VERSION}" ]]; then
echo "Skipping, already installed"
exit 0
fi

curl -sSL https://github.com/elixir-lang/elixir/releases/download/v${TOOL_VERSION}/Precompiled.zip -o elixir.zip
mkdir -p /usr/local/elixir/
unzip -q elixir.zip -d /usr/local/elixir/
mkdir -p /usr/local/${TOOL_NAME}/${TOOL_VERSION}
unzip -q elixir.zip -d /usr/local/${TOOL_NAME}/${TOOL_VERSION}
rm elixir.zip

export_path "/usr/local/elixir/bin"
export_path "/usr/local/${TOOL_NAME}/${TOOL_VERSION}/bin"

elixir --version
mix --version
Expand Down
8 changes: 4 additions & 4 deletions src/usr/local/buildpack/tools/gradle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ rm gradle.zip

export_path "/usr/local/gradle-${TOOL_VERSION}/bin"

mkdir -p /home/${USER_NAME}/.m2 /home/${USER_NAME}/.gradle
mkdir -p ${USER_HOME}/{.m2,.gradle}

cat > /home/${USER_NAME}/.m2/settings.xml <<- EOM
cat > ${USER_HOME}/.m2/settings.xml <<- EOM
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
Expand All @@ -22,14 +22,14 @@ cat > /home/${USER_NAME}/.m2/settings.xml <<- EOM
</settings>
EOM

cat > /home/${USER_NAME}/.gradle/gradle.properties <<- EOM
cat > ${USER_HOME}/.gradle/gradle.properties <<- EOM
org.gradle.parallel=true
org.gradle.configureondemand=true
org.gradle.daemon=false
org.gradle.caching=false
EOM

chown -R ${USER_ID} /home/${USER_NAME}/.m2 /home/${USER_NAME}/.gradle
chown -R ${USER_ID} ${USER_HOME}/.m2 ${USER_HOME}/.gradle

gradle --version

Expand Down
8 changes: 4 additions & 4 deletions src/usr/local/buildpack/tools/helm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ if [[ ! "${MAJOR}" || ! "${MINOR}" || ! "${PATCH}" ]]; then
exit 1
fi

if [[ -d "/usr/local/helm/${TOOL_VERSION}" ]]; then
if [[ -d "/usr/local/${TOOL_NAME}/${TOOL_VERSION}" ]]; then
echo "Skipping, already installed"
exit 0
fi

mkdir -p /usr/local/helm/${TOOL_VERSION}
mkdir -p /usr/local/${TOOL_NAME}/${TOOL_VERSION}
curl -sSL https://get.helm.sh/helm-v${TOOL_VERSION}-linux-amd64.tar.gz --output helm.tgz
tar --strip 1 -C /usr/local/helm/${TOOL_VERSION} -xzf helm.tgz
tar --strip 1 -C /usr/local/${TOOL_NAME}/${TOOL_VERSION} -xzf helm.tgz
rm helm.tgz

export_path "/usr/local/helm/${TOOL_VERSION}"
export_path "/usr/local/${TOOL_NAME}/${TOOL_VERSION}"

helm version

Expand Down
2 changes: 1 addition & 1 deletion src/usr/local/buildpack/tools/nix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ln -s /nix/var/nix/profiles/default/etc/profile.d/nix.sh /etc/profile.d/

rm -r nix-${TOOL_VERSION}-x86_64-linux*

export_path "/home/${USER_NAME}/.nix-profile/bin"
export_path "${USER_HOME}/.nix-profile/bin"
export_env NIX_PATH /nix/var/nix/profiles/per-user/${USER_NAME}/channels

nix-collect-garbage --delete-old
Expand Down
2 changes: 1 addition & 1 deletion src/usr/local/buildpack/tools/node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fi
echo node: $(node --version) $(command -v node)
echo npm: $(npm --version) $(command -v npm)

NPM_CONFIG_PREFIX="/home/${USER_NAME}/.npm-global"
NPM_CONFIG_PREFIX="${USER_HOME}/.npm-global"

# npm 7 bug
mkdir -p $NPM_CONFIG_PREFIX/{bin,lib}
Expand Down
4 changes: 1 addition & 3 deletions src/usr/local/buildpack/tools/poetry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,4 @@ update_env ${tool_path}

poetry --version

if [[ $EUID -eq 0 ]]; then
shell_wrapper poetry
fi
shell_wrapper poetry
12 changes: 6 additions & 6 deletions src/usr/local/buildpack/tools/powershell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if [[ ! "${MAJOR}" || ! "${MINOR}" || ! "${PATCH}" ]]; then
exit 1
fi

if [[ -d "/usr/local/powershell/${TOOL_VERSION}" ]]; then
if [[ -d "/usr/local/${TOOL_NAME}/${TOOL_VERSION}" ]]; then
echo "Skipping, already installed"
exit 0
fi
Expand All @@ -22,12 +22,12 @@ case "$VERSION_CODENAME" in
"focal") apt_install libc6 libgcc1 libgssapi-krb5-2 libicu66 libssl1.1 libstdc++6 zlib1g;;
esac

mkdir -p /usr/local/powershell/${TOOL_VERSION}
curl -sSL https://github.com/PowerShell/PowerShell/releases/download/v${TOOL_VERSION}/powershell-${TOOL_VERSION}-linux-x64.tar.gz --output powershell.tgz
tar --strip 1 -C /usr/local/powershell/${TOOL_VERSION} -xzf powershell.tgz
rm powershell.tgz
mkdir -p /usr/local/${TOOL_NAME}/${TOOL_VERSION}
curl -sSL https://github.com/PowerShell/PowerShell/releases/download/v${TOOL_VERSION}/powershell-${TOOL_VERSION}-linux-x64.tar.gz --output ${TOOL_NAME}.tgz
tar --strip 1 -C /usr/local/${TOOL_NAME}/${TOOL_VERSION} -xzf ${TOOL_NAME}.tgz
rm ${TOOL_NAME}.tgz

export_path "/usr/local/powershell/${TOOL_VERSION}"
export_path "/usr/local/${TOOL_NAME}/${TOOL_VERSION}"

pwsh -Version

Expand Down
13 changes: 10 additions & 3 deletions src/usr/local/buildpack/tools/python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,24 @@ if [[ -z "${tool_path}" ]]; then

fix_python_shebangs

# TODO: can be wrong pip if another python already exists
pip install --upgrade pip

# clean cache https://pip.pypa.io/en/stable/reference/pip_cache/#pip-cache
pip cache purge

shell_wrapper python
shell_wrapper pip
# TODO: support multiple installed python versions
# shell_wrapper python${MAJOR}
# shell_wrapper python${MAJOR}.${MINOR}
# shell_wrapper pip${MAJOR}
# shell_wrapper pip${MAJOR}.${MINOR}
else
echo "Already installed, resetting env"
update_env ${tool_path}
fi

python --version
pip --version

if [[ $EUID -eq 0 ]]; then
shell_wrapper python
fi
14 changes: 7 additions & 7 deletions src/usr/local/buildpack/tools/ruby.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ if [[ ! "${MAJOR}" || ! "${MINOR}" || ! "${PATCH}" ]]; then
exit 1
fi

if [[ -d "/usr/local/ruby/${TOOL_VERSION}" ]]; then
if [[ -d "/usr/local/${TOOL_NAME}/${TOOL_VERSION}" ]]; then
echo "Skipping, already installed"
exit 0
fi

mkdir -p /usr/local/ruby
mkdir -p /usr/local/${TOOL_NAME}

ARCH=$(uname -p)
CODENAME=$(. /etc/os-release && echo ${VERSION_CODENAME})
Expand Down Expand Up @@ -48,15 +48,15 @@ else
rm -rf ruby-build
fi

ruby-build $TOOL_VERSION /usr/local/ruby/${TOOL_VERSION}
ruby-build $TOOL_VERSION /usr/local/${TOOL_NAME}/${TOOL_VERSION}
fi

export_env GEM_HOME "/home/${USER_NAME}/.gem-global"
export_path "\$GEM_HOME/bin:\$HOME/.gem/ruby/${MAJOR}.${MINOR}.0/bin:/usr/local/ruby/${TOOL_VERSION}/bin"
export_env GEM_HOME "${USER_HOME}/.gem-global"
export_path "\$GEM_HOME/bin:\$HOME/.gem/ruby/${MAJOR}.${MINOR}.0/bin:/usr/local/${TOOL_NAME}/${TOOL_VERSION}/bin"

# System settings
mkdir -p /usr/local/ruby/${TOOL_VERSION}/etc
cat > /usr/local/ruby/${TOOL_VERSION}/etc/gemrc <<- EOM
mkdir -p /usr/local/${TOOL_NAME}/${TOOL_VERSION}/etc
cat > /usr/local/${TOOL_NAME}/${TOOL_VERSION}/etc/gemrc <<- EOM
gem: --no-document
:benchmark: false
:verbose: true
Expand Down
10 changes: 5 additions & 5 deletions src/usr/local/buildpack/tools/rust.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ if [[ ! "${MAJOR}" || ! "${MINOR}" || ! "${PATCH}" ]]; then
exit 1
fi

if [[ -d "/usr/local/rust/${TOOL_VERSION}" ]]; then
if [[ -d "/usr/local/${TOOL_NAME}/${TOOL_VERSION}" ]]; then
echo "Skipping, already installed"
exit 0
fi

mkdir -p /usr/local/rust
mkdir -p /usr/local/${TOOL_NAME}

curl -sSfLo rust.tar.gz https://static.rust-lang.org/dist/rust-${TOOL_VERSION}-x86_64-unknown-linux-gnu.tar.gz
mkdir rust
pushd rust
tar --strip 1 -xf ../rust.tar.gz
./install.sh --prefix=/usr/local/rust/${TOOL_VERSION} --components=cargo,rust-std-x86_64-unknown-linux-gnu,rustc
./install.sh --prefix=/usr/local/${TOOL_NAME}/${TOOL_VERSION} --components=cargo,rust-std-x86_64-unknown-linux-gnu,rustc
popd
rm rust.tar.gz
rm -rf rust

export_env RUST_BACKTRACE 1
export_env CARGO_HOME "/home/${USER_NAME}/.cargo"
export_path "\$CARGO_HOME/bin:/usr/local/rust/${TOOL_VERSION}/bin"
export_env CARGO_HOME "${USER_HOME}/.cargo"
export_path "\$CARGO_HOME/bin:/usr/local/${TOOL_NAME}/${TOOL_VERSION}/bin"

cargo --version
rustc --version
Expand Down
2 changes: 1 addition & 1 deletion src/usr/local/buildpack/tools/swift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if [[ ! "${MAJOR}" || ! "${MINOR}" ]]; then
exit 1
fi

SWIFT_INSTALL_DIR=/usr/local/swift/${TOOL_VERSION}
SWIFT_INSTALL_DIR=/usr/local/${TOOL_NAME}/${TOOL_VERSION}

if [[ -d "${SWIFT_INSTALL_DIR}" ]]; then
echo "Skipping, already installed"
Expand Down
5 changes: 5 additions & 0 deletions src/usr/local/buildpack/tools/terraform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ if [[ ! "${MAJOR}" || ! "${MINOR}" || ! "${PATCH}" ]]; then
exit 1
fi

if [[ -d "/usr/local/bin/${TOOL_NAME}" ]]; then
echo "Skipping, already installed"
exit 0
fi

DISTRO=linux_amd64
URL=https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_${DISTRO}.zip

Expand Down
4 changes: 1 addition & 3 deletions src/usr/local/buildpack/tools/yarn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ npm install -g yarn@${TOOL_VERSION}

yarn --version

if [[ $EUID -eq 0 ]]; then
shell_wrapper yarn
fi
shell_wrapper yarn
6 changes: 3 additions & 3 deletions src/usr/local/buildpack/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,14 @@ function get_install_dir () {
if [[ $EUID -eq 0 ]]; then
echo /usr/local
else
echo /home/${USER_NAME}
echo ${USER_HOME}
fi
}

function find_tool_path () {
if [[ -d "/usr/local/${TOOL_NAME}/${TOOL_VERSION}" ]]; then
echo /usr/local/${TOOL_NAME}/${TOOL_VERSION}
elif [[ -d "/home/${USER_NAME}/${TOOL_NAME}/${TOOL_VERSION}" ]]; then
echo /home/${USER_NAME}/${TOOL_NAME}/${TOOL_VERSION}
elif [[ -d "${USER_HOME}/${TOOL_NAME}/${TOOL_VERSION}" ]]; then
echo ${USER_HOME}/${TOOL_NAME}/${TOOL_VERSION}
fi
}
5 changes: 3 additions & 2 deletions test/erlang/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ RUN install-tool elixir 1.11.4

WORKDIR /tmp

COPY --chown=1000:0 test test
# test openshift compatibility 1000<>1001
COPY --chown=1001:0 test test

USER 1000
USER 1001

RUN set -ex; \
cd test/a; \
Expand Down
2 changes: 2 additions & 0 deletions test/python/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ RUN set -ex; \
#--------------------------------------
FROM build-rootless as testg

RUN set -ex; env; ls -la $HOME/bin; cat $BASH_ENV

# renovate: datasource=pypi
RUN install-pip pipenv 2020.11.15

Expand Down

0 comments on commit db6bb46

Please sign in to comment.