freedombone-utils-nodejs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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='8.11.1'
  36. NODEJS_N_VERSION='2.1.7'
  37. NPM_VERSION='5.6.0'
  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. NODE_UPGRADE=
  69. exit 6389252
  70. fi
  71. $mesh_install_nodejs_prefix apt-key add /root/node.gpg.key
  72. echo "deb https://deb.nodesource.com/node_8.x stretch main" > "$rootdir/etc/apt/sources.list.d/nodesource.list"
  73. echo "deb-src https://deb.nodesource.com/node_8.x stretch main" >> "$rootdir/etc/apt/sources.list.d/nodesource.list"
  74. $mesh_install_nodejs_prefix apt-mark -q unhold nodejs
  75. $mesh_install_nodejs_prefix apt-get update
  76. $mesh_install_nodejs_prefix apt-get -yq remove --purge nodejs
  77. if [ ! $NODE_UPGRADE ]; then
  78. if [ -d "$rootdir/usr/local/lib/node_modules" ]; then
  79. rm -rf "$rootdir/usr/local/lib/node_modules"
  80. fi
  81. fi
  82. if [ -f "$rootdir/usr/local/bin/node" ]; then
  83. rm "$rootdir/usr/local/bin/node"
  84. fi
  85. if [ -f "$rootdir/usr/bin/node" ]; then
  86. rm "$rootdir/usr/bin/node"
  87. fi
  88. if [ -f "$rootdir/usr/bin/nodejs" ]; then
  89. rm "$rootdir/usr/bin/nodejs"
  90. fi
  91. $mesh_install_nodejs_prefix apt-get -yq install nodejs
  92. if [ -f "$rootdir/usr/bin/nodejs" ]; then
  93. cp "$rootdir/usr/bin/nodejs" "$rootdir/usr/bin/node"
  94. fi
  95. if [ -f "$rootdir/usr/bin/node" ]; then
  96. cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
  97. fi
  98. $mesh_install_nodejs_prefix apt-mark -q hold nodejs
  99. if [ ! -f "${rootdir}/usr/bin/node" ]; then
  100. if [ ! -f "${rootdir}/usr/local/bin/node" ]; then
  101. if [ ! -f "${rootdir}/usr/bin/nodejs" ]; then
  102. echo $'nodejs was not installed'
  103. NODE_UPGRADE=
  104. exit 63962
  105. fi
  106. fi
  107. fi
  108. if [ ! -f "$rootdir/usr/bin/node" ]; then
  109. echo $'/usr/bin/node not found'
  110. NODE_UPGRADE=
  111. exit 7235728
  112. fi
  113. get_npm_arch
  114. $mesh_install_nodejs_prefix npm config set unsafe-perm true
  115. if [ -f "$rootdir/usr/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js" ]; then
  116. # fix for failing cpu detection during image build with qemu, see https://github.com/npm/npm/issues/19265
  117. sed -i "s|require('os').cpus().length|1|g" "$rootdir/usr/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js"
  118. fi
  119. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g npm@${NPM_VERSION} --save
  120. if [ -f "$rootdir/usr/local/bin/npm" ]; then
  121. cp "$rootdir/usr/local/bin/npm" /usr/bin/npm
  122. fi
  123. cp "$rootdir/usr/bin/npm" "$rootdir/root/npm"
  124. # update from the old debian nodejs version
  125. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g n@${NODEJS_N_VERSION} --save
  126. $mesh_install_nodejs_prefix n --arch $N_ARCH ${NODEJS_VERSION}
  127. cp "$rootdir/root/npm" "$rootdir/usr/bin/npm"
  128. # deliberate second install of npm
  129. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g npm@${NPM_VERSION} --save
  130. if [ -f "$rootdir/usr/local/bin/npm" ]; then
  131. cp "$rootdir/usr/local/bin/npm" /usr/bin/npm
  132. fi
  133. cp "$rootdir/usr/bin/npm" "$rootdir/root/npm"
  134. if [ -f "$rootdir/usr/bin/node" ]; then
  135. cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
  136. fi
  137. # check the version numbers
  138. cat <<EOF > "$rootdir/usr/bin/test_nodejs_install"
  139. #!/bin/bash
  140. node_version=\$(node -v)
  141. if [[ "\$node_version" != "v${NODEJS_VERSION}" ]]; then
  142. echo $"nodejs version expected to be ${NODEJS_VERSION} but found \$node_version"
  143. exit 1
  144. fi
  145. npm_version=\$(npm -v)
  146. if [[ "\$npm_version" != "${NPM_VERSION}" ]]; then
  147. echo $"npm version expected to be ${NPM_VERSION} but found \$npm_version"
  148. exit 2
  149. fi
  150. EOF
  151. chmod +x "$rootdir/usr/bin/test_nodejs_install"
  152. if ! $mesh_install_nodejs_prefix /usr/bin/test_nodejs_install; then
  153. echo $"nodejs version numbers did not match. Architecture is $NPM_ARCH."
  154. NODE_UPGRADE=
  155. exit 76835282
  156. fi
  157. rm "$rootdir/usr/bin/test_nodejs_install"
  158. NODE_UPGRADE=
  159. nodejs_setup_global_modules
  160. }
  161. function nodejs_upgrade {
  162. if [ ! -f /etc/apt/sources.list.d/nodesource.list ]; then
  163. return
  164. fi
  165. nodejs_setup_global_modules
  166. if grep -q "node_8.x" /etc/apt/sources.list.d/nodesource.list; then
  167. if [ -f /usr/local/bin/node ]; then
  168. CURR_NODE_VERSION=$(node --version)
  169. if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
  170. return
  171. fi
  172. fi
  173. fi
  174. if [ -f /usr/local/bin/node ]; then
  175. CURR_NODE_VERSION=$(node --version)
  176. if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
  177. return
  178. fi
  179. fi
  180. read_config_param ARCHITECTURE
  181. get_npm_arch
  182. NODE_UPGRADE=1
  183. rootdir=
  184. mesh_install_nodejs
  185. npm update -g
  186. }
  187. function nodejs_setup_global_modules {
  188. if [ ! -f /usr/local/bin/node ]; then
  189. return
  190. fi
  191. if [ ! -d "$rootdir/root/.npm-global" ]; then
  192. mkdir "$rootdir/root/.npm-global"
  193. fi
  194. $mesh_install_nodejs_prefix npm config set prefix '/root/.npm-global'
  195. if ! grep -q "PATH=/root/.npm-global/bin" "$rootdir/root/.bashrc"; then
  196. echo "PATH=/root/.npm-global/bin:\$PATH" >> "$rootdir/root/.bashrc"
  197. fi
  198. if ! grep -q "NPM_CONFIG_PREFIX=" "$rootdir/root/.bashrc"; then
  199. echo "export NPM_CONFIG_PREFIX=/root/.npm-global" >> "$rootdir/root/.bashrc"
  200. fi
  201. if [ ! "$rootdir" ]; then
  202. export PATH=/root/.npm-global/bin:$PATH
  203. export NPM_CONFIG_PREFIX=/root/.npm-global
  204. fi
  205. }
  206. function remove_nodejs {
  207. if [ ! "$1" ]; then
  208. return
  209. fi
  210. if [ ! -f "$NODEJS_INSTALLED_APPS_FILE" ]; then
  211. #remove_app nodejs
  212. return
  213. fi
  214. sed -i "/install_${1}/d" "$NODEJS_INSTALLED_APPS_FILE"
  215. if ! grep -q "install_" "$NODEJS_INSTALLED_APPS_FILE"; then
  216. apt-get -yq remove --purge nodejs
  217. if [ -f /usr/bin/nodejs ]; then
  218. rm /usr/bin/nodejs
  219. fi
  220. if [ -f /usr/local/bin/node ]; then
  221. rm /usr/local/bin/node
  222. fi
  223. if [ -f /usr/bin/node ]; then
  224. rm /usr/bin/node
  225. fi
  226. if [ -d /usr/lib/node_modules ]; then
  227. rm -rf /usr/lib/node_modules
  228. fi
  229. if [ -f /usr/bin/n ]; then
  230. rm /usr/bin/n
  231. fi
  232. remove_app nodejs
  233. rm "$NODEJS_INSTALLED_APPS_FILE"
  234. apt-get -yq autoremove
  235. fi
  236. }
  237. function upgrade_nodejs {
  238. CURR_NODE_VERSION=$(node --version)
  239. CURR_NPM_VERSION=$(npm --version)
  240. CURR_N_VERSION=$(n --version)
  241. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  242. npm upgrade -g npm@${NPM_VERSION} --save
  243. fi
  244. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  245. npm upgrade -g n@${NODEJS_N_VERSION} --save
  246. fi
  247. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  248. n ${NODEJS_VERSION}
  249. fi
  250. cp /usr/local/bin/n /usr/bin/n
  251. if [ -f /usr/local/bin/npm ]; then
  252. cp /usr/local/bin/npm /usr/bin/npm
  253. fi
  254. if [ -f /usr/local/bin/node ]; then
  255. cp /usr/local/bin/node /usr/bin/nodejs
  256. cp /usr/local/bin/node /usr/bin/node
  257. fi
  258. }
  259. function install_nodejs {
  260. if [ "$INSTALLING_MESH" ]; then
  261. mesh_install_nodejs
  262. return
  263. fi
  264. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  265. upgrade_nodejs
  266. return
  267. fi
  268. if [ ! "$ARCHITECTURE" ]; then
  269. ARCHITECTURE=$(uname -m)
  270. fi
  271. rootdir=
  272. mesh_install_nodejs
  273. # verify nodejs versions are what we expect
  274. CURR_NODE_VERSION=$(node --version)
  275. CURR_NPM_VERSION=$(npm --version)
  276. CURR_N_VERSION=$(n --version)
  277. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  278. echo $"Expected npm version $NPM_VERSION but actually have $CURR_NPM_VERSION"
  279. exit 6728252
  280. fi
  281. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  282. echo $"Expected n version $NODEJS_N_VERSION but actually have $CURR_N_VERSION"
  283. exit 5267452
  284. fi
  285. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  286. echo $"Expected node version $NODEJS_VERSION but actually have $CURR_NODE_VERSION"
  287. exit 5274527
  288. fi
  289. if [ "$1" ]; then
  290. if ! grep -q "install_${1}" "$NODEJS_INSTALLED_APPS_FILE"; then
  291. echo "install_${1}" >> "$NODEJS_INSTALLED_APPS_FILE"
  292. fi
  293. fi
  294. mark_completed "${FUNCNAME[0]}"
  295. }
  296. # NOTE: deliberately there is no "exit 0"