freedombone-utils-nodejs 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # nodejs functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2017 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. # For reasons unknown we initially have to upgrade to an intermediate version
  31. # before getting to the version we want
  32. VARIANTS='mesh'
  33. # change these versions at your peril. Things will often crash if you don't
  34. # have specifically the correct versions
  35. NODEJS_VERSION='6.11.3'
  36. NODEJS_N_VERSION='2.1.7'
  37. NPM_VERSION='4.0.5'
  38. # This file keeps track of the apps needing nodejs
  39. # so that it can be removed if tere are no apps which need it
  40. NODEJS_INSTALLED_APPS_FILE=$HOME/.nodejs-apps
  41. function get_npm_arch {
  42. N_ARCH='x86'
  43. NPM_ARCH='ia32'
  44. if [[ $ARCHITECTURE == 'arm'* ]]; then
  45. NPM_ARCH='armv7l'
  46. N_ARCH='arm'
  47. fi
  48. if [[ $ARCHITECTURE == *"aarch"* ]]; then
  49. NPM_ARCH='arm64'
  50. N_ARCH='arm64'
  51. fi
  52. if [[ $ARCHITECTURE == 'x86_64' || $ARCHITECTURE == 'amd64' ]]; then
  53. NPM_ARCH='x64'
  54. N_ARCH='x64'
  55. fi
  56. }
  57. function mesh_install_nodejs {
  58. mesh_install_nodejs_prefix=
  59. if [ $rootdir ]; then
  60. mesh_install_nodejs_prefix="chroot $rootdir"
  61. fi
  62. $mesh_install_nodejs_prefix apt-get -yq install g++ m4 libtool automake nodejs curl
  63. $mesh_install_nodejs_prefix apt-get -yq install libxext-dev libxtst-dev libxkbfile-dev
  64. if [ ! -f ${rootdir}/usr/bin/nodejs ]; then
  65. echo $'nodejs was not installed'
  66. exit 63962
  67. fi
  68. if [ -f $rootdir/usr/bin/nodejs ]; then
  69. cp $rootdir/usr/bin/nodejs $rootdir/usr/bin/node
  70. fi
  71. if [ ! -f $rootdir/usr/bin/node ]; then
  72. echo $'/usr/bin/node not found'
  73. exit 7235728
  74. fi
  75. wget https://www.npmjs.com/install.sh -O $rootdir/root/npm_install.sh
  76. if [ ! -f $rootdir/root/npm_install.sh ]; then
  77. echo $'Unable to download npm installer'
  78. exit 8793636
  79. fi
  80. $mesh_install_nodejs_prefix chmod +x /root/npm_install.sh
  81. sed -i "s|t=\"\${npm_install}\"|t=\"$NPM_VERSION\"|g" $rootdir/root/npm_install.sh
  82. $mesh_install_nodejs_prefix /root/npm_install.sh
  83. if [ ! -f $rootdir/usr/bin/npm ]; then
  84. echo $'npm was not installed'
  85. exit 5290462
  86. fi
  87. cp $rootdir/usr/bin/npm $rootdir/root/npm
  88. # update from the old debian nodejs version
  89. get_npm_arch
  90. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g n@${NODEJS_N_VERSION} --save
  91. $mesh_install_nodejs_prefix n --arch $N_ARCH ${NODEJS_VERSION}
  92. cp $rootdir/root/npm $rootdir/usr/bin/npm
  93. }
  94. function remove_nodejs {
  95. if [ ! $1 ]; then
  96. return
  97. fi
  98. if [ ! -f $NODEJS_INSTALLED_APPS_FILE ]; then
  99. #remove_app nodejs
  100. return
  101. fi
  102. sed -i "/install_${1}/d" $NODEJS_INSTALLED_APPS_FILE
  103. if ! grep -q "install_" $NODEJS_INSTALLED_APPS_FILE; then
  104. apt-get -yq remove --purge nodejs
  105. if [ -f /usr/bin/nodejs ]; then
  106. rm /usr/bin/nodejs
  107. fi
  108. if [ -f /usr/local/bin/node ]; then
  109. rm /usr/local/bin/node
  110. fi
  111. if [ -f /usr/bin/node ]; then
  112. rm /usr/bin/node
  113. fi
  114. if [ -d /usr/lib/node_modules ]; then
  115. rm -rf /usr/lib/node_modules
  116. fi
  117. if [ -f /usr/bin/n ]; then
  118. rm /usr/bin/n
  119. fi
  120. remove_app nodejs
  121. rm $NODEJS_INSTALLED_APPS_FILE
  122. apt-get -yq autoremove
  123. fi
  124. }
  125. function upgrade_nodejs {
  126. CURR_NODE_VERSION=$(node --version)
  127. CURR_NPM_VERSION=$(npm --version)
  128. CURR_N_VERSION=$(n --version)
  129. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  130. npm upgrade -g npm@${NPM_VERSION} --save
  131. fi
  132. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  133. npm upgrade -g n@${NODEJS_N_VERSION} --save
  134. fi
  135. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  136. n {NODEJS_VERSION}
  137. fi
  138. cp /usr/local/bin/n /usr/bin/n
  139. if [ -f /usr/local/bin/npm ]; then
  140. cp /usr/local/bin/npm /usr/bin/npm
  141. fi
  142. if [ -f /usr/local/bin/node ]; then
  143. cp /usr/local/bin/node /usr/bin/nodejs
  144. cp /usr/local/bin/node /usr/bin/node
  145. fi
  146. }
  147. function install_nodejs {
  148. if [ $INSTALLING_MESH ]; then
  149. mesh_install_nodejs
  150. return
  151. fi
  152. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  153. upgrade_nodejs
  154. return
  155. fi
  156. if [ ! $ARCHITECTURE ]; then
  157. ARCHITECTURE=$(uname -m)
  158. fi
  159. rootdir=
  160. mesh_install_nodejs
  161. #if [ ! -f /usr/bin/nodejs ]; then
  162. # Note: this has to be jessie for now
  163. # wget -qO- https://deb.nodesource.com/gpgkey/nodesource.gpg.key > /root/node.gpg.key
  164. # apt-key add /root/node.gpg.key
  165. # echo "deb https://deb.nodesource.com/node_6.x stretch main" > /etc/apt/sources.list.d/nodesource.list
  166. # echo "deb-src https://deb.nodesource.com/node_6.x stretch main" >> /etc/apt/sources.list.d/nodesource.list
  167. # apt-get update
  168. # apt-get -yq remove --purge nodejs
  169. # if [ -d /usr/local/lib/node_modules ]; then
  170. # rm -rf /usr/local/lib/node_modules
  171. # fi
  172. # if [ -f /usr/local/bin/node ]; then
  173. # rm /usr/local/bin/node
  174. # fi
  175. # if [ -f /usr/bin/node ]; then
  176. # rm /usr/bin/node
  177. # fi
  178. # if [ -f /usr/bin/nodejs ]; then
  179. # rm /usr/bin/nodejs
  180. # fi
  181. # apt-get -yq install nodejs
  182. # apt-get -yq install curl
  183. # if [ ! -f /usr/bin/nodejs ]; then
  184. # echo $'nodejs was not installed'
  185. # exit 63962
  186. # fi
  187. #fi
  188. #npm install -g npm@${NPM_VERSION} --save
  189. #npm install -g n@${NODEJS_N_VERSION} --save
  190. #n ${NODEJS_VERSION}
  191. #npm install -g pug@2.0.0-beta6 --save
  192. #npm install -g graceful-fs@4.1.10 --save
  193. #npm install -g minimatch@3.0.3 --save
  194. npm install -g npm@${NPM_VERSION} --save
  195. #cp /usr/local/bin/node /usr/bin/nodejs
  196. if [ -f /usr/local/bin/npm ]; then
  197. cp /usr/local/bin/npm /usr/bin/npm
  198. fi
  199. # node seems tricky so here we're going to double check
  200. # that the versions we expect did get installed
  201. CURR_NODE_VERSION=$(node --version)
  202. CURR_NPM_VERSION=$(npm --version)
  203. CURR_N_VERSION=$(n --version)
  204. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  205. echo $"Expected npm version $NPM_VERSION but actually have $CURR_NPM_VERSION"
  206. exit 6728252
  207. fi
  208. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  209. echo $"Expected n version $NODEJS_N_VERSION but actually have $CURR_N_VERSION"
  210. exit 5267452
  211. fi
  212. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  213. echo $"Expected node version $NODEJS_VERSION but actually have $CURR_NODE_VERSION"
  214. exit 5274527
  215. fi
  216. if [ $1 ]; then
  217. if ! grep -q "install_${1}" $NODEJS_INSTALLED_APPS_FILE; then
  218. echo "install_${1}" >> $NODEJS_INSTALLED_APPS_FILE
  219. fi
  220. fi
  221. mark_completed $FUNCNAME
  222. }
  223. # NOTE: deliberately there is no "exit 0"