freedombone-utils-nodejs 12KB

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