freedombone-utils-nodejs 8.2KB

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