Skip to content
Snippets Groups Projects
Commit 7c27b35c authored by Bob Mottram's avatar Bob Mottram
Browse files

Break up nodejs install into subfunctions

parent 3d2e7eb8
No related branches found
No related tags found
No related merge requests found
......@@ -89,91 +89,69 @@ function nodejs_fix_cpu_detection {
#$mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g worker-farm@1.6.0 --save
}
function mesh_install_nodejs {
mesh_install_nodejs_prefix=
if [ "$rootdir" ]; then
mesh_install_nodejs_prefix="chroot $rootdir"
function nodejs_setup_repo {
if [ -f "$rootdir/etc/apt/sources.list.d/nodesource.list" ]; then
return
fi
if [ ! -f "$rootdir/etc/apt/sources.list.d/nodesource.list" ]; then
echo $'Setting up nodejs debian repos'
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix $INSTALL_PACKAGES wget curl g++ m4 libtool automake
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix $INSTALL_PACKAGES libxext-dev libxtst-dev libxkbfile-dev
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix $INSTALL_PACKAGES apt-transport-https
$mesh_install_nodejs_prefix wget https://deb.nodesource.com/gpgkey/nodesource.gpg.key -O /root/node.gpg.key
if [ ! -f "$rootdir/root/node.gpg.key" ]; then
echo $'Unable to obtain gpg key for nodejs repo'
NODE_UPGRADE=
exit 63
fi
$mesh_install_nodejs_prefix apt-key add /root/node.gpg.key
echo "deb https://deb.nodesource.com/node_8.x stretch main" > "$rootdir/etc/apt/sources.list.d/nodesource.list"
echo "deb-src https://deb.nodesource.com/node_8.x stretch main" >> "$rootdir/etc/apt/sources.list.d/nodesource.list"
echo $'nodejs repos added'
fi
if [ ! $NODE_UPGRADE ]; then
echo $'Removing any previous nodejs installations'
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix $PACKAGE_UNHOLD nodejs
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix $UPDATE_PACKAGES
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix $REMOVE_PACKAGES_PURGE nodejs
echo $'Removing any previous nodejs binaries and modules'
if [ -d "$rootdir/usr/local/lib/node_modules" ]; then
rm -rf "$rootdir/usr/local/lib/node_modules"
fi
if [ -f "$rootdir/usr/local/bin/node" ]; then
rm "$rootdir/usr/local/bin/node"
fi
if [ -f "$rootdir/usr/bin/node" ]; then
rm "$rootdir/usr/bin/node"
fi
if [ -f "$rootdir/usr/bin/nodejs" ]; then
rm "$rootdir/usr/bin/nodejs"
fi
echo $'Setting up nodejs debian repos'
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix $INSTALL_PACKAGES wget curl g++ m4 libtool automake
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix $INSTALL_PACKAGES libxext-dev libxtst-dev libxkbfile-dev
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix $INSTALL_PACKAGES apt-transport-https
$mesh_install_nodejs_prefix wget https://deb.nodesource.com/gpgkey/nodesource.gpg.key -O /root/node.gpg.key
if [ ! -f "$rootdir/root/node.gpg.key" ]; then
echo $'Unable to obtain gpg key for nodejs repo'
NODE_UPGRADE=
exit 63
fi
$mesh_install_nodejs_prefix apt-key add /root/node.gpg.key
echo "deb https://deb.nodesource.com/node_8.x stretch main" > "$rootdir/etc/apt/sources.list.d/nodesource.list"
echo "deb-src https://deb.nodesource.com/node_8.x stretch main" >> "$rootdir/etc/apt/sources.list.d/nodesource.list"
echo $'nodejs repos added'
}
echo $'Installing nodejs from debian package'
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix $INSTALL_PACKAGES nodejs
function nodejs_install_debian_package {
if [ $NODE_UPGRADE ]; then
return
fi
if [ -f "$rootdir/usr/bin/nodejs" ]; then
cp "$rootdir/usr/bin/nodejs" "$rootdir/usr/bin/node"
echo $'Removing any previous nodejs installations'
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix $PACKAGE_UNHOLD nodejs
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix $UPDATE_PACKAGES
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix $REMOVE_PACKAGES_PURGE nodejs
echo $'Removing any previous nodejs binaries and modules'
if [ -d "$rootdir/usr/local/lib/node_modules" ]; then
rm -rf "$rootdir/usr/local/lib/node_modules"
fi
if [ -f "$rootdir/usr/local/bin/node" ]; then
rm "$rootdir/usr/local/bin/node"
fi
if [ -f "$rootdir/usr/bin/node" ]; then
cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
rm "$rootdir/usr/bin/node"
fi
if [ -f "$rootdir/usr/bin/nodejs" ]; then
rm "$rootdir/usr/bin/nodejs"
fi
echo $'Installing nodejs from debian package'
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix $INSTALL_PACKAGES nodejs
echo $'Holding nodejs debian package'
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix $PACKAGE_HOLD nodejs
}
if [ ! -f "${rootdir}/usr/bin/node" ]; then
if [ ! -f "${rootdir}/usr/local/bin/node" ]; then
if [ ! -f "${rootdir}/usr/bin/nodejs" ]; then
echo $'nodejs was not installed'
NODE_UPGRADE=
exit 63
fi
fi
fi
if [ ! -f "$rootdir/usr/bin/node" ]; then
echo $'/usr/bin/node not found'
NODE_UPGRADE=
exit 72
fi
function nodejs_install_npm {
get_npm_arch
$mesh_install_nodejs_prefix npm config set unsafe-perm true
nodejs_setup_global_modules
nodejs_fix_cpu_detection
......@@ -189,41 +167,55 @@ function mesh_install_nodejs {
fi
cp "$rootdir/usr/bin/npm" "$rootdir/root/npm"
$mesh_install_nodejs_prefix ln -s /root/.npm-global/lib/node_modules /usr/local/bin/node_modules
}
function nodejs_install_npm_secondary {
if [ "$rootdir" ]; then
return
fi
# deliberate second install of npm
echo $"Installing npm ${NPM_VERSION}"
$mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g npm@${NPM_VERSION} --save
if [ -f "$rootdir/root/.npm-global/lib/node_modules/npm/bin/npm" ]; then
cp "$rootdir/root/.npm-global/lib/node_modules/npm/bin/npm" "$rootdir/usr/local/bin/npm"
cp "$rootdir/root/.npm-global/lib/node_modules/npm/bin/npm" "$rootdir/usr/bin/npm"
fi
if [ -f "$rootdir/usr/local/bin/npm" ]; then
cp "$rootdir/usr/local/bin/npm" "$rootdir/usr/bin/npm"
fi
cp "$rootdir/usr/bin/npm" "$rootdir/root/npm"
}
function nodejs_install_n {
# update from the old debian nodejs version
echo $"Installing n ${NODEJS_N_VERSION} on ${NPM_ARCH}"
$mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g n@${NODEJS_N_VERSION} --save
if [ ! "$rootdir" ]; then
# Don't do this if we're building an image,
# because cpu detection faults occur.
# This condition may no longer be needed in future once the bug is fixed
echo $"Installing nodejs ${NODEJS_VERSION} via n"
$mesh_install_nodejs_prefix n --arch $N_ARCH ${NODEJS_VERSION}
nodejs_fix_cpu_detection
cp "$rootdir/root/npm" "$rootdir/usr/bin/npm"
cp "$rootdir/root/npm" "$rootdir/usr/local/bin/npm"
# deliberate second install of npm
echo $"Installing npm ${NPM_VERSION}"
$mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g npm@${NPM_VERSION} --save
if [ -f "$rootdir/root/.npm-global/lib/node_modules/npm/bin/npm" ]; then
cp "$rootdir/root/.npm-global/lib/node_modules/npm/bin/npm" "$rootdir/usr/local/bin/npm"
cp "$rootdir/root/.npm-global/lib/node_modules/npm/bin/npm" "$rootdir/usr/bin/npm"
fi
if [ -f "$rootdir/usr/local/bin/npm" ]; then
cp "$rootdir/usr/local/bin/npm" "$rootdir/usr/bin/npm"
fi
cp "$rootdir/usr/bin/npm" "$rootdir/root/npm"
}
function nodejs_install_from_n {
# Don't do this if we're building an image,
# because cpu detection faults occur.
# This condition may no longer be needed in future once the bug is fixed
if [ "$rootdir" ]; then
return
fi
if [ -f "$rootdir/usr/bin/node" ]; then
cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
echo $"Installing nodejs ${NODEJS_VERSION} via n"
$mesh_install_nodejs_prefix n --arch $N_ARCH ${NODEJS_VERSION}
nodejs_fix_cpu_detection
cp "$rootdir/root/npm" "$rootdir/usr/bin/npm"
cp "$rootdir/root/npm" "$rootdir/usr/local/bin/npm"
}
function nodejs_check_versions {
if [ "$rootdir" ]; then
return
fi
if [ ! "$rootdir" ]; then
echo $'Checking that correct nodejs and npm versions were installed'
# check the version numbers
cat <<EOF > "$rootdir/usr/bin/test_nodejs_install"
echo $'Checking that correct nodejs and npm versions were installed'
# check the version numbers
cat <<EOF > "$rootdir/usr/bin/test_nodejs_install"
#!/bin/bash
node_version=\$(node -v)
if [[ "\$node_version" != "v${NODEJS_VERSION}" ]]; then
......@@ -238,14 +230,63 @@ if [[ "\$npm_version" != "${NPM_VERSION}" ]]; then
exit 2
fi
EOF
chmod +x "$rootdir/usr/bin/test_nodejs_install"
if ! $mesh_install_nodejs_prefix /usr/bin/test_nodejs_install; then
echo $"nodejs version numbers did not match. Architecture is $NPM_ARCH."
NODE_UPGRADE=
exit 76
chmod +x "$rootdir/usr/bin/test_nodejs_install"
if ! $mesh_install_nodejs_prefix /usr/bin/test_nodejs_install; then
echo $"nodejs version numbers did not match. Architecture is $NPM_ARCH."
NODE_UPGRADE=
exit 76
fi
rm "$rootdir/usr/bin/test_nodejs_install"
}
function nodejs_check_install_succeeded {
# rename node command, so there isn't confusion between 'node' and 'nodejs'
if [ -f "$rootdir/usr/bin/nodejs" ]; then
cp "$rootdir/usr/bin/nodejs" "$rootdir/usr/bin/node"
fi
if [ -f "$rootdir/usr/bin/node" ]; then
cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
fi
# Check that the node command exists
if [ ! -f "${rootdir}/usr/bin/node" ]; then
if [ ! -f "${rootdir}/usr/local/bin/node" ]; then
if [ ! -f "${rootdir}/usr/bin/nodejs" ]; then
echo $'nodejs was not installed'
NODE_UPGRADE=
exit 63
fi
fi
rm "$rootdir/usr/bin/test_nodejs_install"
fi
if [ ! -f "$rootdir/usr/bin/node" ]; then
echo $'/usr/bin/node not found'
NODE_UPGRADE=
exit 72
fi
}
function mesh_install_nodejs {
mesh_install_nodejs_prefix=
if [ "$rootdir" ]; then
mesh_install_nodejs_prefix="chroot $rootdir"
fi
nodejs_setup_repo
nodejs_install_debian_package
nodejs_check_install_succeeded
nodejs_install_npm
nodejs_install_n
nodejs_install_from_n
nodejs_install_npm_secondary
if [ -f "$rootdir/usr/bin/node" ]; then
cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
fi
nodejs_check_versions
NODE_UPGRADE=
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment