Skip to content

Commit

Permalink
fix: legacy install of npm and yarn (#307)
Browse files Browse the repository at this point in the history
* fix: legacy install of npm and yarn

* fix: use temp folder for node installs

* chore: clean cacache folder

* chore: clean cache before install

* chore: remove attempted cleanup of _cacache

* chore: 775 on test folder plus chmod 775 after install

* chore: even more 775
  • Loading branch information
Chumper authored Feb 16, 2022
1 parent e3de4e5 commit 9dde0f0
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 40 deletions.
7 changes: 5 additions & 2 deletions src/usr/local/bin/install-npm
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ case "$TOOL_NAME" in
check_command npm
unset NPM_CONFIG_PREFIX

temp_folder=$(mktemp -u)
mkdir -p "${temp_folder}"

echo "Installing npm tool ${TOOL_NAME} v${TOOL_VERSION}"
npm install "${TOOL_NAME}@${TOOL_VERSION}" --global --unsafe --cache /tmp/empty-cache
npm install "${TOOL_NAME}@${TOOL_VERSION}" --global --unsafe --cache "${temp_folder}"

# Clean download cache
npm cache clean --force
# Clean node-gyp cache
rm -rf /root/.cache /tmp/empty-cache
rm -rf /root/.cache "${temp_folder}"
;;
esac

12 changes: 6 additions & 6 deletions src/usr/local/buildpack/tools/lerna.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ check_command node
tool_path=$(find_versioned_tool_path)

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

mkdir -p "${tool_path}"
temp_folder=$(mktemp -u)
mkdir -p "${temp_folder}"

NPM_CONFIG_PREFIX=$tool_path npm install --cache /tmp/empty-cache -g "${TOOL_NAME}@${TOOL_VERSION}"
npm cache clean --force
NPM_CONFIG_PREFIX=$tool_path npm install --cache "${temp_folder}" -g "${TOOL_NAME}@${TOOL_VERSION}"

# Clean download cache
NPM_CONFIG_PREFIX=$tool_path npm cache clean --force
# Clean node-gyp cache
rm -rf "$HOME/.cache /tmp/empty-cache"
rm -rf "$HOME/.cache" "${temp_folder}"
fi

link_wrapper "${TOOL_NAME}" "$tool_path/bin"
Expand Down
12 changes: 6 additions & 6 deletions src/usr/local/buildpack/tools/node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ fi

NODE_DISTRO=linux-x64
tool_path=$(find_versioned_tool_path)
INSTALL_DIR=$(get_install_dir)
PREFIX="${USER_HOME}/.npm-global"


Expand Down Expand Up @@ -62,11 +61,12 @@ function prepare_user_config () {
}

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

tool_path="$(create_versioned_tool_path)"
npm=${tool_path}/bin/npm

mkdir -p "$tool_path"
temp_folder=$(mktemp -u)
mkdir -p "${temp_folder}"

file=/tmp/${TOOL_NAME}.tar.xz

Expand All @@ -90,8 +90,8 @@ if [[ -z "${tool_path}" ]]; then

if [[ ${MAJOR} -lt 15 ]]; then
# update to latest node-gyp to fully support python3
NPM_CONFIG_PREFIX=$tool_path $npm explore npm -g -- "$npm" install --cache /tmp/empty-cache node-gyp@latest
rm -rf /tmp/empty-cache
NPM_CONFIG_PREFIX=$tool_path $npm explore npm -g -- "$npm" install --cache "${temp_folder}" node-gyp@latest
rm -rf "${temp_folder}"
fi

# Clean download cache
Expand Down
18 changes: 9 additions & 9 deletions src/usr/local/buildpack/tools/npm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ tool_path=$(find_versioned_tool_path)
npm=$(command -v npm)

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

mkdir -p "${tool_path}"

NPM_CONFIG_PREFIX=$tool_path $npm install --cache /tmp/empty-cache -g "npm@${TOOL_VERSION}"
temp_folder=$(mktemp -u)
mkdir -p "${temp_folder}"

$npm cache clean --force
NPM_CONFIG_PREFIX=$tool_path $npm install --cache "${temp_folder}" -g "npm@${TOOL_VERSION}"

if [[ ${MAJOR} -lt 7 ]]; then
# update to latest node-gyp to fully support python3
NPM_CONFIG_PREFIX=$tool_path $npm explore npm -g -- npm install --cache /tmp/empty-cache node-gyp@latest
rm -rf /tmp/empty-cache
NPM_CONFIG_PREFIX=$tool_path $npm explore npm -g -- npm install --cache "${temp_folder}" node-gyp@latest
rm -rf "${temp_folder}"
fi

# Clean download cache
NPM_CONFIG_PREFIX=$tool_path $npm cache clean --force
# Clean node-gyp cache
rm -rf "$HOME/.cache /tmp/empty-cache"
rm -rf "$HOME/.cache" "${temp_folder}"
chmod -R 775 "${HOME}/.npm" || true
fi

link_wrapper "${TOOL_NAME}" "$tool_path/bin"
Expand Down
12 changes: 6 additions & 6 deletions src/usr/local/buildpack/tools/pnpm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ check_command node
tool_path=$(find_versioned_tool_path)

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

mkdir -p "${tool_path}"
temp_folder=$(mktemp -u)
mkdir -p "${temp_folder}"

NPM_CONFIG_PREFIX=$tool_path npm install --cache /tmp/empty-cache -g "${TOOL_NAME}@${TOOL_VERSION}"
npm cache clean --force
NPM_CONFIG_PREFIX=$tool_path npm install --cache "${temp_folder}" -g "${TOOL_NAME}@${TOOL_VERSION}"

# Clean download cache
NPM_CONFIG_PREFIX=$tool_path npm cache clean --force
# Clean node-gyp cache
rm -rf "$HOME/.cache" /tmp/empty-cache
rm -rf "$HOME/.cache" "${temp_folder}"
fi

link_wrapper "${TOOL_NAME}" "$tool_path/bin"
Expand Down
12 changes: 7 additions & 5 deletions src/usr/local/buildpack/tools/yarn-slim.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ check_command node
tool_path=$(find_versioned_tool_path)

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

temp_folder=$(mktemp -u)
mkdir -p "${temp_folder}"

mkdir -p "${tool_path}"

NPM_CONFIG_PREFIX=$tool_path npm install --cache /tmp/empty-cache -g "yarn@${TOOL_VERSION}"
npm cache clean --force
NPM_CONFIG_PREFIX=$tool_path npm install --cache "${temp_folder}" -g "yarn@${TOOL_VERSION}"

# Clean download cache
NPM_CONFIG_PREFIX=$tool_path npm cache clean --force
# Clean node-gyp cache
rm -rf "$HOME/.cache" /tmp/empty-cache
rm -rf "$HOME/.cache" "${temp_folder}"

# patch yarn
sed -i 's/ steps,/ steps.slice(0,1),/' "$tool_path/lib/node_modules/yarn/lib/cli.js"
Expand Down
13 changes: 7 additions & 6 deletions src/usr/local/buildpack/tools/yarn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ check_command node
tool_path=$(find_versioned_tool_path)

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

mkdir -p "${tool_path}"
temp_folder=$(mktemp -u)
mkdir -p "${temp_folder}"

NPM_CONFIG_PREFIX=$tool_path npm install --cache /tmp/empty-cache -g "${TOOL_NAME}@${TOOL_VERSION}"
npm cache clean --force
NPM_CONFIG_PREFIX=$tool_path npm install --cache "${temp_folder}" -g "${TOOL_NAME}@${TOOL_VERSION}"

# Clean download cache
NPM_CONFIG_PREFIX=$tool_path npm cache clean --force
# Clean node-gyp cache
rm -rf "$HOME/.cache" /tmp/empty-cache
rm -rf "$HOME/.cache" "${temp_folder}"
chmod -R 775 "${HOME}/.npm" || true
fi

link_wrapper "${TOOL_NAME}" "${tool_path}/bin"
Expand Down
60 changes: 60 additions & 0 deletions test/node/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ RUN touch /.dummy
USER 1000

COPY --chown=1000:0 test test
RUN chmod -R 775 /test

WORKDIR /test

Expand Down Expand Up @@ -246,6 +247,63 @@ RUN npm --version
RUN npm --version | grep '6.14.15'
RUN set -ex; cd a; npm install

#--------------------------------------
# test: openshift yarn
#--------------------------------------
FROM build as testj

USER 1111:0

ARG APT_HTTP_PROXY

RUN npm install -g yarn

RUN set -ex; \
[ "$(command -v yarn)" = "/home/${USER_NAME}/.npm-global/bin/yarn" ] && echo "works" || exit 1; \
yarn --version;

RUN set -ex; ls -lah /test/a; cd a; yarn

SHELL [ "/bin/sh", "-c" ]

# renovate: datasource=npm
RUN install-tool yarn 1.22.17

# renovate: datasource=npm
RUN install-tool lerna 4.0.0

RUN set -ex; \
[ "$(command -v yarn)" = "/home/user/bin/yarn" ] && echo "works" || exit 1; \
yarn --version

RUN set -ex; \
[ "$(command -v lerna)" = "/home/user/bin/lerna" ] && echo "works" || exit 1; \
lerna --version

RUN install-tool yarn 1.22.15

#--------------------------------------
# test: openshift npm (install-tool npm)
#--------------------------------------
FROM build as testk

USER 1111:0

SHELL [ "/bin/sh", "-c" ]

# don't update!!
RUN install-tool npm 8.0.0

RUN npm --version
RUN npm --version | grep '8.0.0'

# don't update!!
RUN install-tool npm 7.24.2

RUN node --version
RUN npm --version
RUN npm --version | grep '7.24.2'

#--------------------------------------
# final
#--------------------------------------
Expand All @@ -260,3 +318,5 @@ COPY --from=testf /.dummy /.dummy
COPY --from=testg /.dummy /.dummy
COPY --from=testh /.dummy /.dummy
COPY --from=testi /.dummy /.dummy
COPY --from=testj /.dummy /.dummy
COPY --from=testk /.dummy /.dummy
Empty file modified test/node/test/a/package.json
100644 → 100755
Empty file.

0 comments on commit 9dde0f0

Please sign in to comment.