Skip to content
Snippets Groups Projects
freedombone-utils-nodejs 15.6 KiB
Newer Older
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
#
# nodejs functions
#
# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2014-2019 Bob Mottram <bob@freedombone.net>
#
# 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
Bob Mottram's avatar
Bob Mottram committed
NODEJS_VERSION='8.12.0'
NODEJS_N_VERSION='2.1.12'
Bob Mottram's avatar
Bob Mottram committed
NPM_VERSION='5.8.0'
Bob Mottram's avatar
Bob Mottram committed
# 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

function get_npm_arch {
Bob Mottram's avatar
Bob Mottram committed
    N_ARCH='x86'
    NPM_ARCH='ia32'
    if [[ "$ARCHITECTURE" == 'arm'* ]]; then
        NPM_ARCH='armv7l'
Bob Mottram's avatar
Bob Mottram committed
        N_ARCH='armv7l'
    if [[ "$ARCHITECTURE" == *"aarch"* ]]; then
        NPM_ARCH='arm64'
        N_ARCH='arm64'
    fi
    if [[ "$ARCHITECTURE" == 'x86_64' || $ARCHITECTURE == 'amd64' ]]; then
        NPM_ARCH='x64'
Bob Mottram's avatar
Bob Mottram committed
        N_ARCH='x64'
function nodejs_fix_cpu_detection {
Bob Mottram's avatar
Bob Mottram committed
    # 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
Bob Mottram's avatar
Bob Mottram committed
        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"

    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


Bob Mottram's avatar
Bob Mottram committed
    if [ -f "$rootdir/.npm-global/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js" ]; then
Bob Mottram's avatar
Bob Mottram committed
        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"
Bob Mottram's avatar
Bob Mottram committed
    fi

    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

Bob Mottram's avatar
Bob Mottram committed
    # installing worker farm fixes the cpu detection bug
    #$mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g worker-farm@1.6.0 --save
function nodejs_setup_repo {
    if [ -f "$rootdir/etc/apt/sources.list.d/nodesource.list" ]; then
        return
    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'
}
function nodejs_install_debian_package {
    if [ $NODE_UPGRADE ]; then
        return
    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"
    if [ -f "$rootdir/usr/local/bin/node" ]; then
        rm "$rootdir/usr/local/bin/node"
    fi
Bob Mottram's avatar
Bob Mottram committed
    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"
    echo $'Installing nodejs from debian package'
    # shellcheck disable=SC2086
    $mesh_install_nodejs_prefix $INSTALL_PACKAGES nodejs

    echo $'Holding nodejs debian package'
Bob Mottram's avatar
Bob Mottram committed
    # shellcheck disable=SC2086
    $mesh_install_nodejs_prefix $PACKAGE_HOLD nodejs
function nodejs_install_npm {
Bob Mottram's avatar
Bob Mottram committed
    $mesh_install_nodejs_prefix npm config set unsafe-perm true
Bob Mottram's avatar
Bob Mottram committed
    nodejs_setup_global_modules
    nodejs_fix_cpu_detection
    if [ $NODE_UPGRADE ]; then
        echo $"Installing npm ${NPM_VERSION}"
        npm install -g npm@${NPM_VERSION} --save
    else
        echo $"Installing npm ${NPM_VERSION} on ${NPM_ARCH}"
        $mesh_install_nodejs_prefix npm install --arch=${NPM_ARCH} -g npm@${NPM_VERSION} --save
    fi
Bob Mottram's avatar
Bob Mottram committed
    # NOTE: trying to copy here breaks the build
    #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
    if [ -f "$rootdir/usr/local/bin/npm" ]; then
Bob Mottram's avatar
Bob Mottram committed
        cp "$rootdir/usr/local/bin/npm" "$rootdir/usr/bin/npm"
    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 -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 {
    if [ "$rootdir" ]; then
        return
    fi
    # update from the old debian nodejs version
    echo $"Installing n ${NODEJS_N_VERSION} on ${NPM_ARCH}"
Bob Mottram's avatar
Bob Mottram committed
    npm install -g n@${NODEJS_N_VERSION} --save
}

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
    echo $"Installing nodejs ${NODEJS_VERSION} via n"
Bob Mottram's avatar
Bob Mottram committed
    n ${NODEJS_VERSION}
    nodejs_fix_cpu_detection
    if [ -f "$rootdir/usr/local/bin/node" ]; then
        cp "$rootdir/usr/local/bin/node" "$rootdir/usr/bin/node"
    fi
    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
Bob Mottram's avatar
Bob Mottram committed
    fi
    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
    find / -name node
    echo $"nodejs version expected to be ${NODEJS_VERSION} but found \$node_version"
    exit 1
fi
npm_version=\$(npm -v)
Bob Mottram's avatar
Bob Mottram committed
if [[ "\$npm_version" != "${NPM_VERSION}" ]]; then
    find / -name npm
    echo $"npm version expected to be ${NPM_VERSION} but found \$npm_version"
    exit 2
fi
EOF
    chmod +x "$rootdir/usr/bin/test_nodejs_install"
Bob Mottram's avatar
Bob Mottram committed
    if ! /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

    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

function nodejs_upgrade {
    if [ ! -f /etc/apt/sources.list.d/nodesource.list ]; then
        return
    fi
    if grep -q "node_8.x" /etc/apt/sources.list.d/nodesource.list; then
        if [ -f /usr/local/bin/node ]; then
Bob Mottram's avatar
Bob Mottram committed
            CURR_NODE_VERSION=$(node --version)
            if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
                return
            fi
        fi
    fi
    if [ -f /usr/local/bin/node ]; then
        CURR_NODE_VERSION=$(node --version)
        if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
            return
        fi
Bob Mottram's avatar
Bob Mottram committed
    read_config_param ARCHITECTURE
    get_npm_arch
    rootdir=
    mesh_install_nodejs
Bob Mottram's avatar
Bob Mottram committed
    npm update -g
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
echo 'export NPM_CONFIG_PREFIX=~/.npm-global' >> ~/.bashrc
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 {
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f "$rootdir/usr/local/bin/node" ]; then
    if [ ! -d "$rootdir/root/.npm-global" ]; then
        mkdir "$rootdir/root/.npm-global"
    $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"
    if ! grep -q "NPM_CONFIG_PREFIX=" "$rootdir/root/.bashrc"; then
        echo "export NPM_CONFIG_PREFIX=/root/.npm-global" >> "$rootdir/root/.bashrc"
    fi
Bob Mottram's avatar
Bob Mottram committed
    # shellcheck disable=SC2086
    if [ ! "$rootdir" ]; then
        $mesh_install_nodejs_prefix export PATH=/root/.npm-global/bin:$PATH
        $mesh_install_nodejs_prefix export NPM_CONFIG_PREFIX=/root/.npm-global
    fi
Bob Mottram's avatar
Bob Mottram committed
function remove_nodejs {
    if [ ! "$1" ]; then
Bob Mottram's avatar
Bob Mottram committed
        return
    fi
    if [ ! -f "$NODEJS_INSTALLED_APPS_FILE" ]; then
        #remove_app nodejs
Bob Mottram's avatar
Bob Mottram committed
        return
    fi
    sed -i "/install_${1}/d" "$NODEJS_INSTALLED_APPS_FILE"
    if ! grep -q "install_" "$NODEJS_INSTALLED_APPS_FILE"; then
        $REMOVE_PACKAGES_PURGE nodejs
Bob Mottram's avatar
Bob Mottram committed

        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
        if [ -d /usr/lib/node_modules ]; then
            rm -rf /usr/lib/node_modules
        fi
Bob Mottram's avatar
Bob Mottram committed
        if [ -f /usr/bin/n ]; then
            rm /usr/bin/n
        fi
Bob Mottram's avatar
Bob Mottram committed

        remove_app nodejs

        rm "$NODEJS_INSTALLED_APPS_FILE"
Bob Mottram's avatar
Bob Mottram committed
        $REMOVE_UNUSED_PACKAGES
Bob Mottram's avatar
Bob Mottram committed
    fi
}

Bob Mottram's avatar
Bob Mottram committed
function upgrade_nodejs {
Bob Mottram's avatar
Bob Mottram committed
    CURR_NODE_VERSION=$(node --version)
    CURR_NPM_VERSION=$(npm --version)
    CURR_N_VERSION=$(n --version)
    if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
        npm upgrade -g npm@${NPM_VERSION} --save
Bob Mottram's avatar
Bob Mottram committed
    fi
    if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
        npm upgrade -g n@${NODEJS_N_VERSION} --save
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
        n ${NODEJS_VERSION}
Bob Mottram's avatar
Bob Mottram committed
    fi
    cp /usr/local/bin/n /usr/bin/n
Bob Mottram's avatar
Bob Mottram committed
    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
Bob Mottram's avatar
Bob Mottram committed
}

function install_nodejs {
    if [ "$INSTALLING_MESH" ]; then
        mesh_install_nodejs
        return
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
    if [ ! "$ARCHITECTURE" ]; then
        ARCHITECTURE=$(uname -m)
    rootdir=
    mesh_install_nodejs
Bob Mottram's avatar
Bob Mottram committed
    # 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"
    fi
    if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
        echo $"Expected n version $NODEJS_N_VERSION but actually have $CURR_N_VERSION"
    fi
    if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
        echo $"Expected node version $NODEJS_VERSION but actually have $CURR_NODE_VERSION"
Bob Mottram's avatar
Bob Mottram committed
    fi

    if [ "$1" ]; then
        if ! grep -q "install_${1}" "$NODEJS_INSTALLED_APPS_FILE"; then
            echo "install_${1}" >> "$NODEJS_INSTALLED_APPS_FILE"
Bob Mottram's avatar
Bob Mottram committed
        fi
    fi

Bob Mottram's avatar
Bob Mottram committed
    mark_completed "${FUNCNAME[0]}"
}

# NOTE: deliberately there is no "exit 0"