Skip to content

Commit

Permalink
feat(composer): allow dynamic install (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice authored Apr 7, 2021
1 parent 2b063c3 commit 2d2090e
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LABEL maintainer="Rhys Arkins <[email protected]>" \
org.opencontainers.image.source="https://github.com/containerbase/buildpack"

# autoloading buildpack env
ENV BASH_ENV=/usr/local/etc/env
ENV BASH_ENV=/usr/local/etc/env PATH=/home/$USER_NAME/bin:$PATH
SHELL ["/bin/bash" , "-c"]

ENTRYPOINT ["docker-entrypoint.sh"]
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.bionic
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LABEL maintainer="Rhys Arkins <[email protected]>" \
org.opencontainers.image.source="https://github.com/containerbase/buildpack"

# autoloading buildpack env
ENV BASH_ENV=/usr/local/etc/env
ENV BASH_ENV=/usr/local/etc/env PATH=/home/$USER_NAME/bin:$PATH
SHELL ["/bin/bash" , "-c"]

COPY src/ /
Expand Down
2 changes: 1 addition & 1 deletion docs/custom-base-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ARG USER_ID=1000
ARG APP_ROOT=/usr/src/app

# Set env and shell
ENV BASH_ENV=/usr/local/etc/env
ENV BASH_ENV=/usr/local/etc/env PATH=/home/$USER_NAME/bin:$PATH
SHELL ["/bin/bash" , "-c"]

# This entry point ensures that dumb-init is run
Expand Down
2 changes: 1 addition & 1 deletion src/usr/local/bin/install-apt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -e

require_root

apt_install $@
apt_install "$@"

# cleanup
rm -rf /var/lib/apt/lists/*
16 changes: 9 additions & 7 deletions src/usr/local/bin/install-buildpack
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ if [[ -z "${BASH_ENV+x}" ]]; then
fi


export_env BUILDPACK 1

# env helper, loads tool specific env
cat >> $BASH_ENV <<- EOM
export BUILDPACK=1 USER_NAME="${USER_NAME}" USER_ID="${USER_ID}"
if [ -d /usr/local/env.d ]; then
for i in /usr/local/env.d/*.sh; do
if [ -r \$i ]; then
Expand All @@ -37,8 +37,8 @@ if [ -d /usr/local/env.d ]; then
done
unset i
fi
if [ -d \$HOME/.local/env.d ]; then
for i in \$HOME/.local/env.d/*.sh; do
if [ -d /home/${USER_NAME}/env.d ]; then
for i in /home/${USER_NAME}/env.d/*.sh; do
if [ -r \$i ]; then
. \$i
fi
Expand All @@ -58,14 +58,16 @@ useradd --uid ${USER_ID} --gid 0 --groups ${USER_NAME} --shell /bin/bash --creat

# create env helper paths
mkdir /usr/local/env.d
su ${USER_NAME} -c 'mkdir -p /home/${USER_NAME}/.local/env.d'
su ${USER_NAME} -c 'mkdir -p /home/${USER_NAME}/{env.d,bin}'

# OpenShift
chmod g+w /home/${USER_NAME}/{,env.d,bin}



export_env USER_NAME ${USER_NAME}
export_env USER_ID ${USER_ID}
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
2 changes: 1 addition & 1 deletion src/usr/local/bin/install-gem
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e

. /usr/local/buildpack/util.sh

require_tool $@
require_tool "$@"
check_command gem

if [[ $EUID -eq 0 ]]; then
Expand Down
2 changes: 1 addition & 1 deletion src/usr/local/bin/install-pip
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e

. /usr/local/buildpack/util.sh

require_tool $@
require_tool "$@"
check_command pip

echo "Installing pip tool ${TOOL_NAME} v${TOOL_VERSION}"
Expand Down
2 changes: 1 addition & 1 deletion src/usr/local/bin/install-tool
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -e

require_distro
require_user
require_tool $@
require_tool "$@"

TOOL="/usr/local/buildpack/tools/${TOOL_NAME}.sh"
if [[ ! -f "$TOOL" ]]; then
Expand Down
45 changes: 43 additions & 2 deletions src/usr/local/buildpack/tools/composer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,50 @@

set -e

require_root

check_command php

curl https://raw.githubusercontent.com/composer/getcomposer.org/76a7060ccb93902cd7576b67264ad91c8a2700e2/web/installer | php -- --version=$TOOL_VERSION --install-dir=/usr/local/bin --filename=composer
if [[ "${TOOL_VERSION}" -eq "latest" ]]; then
export "TOOL_VERSION=$(curl -s https://api.github.com/repos/composer/composer/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")')"
fi

check_semver ${TOOL_VERSION}


if [[ ! "${MAJOR}" || ! "${MINOR}" || ! "${PATCH}" ]]; then
echo Invalid version: ${TOOL_VERSION}
exit 1
fi


tool_path=$(find_tool_path)

function update_env () {
reset_tool_env
export_tool_path "${1}/bin"
}


if [[ -z "${tool_path}" ]]; then
INSTALL_DIR=$(get_install_dir)
base_path=${INSTALL_DIR}/${TOOL_NAME}
tool_path=${base_path}/${TOOL_VERSION}

mkdir -p ${tool_path}/bin

# OpenShift
chmod g+w ${base_path}

BASE_URL="https://github.com/composer/composer/releases/download"

curl -sSfLo ${tool_path}/bin/composer ${BASE_URL}/${TOOL_VERSION}/composer.phar
chmod +x ${tool_path}/bin/composer

update_env ${tool_path}
shell_wrapper composer
else
echo "Already installed, resetting env"
update_env ${tool_path}
fi

composer --version
7 changes: 2 additions & 5 deletions src/usr/local/buildpack/tools/php.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ tool_path=$(find_tool_path)

function update_env () {
reset_tool_env
export_tool_path "\$HOME/.local/bin:${1}/bin"
export_tool_path "${1}/bin"
}

if [[ -z "${tool_path}" ]]; then
Expand Down Expand Up @@ -65,13 +65,10 @@ if [[ -z "${tool_path}" ]]; then
fi

update_env ${tool_path}
shell_wrapper php
else
echo "Already installed, resetting env"
update_env ${tool_path}
fi

php --version

if [[ $EUID -eq 0 ]]; then
shell_wrapper php
fi
16 changes: 9 additions & 7 deletions src/usr/local/buildpack/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ function export_tool_path () {

# use this if custom env is required, creates a shell wrapper to /usr/local/bin
function shell_wrapper () {
local FILE="/usr/local/bin/${1}"
local install_dir=$(get_install_dir)
local FILE="${install_dir}/bin/${1}"
check_command $1
cat > $FILE <<- EOM
#!/bin/bash
Expand All @@ -70,14 +71,15 @@ if [[ "\$(command -v ${1})" == "$FILE" ]]; then
exit 1
fi
${1} \${@}
${1} "\$@"
EOM
chmod +x $FILE
}

# use this for simple symlink to /usr/local/bin
function link_wrapper () {
local TARGET="/usr/local/bin/${1}"
local install_dir=$(get_install_dir)
local TARGET="${install_dir}/bin/${1}"
local SOURCE=$(command -v ${1})
check_command $1
ln -sf $SOURCE $TARGET
Expand Down Expand Up @@ -120,7 +122,7 @@ function check_semver () {
function apt_install () {
echo "Installing apt packages: ${@}"
apt-get update
apt-get install -y $@
apt-get install -y "$@"
}

function require_distro () {
Expand Down Expand Up @@ -188,14 +190,14 @@ function get_install_dir () {
if [[ $EUID -eq 0 ]]; then
echo /usr/local
else
echo ${HOME}/.local
echo /home/${USER_NAME}
fi
}

function find_tool_path () {
if [[ -d "/usr/local/${TOOL_NAME}/${TOOL_VERSION}" ]]; then
echo /usr/local/${TOOL_NAME}/${TOOL_VERSION}
elif [[ -d "$HOME/.local/${TOOL_NAME}/${TOOL_VERSION}" ]]; then
echo $HOME/.local/${TOOL_NAME}/${TOOL_VERSION}
elif [[ -d "/home/${USER_NAME}/${TOOL_NAME}/${TOOL_VERSION}" ]]; then
echo /home/${USER_NAME}/${TOOL_NAME}/${TOOL_VERSION}
fi
}
11 changes: 8 additions & 3 deletions test/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ RUN composer --version


RUN set -ex; \
git clone https://github.com/renovate-tests/composerlock3.git; \
cd composerlock3/backend; \
git clone https://github.com/containerbase/test-php1.git; \
cd test-php1/test1; \
composer install --no-ansi --no-interaction; \
cd ..; \
git status -s;
Expand Down Expand Up @@ -80,13 +80,18 @@ RUN composer --version
#--------------------------------------
FROM base as testc

# no auto env for testing
SHELL [ "/bin/sh", "-c" ]

# renovate: datasource=github-releases lookupName=containerbase/php-prebuild
RUN install-tool php 8.0.3

USER 1000

# renovate: datasource=github-releases lookupName=composer/composer
RUN install-tool composer 2.0.12

USER 1000
RUN install-tool composer latest

RUN set -ex; \
[ ! -z "$(command -v php)" ] && echo "php installed" || exit 1;
Expand Down

0 comments on commit 2d2090e

Please sign in to comment.