freedombone-utils-nodejs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # nodejs functions
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. # For reasons unknown we initially have to upgrade to an intermediate version
  29. # before getting to the version we want
  30. VARIANTS='mesh'
  31. # change these versions at your peril. Things will often crash if you don't
  32. # have specifically the correct versions
  33. NODEJS_VERSION='8.11.1'
  34. NODEJS_N_VERSION='2.1.7'
  35. NPM_VERSION='5.8.0'
  36. # This file keeps track of the apps needing nodejs
  37. # so that it can be removed if tere are no apps which need it
  38. NODEJS_INSTALLED_APPS_FILE=$HOME/.nodejs-apps
  39. function get_npm_arch {
  40. N_ARCH='x86'
  41. NPM_ARCH='ia32'
  42. if [[ $ARCHITECTURE == 'arm'* ]]; then
  43. NPM_ARCH='armv7l'
  44. N_ARCH='armv7l'
  45. fi
  46. if [[ $ARCHITECTURE == *"aarch"* ]]; then
  47. NPM_ARCH='arm64'
  48. N_ARCH='arm64'
  49. fi
  50. if [[ $ARCHITECTURE == 'x86_64' || $ARCHITECTURE == 'amd64' ]]; then
  51. NPM_ARCH='x64'
  52. N_ARCH='x64'
  53. fi
  54. }
  55. function nodejs_fix_cpu_detection {
  56. # fix for failing cpu detection during image build with qemu, see https://github.com/npm/npm/issues/19265
  57. if [ -f "$rootdir/usr/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js" ]; then
  58. sed -i "s/require('os').cpus().length/(require('os').cpus() || { length: 1 }).length/g" "$rootdir/usr/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js"
  59. fi
  60. if [ -f "$rootdir/usr/lib/node_modules/npm/node_modules/node-gyp/lib/build.js" ]; then
  61. sed -i "s/require('os').cpus().length/(require('os').cpus() || { length: 1 }).length/g" "$rootdir/usr/lib/node_modules/npm/node_modules/node-gyp/lib/build.js"
  62. fi
  63. if [ -f "$rootdir/usr/lib/node_modules/npm/node_modules/worker-farm/examples/pi/index.js" ]; then
  64. sed -i "s/require('os').cpus().length/(require('os').cpus() || { length: 1 }).length/g" "$rootdir/usr/lib/node_modules/npm/node_modules/worker-farm/examples/pi/index.js"
  65. fi
  66. if [ -f "$rootdir/.npm-global/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js" ]; then
  67. sed -i "s/require('os').cpus().length/(require('os').cpus() || { length: 1 }).length/g" "$rootdir/.npm-global/lib/node_modules/npm/node_modules/worker-farm/lib/farm.js"
  68. fi
  69. if [ -f "$rootdir/.npm-global/lib/node_modules/npm/node_modules/node-gyp/lib/build.js" ]; then
  70. sed -i "s/require('os').cpus().length/(require('os').cpus() || { length: 1 }).length/g" "$rootdir/.npm-global/lib/node_modules/npm/node_modules/node-gyp/lib/build.js"
  71. fi
  72. if [ -f "$rootdir/.npm-global/lib/node_modules/npm/node_modules/worker-farm/examples/pi/index.js" ]; then
  73. sed -i "s/require('os').cpus().length/(require('os').cpus() || { length: 1 }).length/g" "$rootdir/.npm-global/lib/node_modules/npm/node_modules/worker-farm/examples/pi/index.js"
  74. fi
  75. # installing worker farm fixes the cpu detection bug
  76. #$mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g worker-farm@1.6.0 --save
  77. }
  78. function mesh_install_nodejs {
  79. mesh_install_nodejs_prefix=
  80. if [ "$rootdir" ]; then
  81. mesh_install_nodejs_prefix="chroot $rootdir"
  82. fi
  83. $mesh_install_nodejs_prefix apt-get -yq install wget curl g++ m4 libtool automake
  84. $mesh_install_nodejs_prefix apt-get -yq install libxext-dev libxtst-dev libxkbfile-dev
  85. $mesh_install_nodejs_prefix apt-get -yq install apt-transport-https
  86. $mesh_install_nodejs_prefix wget https://deb.nodesource.com/gpgkey/nodesource.gpg.key -O /root/node.gpg.key
  87. if [ ! -f "$rootdir/root/node.gpg.key" ]; then
  88. echo $'Unable to obtain gpg key for nodejs repo'
  89. NODE_UPGRADE=
  90. exit 6389252
  91. fi
  92. $mesh_install_nodejs_prefix apt-key add /root/node.gpg.key
  93. echo "deb https://deb.nodesource.com/node_8.x stretch main" > "$rootdir/etc/apt/sources.list.d/nodesource.list"
  94. echo "deb-src https://deb.nodesource.com/node_8.x stretch main" >> "$rootdir/etc/apt/sources.list.d/nodesource.list"
  95. $mesh_install_nodejs_prefix apt-mark -q unhold nodejs
  96. $mesh_install_nodejs_prefix apt-get update
  97. $mesh_install_nodejs_prefix apt-get -yq remove --purge nodejs
  98. if [ ! $NODE_UPGRADE ]; then
  99. if [ -d "$rootdir/usr/local/lib/node_modules" ]; then
  100. rm -rf "$rootdir/usr/local/lib/node_modules"
  101. fi
  102. fi
  103. if [ -f "$rootdir/usr/local/bin/node" ]; then
  104. rm "$rootdir/usr/local/bin/node"
  105. fi
  106. if [ -f "$rootdir/usr/bin/node" ]; then
  107. rm "$rootdir/usr/bin/node"
  108. fi
  109. if [ -f "$rootdir/usr/bin/nodejs" ]; then
  110. rm "$rootdir/usr/bin/nodejs"
  111. fi
  112. $mesh_install_nodejs_prefix apt-get -yq install nodejs
  113. if [ -f "$rootdir/usr/bin/nodejs" ]; then
  114. cp "$rootdir/usr/bin/nodejs" "$rootdir/usr/bin/node"
  115. fi
  116. if [ -f "$rootdir/usr/bin/node" ]; then
  117. cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
  118. fi
  119. $mesh_install_nodejs_prefix apt-mark -q hold nodejs
  120. if [ ! -f "${rootdir}/usr/bin/node" ]; then
  121. if [ ! -f "${rootdir}/usr/local/bin/node" ]; then
  122. if [ ! -f "${rootdir}/usr/bin/nodejs" ]; then
  123. echo $'nodejs was not installed'
  124. NODE_UPGRADE=
  125. exit 63962
  126. fi
  127. fi
  128. fi
  129. if [ ! -f "$rootdir/usr/bin/node" ]; then
  130. echo $'/usr/bin/node not found'
  131. NODE_UPGRADE=
  132. exit 7235728
  133. fi
  134. get_npm_arch
  135. $mesh_install_nodejs_prefix npm config set unsafe-perm true
  136. nodejs_setup_global_modules
  137. nodejs_fix_cpu_detection
  138. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g npm@${NPM_VERSION} --save
  139. if [ -f "$rootdir/.npm-global/bin/npm" ]; then
  140. cp "$rootdir/.npm-global/bin/npm" "$rootdir/usr/local/bin/npm"
  141. cp "$rootdir/.npm-global/bin/npm" "$rootdir/usr/bin/npm"
  142. fi
  143. if [ -f "$rootdir/usr/local/bin/npm" ]; then
  144. cp "$rootdir/usr/local/bin/npm" "$rootdir/usr/bin/npm"
  145. fi
  146. cp "$rootdir/usr/bin/npm" "$rootdir/root/npm"
  147. # update from the old debian nodejs version
  148. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g n@${NODEJS_N_VERSION} --save
  149. if [ ! "$rootdir" ]; then
  150. # Don't do this if we're building an image,
  151. # because cpu detection faults occur.
  152. # This condition may no longer be needed in future once the bug is fixed
  153. $mesh_install_nodejs_prefix n --arch $N_ARCH ${NODEJS_VERSION}
  154. nodejs_fix_cpu_detection
  155. cp "$rootdir/root/npm" "$rootdir/usr/bin/npm"
  156. cp "$rootdir/root/npm" "$rootdir/usr/local/bin/npm"
  157. # deliberate second install of npm
  158. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g npm@${NPM_VERSION} --save
  159. if [ -f "$rootdir/usr/local/bin/npm" ]; then
  160. cp "$rootdir/usr/local/bin/npm" "$rootdir/usr/bin/npm"
  161. fi
  162. cp "$rootdir/usr/bin/npm" "$rootdir/root/npm"
  163. fi
  164. if [ -f "$rootdir/usr/bin/node" ]; then
  165. cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
  166. fi
  167. # check the version numbers
  168. cat <<EOF > "$rootdir/usr/bin/test_nodejs_install"
  169. #!/bin/bash
  170. node_version=\$(node -v)
  171. if [[ "\$node_version" != "v${NODEJS_VERSION}" ]]; then
  172. echo $"nodejs version expected to be ${NODEJS_VERSION} but found \$node_version"
  173. exit 1
  174. fi
  175. npm_version=\$(npm -v)
  176. if [[ "\$npm_version" != "${NPM_VERSION}" ]]; then
  177. echo $"npm version expected to be ${NPM_VERSION} but found \$npm_version"
  178. exit 2
  179. fi
  180. EOF
  181. chmod +x "$rootdir/usr/bin/test_nodejs_install"
  182. if ! $mesh_install_nodejs_prefix /usr/bin/test_nodejs_install; then
  183. echo $"nodejs version numbers did not match. Architecture is $NPM_ARCH."
  184. NODE_UPGRADE=
  185. exit 76835282
  186. fi
  187. rm "$rootdir/usr/bin/test_nodejs_install"
  188. NODE_UPGRADE=
  189. }
  190. function nodejs_upgrade {
  191. if [ ! -f /etc/apt/sources.list.d/nodesource.list ]; then
  192. return
  193. fi
  194. nodejs_setup_global_modules
  195. if grep -q "node_8.x" /etc/apt/sources.list.d/nodesource.list; then
  196. if [ -f /usr/local/bin/node ]; then
  197. CURR_NODE_VERSION=$(node --version)
  198. if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
  199. return
  200. fi
  201. fi
  202. fi
  203. if [ -f /usr/local/bin/node ]; then
  204. CURR_NODE_VERSION=$(node --version)
  205. if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
  206. return
  207. fi
  208. fi
  209. read_config_param ARCHITECTURE
  210. get_npm_arch
  211. NODE_UPGRADE=1
  212. rootdir=
  213. mesh_install_nodejs
  214. npm update -g
  215. }
  216. function mesh_setup_npm {
  217. cat <<EOF > "$rootdir/usr/bin/install_npm_global"
  218. #!/bin/bash
  219. if [ ! -d ~/.npm-global ]; then
  220. mkdir ~/.npm-global
  221. fi
  222. npm config set prefix '~/.npm-global'
  223. if [[ "$PATH" != *'~/.npm-global/bin'* ]]; then
  224. export PATH=~/.npm-global/bin:$PATH
  225. echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
  226. fi
  227. export NPM_CONFIG_PREFIX=~/.npm-global
  228. echo 'export NPM_CONFIG_PREFIX=~/.npm-global' >> ~/.bashrc
  229. source ~/.profile
  230. EOF
  231. chroot "$rootdir" /bin/chmod +x /usr/bin/install_npm_global
  232. chroot "$rootdir" /usr/bin/install_npm_global
  233. rm "$rootdir/usr/bin/install_npm_global"
  234. }
  235. function nodejs_setup_global_modules {
  236. if [ ! -f /usr/local/bin/node ]; then
  237. return
  238. fi
  239. if [ ! -d "$rootdir/root/.npm-global" ]; then
  240. mkdir "$rootdir/root/.npm-global"
  241. fi
  242. $mesh_install_nodejs_prefix npm config set prefix '/root/.npm-global'
  243. if ! grep -q "PATH=/root/.npm-global/bin" "$rootdir/root/.bashrc"; then
  244. echo "PATH=/root/.npm-global/bin:\$PATH" >> "$rootdir/root/.bashrc"
  245. fi
  246. if ! grep -q "NPM_CONFIG_PREFIX=" "$rootdir/root/.bashrc"; then
  247. echo "export NPM_CONFIG_PREFIX=/root/.npm-global" >> "$rootdir/root/.bashrc"
  248. fi
  249. # shellcheck disable=SC2086
  250. $mesh_install_nodejs_prefix export PATH=/root/.npm-global/bin:$PATH
  251. $mesh_install_nodejs_prefix export NPM_CONFIG_PREFIX=/root/.npm-global
  252. }
  253. function remove_nodejs {
  254. if [ ! "$1" ]; then
  255. return
  256. fi
  257. if [ ! -f "$NODEJS_INSTALLED_APPS_FILE" ]; then
  258. #remove_app nodejs
  259. return
  260. fi
  261. sed -i "/install_${1}/d" "$NODEJS_INSTALLED_APPS_FILE"
  262. if ! grep -q "install_" "$NODEJS_INSTALLED_APPS_FILE"; then
  263. apt-get -yq remove --purge nodejs
  264. if [ -f /usr/bin/nodejs ]; then
  265. rm /usr/bin/nodejs
  266. fi
  267. if [ -f /usr/local/bin/node ]; then
  268. rm /usr/local/bin/node
  269. fi
  270. if [ -f /usr/bin/node ]; then
  271. rm /usr/bin/node
  272. fi
  273. if [ -d /usr/lib/node_modules ]; then
  274. rm -rf /usr/lib/node_modules
  275. fi
  276. if [ -f /usr/bin/n ]; then
  277. rm /usr/bin/n
  278. fi
  279. remove_app nodejs
  280. rm "$NODEJS_INSTALLED_APPS_FILE"
  281. apt-get -yq autoremove
  282. fi
  283. }
  284. function upgrade_nodejs {
  285. CURR_NODE_VERSION=$(node --version)
  286. CURR_NPM_VERSION=$(npm --version)
  287. CURR_N_VERSION=$(n --version)
  288. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  289. npm upgrade -g npm@${NPM_VERSION} --save
  290. fi
  291. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  292. npm upgrade -g n@${NODEJS_N_VERSION} --save
  293. fi
  294. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  295. n ${NODEJS_VERSION}
  296. fi
  297. cp /usr/local/bin/n /usr/bin/n
  298. if [ -f /usr/local/bin/npm ]; then
  299. cp /usr/local/bin/npm /usr/bin/npm
  300. fi
  301. if [ -f /usr/local/bin/node ]; then
  302. cp /usr/local/bin/node /usr/bin/nodejs
  303. cp /usr/local/bin/node /usr/bin/node
  304. fi
  305. }
  306. function install_nodejs {
  307. if [ "$INSTALLING_MESH" ]; then
  308. mesh_install_nodejs
  309. return
  310. fi
  311. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  312. upgrade_nodejs
  313. return
  314. fi
  315. if [ ! "$ARCHITECTURE" ]; then
  316. ARCHITECTURE=$(uname -m)
  317. fi
  318. rootdir=
  319. mesh_install_nodejs
  320. # verify nodejs versions are what we expect
  321. CURR_NODE_VERSION=$(node --version)
  322. CURR_NPM_VERSION=$(npm --version)
  323. CURR_N_VERSION=$(n --version)
  324. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  325. echo $"Expected npm version $NPM_VERSION but actually have $CURR_NPM_VERSION"
  326. exit 6728252
  327. fi
  328. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  329. echo $"Expected n version $NODEJS_N_VERSION but actually have $CURR_N_VERSION"
  330. exit 5267452
  331. fi
  332. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  333. echo $"Expected node version $NODEJS_VERSION but actually have $CURR_NODE_VERSION"
  334. exit 5274527
  335. fi
  336. if [ "$1" ]; then
  337. if ! grep -q "install_${1}" "$NODEJS_INSTALLED_APPS_FILE"; then
  338. echo "install_${1}" >> "$NODEJS_INSTALLED_APPS_FILE"
  339. fi
  340. fi
  341. mark_completed "${FUNCNAME[0]}"
  342. }
  343. # NOTE: deliberately there is no "exit 0"