freedombone/src/freedombone-utils-nodejs

388 lines
13 KiB
Plaintext
Raw Normal View History

2016-08-15 10:27:55 +02:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2016-08-15 10:27:55 +02:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2016-08-15 10:27:55 +02:00
#
# nodejs functions
#
# License
# =======
#
2018-01-25 19:35:39 +01:00
# Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
2016-08-15 10:27:55 +02:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# For reasons unknown we initially have to upgrade to an intermediate version
# before getting to the version we want
VARIANTS='mesh'
# change these versions at your peril. Things will often crash if you don't
# have specifically the correct versions
2018-06-15 12:47:12 +02:00
NODEJS_VERSION='8.11.3'
2018-04-18 10:54:43 +02:00
NODEJS_N_VERSION='2.1.7'
2018-04-03 17:10:47 +02:00
NPM_VERSION='5.8.0'
2016-08-15 10:27:55 +02:00
2016-11-04 15:06:09 +01:00
# This file keeps track of the apps needing nodejs
# so that it can be removed if tere are no apps which need it
NODEJS_INSTALLED_APPS_FILE=$HOME/.nodejs-apps
2017-09-14 14:19:30 +02:00
function get_npm_arch {
2017-09-14 14:34:22 +02:00
N_ARCH='x86'
2017-09-14 14:19:30 +02:00
NPM_ARCH='ia32'
2017-09-18 16:44:59 +02:00
if [[ $ARCHITECTURE == 'arm'* ]]; then
2017-09-14 14:19:30 +02:00
NPM_ARCH='armv7l'
2017-10-14 12:58:02 +02:00
N_ARCH='armv7l'
2017-09-14 14:19:30 +02:00
fi
2017-09-18 16:44:59 +02:00
if [[ $ARCHITECTURE == *"aarch"* ]]; then
NPM_ARCH='arm64'
N_ARCH='arm64'
fi
2017-09-14 14:19:30 +02:00
if [[ $ARCHITECTURE == 'x86_64' || $ARCHITECTURE == 'amd64' ]]; then
NPM_ARCH='x64'
2017-09-14 14:34:22 +02:00
N_ARCH='x64'
2017-09-14 14:19:30 +02:00
fi
}
function nodejs_fix_cpu_detection {
2018-04-02 23:52:32 +02:00
# fix for failing cpu detection during image build with qemu, see https://github.com/npm/npm/issues/19265
if [ -f "$rootdir/usr/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js" ]; then
2018-04-03 13:10:45 +02:00
sed -i "s/require('os').cpus().length/(require('os').cpus() || { length: 1 }).length/g" "$rootdir/usr/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js"
fi
2018-04-03 13:50:18 +02:00
if [ -f "$rootdir/usr/lib/node_modules/npm/node_modules/node-gyp/lib/build.js" ]; then
sed -i "s/require('os').cpus().length/(require('os').cpus() || { length: 1 }).length/g" "$rootdir/usr/lib/node_modules/npm/node_modules/node-gyp/lib/build.js"
fi
if [ -f "$rootdir/usr/lib/node_modules/npm/node_modules/worker-farm/examples/pi/index.js" ]; then
sed -i "s/require('os').cpus().length/(require('os').cpus() || { length: 1 }).length/g" "$rootdir/usr/lib/node_modules/npm/node_modules/worker-farm/examples/pi/index.js"
fi
2018-04-02 23:52:32 +02:00
if [ -f "$rootdir/.npm-global/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js" ]; then
2018-04-03 13:10:45 +02:00
sed -i "s/require('os').cpus().length/(require('os').cpus() || { length: 1 }).length/g" "$rootdir/.npm-global/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js"
2018-04-02 23:52:32 +02:00
fi
2018-04-03 13:50:18 +02:00
if [ -f "$rootdir/.npm-global/lib/node_modules/npm/node_modules/node-gyp/lib/build.js" ]; then
sed -i "s/require('os').cpus().length/(require('os').cpus() || { length: 1 }).length/g" "$rootdir/.npm-global/lib/node_modules/npm/node_modules/node-gyp/lib/build.js"
fi
if [ -f "$rootdir/.npm-global/lib/node_modules/npm/node_modules/worker-farm/examples/pi/index.js" ]; then
sed -i "s/require('os').cpus().length/(require('os').cpus() || { length: 1 }).length/g" "$rootdir/.npm-global/lib/node_modules/npm/node_modules/worker-farm/examples/pi/index.js"
fi
2018-04-02 16:23:53 +02:00
# installing worker farm fixes the cpu detection bug
#$mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g worker-farm@1.6.0 --save
}
2016-08-15 10:27:55 +02:00
function mesh_install_nodejs {
2017-09-19 12:03:45 +02:00
mesh_install_nodejs_prefix=
2018-03-02 21:33:00 +01:00
if [ "$rootdir" ]; then
2017-09-19 12:03:45 +02:00
mesh_install_nodejs_prefix="chroot $rootdir"
fi
$mesh_install_nodejs_prefix apt-get -yq install wget curl g++ m4 libtool automake
2017-09-19 12:03:45 +02:00
$mesh_install_nodejs_prefix apt-get -yq install libxext-dev libxtst-dev libxkbfile-dev
$mesh_install_nodejs_prefix apt-get -yq install apt-transport-https
2017-10-13 20:56:31 +02:00
$mesh_install_nodejs_prefix wget https://deb.nodesource.com/gpgkey/nodesource.gpg.key -O /root/node.gpg.key
2018-03-02 21:33:00 +01:00
if [ ! -f "$rootdir/root/node.gpg.key" ]; then
2017-10-13 20:56:31 +02:00
echo $'Unable to obtain gpg key for nodejs repo'
NODE_UPGRADE=
2017-10-13 20:56:31 +02:00
exit 6389252
fi
$mesh_install_nodejs_prefix apt-key add /root/node.gpg.key
2018-03-30 12:24:42 +02:00
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"
2018-03-28 13:34:17 +02:00
$mesh_install_nodejs_prefix apt-mark -q unhold nodejs
$mesh_install_nodejs_prefix apt-get update
$mesh_install_nodejs_prefix apt-get -yq remove --purge nodejs
if [ ! $NODE_UPGRADE ]; then
if [ -d "$rootdir/usr/local/lib/node_modules" ]; then
rm -rf "$rootdir/usr/local/lib/node_modules"
fi
fi
2018-03-02 21:33:00 +01:00
if [ -f "$rootdir/usr/local/bin/node" ]; then
rm "$rootdir/usr/local/bin/node"
fi
2018-03-02 21:33:00 +01:00
if [ -f "$rootdir/usr/bin/node" ]; then
2018-03-30 13:07:06 +02:00
rm "$rootdir/usr/bin/node"
fi
2018-03-02 21:33:00 +01:00
if [ -f "$rootdir/usr/bin/nodejs" ]; then
rm "$rootdir/usr/bin/nodejs"
fi
$mesh_install_nodejs_prefix apt-get -yq install nodejs
2016-08-15 10:27:55 +02:00
2018-03-02 21:33:00 +01:00
if [ -f "$rootdir/usr/bin/nodejs" ]; then
cp "$rootdir/usr/bin/nodejs" "$rootdir/usr/bin/node"
2017-09-13 21:10:28 +02:00
fi
2018-03-30 12:51:53 +02:00
if [ -f "$rootdir/usr/bin/node" ]; then
cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
fi
2018-03-30 13:02:34 +02:00
$mesh_install_nodejs_prefix apt-mark -q hold nodejs
2018-03-02 21:33:00 +01:00
if [ ! -f "${rootdir}/usr/bin/node" ]; then
if [ ! -f "${rootdir}/usr/local/bin/node" ]; then
if [ ! -f "${rootdir}/usr/bin/nodejs" ]; then
2017-10-13 21:50:21 +02:00
echo $'nodejs was not installed'
NODE_UPGRADE=
2017-10-13 21:50:21 +02:00
exit 63962
fi
fi
fi
2018-03-02 21:33:00 +01:00
if [ ! -f "$rootdir/usr/bin/node" ]; then
2017-09-13 21:10:28 +02:00
echo $'/usr/bin/node not found'
NODE_UPGRADE=
2017-09-13 21:10:28 +02:00
exit 7235728
fi
get_npm_arch
2017-10-14 00:06:27 +02:00
$mesh_install_nodejs_prefix npm config set unsafe-perm true
2018-04-02 23:52:32 +02:00
nodejs_setup_global_modules
nodejs_fix_cpu_detection
$mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g npm@${NPM_VERSION} --save
2018-04-03 18:07:08 +02:00
if [ -f "$rootdir/.npm-global/bin/npm" ]; then
cp "$rootdir/.npm-global/bin/npm" "$rootdir/usr/local/bin/npm"
cp "$rootdir/.npm-global/bin/npm" "$rootdir/usr/bin/npm"
fi
2018-03-02 21:33:00 +01:00
if [ -f "$rootdir/usr/local/bin/npm" ]; then
2018-04-03 15:17:47 +02:00
cp "$rootdir/usr/local/bin/npm" "$rootdir/usr/bin/npm"
2017-09-14 14:19:30 +02:00
fi
2018-03-02 21:33:00 +01:00
cp "$rootdir/usr/bin/npm" "$rootdir/root/npm"
2017-09-14 14:19:30 +02:00
# update from the old debian nodejs version
2017-09-19 12:03:45 +02:00
$mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g n@${NODEJS_N_VERSION} --save
2018-04-03 19:06:58 +02:00
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
$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
$mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g npm@${NPM_VERSION} --save
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"
fi
2018-03-30 13:26:57 +02:00
if [ -f "$rootdir/usr/bin/node" ]; then
cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
fi
2017-12-04 12:37:31 +01:00
2017-12-04 12:22:03 +01:00
# check the version numbers
2018-03-02 21:33:00 +01:00
cat <<EOF > "$rootdir/usr/bin/test_nodejs_install"
2017-12-04 12:22:03 +01:00
#!/bin/bash
node_version=\$(node -v)
if [[ "\$node_version" != "v${NODEJS_VERSION}" ]]; then
echo $"nodejs version expected to be ${NODEJS_VERSION} but found \$node_version"
exit 1
fi
npm_version=\$(npm -v)
2017-12-04 12:47:48 +01:00
if [[ "\$npm_version" != "${NPM_VERSION}" ]]; then
2017-12-04 12:22:03 +01:00
echo $"npm version expected to be ${NPM_VERSION} but found \$npm_version"
exit 2
fi
EOF
2018-03-02 21:33:00 +01:00
chmod +x "$rootdir/usr/bin/test_nodejs_install"
if ! $mesh_install_nodejs_prefix /usr/bin/test_nodejs_install; then
2017-12-04 12:22:03 +01:00
echo $"nodejs version numbers did not match. Architecture is $NPM_ARCH."
NODE_UPGRADE=
2017-12-04 12:22:03 +01:00
exit 76835282
fi
2018-03-02 21:33:00 +01:00
rm "$rootdir/usr/bin/test_nodejs_install"
NODE_UPGRADE=
2016-08-15 10:27:55 +02:00
}
2018-03-30 12:24:42 +02:00
function nodejs_upgrade {
if [ ! -f /etc/apt/sources.list.d/nodesource.list ]; then
return
fi
nodejs_setup_global_modules
2018-03-30 12:24:42 +02:00
if grep -q "node_8.x" /etc/apt/sources.list.d/nodesource.list; then
2018-03-30 12:31:53 +02:00
if [ -f /usr/local/bin/node ]; then
2018-03-30 12:32:41 +02:00
CURR_NODE_VERSION=$(node --version)
if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
return
fi
2018-03-30 12:31:53 +02:00
fi
fi
if [ -f /usr/local/bin/node ]; then
CURR_NODE_VERSION=$(node --version)
if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
return
fi
2018-03-30 12:24:42 +02:00
fi
2018-03-30 12:39:04 +02:00
read_config_param ARCHITECTURE
get_npm_arch
NODE_UPGRADE=1
2018-03-30 12:24:42 +02:00
rootdir=
mesh_install_nodejs
2018-03-30 14:36:07 +02:00
npm update -g
2018-03-30 12:24:42 +02:00
}
2018-04-13 12:16:49 +02:00
function mesh_setup_npm {
cat <<EOF > "$rootdir/usr/bin/install_npm_global"
#!/bin/bash
if [ ! -d ~/.npm-global ]; then
mkdir ~/.npm-global
fi
npm config set prefix '~/.npm-global'
if [[ "$PATH" != *'~/.npm-global/bin'* ]]; then
export PATH=~/.npm-global/bin:$PATH
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
fi
export NPM_CONFIG_PREFIX=~/.npm-global
2018-04-13 15:43:17 +02:00
echo 'export NPM_CONFIG_PREFIX=~/.npm-global' >> ~/.bashrc
2018-04-13 12:16:49 +02:00
source ~/.profile
EOF
chroot "$rootdir" /bin/chmod +x /usr/bin/install_npm_global
chroot "$rootdir" /usr/bin/install_npm_global
rm "$rootdir/usr/bin/install_npm_global"
}
function nodejs_setup_global_modules {
if [ ! -f /usr/local/bin/node ]; then
return
fi
if [ ! -d "$rootdir/root/.npm-global" ]; then
mkdir "$rootdir/root/.npm-global"
fi
$mesh_install_nodejs_prefix npm config set prefix '/root/.npm-global'
if ! grep -q "PATH=/root/.npm-global/bin" "$rootdir/root/.bashrc"; then
echo "PATH=/root/.npm-global/bin:\$PATH" >> "$rootdir/root/.bashrc"
fi
if ! grep -q "NPM_CONFIG_PREFIX=" "$rootdir/root/.bashrc"; then
echo "export NPM_CONFIG_PREFIX=/root/.npm-global" >> "$rootdir/root/.bashrc"
fi
2018-04-02 17:04:57 +02:00
# shellcheck disable=SC2086
$mesh_install_nodejs_prefix export PATH=/root/.npm-global/bin:$PATH
$mesh_install_nodejs_prefix export NPM_CONFIG_PREFIX=/root/.npm-global
}
2016-11-04 15:06:09 +01:00
function remove_nodejs {
2018-03-02 21:33:00 +01:00
if [ ! "$1" ]; then
2016-11-04 15:06:09 +01:00
return
fi
2018-03-02 21:33:00 +01:00
if [ ! -f "$NODEJS_INSTALLED_APPS_FILE" ]; then
2017-05-18 13:50:24 +02:00
#remove_app nodejs
2016-11-04 15:06:09 +01:00
return
fi
2018-03-02 21:33:00 +01:00
sed -i "/install_${1}/d" "$NODEJS_INSTALLED_APPS_FILE"
if ! grep -q "install_" "$NODEJS_INSTALLED_APPS_FILE"; then
2016-11-04 15:06:09 +01:00
apt-get -yq remove --purge nodejs
if [ -f /usr/bin/nodejs ]; then
rm /usr/bin/nodejs
fi
if [ -f /usr/local/bin/node ]; then
rm /usr/local/bin/node
fi
if [ -f /usr/bin/node ]; then
rm /usr/bin/node
fi
2016-11-04 18:42:17 +01:00
if [ -d /usr/lib/node_modules ]; then
rm -rf /usr/lib/node_modules
fi
2017-04-02 12:40:34 +02:00
if [ -f /usr/bin/n ]; then
rm /usr/bin/n
fi
2016-11-04 15:06:09 +01:00
2016-11-04 15:50:11 +01:00
remove_app nodejs
2018-03-02 21:33:00 +01:00
rm "$NODEJS_INSTALLED_APPS_FILE"
2016-11-04 15:41:20 +01:00
apt-get -yq autoremove
2016-11-04 15:06:09 +01:00
fi
}
2017-04-02 12:40:34 +02:00
function upgrade_nodejs {
CURR_NODE_VERSION=$(node --version)
CURR_NPM_VERSION=$(npm --version)
CURR_N_VERSION=$(n --version)
if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
2017-04-02 12:42:07 +02:00
npm upgrade -g npm@${NPM_VERSION} --save
2017-04-02 12:40:34 +02:00
fi
if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
npm upgrade -g n@${NODEJS_N_VERSION} --save
fi
2017-04-02 15:15:45 +02:00
if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
2018-02-10 21:07:50 +01:00
n ${NODEJS_VERSION}
2017-04-02 12:40:34 +02:00
fi
cp /usr/local/bin/n /usr/bin/n
2017-06-04 16:38:50 +02:00
if [ -f /usr/local/bin/npm ]; then
cp /usr/local/bin/npm /usr/bin/npm
fi
if [ -f /usr/local/bin/node ]; then
cp /usr/local/bin/node /usr/bin/nodejs
cp /usr/local/bin/node /usr/bin/node
fi
2017-04-02 12:40:34 +02:00
}
2016-08-15 10:27:55 +02:00
function install_nodejs {
2018-03-02 21:33:00 +01:00
if [ "$INSTALLING_MESH" ]; then
2016-08-15 10:27:55 +02:00
mesh_install_nodejs
return
fi
2018-02-25 13:50:46 +01:00
if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
upgrade_nodejs
return
fi
2016-08-15 10:27:55 +02:00
2018-03-02 21:33:00 +01:00
if [ ! "$ARCHITECTURE" ]; then
2017-09-19 00:44:12 +02:00
ARCHITECTURE=$(uname -m)
fi
2017-09-19 00:44:12 +02:00
rootdir=
mesh_install_nodejs
2017-05-18 13:50:24 +02:00
2017-10-14 13:29:20 +02:00
# verify nodejs versions are what we expect
CURR_NODE_VERSION=$(node --version)
CURR_NPM_VERSION=$(npm --version)
CURR_N_VERSION=$(n --version)
if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
echo $"Expected npm version $NPM_VERSION but actually have $CURR_NPM_VERSION"
exit 6728252
fi
if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
echo $"Expected n version $NODEJS_N_VERSION but actually have $CURR_N_VERSION"
exit 5267452
fi
if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
echo $"Expected node version $NODEJS_VERSION but actually have $CURR_NODE_VERSION"
exit 5274527
2017-04-02 12:40:34 +02:00
fi
2018-03-02 21:33:00 +01:00
if [ "$1" ]; then
if ! grep -q "install_${1}" "$NODEJS_INSTALLED_APPS_FILE"; then
echo "install_${1}" >> "$NODEJS_INSTALLED_APPS_FILE"
2016-11-04 15:06:09 +01:00
fi
fi
2018-02-25 13:50:46 +01:00
mark_completed "${FUNCNAME[0]}"
2016-08-15 10:27:55 +02:00
}
# NOTE: deliberately there is no "exit 0"