freedombone-utils-nodejs 7.2KB

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