freedombone-utils-nodejs 7.0KB

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