freedombone-utils-nodejs 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. if [[ $VARIANT == "mesh" ]]; then
  61. return
  62. fi
  63. mesh_install_nodejs_prefix="chroot $rootdir"
  64. fi
  65. $mesh_install_nodejs_prefix apt-get -yq install g++ m4 libtool automake nodejs curl
  66. $mesh_install_nodejs_prefix apt-get -yq install libxext-dev libxtst-dev libxkbfile-dev
  67. if [ ! -f ${rootdir}/usr/bin/nodejs ]; then
  68. echo $'nodejs was not installed'
  69. exit 63962
  70. fi
  71. if [ -f $rootdir/usr/bin/nodejs ]; then
  72. cp $rootdir/usr/bin/nodejs $rootdir/usr/bin/node
  73. fi
  74. if [ ! -f $rootdir/usr/bin/node ]; then
  75. echo $'/usr/bin/node not found'
  76. exit 7235728
  77. fi
  78. $mesh_install_nodejs_prefix apt-get -qy install wget
  79. $mesh_install_nodejs_prefix wget https://www.npmjs.com/install.sh -O /root/npm_install.sh
  80. if [ ! -f $rootdir/root/npm_install.sh ]; then
  81. echo $'Unable to download npm installer'
  82. exit 8793636
  83. fi
  84. if ! grep -q "t=\"\${npm_install}\"" $rootdir/root/npm_install.sh; then
  85. echo $'Unable to set npm version within npm_install.sh'
  86. exit 629052
  87. fi
  88. $mesh_install_nodejs_prefix chmod +x /root/npm_install.sh
  89. $mesh_install_nodejs_prefix sed -i "s|t=\"\${npm_install}\"|t=\"$NPM_VERSION\"|g" /root/npm_install.sh
  90. $mesh_install_nodejs_prefix /root/npm_install.sh
  91. if [ ! -f $rootdir/usr/bin/npm ]; then
  92. echo $'npm was not installed'
  93. exit 5290462
  94. fi
  95. cp $rootdir/usr/bin/npm $rootdir/root/npm
  96. # update from the old debian nodejs version
  97. get_npm_arch
  98. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g n@${NODEJS_N_VERSION} --save
  99. $mesh_install_nodejs_prefix n --arch $N_ARCH ${NODEJS_VERSION}
  100. cp $rootdir/root/npm $rootdir/usr/bin/npm
  101. }
  102. function remove_nodejs {
  103. if [ ! $1 ]; then
  104. return
  105. fi
  106. if [ ! -f $NODEJS_INSTALLED_APPS_FILE ]; then
  107. #remove_app nodejs
  108. return
  109. fi
  110. sed -i "/install_${1}/d" $NODEJS_INSTALLED_APPS_FILE
  111. if ! grep -q "install_" $NODEJS_INSTALLED_APPS_FILE; then
  112. apt-get -yq remove --purge nodejs
  113. if [ -f /usr/bin/nodejs ]; then
  114. rm /usr/bin/nodejs
  115. fi
  116. if [ -f /usr/local/bin/node ]; then
  117. rm /usr/local/bin/node
  118. fi
  119. if [ -f /usr/bin/node ]; then
  120. rm /usr/bin/node
  121. fi
  122. if [ -d /usr/lib/node_modules ]; then
  123. rm -rf /usr/lib/node_modules
  124. fi
  125. if [ -f /usr/bin/n ]; then
  126. rm /usr/bin/n
  127. fi
  128. remove_app nodejs
  129. rm $NODEJS_INSTALLED_APPS_FILE
  130. apt-get -yq autoremove
  131. fi
  132. }
  133. function upgrade_nodejs {
  134. CURR_NODE_VERSION=$(node --version)
  135. CURR_NPM_VERSION=$(npm --version)
  136. CURR_N_VERSION=$(n --version)
  137. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  138. npm upgrade -g npm@${NPM_VERSION} --save
  139. fi
  140. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  141. npm upgrade -g n@${NODEJS_N_VERSION} --save
  142. fi
  143. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  144. n {NODEJS_VERSION}
  145. fi
  146. cp /usr/local/bin/n /usr/bin/n
  147. if [ -f /usr/local/bin/npm ]; then
  148. cp /usr/local/bin/npm /usr/bin/npm
  149. fi
  150. if [ -f /usr/local/bin/node ]; then
  151. cp /usr/local/bin/node /usr/bin/nodejs
  152. cp /usr/local/bin/node /usr/bin/node
  153. fi
  154. }
  155. function install_nodejs {
  156. if [ $INSTALLING_MESH ]; then
  157. mesh_install_nodejs
  158. return
  159. fi
  160. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  161. upgrade_nodejs
  162. return
  163. fi
  164. if [ ! $ARCHITECTURE ]; then
  165. ARCHITECTURE=$(uname -m)
  166. fi
  167. rootdir=
  168. mesh_install_nodejs
  169. #if [ ! -f /usr/bin/nodejs ]; then
  170. # Note: this has to be jessie for now
  171. # wget -qO- https://deb.nodesource.com/gpgkey/nodesource.gpg.key > /root/node.gpg.key
  172. # apt-key add /root/node.gpg.key
  173. # echo "deb https://deb.nodesource.com/node_6.x stretch main" > /etc/apt/sources.list.d/nodesource.list
  174. # echo "deb-src https://deb.nodesource.com/node_6.x stretch main" >> /etc/apt/sources.list.d/nodesource.list
  175. # apt-get update
  176. # apt-get -yq remove --purge nodejs
  177. # if [ -d /usr/local/lib/node_modules ]; then
  178. # rm -rf /usr/local/lib/node_modules
  179. # fi
  180. # if [ -f /usr/local/bin/node ]; then
  181. # rm /usr/local/bin/node
  182. # fi
  183. # if [ -f /usr/bin/node ]; then
  184. # rm /usr/bin/node
  185. # fi
  186. # if [ -f /usr/bin/nodejs ]; then
  187. # rm /usr/bin/nodejs
  188. # fi
  189. # apt-get -yq install nodejs
  190. # apt-get -yq install curl
  191. # if [ ! -f /usr/bin/nodejs ]; then
  192. # echo $'nodejs was not installed'
  193. # exit 63962
  194. # fi
  195. #fi
  196. #npm install -g npm@${NPM_VERSION} --save
  197. #npm install -g n@${NODEJS_N_VERSION} --save
  198. #n ${NODEJS_VERSION}
  199. #npm install -g pug@2.0.0-beta6 --save
  200. #npm install -g graceful-fs@4.1.10 --save
  201. #npm install -g minimatch@3.0.3 --save
  202. npm install -g npm@${NPM_VERSION} --save
  203. #cp /usr/local/bin/node /usr/bin/nodejs
  204. if [ -f /usr/local/bin/npm ]; then
  205. cp /usr/local/bin/npm /usr/bin/npm
  206. fi
  207. # node seems tricky so here we're going to double check
  208. # that the versions we expect did get installed
  209. CURR_NODE_VERSION=$(node --version)
  210. CURR_NPM_VERSION=$(npm --version)
  211. CURR_N_VERSION=$(n --version)
  212. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  213. echo $"Expected npm version $NPM_VERSION but actually have $CURR_NPM_VERSION"
  214. exit 6728252
  215. fi
  216. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  217. echo $"Expected n version $NODEJS_N_VERSION but actually have $CURR_N_VERSION"
  218. exit 5267452
  219. fi
  220. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  221. echo $"Expected node version $NODEJS_VERSION but actually have $CURR_NODE_VERSION"
  222. exit 5274527
  223. fi
  224. if [ $1 ]; then
  225. if ! grep -q "install_${1}" $NODEJS_INSTALLED_APPS_FILE; then
  226. echo "install_${1}" >> $NODEJS_INSTALLED_APPS_FILE
  227. fi
  228. fi
  229. mark_completed $FUNCNAME
  230. }
  231. # NOTE: deliberately there is no "exit 0"