| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 | 
							- #!/bin/bash
 - #
 - # .---.                  .              .
 - # |                      |              |
 - # |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
 - # |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
 - # '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
 - #
 - #                    Freedom in the Cloud
 - #
 - # nodejs functions
 - #
 - # License
 - # =======
 - #
 - # Copyright (C) 2014-2017 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
 - NODEJS_VERSION='6.11.4'
 - NODEJS_N_VERSION='2.1.8'
 - NPM_VERSION='4.0.5'
 - 
 - # 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 {
 -     N_ARCH='x86'
 -     NPM_ARCH='ia32'
 -     if [[ $ARCHITECTURE == 'arm'* ]]; then
 -         NPM_ARCH='armv7l'
 -         N_ARCH='armv7l'
 -     fi
 -     if [[ $ARCHITECTURE == *"aarch"* ]]; then
 -         NPM_ARCH='arm64'
 -         N_ARCH='arm64'
 -     fi
 -     if [[ $ARCHITECTURE == 'x86_64' || $ARCHITECTURE == 'amd64' ]]; then
 -         NPM_ARCH='x64'
 -         N_ARCH='x64'
 -     fi
 - }
 - 
 - function mesh_install_nodejs {
 -     mesh_install_nodejs_prefix=
 -     if [ $rootdir ]; then
 -         if [[ $VARIANT == "mesh" ]]; then
 -             return
 -         fi
 -         mesh_install_nodejs_prefix="chroot $rootdir"
 -     fi
 - 
 -     $mesh_install_nodejs_prefix apt-get -yq install wget curl g++ m4 libtool automake
 -     $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
 -     $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'
 -         exit 6389252
 -     fi
 -     $mesh_install_nodejs_prefix apt-key add /root/node.gpg.key
 -     echo "deb https://deb.nodesource.com/node_6.x stretch main" > $rootdir/etc/apt/sources.list.d/nodesource.list
 -     echo "deb-src https://deb.nodesource.com/node_6.x stretch main" >> $rootdir/etc/apt/sources.list.d/nodesource.list
 - 
 -     $mesh_install_nodejs_prefix apt-get update
 -     $mesh_install_nodejs_prefix apt-get -yq remove --purge nodejs
 - 
 -     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 /usr/bin/node
 -     fi
 -     if [ -f $rootdir/usr/bin/nodejs ]; then
 -         rm $rootdir/usr/bin/nodejs
 -     fi
 - 
 -     $mesh_install_nodejs_prefix apt-get -yq install 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
 -         if [ ! -f ${rootdir}/usr/local/bin/node ]; then
 -             if [ ! -f ${rootdir}/usr/bin/nodejs ]; then
 -                 echo $'nodejs was not installed'
 -                 exit 63962
 -             fi
 -         fi
 -     fi
 - 
 -     if [ ! -f $rootdir/usr/bin/node ]; then
 -         echo $'/usr/bin/node not found'
 -         exit 7235728
 -     fi
 - 
 -     #$mesh_install_nodejs_prefix wget https://www.npmjs.com/install.sh -O /root/npm_install.sh
 -     #if [ ! -f $rootdir/root/npm_install.sh ]; then
 -     #    echo $'Unable to download npm installer'
 -     #    exit 8793636
 -     #fi
 -     #if ! grep -q "t=\"\${npm_install}\"" $rootdir/root/npm_install.sh; then
 -     #    echo $'Unable to set npm version within npm_install.sh'
 -     #    exit 629052
 -     #fi
 -     #$mesh_install_nodejs_prefix chmod +x /root/npm_install.sh
 -     #$mesh_install_nodejs_prefix sed -i "s|t=\"\${npm_install}\"|t=\"$NPM_VERSION\"|g" /root/npm_install.sh
 -     #$mesh_install_nodejs_prefix /root/npm_install.sh
 -     #
 -     #if [ ! -f $rootdir/usr/bin/npm ]; then
 -     #    echo $'npm was not installed'
 -     #    exit 5290462
 -     #fi
 -     #cp $rootdir/usr/bin/npm $rootdir/root/npm
 - 
 -     get_npm_arch
 - 
 -     $mesh_install_nodejs_prefix npm config set unsafe-perm true
 -     $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 /usr/bin/npm
 -     fi
 -     cp $rootdir/usr/bin/npm $rootdir/root/npm
 - 
 -     # update from the old debian nodejs version
 -     $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g n@${NODEJS_N_VERSION} --save
 -     $mesh_install_nodejs_prefix n --arch $N_ARCH ${NODEJS_VERSION}
 -     cp $rootdir/root/npm $rootdir/usr/bin/npm
 - }
 - 
 - function remove_nodejs {
 -     if [ ! $1 ]; then
 -         return
 -     fi
 -     if [ ! -f $NODEJS_INSTALLED_APPS_FILE ]; then
 -         #remove_app nodejs
 -         return
 -     fi
 -     sed -i "/install_${1}/d" $NODEJS_INSTALLED_APPS_FILE
 -     if ! grep -q "install_" $NODEJS_INSTALLED_APPS_FILE; then
 -         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
 -         if [ -d /usr/lib/node_modules ]; then
 -             rm -rf /usr/lib/node_modules
 -         fi
 -         if [ -f /usr/bin/n ]; then
 -             rm /usr/bin/n
 -         fi
 - 
 -         remove_app nodejs
 - 
 -         rm $NODEJS_INSTALLED_APPS_FILE
 - 
 -         apt-get -yq autoremove
 -     fi
 - }
 - 
 - 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
 -         npm upgrade -g npm@${NPM_VERSION} --save
 -     fi
 -     if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
 -         npm upgrade -g n@${NODEJS_N_VERSION} --save
 -     fi
 -     if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
 -         n {NODEJS_VERSION}
 -     fi
 -     cp /usr/local/bin/n /usr/bin/n
 -     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
 - }
 - 
 - function install_nodejs {
 -     if [ $INSTALLING_MESH ]; then
 -         mesh_install_nodejs
 -         return
 -     fi
 -     if [[ $(is_completed $FUNCNAME) == "1" ]]; then
 -         upgrade_nodejs
 -         return
 -     fi
 - 
 -     if [ ! $ARCHITECTURE ]; then
 -         ARCHITECTURE=$(uname -m)
 -     fi
 -     rootdir=
 -     mesh_install_nodejs
 - 
 - 
 -     #if [ ! -f /usr/bin/nodejs ]; then
 -     #    wget -qO- https://deb.nodesource.com/gpgkey/nodesource.gpg.key > /root/node.gpg.key
 -     #    apt-key add /root/node.gpg.key
 -     #    echo "deb https://deb.nodesource.com/node_6.x stretch main" > /etc/apt/sources.list.d/nodesource.list
 -     #    echo "deb-src https://deb.nodesource.com/node_6.x stretch main" >> /etc/apt/sources.list.d/nodesource.list
 - 
 -     #    apt-get update
 - 
 -     #    apt-get -yq remove --purge nodejs
 - 
 -     #    if [ -d /usr/local/lib/node_modules ]; then
 -     #        rm -rf /usr/local/lib/node_modules
 -     #    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 [ -f /usr/bin/nodejs ]; then
 -     #        rm /usr/bin/nodejs
 -     #    fi
 - 
 -     #    apt-get -yq install nodejs
 -     #    apt-get -yq install curl
 - 
 -     #    if [ ! -f /usr/bin/nodejs ]; then
 -     #        echo $'nodejs was not installed'
 -     #        exit 63962
 -     #    fi
 -     #fi
 - 
 -     #npm install -g npm@${NPM_VERSION} --save
 -     #npm install -g n@${NODEJS_N_VERSION} --save
 -     #n ${NODEJS_VERSION}
 -     #npm install -g pug@2.0.0-beta6 --save
 -     #npm install -g graceful-fs@4.1.10 --save
 -     #npm install -g minimatch@3.0.3 --save
 - 
 -     # node seems tricky so here we're going to double check
 -     # that the versions we expect did get installed
 -     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
 -     fi
 - 
 -     if [ $1 ]; then
 -         if ! grep -q "install_${1}" $NODEJS_INSTALLED_APPS_FILE; then
 -             echo "install_${1}" >> $NODEJS_INSTALLED_APPS_FILE
 -         fi
 -     fi
 - 
 -     mark_completed $FUNCNAME
 - }
 - 
 - # NOTE: deliberately there is no "exit 0"
 
 
  |