freedombone-utils-nodejs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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.8.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/.npm-global/bin/npm" ]; then
  142. cp "$rootdir/.npm-global/bin/npm" "$rootdir/usr/local/bin/npm"
  143. cp "$rootdir/.npm-global/bin/npm" "$rootdir/usr/bin/npm"
  144. fi
  145. if [ -f "$rootdir/usr/local/bin/npm" ]; then
  146. cp "$rootdir/usr/local/bin/npm" "$rootdir/usr/bin/npm"
  147. fi
  148. cp "$rootdir/usr/bin/npm" "$rootdir/root/npm"
  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. cp "$rootdir/root/npm" "$rootdir/usr/local/bin/npm"
  154. # deliberate second install of npm
  155. $mesh_install_nodejs_prefix npm install --arch=$NPM_ARCH -g npm@${NPM_VERSION} --save
  156. if [ -f "$rootdir/usr/local/bin/npm" ]; then
  157. cp "$rootdir/usr/local/bin/npm" "$rootdir/usr/bin/npm"
  158. fi
  159. cp "$rootdir/usr/bin/npm" "$rootdir/root/npm"
  160. if [ -f "$rootdir/usr/bin/node" ]; then
  161. cp "$rootdir/usr/bin/node" "$rootdir/usr/local/bin/node"
  162. fi
  163. # check the version numbers
  164. cat <<EOF > "$rootdir/usr/bin/test_nodejs_install"
  165. #!/bin/bash
  166. node_version=\$(node -v)
  167. if [[ "\$node_version" != "v${NODEJS_VERSION}" ]]; then
  168. echo $"nodejs version expected to be ${NODEJS_VERSION} but found \$node_version"
  169. exit 1
  170. fi
  171. npm_version=\$(npm -v)
  172. if [[ "\$npm_version" != "${NPM_VERSION}" ]]; then
  173. echo $"npm version expected to be ${NPM_VERSION} but found \$npm_version"
  174. exit 2
  175. fi
  176. EOF
  177. chmod +x "$rootdir/usr/bin/test_nodejs_install"
  178. if ! $mesh_install_nodejs_prefix /usr/bin/test_nodejs_install; then
  179. echo $"nodejs version numbers did not match. Architecture is $NPM_ARCH."
  180. NODE_UPGRADE=
  181. exit 76835282
  182. fi
  183. rm "$rootdir/usr/bin/test_nodejs_install"
  184. NODE_UPGRADE=
  185. }
  186. function nodejs_upgrade {
  187. if [ ! -f /etc/apt/sources.list.d/nodesource.list ]; then
  188. return
  189. fi
  190. nodejs_setup_global_modules
  191. if grep -q "node_8.x" /etc/apt/sources.list.d/nodesource.list; then
  192. if [ -f /usr/local/bin/node ]; then
  193. CURR_NODE_VERSION=$(node --version)
  194. if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
  195. return
  196. fi
  197. fi
  198. fi
  199. if [ -f /usr/local/bin/node ]; then
  200. CURR_NODE_VERSION=$(node --version)
  201. if [[ "$CURR_NODE_VERSION" == "v${NODEJS_VERSION}" ]]; then
  202. return
  203. fi
  204. fi
  205. read_config_param ARCHITECTURE
  206. get_npm_arch
  207. NODE_UPGRADE=1
  208. rootdir=
  209. mesh_install_nodejs
  210. npm update -g
  211. }
  212. function nodejs_setup_global_modules {
  213. if [ ! -f /usr/local/bin/node ]; then
  214. return
  215. fi
  216. if [ ! -d "$rootdir/root/.npm-global" ]; then
  217. mkdir "$rootdir/root/.npm-global"
  218. fi
  219. $mesh_install_nodejs_prefix npm config set prefix '/root/.npm-global'
  220. if ! grep -q "PATH=/root/.npm-global/bin" "$rootdir/root/.bashrc"; then
  221. echo "PATH=/root/.npm-global/bin:\$PATH" >> "$rootdir/root/.bashrc"
  222. fi
  223. if ! grep -q "NPM_CONFIG_PREFIX=" "$rootdir/root/.bashrc"; then
  224. echo "export NPM_CONFIG_PREFIX=/root/.npm-global" >> "$rootdir/root/.bashrc"
  225. fi
  226. # shellcheck disable=SC2086
  227. $mesh_install_nodejs_prefix export PATH=/root/.npm-global/bin:$PATH
  228. $mesh_install_nodejs_prefix export NPM_CONFIG_PREFIX=/root/.npm-global
  229. }
  230. function remove_nodejs {
  231. if [ ! "$1" ]; then
  232. return
  233. fi
  234. if [ ! -f "$NODEJS_INSTALLED_APPS_FILE" ]; then
  235. #remove_app nodejs
  236. return
  237. fi
  238. sed -i "/install_${1}/d" "$NODEJS_INSTALLED_APPS_FILE"
  239. if ! grep -q "install_" "$NODEJS_INSTALLED_APPS_FILE"; then
  240. apt-get -yq remove --purge nodejs
  241. if [ -f /usr/bin/nodejs ]; then
  242. rm /usr/bin/nodejs
  243. fi
  244. if [ -f /usr/local/bin/node ]; then
  245. rm /usr/local/bin/node
  246. fi
  247. if [ -f /usr/bin/node ]; then
  248. rm /usr/bin/node
  249. fi
  250. if [ -d /usr/lib/node_modules ]; then
  251. rm -rf /usr/lib/node_modules
  252. fi
  253. if [ -f /usr/bin/n ]; then
  254. rm /usr/bin/n
  255. fi
  256. remove_app nodejs
  257. rm "$NODEJS_INSTALLED_APPS_FILE"
  258. apt-get -yq autoremove
  259. fi
  260. }
  261. function upgrade_nodejs {
  262. CURR_NODE_VERSION=$(node --version)
  263. CURR_NPM_VERSION=$(npm --version)
  264. CURR_N_VERSION=$(n --version)
  265. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  266. npm upgrade -g npm@${NPM_VERSION} --save
  267. fi
  268. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  269. npm upgrade -g n@${NODEJS_N_VERSION} --save
  270. fi
  271. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  272. n ${NODEJS_VERSION}
  273. fi
  274. cp /usr/local/bin/n /usr/bin/n
  275. if [ -f /usr/local/bin/npm ]; then
  276. cp /usr/local/bin/npm /usr/bin/npm
  277. fi
  278. if [ -f /usr/local/bin/node ]; then
  279. cp /usr/local/bin/node /usr/bin/nodejs
  280. cp /usr/local/bin/node /usr/bin/node
  281. fi
  282. }
  283. function install_nodejs {
  284. if [ "$INSTALLING_MESH" ]; then
  285. mesh_install_nodejs
  286. return
  287. fi
  288. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  289. upgrade_nodejs
  290. return
  291. fi
  292. if [ ! "$ARCHITECTURE" ]; then
  293. ARCHITECTURE=$(uname -m)
  294. fi
  295. rootdir=
  296. mesh_install_nodejs
  297. # verify nodejs versions are what we expect
  298. CURR_NODE_VERSION=$(node --version)
  299. CURR_NPM_VERSION=$(npm --version)
  300. CURR_N_VERSION=$(n --version)
  301. if [[ "$CURR_NPM_VERSION" != "$NPM_VERSION" ]]; then
  302. echo $"Expected npm version $NPM_VERSION but actually have $CURR_NPM_VERSION"
  303. exit 6728252
  304. fi
  305. if [[ "$CURR_N_VERSION" != "$NODEJS_N_VERSION" ]]; then
  306. echo $"Expected n version $NODEJS_N_VERSION but actually have $CURR_N_VERSION"
  307. exit 5267452
  308. fi
  309. if [[ "$CURR_NODE_VERSION" != "v${NODEJS_VERSION}" ]]; then
  310. echo $"Expected node version $NODEJS_VERSION but actually have $CURR_NODE_VERSION"
  311. exit 5274527
  312. fi
  313. if [ "$1" ]; then
  314. if ! grep -q "install_${1}" "$NODEJS_INSTALLED_APPS_FILE"; then
  315. echo "install_${1}" >> "$NODEJS_INSTALLED_APPS_FILE"
  316. fi
  317. fi
  318. mark_completed "${FUNCNAME[0]}"
  319. }
  320. # NOTE: deliberately there is no "exit 0"